Making Rock Paper Scissors in Python: A Step-by-Step Guide
Introduction
Rock Paper Scissors is a simple game that involves competing against another player to see who has the best hand. It’s a great way to practice basic game development skills and learn about algorithms and data structures. In this article, we’ll walk through how to make a Rock Paper Scissors game in Python.
Setting Up the Game
Before we begin, we need to set up our game. We’ll create a game window with a menu that allows us to play against another player. We’ll also need to store the players’ hands and the game’s current state.
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up some constants
WIDTH, HEIGHT = 400, 400
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Set up the font
font = pygame.font.Font(None, 36)
# Set up the clock
clock = pygame.time.Clock()
# Store the players' hands
players = []
Game Logic
Now that we have our setup, let’s move on to the game logic. We’ll need to determine who wins the game, update the players’ hands, and display the game state.
def get_player_hand(player_index):
"""
Get the hand of a player.
Args:
player_index (int): The index of the player.
Returns:
list: The hand of the player.
"""
players[player_index] = ['rock', 'paper', 'scissors'][random.randint(0, 2)]
return players[player_index]
def get_computer_hand():
"""
Get the hand of the computer.
Returns:
list: The hand of the computer.
"""
return ['rock', 'paper', 'scissors'][random.randint(0, 2)]
def get_computer_score(player_index, computer_index):
"""
Get the score of a player against the computer.
Args:
player_index (int): The index of the player.
computer_index (int): The index of the computer.
Returns:
tuple: The score of the player and the computer.
"""
if players[player_index] == players[computer_index]:
return (1, 0)
elif (players[player_index] == 'rock' and **computer_index == 'scissors') or
(players[player_index] == 'paper' and **computer_index == 'rock') or
(players[player_index] == 'scissors' and **computer_index == 'paper'):
return (0, 1)
else:
return (0, 0)
def game_loop():
"""
The main game loop.
Returns:
bool: Whether the game should continue.
"""
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
players = get_player_hand(0)
elif event.key == pygame.K_p:
players = get_player_hand(1)
elif event.key == pygame.K_s:
players = get_computer_hand()
elif event.key == pygame.K_e:
computer_index = 0
if players[0] == players[1]:
computer_index = 1
elif (players[0] == 'rock' and **computer_index == 'scissors') or
(players[0] == 'paper' and **computer_index == 'rock') or
(players[0] == 'scissors' and **computer_index == 'paper'):
computer_index = 1
score = get_computer_score(0, computer_index)
print(f'Computer wins this round with a score of {score}')
elif event.key == pygame.K_n:
computer_index = 1
if players[0] == players[1]:
computer_index = 0
elif (players[0] == 'rock' and **computer_index == 'scissors') or
(players[0] == 'paper' and **computer_index == 'rock') or
(players[0] == 'scissors' and **computer_index == 'paper'):
computer_index = 0
score = get_computer_score(1, computer_index)
print(f'Computer wins this round with a score of {score}')
elif event.key == pygame.K_o:
players = get_computer_hand()
screen.fill(BLACK)
if len(players) > 1:
player1 = get_player_hand(0)
player2 = get_player_hand(1)
computer = get_computer_hand()
print(f'You chose {player1}')
print(f'Computer chose {computer}')
if player1 == computer:
print('It's a tie!')
elif (player1 == 'rock' and **computer == 'scissors') or
(player1 == 'paper' and **computer == 'rock') or
(player1 == 'scissors' and **computer == 'paper'):
print('You win!')
else:
print('Computer wins this round!')
pygame.display.flip()
clock.tick(60)
pygame.quit()
Running the Game
Now that we have our setup, let’s run the game. We’ll use the keyboard to play against the computer.
- Open a terminal or command prompt and navigate to the directory where you saved the script.
- Type
python rock_paper_scissors.pyto run the game. - Use the R key to play against the computer, or the N key to play against another player.
Tips and Variations
- To make the game more challenging, you can introduce additional rules, such as forcing players to play again if they tie.
- To make the game easier, you can allow players to win immediately if they tie.
- To add more realism to the game, you can introduce player AI that can make random moves.
- To make the game more interactive, you can add a scoring system and display the scores of both players.
Conclusion
Making Rock Paper Scissors in Python is a simple and straightforward process that requires just a few lines of code. By following the steps outlined in this article, you can create a fun and interactive game that can be played against another player or the computer. Whether you’re a beginner or an experienced programmer, this game is a great way to practice your skills and have some fun.
