Post by iCon on Feb 3, 2012 20:02:54 GMT -5
I'm working on a minor game and have a few problems:
1) Where should I make 'onground = false' so the gravity doesn't turn off after the first platform.
2)How can I make 'sub gameover' print gameover before it ends the program.
3)How to improve left/right collision detection? (It is possible to go through the platforms from the sides if entered at the right height from the platform)
4)How to stop the program from lagging every time a platform spawns?
You'll have to use your own images, here's the code:
2)How can I make 'sub gameover' print gameover before it ends the program.
You'll have to use your own images, here's the code:
textmode (TEXT_BUFFERED)
'sets how many frames the cyanman sprite is
dim cyanman (texstripframes ("files\cyanman.png")- 1)
'the variable the animation frames will be loaded into
dim walk (9), stand
dim ploop 'platform loop
dim cl 'collision loop
dim onground = false
const maxplatforms = 99
'loads the imagestrip into cyanman
cyanman = loadtexstrip ("files\cyanman.png")
'Loads the cyanman frames
stand = cyanman (0)
walk (0) = cyanman (8)
walk (1) = cyanman (1)
walk (2) = cyanman (2)
walk (3) = cyanman (3)
walk (4) = cyanman (4)
walk (5) = cyanman (0)
walk (6) = cyanman (5)
walk (7) = cyanman (6)
walk (8) = cyanman (7)
walk (9) = cyanman (0)
'Sets the starting player image
dim sprite
sprite = newsprite (stand)
sprsetpos (320, 241)
'Sets the 1st purple platform
dim platform (maxplatforms), plxsize, plysize
platform (1) = newsprite (loadtex ("files\platform.png"))
sprsetsize (200, 32)
sprsetpos (320, 432)
sprsetyvel (-1)
'spawns platforms in a subroutine
sub platformspawn (platformcount)
dim x = rnd () % 640 + 1
dim xsize = rnd () % 320 + 100
platform (platformcount) = newsprite (loadtex ("files\platform.png"))
sprsetsize (xsize , 32)
sprsetpos (x, 480)
sprsetyvel (-1)
endsub
'GAMEOVER Subroutine
sub gameover ()
locate 16, 12: print "GAME OVER"
end
endsub
'Main animation loop
while true
'Player Control
bindsprite (1)
if scankeydown (VK_RIGHT) then
sprsettextures (walk)
sprsetanimspeed (0.2)
sprsetxflip (false)
sprsetvel (1, 0)
elseif scankeydown (VK_LEFT) then
sprsettextures (walk)
sprsetanimspeed (0.2)
sprsetxflip (true)
sprsetvel (-1, 0)
else
sprsettexture (stand)
sprsetvel (0, 0)
endif
'Stops the player going off screen
if sprleft (1) = -10 and scankeydown (VK_LEFT) then
sprsetxvel (0)
endif
if sprright (1) = spriteareawidth () + 10 and scankeydown (VK_RIGHT) then
sprsetxvel (0)
endif
'platform spawn
for ploop = 2 to maxplatforms
if sprbottom (ploop) = 400 then
platformspawn (ploop + 1)
endif
next
'Platform Collision Detection
for cl = 2 to maxplatforms
'Platform top
if sprx (1) < sprx (cl) + (sprxsize (cl)/2) and sprx (1) > sprx (cl) - (sprxsize (cl)/2) and sprbottom (1) = sprtop (cl) + 3 then
bindsprite (1)
sprsetyvel (-1)
onground = true
endif
'Platform left
if sprtop (1) < spry (cl) + (sprysize (cl)/2) and sprbottom (1) > spry (cl) - (sprysize (cl)/2) and sprright (1) - 16 = sprleft (cl) and scankeydown (VK_RIGHT) then
bindsprite (1)
sprsetxvel (0)
endif
'Platform right
if sprtop (1) < spry (cl) + (sprysize (cl)/2) and sprbottom (1) > spry (cl) - (sprysize (cl)/2) and sprleft (1) + 16 = sprright (cl) and scankeydown (VK_LEFT) then
bindsprite (1)
sprsetxvel (0)
endif
next
'deletes platforms when they reach the top
for cl = 2 to maxplatforms
if sprtop (cl) < 0 then
deletesprite (cl)
endif
next
'gravity
if onground = false then
bindsprite (1)
sprsetyvel (1)
endif
'Game Over
if sprbottom (1) < 0 then
gameover ()
elseif sprtop (1) > 480 then
gameover ()
endif
'Animate the sprites
animatesprites()
drawtext ()
waittimer (20)
wend