0

Im new to coding and was making rock paper scissors in pygame, and the syntax error in the title came up. I know its a lot of code, because everything is all over the place and i can't code efficiently, but I am trying to call the main "game_rps" function but there are some issues. Any help would be appreciated!

#imports modules
import pygame
import random
import time

pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False

# Setting a title and logo
pygame.display.set_caption('Rock Paper Scissors')
game_icon = pygame.image.load('logo.png')
pygame.display.set_icon(game_icon)

#gives displaying a variable
gameDisplay = pygame.display.set_mode((800,600))

#gives player and computer a pick
plpick = 0
cpick = random.randint(1,3)

#Sets text peramiters
black = pygame.color.Color('#000000')
font = pygame.font.Font(None, 40)
text_player = font.render("Player pick", False, black)
text_comp = font.render("Computer pick", False, black)
text_tie = font.render("It's a tie!", False, black)
text_win = font.render("You win!", False, black)
text_lose = font.render("You lose!", False, black)

#Sets scoreboard
player_score = 0
cp_score = 0

#Sets variable for item locations
rock_x = 50
rock_y = 200

paper_x = 300
paper_y = 200

scissors_x = 550
scissors_y = 200

rock2_x = 50
rock2_y = 200

paper2_x = 300
paper2_y = 200

scissors2_x = 550
scissors2_y = 200

#NOTE: 1:rock 2:paper 3: scissors
global mousepos, bgcolor, backrect, rock_rec, paper_rec, scissors_rec, rock, paper, scissors

def game_rps():
    while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True

            #detects where mouse is
            mousepos = pygame.mouse.get_pos()

            #set colors 
            bgcolor = 237, 195, 111
            backrect = 237, 195, 80
            screen.fill(bgcolor) 

            #sets invisible squares behind rps objects
            rock_rec = pygame.draw.rect(screen,bgcolor,(50,230,200,150))

            paper_rec = pygame.draw.rect(screen,bgcolor,(300,200,200,190))

            scissors_rec = pygame.draw.rect(screen,bgcolor,(550,230,200,150))


            #sets rps images as variables
            rock = pygame.image.load('rock.png')
            rock = pygame.transform.scale(rock, (200,200))

            paper = pygame.image.load('paper.png')
            paper = pygame.transform.scale(paper, (200,200)) 

            scissors = pygame.image.load('scissors.png')
            scissors = pygame.transform.scale(scissors, (200,200))

            #display the items    
            gameDisplay.blit(rock, (rock_x,rock_y))

            gameDisplay.blit(paper, (paper_x,paper_y))

            gameDisplay.blit(scissors, (scissors_x,scissors_y))

            #detects if mouse button has been pressed on sprite
            if event.type == pygame.MOUSEBUTTONDOWN:
                # Set the x, y postions of the mouse click
                x, y = pygame.mouse.get_pos()
                if rock_rec.collidepoint(x, y):
                    plpick = 1
                    time.sleep(1)

            if event.type == pygame.MOUSEBUTTONDOWN:
                # Set the x, y postions of the mouse click
                x, y = pygame.mouse.get_pos()
                if paper_rec.collidepoint(x, y):
                    plpick = 2
                    time.sleep(1)

            if event.type == pygame.MOUSEBUTTONDOWN:
                # Set the x, y postions of the mouse click
                x, y = pygame.mouse.get_pos()
                if scissors_rec.collidepoint(x, y):
                    plpick = 3
                    time.sleep(1)

            #checks all different options
            if cpick == 1 and plpick == 1:

                #moves paper and scisors out of the way
                for i in range (500):
                    paper_y = paper_y+0.05
                    scissors_y = scissors_y+0.05

                #displays a new rock
                gameDisplay.blit(rock, (rock2_x,rock2_y))

                #moves rock
                for i in range (500):
                    rock2_x = rock2_x +0.05

                    if rock2_x > 550:
                        rock2_x = 550

                #displays text
                screen.blit(text_tie, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))

            #paper & paper
            elif cpick == 2 and plpick == 2:

                #moves rock and scisors out of the way
                for i in range (500):
                    rock_y = rock_y+0.05
                    scissors_y = scissors_y+0.05

                #displays a new paper
                gameDisplay.blit(paper, (paper2_x,paper2_y))

                #moves new paper
                for i in range (500):
                    paper2_x = paper2_x +0.05

                    if paper2_x > 550:
                        paper2_x = 550

                #moves new paper
                for i in range (500):
                    paper_x = paper_x-0.05

                    if paper_x < 50:
                        paper_x = 50

                #displays text
                screen.blit(text_tie, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))

            #scissors & scissors
            elif cpick == 3 and plpick == 3:

                #moves paper and rock out of the way
                for i in range (500):
                    paper_y = paper_y+0.05
                    rock_y = rock_y+0.05

                #displays a new scissors
                gameDisplay.blit(scissors, (scissors2_x,scissors2_y))

                #moves new scissors
                for i in range (500):
                    scissors2_x = scissors2_x -0.05

                    if scissors2_x < 50:
                        scissors2_x = 50

                #displays text
                screen.blit(text_tie, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))

            #player-paper CP-rock
            elif cpick == 1 and plpick == 2:

                #moves scisors out of the way
                for i in range (500):
                    scissors_y = scissors_y+0.05

                #moves rock
                for i in range (500):
                    rock_x = rock_x +0.05

                    if rock_x > 550:
                        rock_x = 550

                #moves paper
                for i in range (500):
                    paper_x = paper_x-0.05

                    if paper_x < 50:
                        paper_x = 50

                #displays text
                screen.blit(text_win, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))

            #player-scissors CP-paper
            elif cpick == 2 and plpick == 3:
                print("player wins")

                #moves rock out of the way
                for i in range (500):
                    rock_y = rock_y+0.05

                #moves paper
                for i in range (500):
                    paper_x = paper_x +0.05

                    if paper_x > 550:
                        paper_x = 550

                #moves scissors
                for i in range (500):
                    scissors_x = scissors_x-0.05

                    if scissors_x < 50:
                        scissors_x = 50

                #displays text
                screen.blit(text_win, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))

            #player-rock CP-scissors
            elif cpick == 3 and plpick == 1:

                #moves paper out of the way
                for i in range (500):
                    paper_y = paper_y+0.05

                #displays text
                screen.blit(text_win, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))

            #player-scissors CP-rock
            elif cpick == 1 and plpick == 3:

                #moves paper out of the way
                for i in range (500):
                    paper_y = paper_y+0.05

                #moves rock
                for i in range (500):
                    rock_x = rock_x +0.05

                    if rock_x > 550:
                        rock_x = 550

                #moves scissors
                for i in range (500):
                    scissors_x = scissors_x-0.05

                    if scissors_x < 50:
                        scissors_x = 50

                #displays text
                screen.blit(text_lose, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))

            #player-rock CP-paper
            elif cpick == 2 and plpick == 1:

                #moves scissors out of the way
                for i in range (500):
                    scissors_y = scissors_y+0.05

                #moves paper
                for i in range (500):
                    paper_x = paper_x +0.05

                    if paper_x > 550:
                        paper_x = 550

                #moves rock
                for i in range (500):
                    rock_x = rock_x-0.05

                    if rock_x < 50:
                        rock_x = 50

                #displays text
                screen.blit(text_lose, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))

            #player-paper CP-scissors
            elif cpick == 3 and plpick == 2:

                #moves rock out of the way
                for i in range (500):
                    rock_y = rock_y+0.05

                #moves scissors
                for i in range (500):
                    scissors_x = scissors_x +0.05

                    if scissors_x > 550:
                        scissors_x = 550

                #moves paper
                for i in range (500):
                    paper_x = paper_x-0.05

                    if paper_x < 50:
                        paper_x = 50

                #displays text
                screen.blit(text_lose, (350,100))
                screen.blit(text_comp, (550, 500))
                screen.blit(text_player, (50, 500))


            pygame.display.flip()

game_rps()
  • Read about the [`global` statement](https://docs.python.org/3/reference/simple_stmts.html?highlight=global#grammar-token-global-stmt). The variables must be specified `global` within all the functions in which the variables are changed. Each variable must be set before it can be read. The initialization can bed done in global space. e.g.: `done = False` in global space and `global done` inside `def game_rps():` – Rabbid76 Dec 19 '21 at 12:58
  • Please read [ask] and [mre]. – Karl Knechtel Sep 09 '22 at 11:22

0 Answers0