Shoot the Fruit

When the game starts, an apple appears on the screen for you to “shoot.” If you hit it, a “Good shot!” message pops up, and the apple appears at another point on the screen. But if you miss, a “You missed!” message is shown, and the game ends.


How it works

The game is constantly checking whether you’ve clicked the mouse button. Every time you click on the apple, it needs to be drawn again somewhere else on the screen. If you click and miss, the game will end. The below flowchart shows the logic behind the game. The main part of the code is a loop that checks if you have clicked on the apple or not.

Get shooting!

In this program, you’ll start by drawing an apple on the screen, then you’ll learn to place it at random points before you start shooting it. Let’s get coding!

Open Replit

Create a new Repl called shoot_the_fruit.

Set up an image folder

This game uses an image of an apple. In the upper left corner of the screen, click the folder icon with a plus sign and name the folder images.

Put the image into the folder

Click HERE to download the Shoot the Fruit images. Find the file called “apple.png”. Click the three dots and select Upload file. Then, select apple.png from the files.

Drag apple.png into the images folder. Your folders should look something like this now.

Import pgzrun

Remember, in order to run our game, we need to import pgzrun. Add the below code.

import pgzrun

Introducing an Actor

Now you can start writing some code. Go to main.py, erase the sample code, and write this line of code in the editor window. Then click Run.

apple = Actor("apple")
**In computer games development, a sprite is an object, like a coin or an enemy character, that is controlled by code. Actors in Python are like Sprites in Scratch. An Actor can be drawn on the screen, moved around, and even interact with other Actors in the game. Each Actor is given a “script” (the Python code) to tell it how to behave in the game.

Drawing the apple on the screen

Next you need to “draw” the apple on the screen. To do this, you can use a built-in Pygame Zero function called draw(). This function is used to redraw the game screen. For example, if a character has moved or a score has changed, you can use this function to update the screen with those changes. Write this code beneath your previous code.

def draw():
    screen.clear()
    apple.draw()

Call pgzrun.go()

In order to start the game, we have to use our pgzrun import by calling pgzrun.go() at the very end of our program. Add the foillowing line to your program. MAKE SURE IT'S ALWAYS THE LAST LINE OF CODE IN YOUR PROGRAM.

pgzrun.go()

Test the code

Now it’s time to test the code. Go ahead and click Run again and see what happens.

Placing the apple

At the moment, the apple appears in the top-left corner of the game window. You can change the code to place the apple exactly where you want it on the screen. Write this function, which will place the apple at the coordinates (300, 200).

def place_apple():
    apple.x = 300
    apple.y = 200

Running the function

After you’ve written the function to place the apple on the screen, you need to tell Python to run it. Add this extra line of code to run the function called place_apple().

place_apple()

Test it again

Save your file and then run the code again. This time the apple will appear at the point (300, 200).

Dealing with clicks

Now it’s time to write the code that will run when you press the mouse. Pygame Zero has a built-in function called on_mouse_down(), which is run every time you click the mouse. Type this code in between the code you added in Step 9 and Step 10, then run it. You should see the message “Good shot!” in the Command Prompt or Terminal window each time you click the mouse.

def on_mouse_down(pos):
    print("Good shot!")
    place_apple()    

Adding some logic

At this point, the “Good shot!” message is displayed every time you click the mouse, but we only want it to show if the player actually hits the apple. You can do this by changing the code in the previous step to include an if statement. This code checks if the apple and the mouse cursor are in the same position. If they are, the message is displayed.

def on_mouse_down(pos):
    if apple.collidepoint(pos):
        print("Good shot!")
        place_apple()   

Missed a shot? Game over!

Add some more logic to your code, so that if you miss a shot and don’t click on the apple, it quits the game. Add an else statement in order to make this happen.

def on_mouse_down(pos):
    if apple.collidepoint(pos):
        print("Good shot!")
        place_apple()
    else:
        print("You missed!")
        quit()  

Importing Random

The game is very easy at this point, because the apple is always drawn at the same place on the screen. You can use Python’s Random module to make the game more challenging by placing the apple at a random point on the screen each time it is drawn. First, add this code at the very top of your program.

from random import randint

Using Random

Change the code you typed in Step 9 to look like this. The code will now use the randint() function to pick a random number between 10 and 500 for the x coordinate and a random number between 10 and 500 for the y coordinate.

def place_apple():
    apple.x = randint(10, 500)
    apple.y = randint(10, 500)

Time to shoot!

You did it! Run your program to play the game. Each time you “shoot” the apple, it will move to a random place on the screen for you to “shoot” again.

Hacks and Tweaks

Additionally, you can try the following customizations to make your game even more interesting:

Change the fruit image

Find a picture of another fruit in the Python Games Resource Pack or create one using an THIS 8-bit editor online. Make sure you’ve got a suitably sized image before you save it in the images folder. Then name the image and change the code accordingly to use the new fruit as an Actor.

kiwi = Actor("kiwi")   

Keep count

Change your code so that it can keep count of the number of times you click successfully. You can store the count in a variable, start by setting the variable to 0, increase the count by 1 each time you click on the apple, and use print() to show the score in the Command Prompt or Terminal window each time the apple is drawn on the screen.

Add distractions

Why not add another Actor to the game to distract the player in the hope they’ll click on that object by mistake? For instance, a red ball might look similar enough to an apple to fool the player!

Remove the game over condition

Remove the command that quits the game if you miss a shot, making the game easier to play and less frustrating for the user.

Add Levels

Make the game harder as the score increases. You can try adding more bad actors/disractions or using smaller fruit.

Copyright © KX Technology Group, LLC. All Rights Reserved.