Rock-Paper-Scissors Game in Python - Aman Aadi

Rock-Paper-Scissors Game in Python: A Fun Project

Introduction:

Are you ready to revisit your childhood days and play the classic Rock-Paper-Scissors game? In this blog post, we will dive into a fun and interactive Python project that allows you to create your own Rock-Paper-Scissors game. This project is perfect for beginner programmers and game enthusiasts looking to explore the world of Python programming. With a few lines of code, you can build an exciting game that challenges you to outsmart your computer opponent and experience the thrill of Rock-Paper-Scissors gameplay.

Understanding the Rock-Paper-Scissors Game:

Rock-Paper-Scissors is a two-player hand game where each player simultaneously forms one of three shapes with an outstretched hand. The three shapes are rock, paper, and scissors, and their interactions determine the winner. The game follows a straightforward set of rules, with each shape defeating one and losing to another. Rock crushes scissors, Scissors cut paper, and Paper covers rock.

How would the python code work? We’ll use Python to create a command-line version of the game where the player competes against the computer. first it will ask the user to choose the option of rock, paper, scissors or exit. Then the computer will make a random selection. After the interaction of both choice, it will show, who will win or lose .

Requirements:

Before we begin, ensure you have Python installed on your computer. You can download the latest version from the official Python website (https://www.python.org/downloads/). Additionally, a code editor like Visual Studio Code or PyCharm will make coding and testing the game more convenient.

We’ll start by importing the random module within our Python script to allow the computer to make random choices.

Python Code:

# importing modules
import random

# storing some values in variables
user_score = 0
computer_score = 0
is_playing = True

# playing game in a while loop
while is_playing:
    # taking input from the user
    user_choice = input("\nChoose a option:-\n[r] for rock\n[p] for paper\n[s] for scissor\n[e] for exit\n>>> ")

    # taking random choice from the computer or another player
    computer_choices = ["r","p","s"]
    random.shuffle(computer_choices)
    computer_choice = computer_choices[0]

    # creating conditions for the game
    if user_choice == computer_choice:
        print("Tie!")

    elif user_choice == 'r' :
        if computer_choice == 's' :
            user_score += 1
            print('You Win!')
        else:
            computer_score += 1
            print('You Lose!')
        print(f"Your '{user_choice}' {user_score} || Computer '{computer_choice}' {computer_score}")

    elif user_choice == 'p' :
        if computer_choice == 'r' :
            user_score += 1
            print('You Win!')
        else:
            computer_score += 1
            print('You Lose!')
        print(f"Your '{user_choice}' {user_score} || Computer '{computer_choice}' {computer_score}")

    elif user_choice == 's':
        if computer_choice == 'p':
            user_score += 1
            print('You Win!')
        else:
            computer_score += 1
            print('You Lose!')
        print(f"Your '{user_choice}' {user_score} || Computer '{computer_choice}' {computer_score}")

    elif user_choice == 'e':
        if user_score > computer_score:
            print(f"You win, Your Score '{user_score}' and Computer Score '{computer_score}'")
        elif user_score < computer_score:
            print(f"You lose, Your Score '{user_score}' and Computer Score '{computer_score}'")
        else:
            print("Tie!")
        is_playing = False

    else:
        print("Enter valid option")

Commands and Output:

These are some of the commands or input that is given to it and it will show the output.

Rock-Paper-Scissors Game in Python: A Fun Project - Aman Aadi

Conclusion:

Congratulations! You’ve successfully created a Rock, Paper, Scissors game using Python. This interactive Python script brings the classic hand gesture battle to life, allowing you to challenge the computer and test your luck and strategy. Whether you’re a Python enthusiast or a gaming aficionado, this Python-powered Rock, Paper, Scissors game offers a dose of entertainment and nostalgia. Feel free to experiment further and add new features to enhance the gameplay. Happy coding, and may the odds be ever in your favor as you continue your coding adventures!

Discover more from Aman Aadi

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top