Rock-Paper-Scissors Game | Python

PLAY THE GAME HERE

Today, I coded a Rock, Paper, Scissors game which is played against a computer. After opening the program, the user is immediately prompted to enter either 1 (Rock), 2 (Paper), or 3 (Scissors). After making a selection, the terminal presents the user’s selection in ASCII art along with the computer’s selection and a verdict on who won.

This project enabled me to work with modules as well as create functions. Let’s take a look at some of the code:

First, two imports are made. One for ‘ascii_icons’ and one for ‘logic’. The ‘ascii_icons' module enables me to easily reference the ASCII art later in the functions. This also enables the main.py file to be visually cleaner by obfuscating the long print statements required for the ASCII art. I also created a 'logic' module which holds the process for computing a random play for the computer's turn. It’s also worth noting that due to the simplicity of this logic process, a separate module for it is not technically needed. Here is how the ‘logic’ module works:

Held in a separate file, logic.py, the first process is to import a Python standard module called ‘random(yes, a module import inside of a module import…how meta). The ‘random’ module enables the ‘logic’ module to generate a random number. Then, a variable called 'computer_choice' is declared and assigned with a random integer between 0 and 2.

Only the user_print() function is visible here. At the bottom, you can see computer_print(). However, this has been collapsed for brevity.

Next, a function named user_print() is defined. When called, this function checks the user’s input (‘user_choice’) and returns the corresponding ASCII art. If the user enters anything other than 0, 1, or 2, the function falls back to a return of ‘INVALID ENTRY’. The computer_print() function operates in exactly the same way, but references ‘computer_choice’ instead.

After importing the needed modules and defining the functions, we can now begin to write the actual program. First, a prompt is printed asking the user for a selection with a key as to which each value represents. This value is converted to an integer and stored in the ‘user_choice’ variable. Then, the user_print() function is called in a terminal print statement, which prints the corresponding ASCII art based on what is held in ‘user_choice’.

Another print statement with ‘Computer Chose:’ is printed along with the ASCII art from the computer_print() function.

Finally, the logic to determine who won is held at the bottom of the file. For simplicity, I hard coded the three possible winning outcomes with their corresponding print statements. If a win outcomes is not met, the logic then checks to see if ‘user_choice’ and ‘computer_choice’ are equal to each other. If so, a ‘Draw’ print statement is displayed. Otherwise, the computer won and the terminal prints ‘You Lose!’.


You can view and download the source code for this project on my GitHub by clicking here. I have also included a GIF of the gameplay, in the documentation if you are interested in seeing what that looks like without downloading the files and running them locally.

Thank you for your time!

Previous
Previous

Password Generator | Python

Next
Next

Choose Your Own Adventure Game | Python