CLS
SYNC ON
SYNC RATE 0
HIDE MOUSE
P=1
TRUE = 1
FALSE = 0
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,1,1,1,1,1,1,2,1,1,1,1
DATA 1,1,1,1,1,1,1,1,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,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
I've been looking at this code, and figured I'd try my hand at this tile business. I've read through it, I understand how it's working and could probably replicate it myself without just copying and pasting the code. So, I'm quite happy I've gotten that far. Now, there is one thing however that is bothering me ever so slightly.
All I see, is a blue screen, and a white box jumping around at insane speeds. (I've played with the variables to slow the box down, as to get an idea of what the variables did, and then read through the code)
Having looked at another project I did, I noticed I was using sprites for everything. This seems to work. This GET IMAGE and PASTE IMAGE business isn't really doing the trick. As I said, I used sprites for everything, including the backround etc. What I dont want to do, is have 1200 sprites loaded on the screen, and then have all my enemies and player stuff on top of that. Sluggish? Yes, I think it might get a tad silly. So if anyone out there can tell me, or point me in the right direction, I'd really like to know why this bit of code isn't displaying the brown boxes for the platforms like its supposed to (images) and why the player (sprite) seems to display.
Cheers,
- Steve