Tried my luck with rock paper scissors game from one of the post.
It contains terminal/console colors and may not show colors on windows systems.
As always, I welcome feedback.
It contains terminal/console colors and may not show colors on windows systems.
As always, I welcome feedback.
#! /usr/bin/env python3 import random as rnd import os import sys from subprocess import call class Game: def __init__(self): # Default scores self.player_wins = 0 self.computer_wins = 0 # Setup our choices self.options = ['rock', 'paper', 'scissors'] # Clear the screen and display title self.clear() print(f'\033[38;2;15;200;200m\033[4m\033[1mRock Paper Scissors\033[0m') print() while True: try: # Show wins/scores self.score() # Display player Choices print(f'\033[38;2;100;255;100m"q" or "quit" without quotes to exit game\033[0m \n') print(f'\033[38;2;255;190;190mChoices: {", ".join(self.options)}\033[0m') # Give player input field self.player_choice = input('Player Choice: ').lower() # If player wants to exit the game if self.player_choice == 'q' or self.player_choice == 'quit': self.clear() # Display the over all winner if self.player_wins > self.computer_wins: total = self.convert(self.player_wins, self.computer_wins) print(f'Player is over all winner. Player won {total} of the games played.') elif self.player_wins < self.computer_wins: total = self.convert(self.computer_wins, self.player_wins) print(f'Computer is over all winner. Computer won {total} of the games played.') else: print('Nobody is the over all winner') print('\033[38;2;20;190;255mThanks for playing\033[0m') sys.exit() # Setup some errors if wrong input is entered if self.player_choice.replace('.','').isdigit(): self.clear() print(f'\033[38;2;255;0;0mNumbers are not allowed\033[0m') continue elif self.player_choice not in self.options: self.clear() print(f'\033[38;2;255;0;0mPlease use only the given choices\033[0m') continue else: self.clear() print(self.play()) print() except ValueError as error: print(f'Error: {error}') def play(self): # Get the computers random choice computer = self.computer() # Display player and computers choices print(f'\033[38;2;10;250;190mPlayer Choice\033[0m: {self.player_choice}') print(f'\033[38;2;190;190;150mComputer Choice\033[0m: {computer}') # Who wins if self.player_choice == 'rock' and computer == 'scissors': winner = f'\033[38;2;10;250;190mPlayer\033[0m\033[38;2;10;200;250m {self.player_choice} beats {computer}' self.player_wins += 1 elif self.player_choice == 'paper' and computer == 'rock': winner = f'\033[38;2;10;250;190mPlayer\033[0m\033[38;2;10;200;250m {self.player_choice} beats {computer}' self.player_wins += 1 elif self.player_choice == 'scissors' and computer == 'paper': winner = f'\033[38;2;10;250;190mPlayer\033[0m\033[38;2;10;200;250m {self.player_choice} beats {computer}' self.player_wins += 1 elif self.player_choice == computer: winner = 'Player and Computer tie' else: winner = f'\033[38;2;190;190;150mComputer\033[0m\033[38;2;10;200;250m {computer} beats {self.player_choice}' self.computer_wins += 1 return f'\n\033[38;2;10;200;250mWinner: {winner}\033[0m' # Helps clear the screen so there is not a terminal full of text def clear(self): _ = call('clear' if os.name == 'posix' else 'cls') # Define the computers choice def computer(self): self.computer_choice = rnd.choice(self.options) return self.computer_choice # For displaying the score/wins def score(self): print('\033[1m\033[38;2;200;200;150mWins:\033[0m') print(f'\033[38;2;10;250;190mPlayer\033[0m: {self.player_wins} | \033[38;2;190;190;150mComputer\033[0m: {self.computer_wins}') print() def convert(self, num1, num2): total = num1 / (num1 + num2) return format(total, '.0%') def main(): Game() if __name__ == '__main__': main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts