Untitled
unknown
plain_text
a year ago
3.3 kB
9
Indexable
import turtle import random # Constants for the game CHOICES = ['rock', 'paper', 'scissors'] # Initialize the turtle screen wn = turtle.Screen() wn.title("Rock Paper Scissors Game") # Create turtles for drawing choices rock_turtle = turtle.Turtle() paper_turtle = turtle.Turtle() scissors_turtle = turtle.Turtle() # Hide the default turtles until we draw them rock_turtle.hideturtle() paper_turtle.hideturtle() scissors_turtle.hideturtle() # Create a turtle for displaying text text_turtle = turtle.Turtle() text_turtle.hideturtle() text_turtle.penup() text_turtle.goto(0, 200) # Function to draw the rock def draw_rock(t): t.shapesize(3) t.shape('circle') t.fillcolor('gray') t.showturtle() # Function to draw the paper def draw_paper(t): t.shapesize(3) t.shape('square') t.fillcolor('white') t.showturtle() # Function to draw the scissors def draw_scissors(t): t.shapesize(3) t.shape('triangle') t.fillcolor('gold') t.showturtle() # Function to display the game choices def display_choices(): rock_turtle.goto(-150, -50) paper_turtle.goto(0, -50) scissors_turtle.goto(150, -50) draw_rock(rock_turtle) draw_paper(paper_turtle) draw_scissors(scissors_turtle) text_turtle.write("Click to choose rock, paper, or scissors", align="center", font=("Arial", 18, "normal")) # Function to determine and display the winner def determine_winner(user_choice): computer_choice = random.choice(CHOICES) text_turtle.clear() # Determine the winner and display the winning choice if user_choice == computer_choice: text = "It's a tie!" elif (user_choice == 'rock' and computer_choice == 'scissors') or \ (user_choice == 'scissors' and computer_choice == 'paper') or \ (user_choice == 'paper' and computer_choice == 'rock'): text = "You win!" draw_winner_icon(user_choice) else: text = "Computer wins!" draw_winner_icon(computer_choice) text_turtle.write(text + f" You chose {user_choice}. Computer chose {computer_choice}.", align="center", font=("Arial", 18, "normal")) # Ask to play again wn.ontimer(ask_to_play_again, 2000) def draw_winner_icon(choice): winner_turtle = turtle.Turtle() winner_turtle.penup() winner_turtle.goto(0, -150) winner_turtle.hideturtle() if choice == 'rock': draw_rock(winner_turtle) elif choice == 'paper': draw_paper(winner_turtle) elif choice == 'scissors': draw_scissors(winner_turtle) # Clean up and ask to play again def ask_to_play_again(): text_turtle.clear() for t in (rock_turtle, paper_turtle, scissors_turtle): t.clear() display_choices() # Functions to be called when a choice is clicked def on_choice_click(choice): rock_turtle.hideturtle() paper_turtle.hideturtle() scissors_turtle.hideturtle() determine_winner(choice) # Bind the choices to their respective click handlers rock_turtle.onclick(lambda x, y: on_choice_click('rock')) paper_turtle.onclick(lambda x, y: on_choice_click('paper')) scissors_turtle.onclick(lambda x, y: on_choice_click('scissors')) # Display initial choices display_choices() # Keep the window open until closed by the user wn.mainloop()
Editor is loading...
Leave a Comment