Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame zero keep count
Hello, I'm very new to Python. I'm learning from a book called "Coding Games in Python"
The first exercise walks me through a simple "Shoot the Fruit" program. (using pygame zero)
It then suggests that I tweak it so that it can keep count of the number of times I click successfully.

I created the variable "score = 0" then inside the "if successful click" statement I added the line "score = score + 1", I also tried the variation: "score += 1" but always get an error message.

In my mind this is perfectly logical but clearly I'm doing something wrong.
I've tried searching for answers and examples but I think my question is perhaps too basic. (or am not wording my queries properly)
Help and guidance in understanding is appreciated.

OS: Windows 10
Python 3.7.2

Here is my code:

from random import randint

apple = Actor("apple")
score = 0

def draw():

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

def on_mouse_down(pos):
    if apple.collidepoint(pos):
        score = score + 1
        print("Good Shot! Your score is: ", score)
        print("You missed!")

This is the message I recceive:

pygame 1.9.4 Hello from the pygame community. Traceback (most recent call last): File "c:\program files\python37\lib\", line 193, in _run_module_as_main "__main__", mod_spec) File "c:\program files\python37\lib\", line 85, in _run_code exec(code, run_globals) File "C:\Program Files\Python37\Scripts\pgzrun.exe\", line 9, in <module> File "c:\program files\python37\lib\site-packages\pgzero\", line 93, in main run_mod(mod) File "c:\program files\python37\lib\site-packages\pgzero\", line 113, in run_mod PGZeroGame(mod).run() File "c:\program files\python37\lib\site-packages\pgzero\", line 217, in run self.mainloop() File "c:\program files\python37\lib\site-packages\pgzero\", line 247, in mainloop self.dispatch_event(event) File "c:\program files\python37\lib\site-packages\pgzero\", line 172, in dispatch_event handler(event) File "c:\program files\python37\lib\site-packages\pgzero\", line 164, in new_handler return handler(**prepped) File "", line 16, in on_mouse_down score = score + 1 UnboundLocalError: local variable 'score' referenced before assignment
score is unknown to the function on_mouse_down. The best way when not using classes would be to pass that variable to that function. Global keyword is a simple option, but it invites bad programming style.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Count how many carpets you need to fill room floor without multiplication/division Ech0ke 1 47 Apr-20-2019, 07:50 PM
Last Post: ichabod801
  While loop that needs to count up ImLearningPython 4 275 Dec-17-2018, 02:30 PM
Last Post: ImLearningPython
  If + Count Itay 5 324 Nov-09-2018, 09:57 PM
Last Post: nilamo
  Python count mohan7490 3 391 Nov-07-2018, 04:19 AM
Last Post: ichabod801
  Unique Count Talch 0 388 Aug-16-2018, 11:15 AM
Last Post: Talch
  Unable to implement loop count zukochew 3 451 Jul-25-2018, 06:23 PM
Last Post: ichabod801
  Count Letters in a Sentence (without using the Count Function) bhill 3 583 Jun-19-2018, 02:52 AM
Last Post: bhill
  Program: count and find Truman 3 929 Feb-11-2018, 11:06 PM
Last Post: Larz60+
  Count in Dictionaries Wolfpack2605 2 682 Jan-28-2018, 03:34 AM
Last Post: ka06059
  Replacing string values with count tomhuang 2 887 Jul-19-2017, 06:38 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)