Feb-04-2019, 04:24 AM
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:
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