Posted: 18th Mar 2015 14:14
Hi, I have just started AppGameKit and have got stuck on the simplest of things. I have got movement and collisions working but cannot decrease lives!!!! Can someone help please.
I have the code below and the game lives GOSUB is at the bottom.
Thanks for any help.
[
rem load in images
loadimage(1,"goodguy.png")
loadimage(2,"barrierland.png")
loadimage(3,"background.png")
loadimage(5, "intro.jpg")
loadsound (4, "phaser.wav")
rem setup variables
GoodGuy = 1
GoodGuyXpos = 100
GoodGuyYpos = 550
BarrierLand = 2
BarrierLandXpos = 400
BarrierLandYpos = 500
Background = 3
intro = 5
lives = 3
time# = timer()
rem create the sprite
createsprite(Background,3) `when you create a sprite it automatically goes to 0,0
createsprite(goodguy, 1)
createsprite(BarrierLand, 2)
rem put the sprite on the screen
setspriteposition(goodguy, GoodGuyXpos, GoodGuyYpos)
setspriteposition(BarrierLand, BarrierLandXpos, BarrierLandYPos)
`gosub timer
gosub gameintro
do
print("Hello")
gosub MovePlayer
gosub CheckforCollisionWithBarrier
gosub timer
gosub quitkey
gosub gamelives
sync()
loop
MovePlayer:
`keep track of last pos in case of collision
GoodGuyLastX = GoodGuyXPos
GoodGuyLastY = GoodGuyYPos
rem check which key has been pressed
if getrawkeystate(37)
GoodGuyXPos = GoodGuyXPos - 5
endif
if getrawkeystate(38)
GoodGuyYPos = GoodGuyYPos - 5
endif
if getrawkeystate(39)
GoodGuyXPos = GoodGuyXPos + 5
endif
if getrawkeystate(40)
GoodGuyYPos = GoodGuyYPos + 5
endif
setspriteposition(GoodGuy, GoodGuyXpos, GoodGuyYPos)
return
CheckforCollisionWithBarrier:
`set the shape for sprite collision - see: http://www.appgamekit.com/documentation/examples/sprites/5_collision.htm
`SetSpriteShape( spriteA, 2 )
`SetSpriteShape( spriteB, 2 )
`check for collision - 1 = player, 0 anything else on the screen
if getspritecollision(GoodGuy,BarrierLand) = 1
playsound (4)
GoodGuyXPos = random (0,1024)
GoodGuyYPos = random (0,768)
endif
return
``````````````````````````````````````````````````````````````````````````
timer:
`do
print (timer ()-time#)
if time# > 5.0
sync()
exit
endif
`loop
return
`````````````````````````````````````````````````````````````````````````
quitkey:
`q to quit
if getRawKeyPressed(81) = 1
end
endif
return
```````````````````````````````````````````````````````````````````````
gameintro:
`set a variable for intro first
intro = 5
`then create the sprite (from loadimage above)
createsprite(intro,5)
`start the loop
do
`wait for the space bar to be pressed
if getRawKeyPressed(32) = 1
exit
endif
`sync the screen
sync()
loop
`delete the sprite and start the game
deletesprite (intro)
return
````````````````````````````````````````````````````````````````````````````
gamelives:
if getspritecollision(GoodGuy,BarrierLand) = 1
playsound (4)
GoodGuyXPos = random (0,1024)
GoodGuyYPos = random (0,768)
lives = lives-1
if lives = 0
exit
endif
endif
Return]