0

I am very new to codding and am trying to make a game I can call my own. I don't undertand what the error means but here is my code:

import pgzrun
import math
import random

WIDTH = 800                       #5
HEIGHT = 600
CENTER_X = WIDTH/2
CENTER_Y = HEIGHT/2
CENTER = (CENTER_X, CENTER_Y)
FONT_COLOR = (221, 160, 221)      #10
ozol = 0

game_over = False

ozolith = Actor("ozolith")        #15

def draw():
    screen.clear()
    ozolith.draw()
    draw_counters(ozol)           #20

def place_ozolith():
    ozolith.x = CENTER_X
    ozolith.y = CENTER_Y
                                  #25
def draw_counters(ozol):
    screen.draw.text(str(ozol), fontsize=40, center=CENTER, color=FONT_COLOR)

def on_mouse_down(pos):
    if ozolith.collidepoint(pos): #30
        ozol += 1
        place_ozolith
    else:
        quit()
                                  #35
place_ozolith

pgzrun.go()

Here is the error message

Traceback (most recent call last):
  File "C:\Program Files\Python38\python-games\GameCraft\GameCraft.py", line 38, in <module>
    pgzrun.go()
  File "C:\Program Files\Python38\lib\site-packages\pgzrun.py", line 31, in go
    run_mod(mod)
  File "C:\Program Files\Python38\lib\site-packages\pgzero\runner.py", line 113, in run_mod
    PGZeroGame(mod).run()
  File "C:\Program Files\Python38\lib\site-packages\pgzero\game.py", line 217, in run
    self.mainloop()
  File "C:\Program Files\Python38\lib\site-packages\pgzero\game.py", line 247, in mainloop
    self.dispatch_event(event)
  File "C:\Program Files\Python38\lib\site-packages\pgzero\game.py", line 172, in dispatch_event
    handler(event)
  File "C:\Program Files\Python38\lib\site-packages\pgzero\game.py", line 164, in new_handler
    return handler(**prepped)
  File "C:\Program Files\Python38\python-games\GameCraft\GameCraft.py", line 31, in on_mouse_down
    ozol += 1
UnboundLocalError: local variable 'ozol' referenced before assignment

It happens once I click on the image which is stuck on the top left corner.

Swagtonio
  • 13
  • 3
  • You can add `global` in your function. But it is much better to avoid mutable global state. Instead, pass information into your function *as arguments*, and `return` objects to the caller that are needed in the caller. There's a reason that assignment default to local, which is to discourage mutable global state – juanpa.arrivillaga Mar 21 '20 at 05:53

2 Answers2

0
    ozol += 1

is equivalent to

    ozol = ozol + 1

But ozol has no initial value, hence the error.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • 1
    It may not be obvious that `ozol` will default to local, and won't use the global `ozol` indeed, in JS, this would default to global if there is no variable declaration inside the function (this being a well-known wart in the language). Python doesn't *have* declarations, of course, so perhaps it was more obviously pragmatic to make assignment default to local – juanpa.arrivillaga Mar 21 '20 at 05:55
-1

try making ozol global by

    global ozol
    ozol = 0

put global ozol b4 you define ozol and see if that works