Python Forum
pygame zero keep count
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame zero keep count
#1
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():
    screen.clear()
    apple.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)
        place_apple()
    else:
        print("You missed!")
        quit()

place_apple()
This is the message I recceive:

Error:
pygame 1.9.4 Hello from the pygame community. https://www.pygame.org/contribute.html Traceback (most recent call last): File "c:\program files\python37\lib\runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "c:\program files\python37\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Program Files\Python37\Scripts\pgzrun.exe\__main__.py", line 9, in <module> File "c:\program files\python37\lib\site-packages\pgzero\runner.py", line 93, in main run_mod(mod) File "c:\program files\python37\lib\site-packages\pgzero\runner.py", line 113, in run_mod PGZeroGame(mod).run() File "c:\program files\python37\lib\site-packages\pgzero\game.py", line 217, in run self.mainloop() File "c:\program files\python37\lib\site-packages\pgzero\game.py", line 247, in mainloop self.dispatch_event(event) File "c:\program files\python37\lib\site-packages\pgzero\game.py", line 172, in dispatch_event handler(event) File "c:\program files\python37\lib\site-packages\pgzero\game.py", line 164, in new_handler return handler(**prepped) File "shoot.py", line 16, in on_mouse_down score = score + 1 UnboundLocalError: local variable 'score' referenced before assignment
Reply
#2
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.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,065 Jun-19-2018, 02:52 AM
Last Post: bhill

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020