The full code for this project can be found on Michael Linson's GitHub
https://github.com/mykylyn/snake-game/blob/main/userInput/rock_paper.py
That said, let's get started.
from tkinter import *
import random
To begin, we import the libraries, essentially adding the code other people have already written to our project. We want Tkinter for creating the ui and random to simulating randomness.
window=Tk()
intro=Label(text="Rock Paper Scissor")
description=Label(text="Enter Rock, Paper, Scissors:")
Setting up some things, we create a Tk
object by calling Tk()
, which is how a window is made using Tkinter. Additionally, we create some Label
objects that will show on-screen with their respective text
. Each of these are stored in a variable (with the variable name left of the =
) so we can reference them later in our code.
possible_actions=["rock", "paper", "scissors"]
computer_action=random.choice(possible_actions)
Getting into the code proper, we create an array called possible actions with each of the possible moves (stored here as lowercase strings).
computer=Label(text="")
user=Label(text="")
display=Label(text="")
entry=Entry()
computer
will be used to display the computer's move, user
the user's, display
the result, and entry
will use a Tkinter's Entry
object to give a place for text input.
message="none"
user_action=""
We will end up transferring the knowledge of the correct message to display and the user's input between different parts of code, so we declare variables for them out here to be used later.
def Check():
This tells python that the code indented and below this line is not literal instructions to do right now, but code that can be used later via the function Check()
. Here in Check
we want to get the user's action and set message
to the correct text based on the computer's action.
global message
global user_action
user_action=entry.get()
Since these variables were not defined within the function (and not in the same class) we need to use the global keyword to tell python "yes, I'm sure I really do want you to look around for some other variable named message
and user_action
," so we can use them within the function.
There are ways to avoid having to use the messy global keyword (classes for example), but for the sake of a quick project we're not building on too much, "it works."
We can get the text typed into entry
using entry.get()
, which we will read as the user's move of choose.
if user_action==computer_action:
message=f"Both player selected {user_action}. It's a tie!"
elif user_action =="rock":
if computer_action == "scissors":
message="Rock smashes scissors! You win!"
else:
message="Paper covers rock! You lose."
elif user_action =="paper":
if computer_action =="rock":
message="Paper covers the rock! You win!"
else:
message="Scissors cuts paper! You lose"
elif user_action == "scissors":
if computer_action=="paper":
message="Scissors cuts paper! You win!"
else:
message="Rock smashes scissors! You lose."
This is a big chunk of text, but is actually not too difficult to understand. If the user and computer picked the same option, we set message to declare a tie (the f at the beginning of the string allows us to insert user_action
using the surrounding curly brackets {}
). Otherwise, (else and elif statements only run if the previous if or elif statements didn't run) we if the user choose rock, declare a win if the computer choose scissors and a loss otherwise (we already covered the possibility of a tie). On and on for the other two options.
There's many ways to write this code. It's not wrong to just go with straightforward implementations like above, but as you become a better programmer, keep an eye out for ways to condense repetitive code like this (imagine we had 5, 7, or 100 moves, what would you do then?).
def Enter():
global computer_action
global user_action
Check()
computer["text"]="The computer choose "+computer_action
user["text"]="The user choose "+user_action
display["text"] = message
computer_action=random.choice(possible_actions)
Here we define a function to be called when we want to check the result and update the text. Again using global to gain access to our variables, using Check()
(defined above) to set message
and user_action
to the appropriate values. Then, setting the text of the relevant labels to the appropriate text (although computer
, user
, and display
are also variables, Tkinker objects are always global). Finally, we randomize computer_action
again for the next time we play.
Return = Button(window,text="Enter", padx=2, pady=2, bg="lightgreen", font=("Arial, 10"), command=Enter)
Finally, we create a button with the following properties:
- is on our
window
- has
padx
extra horizontal space andpady
extra vertical space around the text - has
bg
background color - uses the given
font
based on its name and font size number - runs
command
function when clicked
intro.pack(fill=X)
description.pack(fill=X)
entry.pack(fill=X, side=BOTTOM)
Return.pack(fill=X, side=BOTTOM)
computer.pack(fill=X, side=BOTTOM)
user.pack(fill=X, side=BOTTOM)
display.pack(fill=X, side=BOTTOM)
window.mainloop()
Putting it all together, remember that our ui elements need to be put on screen with a function such as pack, and that the window's mainloop()
needs to be called for it to actually run.
Top comments (0)