Here's some code for array collision. I can't remember who made it. It's a platform game, but the collision is the same anyway.
CLS
SYNC ON
SYNC RATE 0
HIDE MOUSE
P=1
TRUE = 1
FALSE = 0
set text opaque
tilesize = 16
playersize = 16
screenwidth = 640
screenheight = 480
xtiles = screenwidth/tilesize
ytiles = screenheight/tilesize
` Here we're calculating how many tiles we need to fill the screen.
DIM map(xtiles,ytiles)
` This creates a tilemap, so we can store the kind of tile at every
` location. (it's a bit bigger than it strictly needs to be, but that's
` because I do a check later on which involves adding one to the elements)
blue = RGB(0,0,200)
brown = RGB(100,50,0)
white = RGB(200,200,200)
REM image values
background = 1
platform = 2
` You'd normally have a lot more of these. One value for each kind
` of tile.
player = 1001
REM Getting our images for the tiles
INK blue,0
BOX 0,0,tilesize,tilesize
GET IMAGE background,0,0,tilesize,tilesize
INK brown,0
BOX 0,0,tilesize,tilesize
GET IMAGE platform,0,0,tilesize,tilesize
` Normally, you'd have a tileset to get all your images from.
INK white,0
BOX 0,0,playersize,playersize
GET IMAGE player,0,0,playersize,playersize
` Normally, you would have a sprite animation sheet to get the player from.
REM Read the map into memory
FOR y = 0 TO ytiles
FOR x = 0 TO xtiles
READ map(x,y)
NEXT x
NEXT y
` skip down to the DATA statement for explanation.
xpos# = 50.0
ypos# = 200.0
` we have to store the character's position as reals (instead of integers)
` because we'll be using gravity.
xspeed# = 0.0
yspeed# = 0.0
gravity# = 0.2
acc# = 0.2
friction# = 0.95
jump# = (-5.0)
player_on_ground = FALSE
DO
REM Find out what tile the player is occupying.
xtilepos# = xpos#/tilesize
ytilepos# = ypos#/tilesize
REM Find out what tiles are *beneath* the player.
undertile1 = map(INT(xtilepos#),INT(ytilepos#)+1)
undertile2 = map(INT(xtilepos#)+1,INT(ytilepos#)+1)
REM Should the player fall?
IF undertile1 = background AND undertile2 = background
yspeed# = yspeed# + gravity#
player_on_ground = FALSE
ELSE
yspeed# = 0.0
ypos# = INT(ytilepos#)*16
` (This pops the player up if he is inside of a platform tile)
player_on_ground = TRUE
ENDIF
REM Check keyboard for jump
IF SPACEKEY() AND player_on_ground THEN yspeed# = yspeed# + jump#
ypos# = ypos# + yspeed#
IF ABS(yspeed#) < minspeed# THEN yspeed# = 0.0
REM Check keyboard for move
IF RIGHTKEY() THEN xspeed# = xspeed# + acc#
IF LEFTKEY() THEN xspeed# = xspeed# - acc#
REM Update xpos#
xspeed# = xspeed# * friction#
xpos# = xpos# + xspeed#
IF xpos# < 0.0
xpos# = 0.0
xspeed# = 0.0
ENDIF
IF xpos# > (screenwidth - playersize)
xpos# = (screenwidth - playersize)
xspeed# = 0.0
ENDIF
IF ypos# < 0.0
ypos# = 0.0
yspeed# = 0.0
ENDIF
` Boundary detection.
if P = 1
FOR y = 0 TO ytiles-1
FOR x = 0 TO xtiles-1
PASTE IMAGE map(x,y),x*tilesize,y*tilesize
NEXT x
NEXT y
P = 0
endif
` Yes, we are redrawing the whole screen every loop! This PASTE IMAGE
` statement puts the tile-type of each location in its proper place.
SPRITE player,xpos#,ypos#,player
TEXT 0,0,STR$(SCREEN FPS())
SYNC
LOOP
REM The tilemap!
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2
DATA 1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,2,1,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DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
DATA 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
REMSTART
What the heck is all this?
Well, it's our level. Every number represents a tile on our map.
The 1's represent background, and the 2's represent platforms. I wanted
to keep the data 40x30 form, so you can see the level relatively clearly,
but that wasn't possible. Each *pair* of DATA statements is one horizontal
line of tiles.
Obviously, there are other ways of designing your levels! You'll want to
use a tile-mapping program, and save your levels instead of hard-coding
them in DATA statements like this. But this will do, to show you how the
array holds the whole level.
REMEND
