0

I'm making a simple Rock Paper Scissor game for a newbie in Python, after created a gameloop when your score reach -5 there was a local variable referenced before assignment problem that i don't know how to solve?

Can you give me any advice or way to fix this? And is there any problem in this code? Thank you very much for helping me

import pygame
from random import randint
pygame.init()

dis=pygame.display.set_mode((800,500))
pygame.display.set_caption('OAN TU TI')


keo_y=300
keo_x=100
bua_y=300
bua_x=350
bao_y=300
bao_x=600
computer_y=50
computer_x=350
KEOCOM=pygame.image.load('keocom.png')
BUACOM=pygame.image.load('buacom.png')
BAOCOM=pygame.image.load('baocom.png')
keo=pygame.image.load('keo.png')
bua=pygame.image.load('bua.png')
bao=pygame.image.load('bao.png')
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
font=pygame.font.SysFont("san",50) 

ketqua =""
computer = ""
player =""
diem = 0
if computer == player:
    print("HOA")
    ketqua="HOA"
else:
    if player == "BAO":
        if computer == "BUA":               
            print("THANG")
            ketqua="THANG"
        else:
            print("THUA")
            ketqua="THUA"
    elif player == "BUA":
        if computer == "KEO":
            print("THANG")
            ketqua="THANG"
        else:
            print("THUA")
            ketqua="THUA"
    elif player == "KEO":
        if computer == "BAO":
            print("THANG")
            ketqua="THANG"
        else:
            print("THUA")
            ketqua="THUA" 



def gameLoop():
    game_over = False
    game_close = False


    while not game_over:
                        
        while game_close == True:
            dis.fill(white)
            message("You Lost! Press Q-Quit or C-Play Again", red)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()

        

        for event in pygame.event.get():
            
            mouse_x, mouse_y = pygame.mouse.get_pos()
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                if 100 < mouse_x < 265 and 300 < mouse_y < 475: 
                    player = player + "KEO"
                    print (player)
                    computer = randint(0,2)
                    if computer == 0:
                        computer= "KEO"
                    if computer == 1:
                        computer= "BUA"
                    if computer == 2:
                        computer= "BAO"
                if 350 < mouse_x < 507 and 300 < mouse_y < 475: 
                    player = player + "BUA"
                    print (player)
                    computer = randint(0,2)
                    if computer == 0:
                        computer = "KEO"
                    if computer == 1:
                        computer = "BUA"
                    if computer == 2:
                        computer = "BAO"
                if 600 < mouse_x < 745 and 300 < mouse_y < 475: 
                    player = player + "BAO"
                    print (player)
                    computer = randint(0,2)
                    if computer == 0:
                        computer= "KEO"
                    if computer == 1:
                        computer= "BUA"
                    if computer == 2:
                        computer= "BAO"
                print (computer)
                

                if computer == "KEO":
                    may = KEOCOM_rect=dis.blit(KEOCOM,(computer_x,computer_y))
                elif computer == "BUA":
                    may = BUACOM_rect=dis.blit(BUACOM,(computer_x,computer_y))
                else:
                    may = BAOCOM_rect=dis.blit(BAOCOM,(computer_x,computer_y))



                if ketqua == "THANG":
                    diem = diem + 1
                elif ketqua == "HOA":
                    diem = diem + 0
                else:
                    diem = diem - 1 


        if diem == -5:
            game_close = True
        
        
    pygame.quit()
    quit()

gameLoop()

this is the problem

UnboundLocalError                         Traceback (most recent call last)
<ipython-input-19-08d74ca39527> in <module>
    143     quit()
    144 
--> 145 gameLoop()

<ipython-input-19-08d74ca39527> in gameLoop()
    136 
    137 
--> 138         if diem == -5:
    139             game_close = True
    140 

UnboundLocalError: local variable 'diem' referenced before assignment
  • problem makes `diem = diem - 1 ` - you want to assign value to external/global variable `diem` so you have to use `global diem` inside `gameLoop`. At this moment `diem = diem - 1` tries to assing value to local variable `diem` but for this it has to get previous value - to calculate `diem - 1` - and it can't find local variable `diem` – furas Jul 27 '21 at 08:23

0 Answers0