Python Forum
Trying to create a visual rock paper scissors game - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to create a visual rock paper scissors game (/thread-38858.html)



Trying to create a visual rock paper scissors game - urmom33 - Dec-03-2022

Hello everyone, hope y'all are good. Today I tried creating the classic rock paper scissors game, but not in the console, but opening a window which shows the three options and you have to click on one to choose it, but it doesn't work as intended, it doesn't let you pick the option so you can't play it. Here's my code and I hope I've been clear enough:

import turtle, random

#Window
wn = turtle.Screen()
wn.setup(1080, 720)
wn.title("Rock, paper, scissors!")
wn.bgcolor("black")
wn.tracer(0)

#Shapes
turtle.register_shape("rock.gif")
turtle.register_shape("paper.gif")
turtle.register_shape("scissors.gif")

#Rock
rock = turtle.Turtle()
rock.shape("rock.gif")
rock.penup()
rock.goto(-360, 0)

#Paper
paper = turtle.Turtle()
paper.shape("paper.gif")
paper.penup()
paper.goto(0, 0)

#Scissors
scissors = turtle.Turtle()
scissors.shape("scissors.gif")
scissors.penup()
scissors.goto(360, 0)

#Text
text = turtle.Turtle()
text.penup()
text.goto(0, 290)
text.hideturtle()
text.color("white")
text.write("Choose an option by clicking on it", align="center", font=("Arial", 12, "normal"))

#Player chooses
def r(x, y):
    user_action = "rock"
def p(x, y):
    user_action = "paper"
def s(x, y):
    user_action = "scissors"

rock.onclick(r)
paper.onclick(p)
scissors.onclick(s)

user_action = None

#Computer chooses
computer_options = ["rock", "paper", "scissors"]
computer_action = random.choice(computer_options)

#Choosing who wins
if user_action == computer_action:
        text.clear()
        text.write(f"Both players selected {user_action}. It's a tie!\n", align="center", font=("Arial", 12, "normal"))
elif user_action == "rock":
    if computer_action == "scissors":
        text.clear()
        text.write("Rock smashes scissors! You win!\n", align="center", font=("Arial", 12, "normal"))
        paper.hideturtle()
    else:
        text.clear()
        text.write("Paper covers rock! You lose.\n", align="center", font=("Arial", 12, "normal"))
        scissors.hideturtle()
elif user_action == "paper":
    if computer_action == "rock":
        text.clear()
        text.write("Paper covers rock! You win!\n", align="center", font=("Arial", 12, "normal"))
        scissors.hideturtle()
    else:
            text.clear()
            text.write("Scissors cuts paper! You lose.\n", align="center", font=("Arial", 12, "normal"))
            rock.hideturtle()
elif user_action == "scissors":
    if computer_action == "paper":
        text.clear()
        text.write("Scissors cuts paper! You win!\n", align="center", font=("Arial", 12, "normal"))
        rock.hideturtle()
    else:
            text.clear()
            text.write("Rock smashes scissors! You lose.\n", align="center", font=("Arial", 12, "normal"))
            paper.hideturtle()
while True:
    wn.update()



RE: Trying to create a visual rock paper scissors game - deanhystad - Dec-03-2022

This code needs to be called when the user makes a selection. You need to make it a function that takes the user_action as an argument.
#Choosing who wins
if user_action == computer_action:
        text.clear()
        text.write(f"Both players selected {user_action}. It's a tie!\n", align="center", font=("Arial", 12, "normal"))
elif user_action == "rock":
    if computer_action == "scissors":
        text.clear()
        text.write("Rock smashes scissors! You win!\n", align="center", font=("Arial", 12, "normal"))
        paper.hideturtle()
    else:
        text.clear()
        text.write("Paper covers rock! You lose.\n", align="center", font=("Arial", 12, "normal"))
        scissors.hideturtle()
elif user_action == "paper":
    if computer_action == "rock":
        text.clear()
        text.write("Paper covers rock! You win!\n", align="center", font=("Arial", 12, "normal"))
        scissors.hideturtle()
    else:
            text.clear()
            text.write("Scissors cuts paper! You lose.\n", align="center", font=("Arial", 12, "normal"))
            rock.hideturtle()
elif user_action == "scissors":
    if computer_action == "paper":
        text.clear()
        text.write("Scissors cuts paper! You win!\n", align="center", font=("Arial", 12, "normal"))
        rock.hideturtle()
    else:
            text.clear()
            text.write("Rock smashes scissors! You lose.\n", align="center", font=("Arial", 12, "normal"))
            paper.hideturtle()
while True:
    wn.update()
There is no reason for this code. Call your new function directly and pass the player selection as an argument. If you don't know how this can be done, look for information about lambda expressions.
#Player chooses
def r(x, y):
    user_action = "rock"
def p(x, y):
    user_action = "paper"
def s(x, y):
    user_action = "scissors"