Game Programming‎ > ‎FlappyBird‎ > ‎

Flappy Birds - Random Columns

FlappyBirds - Make the Columns Random


1. Start them out at a set "home" position  
    Set the Top Columns at 300 and Bottom Columns at 500. 

    This gives the player a chance to get used to the game and movement.
    It also prevents the columns being created on top of a bird. (the bird starts at loc 400)

                Add the following to your preOpenCard handler

                        on ...           (preOpenCard or whatever you called your setup handler)
                               put 1 into birdDirection
                               put 4 into birdSpeed
         put 0 into field "score"
         set the bottom of btn "bird" to 400
         repeat with x = 1 to 4
             set the bottom of btn ("top"&x) to 300
             set the top of btn ("bot"&x) to 500
         end repeat
end ...


2. Bring them back on the card at a random position
 
    When the columns go off the card (on the left) start them on the right with random locations
    I used "200" for the gap. You can use "150" if you want it harder to play

    The Random(200) function generates numbers between 1 and 200

    I start the top columns at 300 + the random number and the bottom columns at 500+that same random number. 
    That gives a separation of 200 pixels (500-300). You can make it harder by using 350 and 500, or other base numbers that are closer together

                Add the following to your "moveCols" handler where the columns are moved to the other side of the screen

         put random(200) into z
         set the bottom of btn ("top"&x) to z+300
         set the top of btn ("bot"&x) to z+500

     You can make it harder by setting them to 350 and 500. Experiment to see which works best 

    for example:

Example #1

on moveCols
   lock screen
   repeat with x = 1 to 4
      put the right of btn ("top"&x) - colSpeed into newPosition
      if newPosition < 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"
         put random(200) into z
         set the bottom of btn ("top"&x) to z+300
         set the top of btn ("bot"&x) to z+500
         add 10 to field "score"
      else
         set the right of btn ("top"&x) to newPosition
         set the right of btn ("bot"&x) to newPosition
      end if
   end repeat
   unlock screen
end moveCols

            Example #2

on moveCols
   lock screen
   repeat with x = 1 to 4

      // Do the Top of the Column
      set the right of btn ("top"&x) to the right of btn ("top"&x) -4
      if the right of btn ("top"&x) < 0 then
         set the left of btn ("top"&x) to the right of card "main"
         put random(200) into z                 //generate a random # between 0-200
         set the bottom of btn ("top"&x) to 300+z                
         set the top of btn ("bot"&x) to 500+z
         add 10 to field "score"
      end if
      
      // Do the Bottom
      set the right of btn ("bot"&x) to the right of btn ("bot"&x) -4
      if the right of btn ("bot"&x) < 0 then
         set the left of btn ("bot"&x) to the right of card "main"
      end if
   end repeat
   unlock screen
end moveCols

            Example #2 - Simplified (re-factored)

               The above code can be simplified (refactored) to this  (I added comments to explain)

on moveCols
   lock screen
   repeat with x = 1 to 4                                                                    // Do for all 4 columns/pipes

                  // Do the Top and the Bottom of the column together
      set the right of btn ("top"&x) to the right of btn ("top"&x) -4        // move top one
      set the right of btn ("bot"&x) to the right of btn ("bot"&x) -4        // move bottom one

      if the right of btn ("top"&x) < 0 then
         set the left of btn ("top"&x) to the right of card "main"             // off card, start at the other side
         set the left of btn ("bot"&x) to the right of card "main"
         put random(200) into z                                                            // generate a random # between 0-200
         set the bottom of btn ("top"&x) to 300+z                                 // Keep them 200 pixels apart 
         set the top of btn ("bot"&x) to 500+z                                       // and the middle of the page
         add 10 to field "score"
      end if
   end repeat
   unlock screen
end moveCols

Comments