![]() |
My version of Rock Paper Scissors - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: My version of Rock Paper Scissors (/thread-27282.html) Pages:
1
2
|
RE: My version of Rock Paper Scissors - menator01 - Jun-02-2020 I can't think of anything. RE: My version of Rock Paper Scissors - GOTO10 - Jun-02-2020 FYI, I figured it out. It runs fine if I run it from the command prompt in Windows or a terminal window in Linux. My mistake is that I was running it directly in IDLE. Cool program! Very sophisticated by Rock, Paper, Scissors standards. RE: My version of Rock Paper Scissors - menator01 - Jun-27-2020 I've added speech to the game. A little slower but, I think it's kinda cool. Requires espeak to be installed. pip install espeak #! /usr/bin/env python3 # Do the imports import random as rnd import os from time import sleep as os_sleep from subprocess import call # Check if the os is *nix or windows # if it's windows import colorama and initiate if os.name == 'nt': import colorama colorama.init() else: pass # Define some basic ansi colors red = '\033[31m' yellow = '\033[33m' cyan = '\033[36m' blue = '\033[34m' magenta = '\033[35m' green = '\033[32m' bright_red = '\033[91m' bright_yellow = '\033[33;1m' bright_cyan = '\033[36;1m' bright_blue = '\033[34;1m' bright_magenta = '\033[35;1m' bright_green = '\033[32;1m' # Other ansi sequences underline = '\033[4m' bold = '\033[1m' # Resets all ansi to system default reset = '\033[0m' class Game: def __init__(self): # Set some default options self.player_wins = 0 self.computer_wins = 0 self.ties = 0 self.games_played = 0 self.player_name = '' # Define our choices self.options = ['rock', 'paper', 'scissors'] # Clears the screen so we do not have cluttered text self.clear() # Display the game title print(f'{underline}{bright_blue}Rock Paper Scissors{reset}\n') # Start the game loop call(['espeak', 'What is your name?']) while True: try: # Check if we have a player name. If not ask for one. if not self.player_name: self.player_name = input("What's your name?: ").lower() # This throws an error if player does not enter a name. if not self.player_name: self.clear() call(['espeak', 'Please enter a name.']) print(f'{bright_red}Error!{reset}: {bright_yellow}Please enter a player name.{reset}') continue # Welcome the player to the game and do a countdown to game start else: self.clear() print(f'Hello {bright_cyan}{self.player_name.title()}{reset}! Welcome to the {bright_blue}Rock Paper Scissors{reset} Game!') call(['espeak', f'Hello {self.player_name}! Welcome to rock paper and scissors!']) # i = 10 # while i > 0: # print(f'Hello {bright_cyan}{self.player_name.title()}{reset}! Welcome to the {bright_blue}Rock Paper Scissors{reset} Game!') # print(f'Game starting in {bright_yellow}{i}{reset}') # i -= 1 # os_sleep(1) # self.clear() # Everything is ok up to this point. Start the game print(f'\n{yellow}Type "q" or "quit" without quotes to exit game.{reset}\n') print(f'{bright_blue}Choices: {", ".join(self.options).title()}{reset}') self.player_choice = input(f'Player Choice: ').lower() # Player enter q or qiut to exit the game if self.player_choice == 'q' or self.player_choice == 'quit': self.clear() # Check for the overall winner if self.player_wins > self.computer_wins: total = self.convert(self.player_wins, self.computer_wins, self.ties) print(f'{bright_cyan}{self.player_name.title()}{reset} is the overall winner.') print(f'{bright_cyan}{self.player_name.title()}{reset} won {total} of the games played.') call(['espeak', f'{self.player_name} is the overall winner. {self.player_name} won {total} of played games.']) elif self.computer_wins > self.player_wins: total = self.convert(self.computer_wins, self.player_wins, self.ties) print(f'{bright_magenta}Computer{reset} is the overall winner.') print(f'{bright_magenta}Computer{reset} won {total} of the games played.') call(['espeak', f'Computer is the over all winner. Computer won {total} of played games.']) else: print(f'No player is the overall winner.') call(['espeak', 'There is no overall winner.']) print(f'{bright_green}Games Played{reset}: {self.games_played}\n') print(f'{bright_yellow}Thanks for playing{reset} {bright_cyan}{self.player_name.title()}{bright_yellow}!{reset}') call(['espeak', f'Thanks for playing {self.player_name}.']) os.sys.exit() # Setup some errors if wrong input is entered if self.player_choice.replace('.', '').isdigit(): self.clear() print(f'{bright_red}Error!: {bright_yellow}Numbers are not allowed.{reset}') call(['espeak', 'Numbers are not allowed']) continue elif self.player_choice not in self.options: self.clear() print(f'{bright_red}Error!: {bright_yellow}Please use only the given choices.{reset}') call(['espeak', 'Please use only the given choices.']) # No errors continue to play else: self.clear() print(self.play()) # Throw an exception if any unknown errors except ValueError as error: print(f'{bright_red}Error!{reset}: {yellow}{error}{reset}') # Show scores print() self.score() # This function is used to clear the screen of cluttered text def clear(self): # Check if the os is windows or not if os.name == 'nt': _ = os.system('cls') else: _ = os.system('clear') def play(self): # Get the computer's choice computer = self.computer() self.games_played += 1 # Print out what each has chosen print(f'\n{bright_cyan}{self.player_name.title()}{reset} Chose: {self.player_choice.title()}') print(f'{bright_magenta}Computer{reset} Chose: {computer.title()}\n') txt1 = 'Rock Crushes Scissors' txt2 = 'Paper Covers Rock' txt3 = 'Scissors Cuts Paper' # Check to see who won and return if self.player_choice == 'rock' and computer == 'scissors': winner = f'{bright_cyan}{self.player_name.title()}{reset} won! {txt1}.' call(['espeak', f'{txt1}. {self.player_name} wins.']) self.player_wins += 1 elif self.player_choice == 'paper' and computer == 'rock': winner = f'{bright_cyan}{self.player_name.title()}{reset} won! {txt2}.' call(['espeak', f'{txt2}. {self.player_name} wins.']) self.player_wins += 1 elif self.player_choice == 'scissors' and computer == 'paper': winner = f'{bright_cyan}{self.player_name.title()}{reset} won! {txt3}.' call(['espeak', f'{txt3}. {self.player_name} wins.']) self.player_wins += 1 elif computer == 'rock' and self.player_choice == 'scissors': winner = f'{bright_magenta}Computer{reset} won! {txt1}' call(['espeak', f'{txt1}. Computer wins.']) self.computer_wins += 1 elif computer == 'paper' and self.player_choice == 'rock': winner = f'{bright_magenta}Computer{reset} won! {txt2}' call(['espeak', f'{txt2}. Computer wins.']) self.computer_wins += 1 elif computer == 'scissors' and self.player_choice == 'paper': call(['espeak', f'{txt3}. Computer wins.']) winner = f'{bright_magenta}Computer{reset} won! {txt3}' self.computer_wins += 1 else: winner = f'{bright_cyan}{self.player_name.title()}{reset} and {bright_magenta}Computer{reset} have tied.\nThere is no winner.' call(['espeak', f'{self.player_name} and computer have tied.']) self.ties += 1 return winner # Get a random choice for the computer and return def computer(self): self.computer_choice = rnd.choice(self.options) return self.computer_choice # Displays the score/wins def score(self): print(f'{cyan}Score{reset}:') print(f'{bright_cyan}{self.player_name.title()}{reset}: {self.player_wins} | {bright_magenta}Computer{reset}: {self.computer_wins} | {bright_yellow}Ties{reset}: {self.ties}') print() # Convert wins into percentages def convert(self, num1, num2, num3): total = num1 / (num1 + num2 + num3) return format(total, '.0%') def main(): Game() if __name__ == '__main__': main() |