Game Programming‎ > ‎

Shooting Bullets

Shooting Bullets and Other Objects
This will show you how to shoot projectiles across the screen while other events are happening. This is from the game "Pigs Will Fly" that a student wrote. The farmer (you) have to shoot the flying pig with a dart to bring it down. You can download this game from our "student games" page to play it and to look at the code in more detail.

It is recommended that you run a full game loop that controls everything from within it.

e.g. The game loop is updateScreen and it runs everything. sGameRunning is the variable that serves as a flag to start and stop the game

        on updateScreen  
           if sGameRunning is true then      
              movePlayer
              moveTerrain
              moveEnemy
              moveDart
      --      updateScore      
           end if   
        end updateScreen

      Where the start button has the following code:
            on mouseUp
                  put true into sGameRunning
            end mouseUp

        and the Stop button has the following code:
            on  mouseUp
                 put false into sGameRunning   
            end  mouseUp

    Code Setup for firing the Dart, moving it and detecting when it hit the target

        The student that did this code had the following messages:
            - keyDown - the message that tells you the key that was pushed
            - moveDart - that moved the dart towards the target once it was fired 
            - detectCollisions - that watched for the dart to hit the target
        
    Code to fire the dart using the spacebar key on the keyboard:
        He had he following global variables that served as flags:
            - firable - to indicate if the dart was already fired (you can only shoot 1 dart at a time)
            - farmerPos - to save the position of the farmer when he shot the dart. The dart has to go in a straight line from where it was shot while the farmer can still move around (changing his position)

        on keyDown() x
           if x = space then
              if firable  is true then
                 fireDart
              end if
           end if
        end keys1

        He creates a dart (button "buttonShot") to move and saves the position that it was fired from (farmerPos) so he can keep moving it from that location. He makes "firable" false so another dart is not created.
        We also have a new property - "exists". You can check if an object exists before doing something with it (moving it, etc). Otherwise, you will get an error if you try to move an object that does not exist yet. Here he checks if there already is a dart. If not, then he creates one. if it already exists, creating a second one would cause problems

        command fireDart
               local farmerPos
               put the loc of button "farmer1" into farmerPos
               if not exists( button "buttonShot") then
                      clone the button "shotTemplate"
                      set the name of the last button to "buttonShot"
               end if
               set the loc of button "buttonShot" to farmerPos
               put false into firable
        end fireDart


    Code to move the dart:
    He moves the dart (it goes "up") then checks if it hit the Pig. Note that he checks if the dart exists before moving it. Otherwise, he would get an error and the game would give an error. Here he checks if the button exists. If it does, then he can move it

        on moveDart
           if exists ( button "buttonShot") then
                  set the top of button "buttonShot" to the top of button "buttonShot" - 10
                  detectCollisions
                  if exists (button "buttonShot") then
                         if the top of button "buttonShot" < the top of me then
                                delete the button "buttonShot"
                         end if
                  end if
           end if
           put true into firable
        end moveDart

    Code to detct when the Pig was hit with the dart:
    Here he checks for the collision. When the dart hits the Pig, it turns red and stops the gameLoop (sGameRunning = false) He resets the screen and puts up a message. He also speeds up the pig for the next level. There are 3 levels. After the 3rd level, the game is over and you go back to start

        command detectCollisions
               if intersect( button "buttonShot", button "pig1", 255) then
                      set the color of button "pig1" to red
                      put false into sGameRunning
                      if lvl < 3 then
                             put lvl + 1 into lvl
                             resetScreen
                             answer "Pop! You popped that one, but they are getting faster!"
                             put pigSpeed + 5 into pigSpeed
                             if lvl is 3 then
                                    answer "You popped all the piggies, saving the world from a flying pig epidemic."
                                    put 0 into lvl
                                    exit detectCollisions
                                   go to card "Start"
                             end if
                      end if
               end if
        end detectCollisions

Study the code above. It is a little complicated but it is logical and makes sense.
Comments