Shoot
OVERVIEW
This app is a simple game where you have to click/shoot the alien to stop it from reaching the right hand side of the screen.
FILES
- Create a folder called 'Shoot'.
- Inside the Shoot folder create two more folders 'images' and 'sounds'
- Download the files and place them in the appropriate folder


Task 1 - Setup
- Open Mu
- Choose/change mode to Pygame Zero
- Type the following code to setup the screen and draw the alien
alien = Actor('alien')
alien.topleft = (0, 0)
WIDTH = 450
HEIGHT = 800
def draw():
screen.fill((0, 0, 0))
alien.draw()
- The code above
- Creates the alien
- Sets the Width and Height of the screen
- Tells the computer what to draw
- Save the code in the 'Shoot' folder as 'shoot.py'
- Press play and the game should start with a black background and the Alien in the top left corner.
Task 2 - Movement
- Graphics on a computer screen use a different system to the one you are used to in Maths.
- The top left of the screen is (0,0) and the y-axis is positive when going down.
- Actors in the game use the terms below for positioning
- Update your code to include the update function which will add 2 to the aliens x position every time update is called.
alien = Actor('alien')
alien.topleft = (0, 0)
WIDTH = 450
HEIGHT = 800
def draw():
screen.fill((0, 0, 0))
alien.draw()
def update():
alien.left = alien.left + 2
- Your game should now move the alien from the left to the right.
Task 3 - Shooting the Alien
- To shoot the alien you will need to do three things
- Check if the alien has been clicked
- When clicked subtract 50 from the aliens x position
- when clicked give the alien a random y position on the screen
- Update your code to include the
import random
alien = Actor('alien')
alien.topleft = (0, 0)
WIDTH = 450
HEIGHT = 800
def draw():
screen.fill((0, 0, 0))
alien.draw()
def update():
alien.left = alien.left + 2
def on_mouse_down(pos):
if alien.collidepoint(pos):
set_random_position()
def set_random_position():
alien.left = alien.left - 50
alien.top = random.randint(0, HEIGHT)
Task 4 - Score
- To add a score to the game you will need to do three things
- Create a variable to store the score
- Draw the score on the screen
- Update the score when the alien is clicked
- Update your code to include the score
import random
alien = Actor('alien')
alien.topleft = (0, 0)
WIDTH = 450
HEIGHT = 800
score = 0
def draw():
screen.fill((0, 0, 0))
screen.draw.text("Score: " + str(score), (0,0))
alien.draw()
def update():
alien.left = alien.left + 2
def on_mouse_down(pos):
if alien.collidepoint(pos):
set_random_position()
global score
score = score + 1
def set_random_position():
alien.left = alien.left - 50
alien.top = random.randint(0, HEIGHT)
Task 5 - End Game
- To end the game you will need to do two things
- Check if the alien has went off the screen
- If the alien goes off the screen display 'game over'
- Add the code below to display 'Game over' when the alien reaches the screen edge.
import random
alien = Actor('alien')
alien.topleft = (-1000, 0)
WIDTH = 450
HEIGHT = 800
score = 0
def draw():
screen.fill((0, 0, 0))
screen.draw.text("Score: " + str(score), (0,0))
alien.draw()
if alien.left > WIDTH:
screen.draw.text("GAME OVER", (0,20))
def update():
alien.left = alien.left + 2
def on_mouse_down(pos):
if alien.collidepoint(pos):
set_random_position()
global score
score = score + 1
def set_random_position():
alien.left = alien.left - 50
alien.top = random.randint(0, HEIGHT)
Task 6 - Comments
- Comments are used to make your code more readable
- Comments can be written above or on the same line as code
- Comments start with a hashtag
- Add the comments below or use your own
import random # This imports the code that lets you use random numbers
alien = Actor('alien') # This creates an actor using the graphic 'alien'
alien.topleft = (-1000, 0) # This sets the aliens position to the top left of the screen
WIDTH = 450 # VARIABLE - Set width of screen
HEIGHT = 800 # VARIABLE - Set height of screen
score = 0 # VARIABLE - Store the score
def draw():
screen.fill((0, 0, 0)) # Fill black
screen.draw.text("Score: " + str(score), (0,0)) # Draw score
alien.draw() # Draw alien
if alien.left > WIDTH: # If alien goes off screen
screen.draw.text("GAME OVER", (0,20)) # Draw game over
def update():
alien.left = alien.left + 2 # Add 2 to alien x position
def on_mouse_down(pos):
if alien.collidepoint(pos): # If alien collides with mouse position
set_random_position() # Use set_random_position function
global score # We want to use the global variable score
score = score + 1 # Add one to score
def set_random_position():
alien.left = alien.left - 50 # Remove 50 from alien x position
alien.top = random.randint(0, HEIGHT) # Set alien y to random position from 0 to HEIGHTExtension 1 -
Extension 1 - Sounds and Changing Images
- To make the game more interactive you can include sounds and animation
- The code below will play a sound and change the alien image every time the alien is clicked
import random
alien = Actor('alien')
alien.topleft = (0, 0)
WIDTH = 450
HEIGHT = 800
score = 0
def draw():
screen.fill((0, 0, 0))
screen.draw.text("Score: " + str(score), (0,0))
alien.draw()
if alien.left > WIDTH:
screen.draw.text("GAME OVER", (0,20))
def update():
alien.left = alien.left + 2
def on_mouse_down(pos):
if alien.collidepoint(pos):
set_alien_hurt()
set_random_position()
globalscore
score = score + 1
def set_random_position():
alien.left = alien.left - 50
alien.top = random.randint(0, HEIGHT)
def set_alien_hurt():
sounds.eep.play()
alien.image = 'alien_hurt'
clock.schedule_unique(set_alien_normal, 0.5)
def set_alien_normal():
alien.image = 'alien'
- Add comments to the new lines of code explaining what you think each line does.
- If you are unsure of any lines leave them out or ask your teacher.
Extension 2 - Reskin
- Now that the game is working you can add your own style to it.
- Edit the images for the Alien or create your own
- You don't have to keep the theme of aliens
- To keep the transparent background of the images you will need to use a graphics application like paint.net
- Here are some examples
- Will you need to change the code to get the images to work?
Extension 3 - Fixing Text
- The text that displays 'GAME OVER' needs to stand out more
- Edit the code to achieve the following
- Centre the text on the screen
- Change the text colour
- Increase the font size
- Any other effects you would like
- The examples below show how you can change the text, use them to help you.
screen.draw.text("Text color", (50, 30), color="orange")
screen.draw.text("Font name and size", (20, 100), fontname="Boogaloo", fontsize=60)
screen.draw.text("Positioned text", topright=(840, 20))
screen.draw.text("Allow me to demonstrate wrapped text.", (90, 210), width=180, lineheight=1.5)
screen.draw.text("Outlined text", (400, 70), owidth=1.5, ocolor=(255,255,0), color=(0,0,0))
screen.draw.text("Drop shadow", (640, 110), shadow=(2,2), scolor="#202020")
screen.draw.text("Color gradient", (540, 170), color="red", gcolor="purple")
screen.draw.text("Transparency", (700, 240), alpha=0.1)
screen.draw.text("Vertical text", midleft=(40, 440), angle=90)
screen.draw.text("Combining the above options",
midbottom=(427,460), width=360, fontname="Boogaloo", fontsize=48,
color="#AAFF00", gcolor="#66AA00", owidth=1.5, ocolor="black", alpha=0.8)
Extension 4 - Fixing Random Positions
- When the alien is clicked it is moved to a random y position using this line of code
alien.top = random.randint(0, HEIGHT) - This line of code sets the top of the alien to a random number from 0 to the HEIGHT of the screen.
- This means that sometimes the alien goes completely off the bottom of the screen.
- Update the line of code so that the alien will never go off the bottom of the screen
HINTS
- It is ok for the random number to start at zero but the HEIGHT value moves the alien too far down the screen
- The HEIGHT of the screen is 800
- The height of the alien is 92
Extension 5 - Vertical Mode
- Currently the alien moves from the left to the right of the screen.
- Update the code so that the alien will start at the top of the screen and move down
- Update all parts of the code to ensure the game works correctly.