This is a DB Classic Snippet and collects some data from the user, creates a matrix and displays it with a simple menu.
You can then randomize the matrix with the R option on the menu or flatten it with the F option.
When you have the one you want to keep, the P option will put an obelisk shape in the middle so when it's loaded back in, you can differentiate it from a normally random created matrix.
S saves the current data to a file. When saved, flatten the matrix (or you won't see any difference when you load it back in), and use L to load the saved data back in.
The save and load procedures can be adapted to save ANY data in your programs that is stored in variables.
All you have to do is make sure that your Load routine loads the data back into your program in EXACTLY the same order as it is saved or it won't work. Arrays can be saved in For...Next loops.
Rem LoadSave
Rem Demonstrates how to save and load game data...
Set Display Mode 800,600,16
Sync On: Sync Rate 0
CLS 0
Randomize Timer()
Set Text Opaque
Rem Let's get some data from the user...
Input "Please Enter Your Name: ";Name$
Input "Please Enter Your Age: ";Age
CLS
Print "OK ";Name$;", let's make some data...": Print
Input "What tile size matrix would you like ACROSS (min 10/max 70): ";TilesX
If TilesX > 70 Then TilesX = 70
If TilesX < 10 Then TilesX = 10
Input "What tile size matrix would you like DOWN (min 10/max 70): ";TilesZ
If TilesZ > 70 Then TilesZ = 70
If TilesZ < 10 Then TilesZ = 10
Input "What pixel size matrix would you like ACROSS: ";MatWidth
Input "What pixel size matrix would you like DOWN ";MatHeight
Set Camera Range 1.0,MatWidth*2
CLS
Rem Now create the matrix to the user's specifications...
Make Matrix 1,Matwidth,Matheight,TilesX,TilesZ
MatCentreX# = MatWidth/2.0: MatCentreZ# = MatHeight/2.0
Rem Position the camera...
Position Matrix 1, 0-MatCentreX#,0.0,0-MatCentreZ#
Rem Set camera view window and make a simple menu...
Set Camera View 0,50,800,600 : Rem Leaves a control panel at top of the screen
Ink RGB(0,255,0),0
Center Text 400,4, "[R] - Randomize [F] - Flatten [S] - Save Data [L] - Load Data [P] - Personalise [Q] - Quit"
Ink RGB(255,255,255),0
Rem Main Program Loop (Never exits this while program is running)
Do
I$ = Upper$(Inkey$())
Select I$
Case "R"
Gosub RandMatrix
Center Text 400,24, " Matrix Randomized "
EndCase
Case "F"
Gosub FlattenMatrix
Center Text 400,24, " Matrix Flattened "
EndCase
Case "S"
Gosub SaveData
Center Text 400,24, " Matrix Saved "
EndCase
Case "L"
Gosub LoadData
Center Text 400,24, " Matrix Loaded "
EndCase
Case "P"
Gosub Personal
Center Text 400,24, " Personalised Matrix "
EndCase
Case "Q"
End
EndCase
EndSelect
Gosub RotateCamera
Sync
Loop
Rem Procedures called from main program loop...
RotateCamera:
X# = WrapValue(X# + .3): Z# = WrapValue(Z# + .3)
Position camera newxvalue(0,X#,5000.0),3000.0,newzvalue(0,Z#,5000.0)
Point Camera 0.0,0.0,0.0
Return
RandMatrix:
Randomize Matrix 1,1000
Update Matrix 1
Repeat
Until Inkey$()=""
Return
Personal:
Rem Add centre bump so you can see when it's loaded that it's your matrix data
Rem not just another randomized one... :)
For Nz = (TilesZ/2)-1 To (TilesZ/2)+1
For Nx = (TilesX/2)-1 To (TilesX/2)+1
Set Matrix Height 1, Nx, Nz, 2000.0
Next Nx
Next Nz
Update Matrix 1
Repeat
Until Inkey$()=""
Return
FlattenMatrix:
For Nz = 0 To TilesZ
For Nx = 0 To TilesX
Set Matrix Height 1, Nx, Nz, 0.0
Next Nx
Next Nz
Update Matrix 1
Repeat
Until Inkey$()=""
Return
SaveData:
If File Exist("Example.dat") Then Delete File "Example.dat"
Open To Write 1,"Example.dat"
Write String 1,Name$
Write String 1,Str$(Age): Rem <<< Age is numeric var, so convert to string first
For Nz = 0 To TilesZ
For Nx = 0 To TilesX
Temp$ = Str$(Get Matrix Height(1,Nx,Nz))
Write String 1, Temp$
Next Nx
Next Nz
Close File 1
Repeat
Until Inkey$()=""
Return
LoadData:
Open To Read 1,"Example.dat"
Read String 1,Name$
Read String 1, Temp$: Age = Val(Temp$): Rem <<< Age is numeric var, so convert back to numeric
For Nz = 0 To TilesZ
For Nx = 0 To TilesX
Read String 1, Temp$: MatHeight# = VAL(Temp$)
Set Matrix Height 1,Nx,Nz,MatHeight#
Next Nx
Next Nz
Close File 1
Update Matrix 1
Repeat
Until Inkey$()=""
Return
TDK_Man