Dec-03-2022, 06:26 PM
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()
Attached Files
Image(s)
