Game Programming‎ > ‎FlappyBird‎ > ‎

Flappy Bird - 3



global gameisRunning
global birdDirection

on moveBird
   set the bottom of btn "bird" to the bottom of btn "bird" + birdDirection
   
   set the right of btn "city01" to the right of btn "city01" -4
   if the right of btn "city01" < 0 then
      set the left of btn "city01" to the right of btn "city02" -4
   end if
     set the right of btn "city02" to the right of btn "city02" -4
   if the right of btn "city02" < 0 then
      set the left of btn "city02" to the right of btn "city01" -4
   end if
   movecols
   if gameisRunning then
      send moveBird to me in 20 millisecs
   end if
end moveBird

on movecols
   repeat with i=1 to 4
      // move the top and bot columns together
      set the right of btn ("top"& i ) to the right of btn ("top"& i ) -5
      set the right of btn ("bot"& i ) to the right of btn ("bot"& i ) -5
      
      // if they go off the page
      if the right of btn ("top"& i ) < 0 then
         
         // start them again at the right side of the card 
         set the left of btn ("top"& i ) to the right of card "main" -5
         set the left of btn ("bot"& i ) to the right of card "main" -5
         
         // generate a random number between 1 and 100
         put random(100) into newloc
         
         // set the pipes to start at that new location
         set the bottom of btn ("top"& i ) to 180+newLoc
         set the top of btn ("bot"& i ) to 350+newLoc
      end if
   end repeat
   checkbird
end movecols

on checkbird
   // We do this in a loop using nymbers 1 to 4
   
   repeat with i=1 to 4
      // check bird intersect with top1, top2, top3, top4
      if intersect(btn "bird" , btn ("top"&i), 255 ) then
         endGame
      end if
      // check bird intersect with bot1, bot2, bot3, bot4
      if intersect(btn "bird" , btn ("bot"&i), 255 ) then
         endGame
      end if
   end repeat
   
end checkbird

on endGame
   answer "You crashed!"
   stopGame
end endGame
   
on startGame
   put true into gameisRunning
   put +5 into birdDirection
   moveBird
end startGame

on stopGame
   put false into gameisRunning
   set the bottom of btn "bird" to 300
end stopGame

on keyDown theKey
   if theKey is space then
      put -5 into birdDirection
   end if
end keyDown

on keyUp
   put +5 into birdDirection
end keyUp

Comments