pls help me solve this error : UnboundLocalError: local variable 'click' referenced before assignment I'm new to pygame & I am learning to code a game using pygame. I am trying to creating a home menu for my game which contains two buttons : play_button & stats_button. Play button will run the game() function which is yet to coded.Stats button will show the leaderboard & other stuff. but, its not going as expected. When i click the first red button (play button), its still on the home screen & no response. The same with stats button. Also, the "X" does not close the game window now. Plus, an error adds to my problem now !
CODE :
#importing area
import pygame, sys
from pygame.locals import *
#---------------------------------------------------------------------------\
#switching on
pygame.init()
#---------------------------------------------------------------------------\
#on screen area
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("space.io")
icon_game = pygame.image.load(r"C:\Uv Pygame\code\ufo.png") #load icon, r is compulsory
pygame.display.set_icon(icon_game)
background_1 = pygame.image.load(r"C:\Uv Pygame\code\bg.jpg")
#----------------------------------------------------------------------------\
#essentials
run = True
game = False
stats = False
font = pygame.font.SysFont(None, 100)
click = False
#----------------------------------------------------------------------------\
#time
clock = pygame.time.Clock()
#-----------------------------------------------------------------------------\
#core
def game():
game = True
while game:
screen.fill((255,255,255))
draw_text("GAME" ,font,(0,0,0) , screen , 195,80)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
game = False
def stats():
stats = True
while stats:
screen.fill((255,255,255))
draw_text("STATS" ,font,(0,0,0) , screen , 195,80)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
stats = False
def draw_text(text, font , color, surface, x, y):
textobj = font.render(text, 1,color)
textrect = textobj.get_rect()
textrect.topleft = (x,y)
surface.blit(textobj,textrect)
#------------------------------------------------------------------------------\
def main_menu():
while run:
screen.fill((255,255,255))
draw_text("HOME" ,font,(0,0,0) , screen , 195,80)
play_button = pygame.Rect(210,200,180,50)
stats_button = pygame.Rect(210,300,180,50)
pygame.draw.rect(screen, (255,0,0), play_button)
pygame.draw.rect(screen, (255,0,0), stats_button)
mx,my = pygame.mouse.get_pos()
if play_button.collidepoint((mx,my)):
if click:
game()
if stats_button.collidepoint((mx,my)):
if click:
stats()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
pygame.display.update()
main_menu()
ERROR:
Traceback (most recent call last):
File "C:\Uv Pygame\code\pract_1.py", line 95, in <module>
main_menu()
File "C:\Uv Pygame\code\pract_1.py", line 78, in main_menu
if click:
UnboundLocalError: local variable 'click' referenced before assignment
Pls tell me why am I getting this error & how to solve it "THE SIMPLEST WAY"*