avinashkranjan / Amazing-Python-Scripts

🚀 Curated collection of Amazing Python scripts from Basics to Advance with automation task scripts.
https://amazing-python-scripts.avinashranjan.com
MIT License
2.55k stars 1.02k forks source link

[GSSoC'23] Rock-Paper-Scissors Game using Turtle #1722

Open avantika0001 opened 1 year ago

avantika0001 commented 1 year ago

Aim

Details

Screenshot (278)

ShreeparnaDhara commented 1 year ago

Here's an example of a Rock, Paper, Scissors game implemented using the Turtle graphics library in Python: import turtle import random

Initialize the Turtle screen

screen = turtle.Screen() screen.title("Rock, Paper, Scissors") screen.bgcolor("white")

Create the player's turtle

player_turtle = turtle.Turtle() player_turtle.color("blue") player_turtle.shape("turtle") player_turtle.penup() player_turtle.goto(-100, 0)

Create the computer's turtle

computer_turtle = turtle.Turtle() computer_turtle.color("red") computer_turtle.shape("turtle") computer_turtle.penup() computer_turtle.goto(100, 0)

Define the game logic

def play_game():

Prompt the player to choose

player_choice = screen.textinput("Rock, Paper, Scissors", "Choose: rock, paper, or scissors").lower()

# Generate a random choice for the computer
choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)

# Move the turtles
player_turtle.forward(100)
computer_turtle.forward(100)

# Display the choices
player_turtle.write(player_choice.capitalize(), align="center", font=("Arial", 16, "bold"))
computer_turtle.write(computer_choice.capitalize(), align="center", font=("Arial", 16, "bold"))

# Determine the winner
if player_choice == computer_choice:
    result = "It's a tie!"
elif (player_choice == "rock" and computer_choice == "scissors") or \
     (player_choice == "paper" and computer_choice == "rock") or \
     (player_choice == "scissors" and computer_choice == "paper"):
    result = "You win!"
else:
    result = "Computer wins!"

# Display the result
result_turtle = turtle.Turtle()
result_turtle.color("green")
result_turtle.penup()
result_turtle.goto(0, -100)
result_turtle.write(result, align="center", font=("Arial", 24, "bold"))
result_turtle.hideturtle()

Call the game function

play_game()

Exit on click

turtle.exitonclick()

In this code, we use the Turtle library to create two turtles representing the player and the computer. The turtles move forward to indicate the choice made by each. The player is prompted to choose rock, paper, or scissors using the textinput function from the Turtle screen. The computer generates a random choice. The turtles write the choices on the screen, and the winner is determined based on the game logic. Finally, the result is displayed using another turtle. The game ends when you click on the screen.

Can this issue be assigned to me under gssoc 2023

avantika0001 commented 1 year ago

@1e9abhi1e10 Do i start working on this issue? or should i make any further improvements?

avinashkranjan commented 1 year ago

Go Ahead @codewithavantika