Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Help with Lives

Author
Message
rneilg
9
Years of Service
User Offline
Joined: 18th Mar 2015
Location:
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]
rneilg
9
Years of Service
User Offline
Joined: 18th Mar 2015
Location:
Posted: 18th Mar 2015 18:44
Hi fixed it - end instead of exit!
Mike Archer
9
Years of Service
User Offline
Joined: 19th Feb 2015
Location: Wales
Posted: 18th Mar 2015 19:34
I think you wrote 'exit' in the gamelives routine, when you meant to write 'end'.
okee
9
Years of Service
User Offline
Joined: 1st May 2014
Location: Ireland
Posted: 18th Mar 2015 22:22
The lives do seem to seem to decrease.
Instead of exit in the gamelives gosub maybe put in a function
to start the game again

Login to post a reply

Server time is: 2024-04-20 08:23:29
Your offset time is: 2024-04-20 08:23:29