Handling Intersections of the bird with the Pipes (the Columns)
A. Adding code to detect intersections
We are going to add code to detect collisions with the bird and the pipes. We will keep this code separate by putting it in it's own message - checkBird
Add the following code on the card script - anywhere after any other message:
(I am putting it after the "movePipes" code but before the "startGame" code)
... end movePipes
on checkBird
if intersect (btn "bird" , btn ("top" &x) , 255) then
answer "Oops, You crashed !!"
if intersect (btn "bird" , btn ("bot" &x) , 255) then
answer "Oops, You crashed !!"
Add the message "checkBird" right after you move the pipes in the "moveBird" code (it makes sense to check if the bird runs into the pipes right after you moved them)
set the bottom of btn "bird" to the bottom of btn "bird" + birdDirection * birdSpeed
set the right of btn "city01" to the right of btn "city01" - 3
set the right of btn "city02" to the right of btn "city02" - 3
if the right of btn "city01" < 0 then
set the left of btn "city01" to the right of btn "city02" -3
if the right of btn "city02" < 0 then
set the left of btn "city02" to the right of btn "city01" -3
send "moveBird" to me in 20 millisecs
C. The entire code so far, that goes on the card: Card Script: (this code goes on the card)
global birdDirection // +1 is down, -1 is up global birdSpeed // speed the bird falls global gameisRunning on moveBird
lock screen
set the bottom of btn "bird" to the bottom of btn "bird" + birdDirection * birdSpeed set the right of btn "city01" to the right of btn "city01" - 3 set the right of btn "city02" to the right of btn "city02" - 3 if the right of btn "city01" < 0 then set the left of btn "city01" to the right of btn "city02" -3 end if if the right of btn "city02" < 0 then set the left of btn "city02" to the right of btn "city01" -3 end if
unlock screen
movePipes checkBird if gameisRunning then send "moveBird" to me in 20 millisecs end if end moveBird
on movePipes
lock screen
repeat with x = 1 to 3 put "top" & x into t put "bot" &x into b set the right of btn t to the right of btn t - 5 set the right of btn b to the right of btn b -5 if the right of btn ("top" &x) < 0 then set the left of btn ("top" &x) to the right of card "main" set the left of btn ("bot" &x) to the right of card "main" end if end repeat
unlock screen
end movePipes
on checkBird repeat with x = 1 to 3 if intersect (btn "bird" , btn ("top" &x) , 255) then answer "Oops, You crashed !!" stopGame end if if intersect (btn "bird" , btn ("bot" &x) , 255) then answer "Oops, You crashed !!" stopGame end if end repeat end checkBird
on startGame put true into gameisRunning put +1 into birdDirection // bird will be falling put 3 into birdSpeed moveBird end startGame
on stopGame put false into gameisRunning set the bottom of btn "bird" to 200 end stopGame
on keyDown theKey if theKey is space then put -1 into birdDirection // move the bird up end if end keyDown
on keyUp put +1 into birdDirection // let the bird start falling again end keyUp
|
|