Saturday, April 19, 2025

Sprites in GW-BASIC 4

Previously we've stored our graphics data in a very verbose manner. This is not ideal since GW-BASIC is an interpreted language and keeps the entire source code of our program in memory as the it runs. If you keep wasting memory this way your program will eventually crash and GW-BASIC will give you the "out of string space" error message. I know that from personal experience.

So, what to do about it? Preferably we would place all graphics in a separate data file that we load into memory as the program requires them.

But we could also keep using DATA statements but convert the graphics data to the format GW-BASIC's PUT and GET statements uses before our program runs. I'm not going explain how to do that here since it's quite the complicated process with several steps. Another time maybe. But, I will however show you what our program will look like using that approach:
10 SCREEN 1
20 DEFINT A-Z
30 GFXSIZE = 33
50 DIM PLAYERGFX(GFXSIZE): DIM PLAYERMASK(GFXSIZE)
51 DIM MONSTERGFX(GFXSIZE): DIM MONSTERMASK(GFXSIZE)
52 DIM GRASSGFX(GFXSIZE): DIM ICEGFX(GFXSIZE)
53 DIM TOADSGFX(GFXSIZE): DIM TOADSMASK(GFXSIZE)
60 ' This time around we don't need to draw the sprites to the screen
61 ' and then use GET to put them into arrays. We simply put the DATA values
62 ' directly into the arrays with the READ statement.
70 FOR T = 0 TO GFXSIZE: READ PLAYERGFX(T): NEXT T
71 FOR T = 0 TO GFXSIZE: READ PLAYERMASK(T): NEXT T
72 FOR T = 0 TO GFXSIZE: READ MONSTERGFX(T): NEXT T
73 FOR T = 0 TO GFXSIZE: READ MONSTERMASK(T): NEXT T
74 FOR T = 0 TO GFXSIZE: READ GRASSGFX(T): NEXT T
75 FOR T = 0 TO GFXSIZE: READ ICEGFX(T): NEXT T
76 FOR T = 0 TO GFXSIZE: READ TOADSGFX(T): NEXT T
77 FOR T = 0 TO GFXSIZE: READ TOADSMASK(T): NEXT T

100 FOR Y = 0 TO 7
105 GY = Y * 16
110 FOR X = 0 TO 19
115 GX = X * 16
120 IF X &gt 0 AND X &lt 19 AND Y &gt 2 AND Y &lt 7 THEN PUT (GX, GY), ICEGFX, PSET ELSE PUT (GX, GY), GRASSGFX, PSET
130 NEXT X
140 NEXT Y
150 PUT (32, 56), PLAYERMASK, AND
160 PUT (32, 56), PLAYERGFX, OR
170 PUT (128, 80), MONSTERMASK, AND: PUT (128, 80), MONSTERGFX, OR
180 PUT (192, 32), MONSTERMASK, AND: PUT (192, 32), MONSTERGFX, OR
181 PUT (240, 16), TOADSMASK, AND: PUT (240, 16), TOADSGFX, OR
182 PUT (160, 0), TOADSMASK, AND: PUT (160, 0), TOADSGFX, OR
183 PUT (64, 32), TOADSMASK, AND: PUT (64, 32), TOADSGFX, OR

300 LOCATE 17: PRINT "A simple program for demonstrating
310 PRINT "16x16 tiled graphics in GW-BASIC."
400 END

890 ' The sprite data follow below.

900 ' player
901 DATA 32, 16, 0, 0, 10752, 160, -30208, 168, 10754, 170
902 DATA 16131, 255, 5376, 85, 21504, 81, 29952, 165, 16128, 255
903 DATA 16128, 175, -20736, 255, -21758, 254, 27137, 16633, 10752, 168
904 DATA 10240, 40, 10752, 42
905 ' player mask
906 DATA 32, 16, 255, -253, 252, -256, 240, 16128, 240, 16128
907 DATA 240, 16128, 240, 16128, 252, 16128, 252, 16128, 252, 16128
908 DATA 252, 16128, 240, 16128, 240, 3840, 240, 3840, 240, 3840
909 DATA 255, 16128, 255, 16128
910 ' snake
911 DATA 32, 16, 0, 0, 0, -32767, 0, 24576, 256, 24661
912 DATA 1280, -32598, 1536, 0, 1280, 88, 256, 86, 0, 22
913 DATA 5376, 86, 21765, 90, 22037, 168, 6161, 0, 20501, 0
914 DATA 16405, 0, 0, 0
915 ' snake mask
916 DATA 32, 16, -1, 4080, -1, 1008, -3841, 768, -16129, 768
917 DATA -16129, 768, -16129, 3840, -16129, 16128, -16129, 16128, 255, 16128
918 DATA 192, 16128, 0, 16128, 0, 16128, 0, -256, 0, -1
919 DATA 768, -1, 3840, -1
920 ' grass
921 DATA 32, 16, 256, 4096, 272, 0, 16, 4, 16384, 1028
922 DATA 16384, 1024, 4, 64, 4, 16448, 4096, 16640, 4112, 260
923 DATA 16, 4, 256, 0, 257, 1024, 1, 1024, 4160, 4
924 DATA 4160, 4, 0, 4096
925 ' ice
926 DATA 32, 16, 23893, 21853, 21877, 30069, -10795, -10923, 22359, 22359
927 DATA 23893, 21853, 30069, 30069, -10795, -10923, 22359, 22357, 23893, 21853
928 DATA 21877, 30069, -10795, -10923, 22359, 22359, 23893, 23901, 30069, 30069
929 DATA -10795, -10923, 22359, 22357
930 ' toadstool
931 DATA 32, 16, 0, 0, 0, 0, 0, 0, -22016, 186
932 DATA -17918, -24406, -21750, -22357, -22005, -22358, -31998, -24336, 768, 240
933 DATA 768, 240, 768, 240, 768, 240, 768, 240, 1792, 244
934 DATA 256, 80, 0, 0
935 ' toadstool mask
936 DATA 32, 16, -1, -1, -1, -1, 252, 16128, 240, 768
937 DATA 192, 0, 192, 0, 192, 0, 192, 0, 240, 768
938 DATA -3841, -253, -3841, -253, -3841, -253, -16129, -256, -16129, -256
939 DATA -16129, -256, -3841, -253

No comments:

Post a Comment

Maze generator in GW-BASIC

 A while back I was looking at maze making algorithms written in Basic. I was not very happy with any of the ones I could find at the time, ...