avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1725

Abstract

pan class="hljs-symbol">2</span> <span class="hljs-symbol">SCISSORS</span> = <span class="hljs-symbol">3</span></pre></div><p id="43c3">Now, let’s write the game logic to determine the winner:</p><div id="9198"><pre>def determine_winner(player_choice, computer_choice): <span class="hljs-keyword">if</span> player_choice == computer_choice: <span class="hljs-keyword">return</span> <span class="hljs-string">"It's a tie!"</span>

<span class="hljs-keyword">if</span> (player_choice == Choice.ROCK and computer_choice == Choice.SCISSORS) or (player_choice == Choice.SCISSORS and computer_choice == Choice.PAPER) or (player_choice == Choice.PAPER and computer_choice == Choice.ROCK):
    <span class="hljs-keyword">return</span> <span class="hljs-string">"You win!"</span>
<span class="hljs-keyword">else</span>:
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Computer wins!"</span></pre></div><p id="6c7e">Finally, let’s implement the game loop and user input:</p><div id="ac9c"><pre><span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>():
<span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:
    player_input = <span class="hljs-built_in">input</span>(<span class="hljs-string">"Enter your choice (rock, paper, scissors) or 'quit' to exit: "</span>).upper()
    
    <span class="hljs-keyword">if</span> player_input == <span class="hljs-string">'QUIT'</span>:
        <span class="hljs-keyword">break</span>
    
    <span class="hljs-keyword">try</span>:
        player_choice = Choice[player_input]
        computer_choice = <span class="hljs-comment"># generate computer

Options

's choice using random module</span>

        <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Your choice: <span class="hljs-subst">{player_choice.name}</span>"</span>)
        <span class="hljs-built_in">print</span>(<span class="hljs-string">f"Computer's choice: <span class="hljs-subst">{computer_choice.name}</span>"</span>)
        <span class="hljs-built_in">print</span>(determine_winner(player_choice, computer_choice))
    <span class="hljs-keyword">except</span> KeyError:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">"Invalid choice. Please try again."</span>)

<span class="hljs-keyword">if</span> name == <span class="hljs-string">"main"</span>: main()</pre></div><h2 id="df4f">Conclusion</h2><p id="3f0b">You’ve now created a simple Rock, Paper, Scissors game using Python. This project serves as a great introduction to game programming and demonstrates the use of input, loops, Enum objects, functions, and dictionaries in Python.</p><p id="9ee5">Feel free to further enhance the game by adding features like keeping score or creating a graphical user interface. Happy coding!</p><div id="0291" class="link-block"> <a href="https://readmedium.com/python-data-classes-7a7738cc3297"> <div> <div> <h2>Python Data Classes</h2> <div><h3>undefined</h3></div> <div><p>undefined</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*4kSdlOKEQqdYroo_Bdg_dA.jpeg)"></div> </div> </div> </a> </div></article></body>

Python Rock Paper Scissors Game

How to Create a Rock, Paper, Scissors Game in Python

Rock, Paper, Scissors is a classic hand game played between two people. Let’s create a simple command line version of the game using Python.

The Game Logic

In the game, each player simultaneously forms one of three shapes with their hand. The possible shapes are rock, paper, and scissors. The winner is determined by the rules:

  • Rock crushes scissors
  • Scissors cuts paper
  • Paper covers rock

Getting Started

To create the game, we will use Python and follow these steps:

  1. Take user input using input()
  2. Implement a loop to play several games
  3. Utilize Enum objects and functions to clean up the code
  4. Define the game rules using a dictionary

Let’s dive into the code!

The Python Code

First, let’s import the necessary module:

from enum import Enum

Next, let’s define the game options as an enumeration:

class Choice(Enum):
    ROCK = 1
    PAPER = 2
    SCISSORS = 3

Now, let’s write the game logic to determine the winner:

def determine_winner(player_choice, computer_choice):
    if player_choice == computer_choice:
        return "It's a tie!"

    if (player_choice == Choice.ROCK and computer_choice == Choice.SCISSORS) or (player_choice == Choice.SCISSORS and computer_choice == Choice.PAPER) or (player_choice == Choice.PAPER and computer_choice == Choice.ROCK):
        return "You win!"
    else:
        return "Computer wins!"

Finally, let’s implement the game loop and user input:

def main():
    while True:
        player_input = input("Enter your choice (rock, paper, scissors) or 'quit' to exit: ").upper()
        
        if player_input == 'QUIT':
            break
        
        try:
            player_choice = Choice[player_input]
            computer_choice = # generate computer's choice using random module

            print(f"Your choice: {player_choice.name}")
            print(f"Computer's choice: {computer_choice.name}")
            print(determine_winner(player_choice, computer_choice))
        except KeyError:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()

Conclusion

You’ve now created a simple Rock, Paper, Scissors game using Python. This project serves as a great introduction to game programming and demonstrates the use of input, loops, Enum objects, functions, and dictionaries in Python.

Feel free to further enhance the game by adding features like keeping score or creating a graphical user interface. Happy coding!

Paper
Game
ChatGPT
Python
Rock
Recommended from ReadMedium