Author Topic: help with arrays and DATA to make game maps  (Read 3825 times)

0 Members and 1 Guest are viewing this topic.

Offline fistfullofnails

  • Newbie
  • Posts: 12
    • View Profile
help with arrays and DATA to make game maps
« on: April 22, 2021, 07:30:26 pm »
Is there a tutorial somewhere explaining how to design maps using arrays and DATA.  I've got a book on QB64 on kindle by an author called 'Iam Beourd' haha, and he/she gets into designing a map.  They lay out a DATA table consisting of the numbers 0-5 or so, with a zero representing an empty room, a one representing a room containing an enemy, and so on.  I'm having difficulty understanding how it all comes together.  Especially how the player navigates through these rooms and the computer knowing which one you are in.  I'm also have trouble how 'inforoom' is actually working to give statements about each room.  And finally, the math being used to place the "wall" graphics is something I don't really get at all.  Is there a simpler version of something similar that is explained for dummies, or is this as dumbed down as it gets and I just happen to be dumber?

Code: QB64: [Select]
  1. _Title "MAZER"
  2.  
  3. Screen _NewImage(800, 600, 32)
  4. Dim map(10, 10) As Integer
  5.  
  6. Dim textwin&
  7. Dim textbox&
  8.  
  9. textwin& = _NewImage(200, 200, 32)
  10. textbox& = _NewImage(410, 200, 32)
  11.  
  12. Dim roominfo(10) As String
  13.  
  14. newgame:
  15. GoSub prep
  16.  
  17. Do Until k$ = "q"
  18.     Cls
  19.     GoSub drawmap
  20.     k$ = InKey$
  21.     Select Case map(x, y)
  22.         Case 1: GoSub infoport
  23.         Case 2: GoSub charger
  24.         Case 3: GoSub infoport
  25.         Case 4: GoSub creature
  26.         Case 5: GoSub shuttle
  27.     End Select
  28.     _Display
  29.  
  30. drawmap:
  31. 'background
  32. Line (80, 80)-(520, 520), _RGB(128, 15, 15), BF
  33. 'floor
  34. Line (100, 100)-(300, 300), _RGB(128, 150, 150), BF
  35. 'the walls are drawn if value in matrix is 0
  36. 'west wall
  37. If map(x - 1, y) = 0 Then
  38.     Line (100, 100)-(110, 300), _RGB(61, 94, 188), BF
  39.     If k$ = "a" Then x = x - 1
  40.  
  41. 'east wall
  42. If map(x + 1, y) = 0 Then
  43.     Line (290, 100)-(300, 300), _RGB(61, 94, 188), BF
  44.     If k$ = "d" Then x = x + 1
  45.  
  46. 'north wall
  47. If map(x, y - 1) = 0 Then
  48.     Line (100, 100)-(300, 110), _RGB(61, 94, 188), BF
  49.     If k$ = "w" Then y = y - 1
  50.  
  51. 'south wall
  52. If map(x, y + 1) = 0 Then
  53.     Line (100, 290)-(300, 300), _RGB(61, 94, 188), BF
  54.     If k$ = "s" Then y = y + 1
  55.  
  56. 'subroutine tht gives feedback for rooms
  57. infoport:
  58. _Dest textbox& 'after this, anything written goes to this image
  59. Print "In search of an escape shuttle, you navigate the rooms.";
  60. Print "Try to avoid alien nests. Some rooms contain a zapper reloader.";
  61. Print "Run out of zaps though, and the aliens will have you."
  62. Print " USE w, a, s, d to move between rooms."
  63. Print " q quits the game"
  64.  
  65. _Dest textwin&
  66. Print roominfo(map(x, y))
  67. _Dest 0 'this sets us back to drawing in the window
  68. _PutImage (100, 310), textbox& 'draws lower text box
  69. _PutImage (310, 100), textwin& 'draws right text box
  70.  
  71. charger:
  72. zap = 3
  73. map(x, y) = 3
  74. _Dest textbox&
  75. Print "you found a charger!"
  76. Print "your zapper has "; zap; "blasts"
  77. Print "the charger broke!"
  78. Print "You can't use it again."
  79.  
  80. _Dest textwin&
  81. Print "hit any key"
  82. _Dest 0 'back to drawing in window
  83. _PutImage (100, 310), textbox&
  84. _PutImage (310, 100), textwin&
  85.  
  86. creature:
  87. If zap > 0 Then
  88.     zap = zap - 1
  89.     map(x, y) = 3
  90.     GoTo gameover
  91.  
  92. _Dest textbox&
  93. Print "You used a blast!"
  94. Print "Your zapper has "; zap; " blasts left"
  95. Print "the creature is gone, the next"
  96. Print "is scraped now."
  97.  
  98. _Dest textwin&
  99. Print "hit enter key"
  100.  
  101. _PutImage (100, 310), textbox&
  102. _PutImage (310, 100), textwin&
  103.  
  104. shuttle:
  105. _Dest textbox&
  106. Print "You made it to the shuttle."
  107. Print "Still sealed and scanned safe"
  108. Print "You escape the ship and set a beacon"
  109. Print "It says NEVER BOARD THIS SHIP!"
  110. _Dest textwin&
  111. Print "Try Again (Y/N)?"
  112. _PutImage (100, 310), textbox&
  113. _PutImage (310, 100), textwin&
  114. GoTo try
  115.  
  116. 'you ran out of zapper loads and alien gets you
  117.  
  118. gameover:
  119. _Dest textbox&
  120. Print "Being as your zapper is empty, the creature"
  121. Print "eats you slowly.  THE END."
  122. Print "Your regret, is never warning others from"
  123. Print "coming to this ship."
  124.  
  125. _Dest textwin&
  126. Print "Try Again (Y/N)?";
  127. _PutImage (100, 310), textbox&
  128. _PutImage (310, 100), textwin&
  129. GoTo try
  130.  
  131. 'decide to try again
  132.  
  133. try:
  134. If m$ = "Y" Or m$ = "y" Then GoTo newgame
  135.  
  136. 'code for default values for game and loads map
  137.  
  138. prep:
  139.  
  140. 'load map from data
  141. Restore mapdata 'restore points to label in data table
  142. ' this loads map
  143. For y = 0 To 10
  144.     For x = 0 To 10
  145.         Read map(x, y)
  146.     Next
  147.  
  148. zap = 3
  149. x = 1
  150. y = 1
  151. 'table of text for describing each room
  152. roominfo(1) = "The room is empty"
  153. roominfo(2) = "zapper filled"
  154. roominfo(3) = "scrap litters the floor"
  155. roominfo(4) = "creature!"
  156. roominfo(5) = "shuttle to safety"
  157.  
  158.  
  159. 'data table laid out in grid for a map
  160. 'will read this to a 2 dimensional array and interpret the numbers
  161. '0 is wall, 1 empty room, 2 refills zapper, etc
  162.  
  163. mapdata:
  164.  
  165. Data 0,0,0,0,0,0,0,0,0,0,0
  166. Data 0,1,1,1,4,1,1,1,1,1,0
  167. Data 0,1,1,1,2,1,1,4,1,1,0
  168. Data 0,1,1,1,1,1,1,1,1,1,0
  169. Data 0,0,0,0,0,0,0,1,0,0,0
  170. Data 0,2,1,4,1,4,1,1,1,1,0
  171. Data 0,0,1,0,0,0,0,0,0,0,0
  172. Data 0,1,1,0,4,1,1,1,1,1,0
  173. Data 0,2,1,0,1,4,1,1,5,1,0
  174. Data 0,1,1,1,1,4,1,1,1,1,0
  175. Data 0,0,0,0,0,0,0,0,0,0,0
  176.  
  177.  

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: help with arrays and DATA to make game maps
« Reply #1 on: April 22, 2021, 07:53:01 pm »
Its only got the up arrow coded, you can do the rest...good luck and happy coding

unseen


Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(400, 400, 32)
  2.  
  3. '// create map array 10 * 10
  4. DIM Map(9, 9) AS _BYTE
  5.  
  6. '// your position
  7. DIM You(1) AS INTEGER '// array index ref : 0 = column, 1= row
  8.  
  9. '// load the data
  10.  
  11. FOR row% = 0 TO 9
  12.   FOR col% = 0 TO 9
  13.     READ Block%
  14.     IF Block% < 9 THEN
  15.       Map(col%, row%) = Block%
  16.     ELSE
  17.       You(0) = col%
  18.       You(1) = row%
  19.     END IF
  20.   NEXT
  21.  
  22.  
  23.  
  24.  
  25.   _LIMIT 30
  26.   CLS
  27.  
  28.   KB$ = INKEY$
  29.  
  30.   SELECT CASE KB$
  31.     CASE CHR$(0) + CHR$(72) ' Up
  32.       IF You(1) > 0 THEN
  33.         IF Map(You(0), You(1) - 1) = 0 THEN '//you can move into the square
  34.           You(1) = You(1) - 1
  35.         END IF
  36.       ELSE '// Your path is blocked
  37.  
  38.       END IF
  39.     CASE CHR$(0) + CHR$(77) ' Right
  40.     CASE CHR$(0) + CHR$(80) ' Down
  41.     CASE CHR$(0) + CHR$(75) ' Left
  42.  
  43.  
  44.   '// drawing
  45.   y% = 0
  46.   FOR row% = 0 TO 9
  47.     x% = 0
  48.     FOR col% = 0 TO 9
  49.       IF Map(col%, row%) = 1 THEN
  50.         LINE (x%, y%)-(x% + 20, y% + 20), _RGB32(255, 120, 120), BF
  51.       ELSE
  52.         IF You(0) = col% AND You(1) = row% THEN
  53.  
  54.           LINE (x%, y%)-(x% + 20, y% + 20), _RGB32(55, 120, 120), BF
  55.         END IF
  56.       END IF
  57.  
  58.       x% = x% + 20
  59.     NEXT
  60.     y% = y% + 20
  61.   NEXT
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. DATA 1,1,1,1,1,1,1,1,1,1
  70. DATA 0,0,0,0,0,0,0,0,0,0
  71. DATA 0,0,0,0,0,0,0,0,0,0
  72. DATA 0,0,0,0,0,0,0,0,0,0
  73. DATA 0,0,0,0,0,0,0,0,0,0
  74. DATA 0,0,0,0,9,0,0,0,0,0
  75. DATA 0,0,0,0,0,0,0,0,0,0
  76. DATA 0,0,0,0,0,0,0,0,0,0
  77. DATA 0,0,0,0,0,0,0,0,0,0
  78. DATA 0,0,0,0,0,0,0,0,0,0
  79.  

Dont cheat! Heres an edit, now as a basic "game", get the key to ope the door...

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(400, 400, 32)
  2.  
  3. '// create map array 10 * 10
  4. DIM Map(9, 9) AS _BYTE
  5.  
  6. '// your position
  7. DIM You(1) AS INTEGER '// array index ref : 0 = column, 1= row
  8.  
  9. '// load the data
  10.  
  11. FOR row% = 0 TO 9
  12.   FOR col% = 0 TO 9
  13.     READ Block%
  14.     IF Block% < 9 THEN
  15.       Map(col%, row%) = Block%
  16.     ELSE
  17.       You(0) = col%
  18.       You(1) = row%
  19.     END IF
  20.   NEXT
  21.  
  22.  
  23.  
  24.  
  25.   _LIMIT 30
  26.   CLS
  27.  
  28.   KB$ = INKEY$
  29.  
  30.   SELECT CASE KB$
  31.  
  32.     CASE CHR$(0) + CHR$(72) ' Up
  33.  
  34.       IF You(1) > 0 THEN
  35.         IF Map(You(0), You(1) - 1) = 0 THEN '//you can move into the square
  36.           You(1) = You(1) - 1
  37.  
  38.         ELSEIF Map(You(0), You(1) - 1) = 2 THEN
  39.           Map(You(0), You(1) - 1) = 0
  40.           HaveKey% = -1
  41.           You(1) = You(1) - 1
  42.         ELSEIF Map(You(0), You(1) - 1) = 3 THEN '// the exit
  43.           IF HaveKey% THEN
  44.             '// Win level
  45.             END '// exit
  46.           END IF
  47.         END IF
  48.       END IF
  49.  
  50.     CASE CHR$(0) + CHR$(77) ' Right
  51.  
  52.       IF You(0) < 9 THEN
  53.         IF Map(You(0) + 1, You(1)) = 0 THEN '//you can move into the square
  54.           You(0) = You(0) + 1
  55.         ELSEIF Map(You(0) + 1, You(1)) = 2 THEN
  56.           Map(You(0) + 1, You(1)) = 0
  57.           HaveKey% = -1
  58.           You(0) = You(0) + 1
  59.         ELSEIF Map(You(0) + 1, You(1)) = 3 THEN '// the exit
  60.           IF HaveKey% THEN
  61.             END '// exit
  62.           END IF
  63.         END IF
  64.       END IF
  65.  
  66.     CASE CHR$(0) + CHR$(80) ' Down
  67.  
  68.       IF You(1) < 9 THEN
  69.         IF Map(You(0), You(1) + 1) = 0 THEN '//you can move into the square
  70.           You(1) = You(1) + 1
  71.  
  72.         ELSEIF Map(You(0), You(1) + 1) = 2 THEN
  73.           Map(You(0), You(1) + 1) = 0
  74.           HaveKey% = -1
  75.           You(1) = You(1) + 1
  76.         ELSEIF Map(You(0), You(1) + 1) = 3 THEN '// the exit
  77.           IF HaveKey% THEN
  78.             '// Win level
  79.             END '// exit
  80.           END IF
  81.         END IF
  82.       END IF
  83.  
  84.     CASE CHR$(0) + CHR$(75) ' Left
  85.  
  86.       IF You(0) > 0 THEN
  87.         IF Map(You(0) - 1, You(1)) = 0 THEN '//you can move into the square
  88.           You(0) = You(0) - 1
  89.         ELSEIF Map(You(0) - 1, You(1)) = 2 THEN
  90.           Map(You(0) - 1, You(1)) = 0
  91.           HaveKey% = -1
  92.           You(0) = You(0) - 1
  93.         ELSEIF Map(You(0) - 1, You(1)) = 3 THEN '// the exit
  94.           IF HaveKey% THEN
  95.             '// Win level
  96.             END '// exit
  97.           END IF
  98.         END IF
  99.       END IF
  100.  
  101.  
  102.   '// drawing
  103.   y% = 0
  104.   FOR row% = 0 TO 9
  105.     x% = 0
  106.     FOR col% = 0 TO 9
  107.       SELECT CASE Map(col%, row%)
  108.         CASE 1
  109.           LINE (x%, y%)-(x% + 20, y% + 20), _RGB32(255, 120, 120), BF
  110.         CASE 2 '// the Key
  111.           COLOR _RGB32(0, 255, 0)
  112.           _PRINTSTRING (x%, y%), CHR$(0) + CHR$(135)
  113.  
  114.         CASE 3 '// The exit
  115.           LINE (x%, y%)-(x% + 20, y% + 20), _RGB32(0, 255, 0), BF
  116.  
  117.         CASE ELSE
  118.           IF You(0) = col% AND You(1) = row% THEN
  119.  
  120.             LINE (x%, y%)-(x% + 20, y% + 20), _RGB32(55, 120, 120), BF
  121.           END IF
  122.       END SELECT
  123.  
  124.       x% = x% + 20
  125.     NEXT
  126.     y% = y% + 20
  127.   NEXT
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. DATA 1,1,1,1,1,1,1,1,1,1
  136. DATA 1,0,0,0,1,0,0,0,0,1
  137. DATA 1,0,1,0,0,0,1,1,0,1
  138. DATA 1,0,1,1,1,0,1,0,0,1
  139. DATA 1,0,1,0,1,0,1,0,1,1
  140. DATA 1,0,0,2,1,0,1,0,0,0
  141. DATA 1,0,1,1,1,0,1,0,1,1
  142. DATA 1,0,1,0,0,0,1,0,1,1
  143. DATA 9,0,1,0,0,0,1,0,0,3
  144. DATA 1,1,1,1,1,1,1,1,1,1
  145.  
« Last Edit: April 22, 2021, 08:22:04 pm by Unseen Machine »

Offline fistfullofnails

  • Newbie
  • Posts: 12
    • View Profile
Re: help with arrays and DATA to make game maps
« Reply #2 on: April 23, 2021, 01:17:32 pm »
 READ Block%
    IF Block% < 9 THEN
      Map(col%, row%) = Block%

So the program just jumps to the first DATA it sees and uses the nested FOR loop to read that into the map array?  And then the map array is being put into another variable called Block%?  I'm not understanding the purpose of determining if Block% is less than 9 in the if statement.

 IF Map(You(0), You(1) - 1) = 0

I cannot figure out how this math is being done.  Say I was  at location 3,4.  How does this math get solved?  "Map(3, 4 - 1) " is how I'm looking at it.  Or is it taking that grid location's value, which I think is 0, and then doing the math on it.  "0 - 1" and comparing it to zero?




Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help with arrays and DATA to make game maps
« Reply #3 on: April 23, 2021, 02:26:32 pm »
Quote
'// your position
DIM You(1) AS INTEGER '// array index ref : 0 = column, 1= row

Looks like you() is a tiny little array you(0) = column you are in, and you(1) = row you are in.


Quote
"Map(3, 4 - 1) " is how I'm looking at it.
Yes! if that map value = 0 then you can move into it.
« Last Edit: April 23, 2021, 02:27:47 pm by bplus »

Offline 191Brian

  • Newbie
  • Posts: 91
    • View Profile
    • My Itch page
Re: help with arrays and DATA to make game maps
« Reply #4 on: April 23, 2021, 02:30:45 pm »
Hi

It is reading the data into the variable BLOCK% and if it is less than 9 it writes the value into the map array, the 9 in the data indicates the start position of the player which is set in the else part 
ELSE
      You(0) = col%
      You(1) = row%

IF Map(You(0), You(1) - 1) = 0 is testing if the cell the player is trying to move to is an empty (0) i.e not a wall (1) note this if statement changes depending on which direction the player is trying to move.
Brian ...

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: help with arrays and DATA to make game maps
« Reply #5 on: April 23, 2021, 04:52:41 pm »
Thanks to everyone for answering, it's my bad cause yeah, my lack of comments...it's all ways been a bad side of my coding.

The way to do it to have different rooms like the first post describes would be to have the level data blocks (rooms) contain the room index and the size of the room then you could use a _MEM block to store all the data in a single array. Then with a simple method (i can think of a few) when you exit a room the map would contain the index of the room your moving into. You the just REDIM the map array, load the relevant data from the _MEM block and woosh...youve got a basic dungeon engine...it's easy enough to change the painted blocks to textures, and then if you wanna really push it you can easily make a Wolfenstein style raycaster and youre in 3D!

I'll have a look at maybe implementing a basic version of a multi room map but ill add more comments.

Unseen

Offline fistfullofnails

  • Newbie
  • Posts: 12
    • View Profile
Re: help with arrays and DATA to make game maps
« Reply #6 on: April 24, 2021, 05:55:57 pm »
Thanks for your explanations folks.  I literally spent four hours last night staring at the first two sections of Unseen's code before feeling like I was starting to get it.  I ended up rewriting little parts of those sections and adding print statements to those parts to show me what was actually happening in those parts.  For example, I'd just make up a smaller 2 d array and data set, then run through it, but actually print out what it was reading and so forth.  for example

Code: QB64: [Select]
  1. Dim map(4, 4) As Integer
  2.  
  3.  
  4. For row% = 0 To 4
  5.     For col% = 0 To 4
  6.         Read area%
  7.         map(col%, row%) = area%
  8.         Print "map"; col%; ","; row%; " is equal to: "; area%
  9.  
  10.     Next

I think I was starting to get the drawing part after looking at it briefly toward the end of my night.  Guess i'll spend a couple hours with it as well later.
What immediately jumped out to me at first in the drawing part, was the question as to why the y% and z% variable received the value of 0 in the places that they did.

Code: QB64: [Select]
  1.     y% = 0
  2.     For row% = 0 To 9
  3.         x% = 0
  4.         For col% = 0 To 9

Is there perhaps anything I could apply these concepts to that will give me practice that may be a bit easier to wrap my head around at first?  Or should i just keep inducing headaches into myself?  Maybe I've gotten in too far before understanding some simpler concepts?  Finally, thank you for your time everyone that has tried to help me.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help with arrays and DATA to make game maps
« Reply #7 on: April 24, 2021, 06:36:17 pm »
Yeah, here is how I would do drawing, forget about x, y use square and scale map blocks to any size though be careful with _PRINTSTRING:
Code: QB64: [Select]
  1. '// drawing
  2. Const square = 70
  3. Screen _NewImage(square * 10, square * 10, 32)
  4.  
  5. '// create map array 10 * 10
  6. Dim Map(9, 9) As _Byte
  7.  
  8. '// your position
  9. Dim You(1) As Integer '// array index ref : 0 = column, 1= row
  10.  
  11. '// load the data
  12.  
  13. For row% = 0 To 9
  14.     For col% = 0 To 9
  15.         Read Block%
  16.         If Block% < 9 Then
  17.             Map(col%, row%) = Block%
  18.         Else
  19.             You(0) = col%
  20.             You(1) = row%
  21.         End If
  22.     Next
  23.  
  24.  
  25.  
  26.  
  27.     _Limit 30
  28.     Cls
  29.  
  30.     KB$ = InKey$
  31.  
  32.     Select Case KB$
  33.  
  34.         Case Chr$(0) + Chr$(72) ' Up
  35.  
  36.             If You(1) > 0 Then
  37.                 If Map(You(0), You(1) - 1) = 0 Then '//you can move into the square
  38.                     You(1) = You(1) - 1
  39.  
  40.                 ElseIf Map(You(0), You(1) - 1) = 2 Then
  41.                     Map(You(0), You(1) - 1) = 0
  42.                     HaveKey% = -1
  43.                     You(1) = You(1) - 1
  44.                 ElseIf Map(You(0), You(1) - 1) = 3 Then '// the exit
  45.                     If HaveKey% Then
  46.                         '// Win level
  47.                         End '// exit
  48.                     End If
  49.                 End If
  50.             End If
  51.  
  52.         Case Chr$(0) + Chr$(77) ' Right
  53.  
  54.             If You(0) < 9 Then
  55.                 If Map(You(0) + 1, You(1)) = 0 Then '//you can move into the square
  56.                     You(0) = You(0) + 1
  57.                 ElseIf Map(You(0) + 1, You(1)) = 2 Then
  58.                     Map(You(0) + 1, You(1)) = 0
  59.                     HaveKey% = -1
  60.                     You(0) = You(0) + 1
  61.                 ElseIf Map(You(0) + 1, You(1)) = 3 Then '// the exit
  62.                     If HaveKey% Then
  63.                         End '// exit
  64.                     End If
  65.                 End If
  66.             End If
  67.  
  68.         Case Chr$(0) + Chr$(80) ' Down
  69.  
  70.             If You(1) < 9 Then
  71.                 If Map(You(0), You(1) + 1) = 0 Then '//you can move into the square
  72.                     You(1) = You(1) + 1
  73.  
  74.                 ElseIf Map(You(0), You(1) + 1) = 2 Then
  75.                     Map(You(0), You(1) + 1) = 0
  76.                     HaveKey% = -1
  77.                     You(1) = You(1) + 1
  78.                 ElseIf Map(You(0), You(1) + 1) = 3 Then '// the exit
  79.                     If HaveKey% Then
  80.                         '// Win level
  81.                         End '// exit
  82.                     End If
  83.                 End If
  84.             End If
  85.  
  86.         Case Chr$(0) + Chr$(75) ' Left
  87.  
  88.             If You(0) > 0 Then
  89.                 If Map(You(0) - 1, You(1)) = 0 Then '//you can move into the square
  90.                     You(0) = You(0) - 1
  91.                 ElseIf Map(You(0) - 1, You(1)) = 2 Then
  92.                     Map(You(0) - 1, You(1)) = 0
  93.                     HaveKey% = -1
  94.                     You(0) = You(0) - 1
  95.                 ElseIf Map(You(0) - 1, You(1)) = 3 Then '// the exit
  96.                     If HaveKey% Then
  97.                         '// Win level
  98.                         End '// exit
  99.                     End If
  100.                 End If
  101.             End If
  102.  
  103.     End Select
  104.  
  105.     '// drawing
  106.     For row% = 0 To 9
  107.         For col% = 0 To 9
  108.             Select Case Map(col%, row%)
  109.                 Case 1
  110.                     Line (col% * square, row% * square)-Step(square, square), _RGB32(255, 120, 120), BF
  111.                 Case 2 '// the Key
  112.                     Color _RGB32(0, 255, 0)
  113.                     _PrintString (col% * square + square / 2 - 10, row% * square + square / 2 - 8), Chr$(0) + Chr$(135)
  114.                 Case 3 '// The exit
  115.                     Line (col% * square, row% * square)-Step(square, square), _RGB32(0, 255, 0), BF
  116.                 Case Else
  117.                     If You(0) = col% And You(1) = row% Then
  118.                         Line (col% * square, row% * square)-Step(square, square), _RGB32(55, 120, 120), BF
  119.                     End If
  120.             End Select
  121.             Line (col% * square, row% * square)-Step(square, square), &HFFFFFF00, B
  122.         Next
  123.     Next
  124.     _Display
  125.  
  126.  
  127. Data 1,1,1,1,1,1,1,1,1,1
  128. Data 1,0,0,0,1,0,0,0,0,1
  129. Data 1,0,1,0,0,0,1,1,0,1
  130. Data 1,0,1,1,1,0,1,0,0,1
  131. Data 1,0,1,0,1,0,1,0,1,1
  132. Data 1,0,0,2,1,0,1,0,0,0
  133. Data 1,0,1,1,1,0,1,0,1,1
  134. Data 1,0,1,0,0,0,1,0,1,1
  135. Data 9,0,1,0,0,0,1,0,0,3
  136. Data 1,1,1,1,1,1,1,1,1,1
  137.  
  138.  
  139.  

Edit: to scale squares
EDIT again to center key at any scale
« Last Edit: April 24, 2021, 06:46:33 pm by bplus »

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: help with arrays and DATA to make game maps
« Reply #8 on: April 24, 2021, 07:55:52 pm »








So...i'll try my best to help...

X% and y% are reset to 0 each time cause that's where drawing starts from. They are then increased by 20 pixels each loop of the FOR blocks. Try changing them and see the map move around, id suggest trying to make it auto center to the screen...for that you'll need

X% = half of _SCREENWIDTH minus (half size of the column count of the array * the block size (20))
Ill let you figure out Y%

If you resize the map array you need to resize the DATA blocks to the same size. Id suggest a REDIM SHARED for the map array and then before reading the map data read two extra bits of data to size the array. i.e for a level thats 6 * 8

Read Col%
Read Row%
REDIM SHARED MAP(Col%, Row%)
Load the data 

LevelOne: '// Line ref? For use with RESTORE so you can have data stored in any order you like and then just jump to it.
DATA 5, 7
DATA .....THE level data here,


You can easily make the for loop code into a SUB so you can call it repeatedly. The FOR loops would be changed to use the Col%/Row% values.

The drawing method is just the most basic method i could think of, in this sort of thing you would usually use a tile sheet and the DATA would be tile indices. For things like platform games you use multiple arrays for background, level, special objects, sprites etc...

Happy coding and reel free to ask anything else you need help with.

Unseen


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: help with arrays and DATA to make game maps
« Reply #9 on: April 25, 2021, 03:24:59 pm »
Most tiles are square?