<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Devin</title>
    <description>The latest articles on DEV Community by Devin (@devinisaperson).</description>
    <link>https://dev.to/devinisaperson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2378203%2F71cce306-143d-4b7b-9ba1-dbdb790f4253.png</url>
      <title>DEV Community: Devin</title>
      <link>https://dev.to/devinisaperson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devinisaperson"/>
    <language>en</language>
    <item>
      <title>Rock Paper Scissors in Python with Tkinter</title>
      <dc:creator>Devin</dc:creator>
      <pubDate>Fri, 08 Nov 2024 01:06:23 +0000</pubDate>
      <link>https://dev.to/devinisaperson/rock-paper-scissors-in-python-with-tkinter-2994</link>
      <guid>https://dev.to/devinisaperson/rock-paper-scissors-in-python-with-tkinter-2994</guid>
      <description>&lt;p&gt;The full code for this project can be found on Michael Linson's GitHub&lt;br&gt;
&lt;a href="https://github.com/mykylyn/snake-game/blob/main/userInput/rock_paper.py" rel="noopener noreferrer"&gt;https://github.com/mykylyn/snake-game/blob/main/userInput/rock_paper.py&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That said, let's get started.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from tkinter import *
import random
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window=Tk()

intro=Label(text="Rock Paper Scissor")
description=Label(text="Enter Rock, Paper, Scissors:")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting up some things, we create a &lt;code&gt;Tk&lt;/code&gt; object by calling &lt;code&gt;Tk()&lt;/code&gt;, which is how a window is made using Tkinter. Additionally, we create some &lt;code&gt;Label&lt;/code&gt; objects that will show on-screen with their respective &lt;code&gt;text&lt;/code&gt;. Each of these are stored in a variable (with the variable name left of the &lt;code&gt;=&lt;/code&gt;) so we can reference them later in our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;possible_actions=["rock", "paper", "scissors"]
computer_action=random.choice(possible_actions)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Getting into the code proper, we create an array called possible actions with each of the possible moves (stored here as lowercase strings).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;computer=Label(text="")
user=Label(text="")

display=Label(text="")
entry=Entry()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;computer&lt;/code&gt; will be used to display the computer's move, &lt;code&gt;user&lt;/code&gt; the user's, &lt;code&gt;display&lt;/code&gt; the result, and &lt;code&gt;entry&lt;/code&gt; will use a Tkinter's &lt;code&gt;Entry&lt;/code&gt; object to give a place for text input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;message="none"
user_action=""
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def Check():
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;Check()&lt;/code&gt;. Here in &lt;code&gt;Check&lt;/code&gt; we want to get the user's action and set &lt;code&gt;message&lt;/code&gt; to the correct text based on the computer's action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    global message
    global user_action
    user_action=entry.get()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;message&lt;/code&gt; and &lt;code&gt;user_action&lt;/code&gt;," so we can use them within the function.&lt;br&gt;
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."&lt;br&gt;
We can get the text typed into &lt;code&gt;entry&lt;/code&gt; using &lt;code&gt;entry.get()&lt;/code&gt;, which we will read as the user's move of choose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    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."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;user_action&lt;/code&gt; using the surrounding curly brackets &lt;code&gt;{}&lt;/code&gt;). 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.&lt;br&gt;
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?).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;Check()&lt;/code&gt; (defined above) to set &lt;code&gt;message&lt;/code&gt; and &lt;code&gt;user_action&lt;/code&gt; to the appropriate values. Then, setting the text of the relevant labels to the appropriate text (although &lt;code&gt;computer&lt;/code&gt;, &lt;code&gt;user&lt;/code&gt;, and &lt;code&gt;display&lt;/code&gt; are also variables, Tkinker objects are always global). Finally, we randomize &lt;code&gt;computer_action&lt;/code&gt; again for the next time we play.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Return = Button(window,text="Enter", padx=2, pady=2, bg="lightgreen", font=("Arial, 10"), command=Enter)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we create a button with the following properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is on our &lt;code&gt;window&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;has &lt;code&gt;padx&lt;/code&gt; extra horizontal space and &lt;code&gt;pady&lt;/code&gt; extra vertical space around the text&lt;/li&gt;
&lt;li&gt;has &lt;code&gt;bg&lt;/code&gt; background color&lt;/li&gt;
&lt;li&gt;uses the given &lt;code&gt;font&lt;/code&gt; based on its name and font size number&lt;/li&gt;
&lt;li&gt;runs &lt;code&gt;command&lt;/code&gt; function when clicked
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;mainloop()&lt;/code&gt; needs to be called for it to actually run.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
