map in array speichern geht nicht so richtig...

Previous topic - Next topic

cyby

hi,

habe mal versucht eine map in einem Array zu erstellen und diese abzuspeichern, dies klapt leider nicht (es wird irgentwie immer nur die letzte Zeile der Map gesichert)
Code (glbasic) Select
// --------------------------------- //
// Project: save
// Start: Saturday, October 09, 2004
// IDE Version: 2.41007

GLOBAL mapx = 10
GLOBAL mapy = 10
DIM map[mapx][mapy]

//map erstellen , anzeigen und speichern
FOR y = 0 TO mapx-1
FOR x = 0 TO mapx-1
map[x][y] = RND(3)//erstellen
PRINT map[x][y],15*x,15*y//anzeigen
PUTFILE "map.txt", y , map[x][y]//speichern
NEXT
NEXT

SHOWSCREEN
KEYWAIT
Danke für Hilfe :)

MfG
cyby :D

Kitty Hello

Ja. Klar. Weil Du für x=0 bis mapx-1 in die Zeile y schreibst, und damit diese "überschreibst".
Eine Map schreibt man so:
Code (glbasic) Select
FUNCTION SaveMap: file$, map[]
FOR y=0 TO maxy-1
line$=""
FOR x=0 TO maxx-1
line$=line$+FORMAT$(3,0,map[x][y])
NEXT
PUTFILE file$, y, line$
NEXT
ENDFUNCTION

Also, immer 3 Buchstaben sind ein Eintrag in X-Richtung.
Lesen macht man dann so:
Code (glbasic) Select
FUNCTION GetNumFromStr: str$, index
LOCAL a$
a$=MID$(str$, INTEGER(index*3), 3)
RETURN a$
ENDFUNCTION

FUNCTION ReadMap: file$, map[]
FOR y=0 TO maxy-1
GETFILE file$, y, line$
FOR x=0 TO maxx-1
map[x][y]=GetNumFromStr(line$, x)
NEXT
NEXT
ENDFUNCTION

Man kann / sollte mit BOUNDS() die Größe beim Speichern mitspeichern, und beim Lesen dan neu dimensionieren. Außer, wenn alle Level gleich groß sind. Bei Wumbo's Adventure I hab' ich immer ein 100x100 Feld im Speicher gehalten. Bei Wumbo II ist's dynamisch... Ist Geschmackssache.

Viel Erfolg,
Gernot