Learning python: Shoot the fruit

Hey guys! First: I am learning python by doing the course on codeacademy right now. Parallel to this I started learning different things from a book.

And here is where I need your help: I started a project to made a game called “shoot the fruit” with pygame zero. In this game hte player shoot an apple with the mouse. Everytime I hit the apple I want to print out the actual score to the terminal.

This seems so simple but I don’t get how I make this happen.

Here is the code:

from random import randint

apple = Actor("apple")


def draw():
    screen.clear()
    apple.draw()
    
def place_apple():
    apple.x = randint(10, 800)
    apple.y = randint(10, 600)

def on_mouse_down(pos):
    score = 0
    if apple.collidepoint(pos):  
        score = score + 1
        print("Treffer")
        print(score)
        place_apple()
    else:
        print("Daneben!")
        quit()

place_apple()    
    

Everytime I hit the apple the terminal print my points with 1. Always. Can you help me please?

Thank you!

Hello @cy_bergg. Welcome to the forum!

You are assigning 0 to score at the beginning of your on_mouse_down() function each time it is run, so when you add 1 to 0 you get 1. You’ll need a way to store the score outside of the function, and increment it inside the function each time an apple is hit.

2 Likes