Title: Rock, Paper, Scissors Game with Real-Time Score Updates
Introduction:
Are you ready to test your skills in a classic game of Rock, Paper, Scissors? In this tutorial, we will guide you through the process of creating a Python program that plays this game and keeps track of the player and CPU scores. We will also show you how to update the score in real-time, so you can see who is winning the game as it progresses.
Step 1: Import the necessary libraries
To start, we need to import the necessary libraries. We will be using the random
library to generate random moves for the CPU.
import random
Step 2: Define game messages
Next, we will define the messages that will be displayed during the game. These messages will include an introduction, possible moves, and the outcome of each round.
intro_message = "Let's play a game of rock, paper, scissors!"
possible_moves = ["rock", "paper", "scissors"]
Step 3: Initialize game variables
We will now initialize the game variables. These variables will keep track of the number of games played, the CPU score, the player score, and the round score.
games_played = 0
cpu_score = 0
player_score = 0
round_score = f"Player: {player_score}, CPU: {cpu_score}"
Step 4: Play the game
Now we can play the game. We will use a while
loop to play the game until five rounds have been played. Inside the loop, we will prompt the player to enter their move, generate a random move for the CPU, and determine the outcome of the round.
while games_played != 5:
# This is where the player and CPU each select their move.
player_move = input("Choose your move! Rock, paper, scissors! ").lower()
cpu_move = random.choice(possible_moves)
matchup_statement = f"You chose {player_move}, I chose {cpu_move}."
# Determine the outcome of the round.
if player_move == cpu_move:
round_score = f"Player: {player_score}, CPU: {cpu_score}"
elif player_move == "rock" and cpu_move == "scissors":
round_score = f"You won the round! Player: {player_score}, CPU: {cpu_score}"
player_score += 1
elif player_move == "paper" and cpu_move == "rock":
round_score = f"You won the round! Player: {player_score}, CPU: {cpu_score}"
player_score += 1
elif player_move == "scissors" and cpu_move == "paper":
round_score = f"You won the round! Player: {player_score}, CPU: {cpu_score}"
player_score += 1
else:
round_score = f"You lost the round! Sorry! Player: {player_score}, CPU: {cpu_score}"
cpu_score += 1
# Update the game score.
games_played += 1
round_won = "You win the round!"
round_lost = "You lost the round! Sorry!"
round_draw = "This round is a draw!"
round_score = f"Player: {player_score}, CPU: {cpu_score}"
print(round_score)
Step 5: Display the final score
Finally, we will display the final score. We will use an if
statement to determine
📌 Source: reddit.com
Top comments (0)