Is this a 2D game using sprites? Or is this a 3D game, but you only move on the x and y axis? Both use very different methods, here's one for 3D:
rem collision demo with sparky's
rem by TheComet
rem setup screen
sync on
sync rate 60
backdrop on
color backdrop 0
hide mouse
rem global variables
global x# as float
global y# as float
global oldx# as float
global oldy# as float
global speed# as float
global jumppower# as float
global velocity# as float
global x as integer
global y as integer
global t as integer
global sx as byte
global sy as byte
global ey as byte
global c as byte
global width as byte
global height as byte
global limb as word
global startx as integer
global starty as integer
global goalx as integer
global goaly as integer
global powerx as integer
global powery as integer
global powerup as byte
rem setup game
gosub generate_images
gosub generate_player
gosub generate_level
gosub set_values
rem reset ink colors
ink rgb(255,255,255),0
color light 0,rgb(60,60,60)
rem main loop
do
rem store old values
oldx#=x#
oldy#=y#
rem move left or right
if leftkey()=1 then dec x#,speed#
if rightkey()=1 then inc x#,speed#
rem jump
if upkey()=1 and grav#=0.0 then grav#=jumppower#
rem use powerup
if spacekey()=1 and powerup=1
powerup=60
grav#=jumppower#*3
endif
rem apply gravity
dec grav#,velocity#
inc y#,grav#
rem check for collision with level
rem here I will cast a sphere the size of the player, and check if this sphere is colliding with anything on the level
rem Note that storing the old positions of the player stop the player from slipping through the walls when travelling
rem at high speeds. It will of course work with out them : SC_SphereCast(2,x#,y#,0,x#,y#,0,3.5,0)
rem the radius of the sphere is 7/2=3.5, and there are no objects to exclude.
if SC_SphereSlide(2,oldx#,oldy#,0,x#,y#,0,3.5,0)
rem reposition the player so it doesn't fall through any walls
rem Here I am getting the sliding collision points. These are the points where the player would end up if it
rem slid along the face of the collision.
x#=SC_GetCollisionSlideX()
y#=SC_GetCollisionSlideY()
rem cast a ray to the ground to check if the player is touching the ground. If so, reset gravity
if SC_RayCast(2,x#,y#,0,x#,y#-4,0,0)
grav#=0.0
endif
rem cast a ray to the roof to check if the player is touching the roof. If so, reset gravity
if SC_RayCast(2,x#,y#,0,x#,y#+4,0,0)
grav#=0-velocity#
endif
endif
rem update player positions
position object 1,x#,y#,0
rem control camera
set camera to follow x#,y#,0,0,40,2,5,0
point camera x#,y#,0
rem die
if y#<-50
rem die message
for t=0 to 80
center text 512,240,"YOU DIED"
sync
next t
rem respawn player
x#=startx:oldx#=startx
y#=starty:oldy#=starty
endif
rem win
if x#>goalx-5 and x#<goalx+5
if y#>goaly-5 and y#<goaly+5
center text 512,240,"GOAL!!"
endif
endif
rem get the powerup
if powerup>2 then dec powerup:if powerup=2 then powerup=0
if x#>powerx-5 and x#<powerx+5
if y#>powery-5 and y#<powery+5
rem get powerup
center text 512,200,"GOT POWERUP!"
center text 512,220,"USE SPACE TO ACTIVATE"
powerup=1
endif
endif
rem refresh screen
sync
rem end of main loop
loop
rem subroutines ---------------------------
generate_images:
rem stone
create bitmap 1,64,64
for t=0 to 1000
sx=rnd(64)
sy=rnd(64)
ey=rnd(4)+1
c=rnd(64)+80
ink rgb(c,c,c),0
circle sx,sy,ey
next t
blur bitmap 1,3
get image 1,0,0,64,64
delete bitmap 1
rem wood
create bitmap 1,64,64
for t=0 to 1000
sx=rnd(64)
sy=rnd(64)
ey=rnd(64)
c=rnd(64)+80
ink rgb(c,20,20),0
line sx,sy,sx,ey
next t
get image 2,0,0,64,64
delete bitmap 1
return
generate_player:
rem I am going to use a sphere to show best how sliding collision using spheres works
make object sphere 1,7
yrotate object 1,90
rem Note that the player doesn't have to have collision set up, unless you want to check if something
rem else is colliding with the player. If so, you can set it up using normal polygon collision, or sphere
rem collision. I will be using polygon collision (0-polygon, 1-sphere, 2-box)
`SC_SetupObject 1,2,0
return
generate_level:
rem create level
restore leveldata
rem read in dimensions
read width
read height
rem create a cube mesh
make object cube 2,10
make mesh from object 1,2
delete object 2
rem make core object
make object plain 2,0,0
rem create level
limb=0
l=0
for y=height to 1 step -1
for x=width to 1 step -1
read t
if t>0 and t<3
inc limb
add limb 2,limb,1
offset limb 2,limb,x*10,y*10,0
texture limb 2,limb,t
endif
if t=3
x#=0-(x*10):y#=y*10
oldx#=x#:oldy#=y#
startx=x#
starty=y#
endif
if t=4
make object plain 3,10,10
position object 3,0-(x*10),y*10,5
color object 3,rgb(0,255,0)
goalx=0-(x*10)
goaly=y*10
endif
if t=5
make object cylinder 4,10
position object 4,0-(x*10),y*10,0
color object 4,rgb(0,0,255)
powerx=0-(x*10)
powery=y*10
endif
if t>5
inc l
make light l
position light l,0-(x*10),y*10,-8
set light range l,80
color light l,rgb((t=6)*255,(t=7)*255,(t=8)*255)
endif
next x
next y
rem delete mesh used
delete mesh 1
rem set up collision for level object
rem this command will set large objects up for polygon collision. The group number can be used later to check collision
rem with object groups. The faces per node number does something I can't remember, so just leave it at 2 :P
SC_SetupComplexObject 2,1,2
SC_UpdateObject 2
rem make a background
make object plain 6,width*10,height*10
position object 6,0-(width*5),height*5,6
texture object 6,1
scale object texture 6,width,height
return
set_values:
rem play around with these a bit to set player properties
velocity#=0.04
speed#=0.5
jumppower#=1.3
return
rem data statements -----------------------------
rem level data. 0=thin air, 1=stone, 2=wood, 3=starting point, 4=target, 5=powerup, 6=red light, 7=green light, 8=blue light
leveldata:
data 14,18 : rem width and height of level
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 1,0,0,7,0,0,0,0,0,0,0,0,0,1
data 1,0,0,4,0,0,0,0,0,1,1,1,0,1
data 1,0,0,2,2,2,0,0,0,1,6,0,0,1
data 1,0,0,2,2,2,0,0,0,1,0,0,0,1
data 1,0,0,0,6,0,0,0,2,1,0,0,0,1
data 1,0,0,0,0,0,1,0,2,1,0,0,0,1
data 1,0,0,0,0,1,0,7,0,1,0,0,0,1
data 1,0,8,0,1,1,0,0,0,1,0,0,0,1
data 1,1,0,1,0,0,1,0,0,1,0,0,0,1
data 1,0,0,0,0,0,3,0,1,1,0,0,0,1
data 1,1,1,1,0,1,1,1,1,1,0,0,0,1
data 0,0,0,0,6,0,0,0,0,0,0,0,0,1
data 0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 0,0,0,0,0,0,0,0,0,0,0,1,0,1
data 0,0,0,0,0,0,0,0,0,0,0,1,8,1
data 0,0,0,0,2,0,0,0,1,0,0,0,5,1
data 1,0,1,0,1,0,1,0,1,0,0,1,1,1
This will also work with slopes. Moving objects requires more advanced methods. You'd have to get the object the player is currently colliding with, store the offset (object to player), and then position the player at the objects position with that offset. If you move the player, you'll have to apply the movement to the offset instead of the real-world coordinates until the player leaves the object again.
TheComet