pygame zero keep count - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: pygame zero keep count (/thread-15863.html) |
pygame zero keep count - Mirt56 - Feb-04-2019 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:
RE: pygame zero keep count - metulburr - Feb-04-2019 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. |