Python Forum
Trying to create a visual rock paper scissors game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to create a visual rock paper scissors game
#1
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)
           
Reply
#2
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"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock paper scissors in python with "algorithm" Agat0 24 14,995 Sep-04-2025, 09:06 AM
Last Post: dr8478552
  I cannot create a virtual environment on visual studio code using python Willem_Aucamp316 2 12,192 Nov-27-2024, 02:20 PM
Last Post: menator01
  how to create video game mission chart?[SOLVED] kucingkembar 1 1,208 Jun-19-2024, 02:11 PM
Last Post: kucingkembar
  Rock Paper Scissors Project in Python ankitdixit 8 8,567 Feb-23-2024, 03:14 PM
Last Post: DPaul
  Problem restricting user input in my rock paper scissors game ashergreen 6 7,851 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Odd behavior with Rock Paper Scissor game DustinKlent 2 3,203 Aug-27-2020, 03:55 PM
Last Post: DustinKlent
  Trying to implement Best of 3 logic in rock paper scissors game ShAhCh 5 6,329 May-11-2020, 04:31 PM
Last Post: ShAhCh
  Rock Paper Scissors with dictionaries ewgreht 2 5,940 May-01-2020, 03:19 PM
Last Post: deanhystad
  Rock, Paper, Scissors.. Help..hidden bug xxunknownxx 4 4,322 Mar-19-2020, 02:46 AM
Last Post: jefsummers
  Problem with Basic Rock Paper Scissors code BirinderSingh 3 4,571 Sep-13-2019, 03:28 PM
Last Post: ichabod801

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.