Tiles are your friend!
Get to know how to use arrays. You'll need a 2 dimensional array to set up your side scroller's map or platform. You'll need to use a tile system to draw your platform.
Collision detection with your tiles is simply getting your sprites position in your 2D map data and checking to see if it will or is colliding with a tile in that data.
I've provided a really, very basic way of how you can accomplish this. You can substitute the box command and paste sprites in your source. You'll need to make sure your tiles are uniform for this example. That is they will need to be 16x16 or 32x32 or whatever. You'll need to set the
tile_width and
tile_height variables in your source.
Simple tile tutorial:
The source file can be downloaded if you're logged in.
hide mouse
sync on
sync rate 60
rem Map height and width
map_height=10
map_width=10
rem Tile height and width
tile_height=32
tile_width=32
rem Declare map array
dim map(map_width,map_height) as byte
rem Declare game_text array
dim game_text(4) as string
rem Read map data
for loop1=0 to map_height-1
for loop2=0 to map_width-1
read map(loop2,loop1)
if map(loop2,loop1)=2: rem Player start position
sxpos=loop2
sypos=loop1
map(loop2,loop1)=0: rem Clear the start position so it's not an obstacle.
else
if map(loop2,loop1)=3: rem Player finnish position
fxpos=loop2
fypos=loop1
endif
endif
next loop2
next loop1
rem Read game_text data
for loop1=0 to 4
read game_text(loop1)
next loop1
rem Init game
gosub draw_map
xpos=sxpos
ypos=sypos
gosub draw_player
rem Main loop
do
rem Get player input
if upkey()<>0
cx=0
cy=-1
goto check_tile
endif
if downkey()<>0
cx=0
cy=1
goto check_tile
endif
if leftkey()<>0
cx=-1
cy=0
goto check_tile
endif
if rightkey()<>0
cx=1
cy=0
goto check_tile
endif
rem Update graphics
update_graphics:
cls
gosub draw_map
gosub draw_player
sync
rem Level complete
rem I just wanted to show you how to exit a loop.
rem You can also use gosub's and goto's or functions for this.
if level_complete=1
exit
endif
loop
rem End level
end_level:
cls
gosub display_text:
wait key
end
rem Draw map
draw_map:
for loop1=0 to map_height-1
for loop2=0 to map_width-1
if map(loop2,loop1)<>0
height=loop1*tile_height
width=loop2*tile_width
if map(loop2,loop1)=1
ink rgb(255,255,255),rgb(0,0,0): rem Wall color
else
ink rgb(255,0,255),rgb(0,0,0): rem Exit color
endif
box width,height,width+tile_width,height+tile_height
endif
next loop2
next loop1
return
rem Draw player
draw_player:
ink rgb(255,255,0),rgb(0,0,0): rem Player color
height=ypos*tile_height
width=xpos*tile_width
box width,height,width+tile_width,height+tile_height
return
rem Display text
display_text:
ink rgb(255,255,255),rgb(255,255,255): rem Text color
for loop1=0 to 4
for loop2=1 to len(game_text(loop1))
print mid$(game_text(loop1),loop2);
sync
wait 50
next loop2
print
sync
wait 200
next loop1
return
rem Check tile
check_tile:
cxpos=xpos+cx
cypos=ypos+cy
if map(cxpos,cypos)=0: rem Blank tile - Player position is updated.
xpos=cxpos
ypos=cypos
wait 50
else
if map(cxpos,cypos)=3: rem Finnish tile - Success, level is complete.
xpos=fxpos*tile_width
ypos=fypos*tile_height
cls
gosub draw_map
for loop1=0 to 99
ink rgb(rnd(255),rnd(255),rnd(255)),rgb(0,0,0)
box xpos,ypos,xpos+tile_width,ypos+tile_height
sync
wait 50
next loop1
level_complete=1
endif
endif
goto update_graphics:
rem Map data
rem This map is 10x10 but the array isn't map(10,10) it's map(9,9)
rem because 0 is the first in an array not 1.
data 1,1,1,1,1,1,1,1,1,1: rem 0's are empty spaces
data 1,0,0,0,1,0,0,0,3,1: rem 1's are walls
data 1,0,1,0,0,0,1,0,0,1: rem 2 is the start position
data 1,0,1,1,1,1,1,1,1,1: rem 3 is the exit/finnish position
data 1,0,0,0,0,0,0,0,0,1
data 1,1,1,1,1,1,1,1,0,1
data 1,0,0,0,0,0,0,0,0,1
data 1,0,1,1,1,1,1,1,1,1
data 1,0,0,0,0,0,0,0,2,1
data 1,1,1,1,1,1,1,1,1,1
rem Text data
rem There are 5 text data statements but again 0 is first in the
rem array and not 1.
data "You made it to the end."
data "I hope you leanred something in the process."
data "Good luck."
data ""
data "Press any key to end."
So what now?
Where do you go from here? Well I've outlined a list of things you can try to improve on this code. They're listed in order of difficulty or skill level. Honestly, the only thing that you may find difficult is step 7 and 8. Leave those for last.
Things to try:
1) Use sprites or images instead of the box commands.
2) Use a background image as a layer behind your tiled map.
3) Use sounds and music that changes from game play to end sequence.
4) Replace the keyboard input with joystick input.
5) Create multiple maps/levels.
6) Create larger maps/levels.
7) Enhance the movement so that player doesn't move in such large steps.
8) Offset the viewable map area to generate a side scrolling platform.
Once you've finnished the steps above, you'll have a good basic framework for your platform games. All you'll need to add is more sprites for enemies, items, players, and some ai routines for your enemies. I never bothered to mention a scoring system or anything like that. I gather that once you've made it this far, you'll have figured that out.
Show us your stuff!
I'd like to see what you can come up with using this simple source. Have fun, and don't get discouraged. If you get stuck on something, ask for help. If nobody responds, try working on another feature. Eventually, someone will help you or you'll figure it out.
Ps. Here's a penguin