Python - Rock, Paper, and Scissor Game

In this tutorial we will create a Rock, Paper, and Scissor Game using Python. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let do the coding...

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules

After setting up the installation, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor
  1. from tkinter import *
  2. import random

Creating the Interface

This is the interface code for the Python application.The window will automatically generate when the program run. To do that just copy the code below then paste it inside the IDLE text editor.
  1. #================================IMAGES========================================
  2. blank_img=PhotoImage(file="images/blank.png")
  3. player_rock=PhotoImage(file="images/player_rock.png")
  4. sm_player_rock=player_rock.subsample(3, 3)
  5. player_paper=PhotoImage(file="images/player_paper.png")
  6. sm_player_paper=player_paper.subsample(3, 3)
  7. player_scissor=PhotoImage(file="images/player_scissor.png")
  8. sm_player_scissor= player_scissor.subsample(3, 3)
  9. com_rock=PhotoImage(file="images/com_rock.png")
  10. com_paper=PhotoImage(file="images/com_paper.png")
  11. com_scissor=PhotoImage(file="images/com_scissor.png")
  12.  
  13. #===============================LABEL WIDGET=========================================
  14. player_img = Label(root,image=blank_img)
  15. comp_img = Label(root,image=blank_img)
  16. lbl_player = Label(root,text="PLAYER")
  17. lbl_player.grid(row=1, column=1)
  18. lbl_player.config(bg="#99ff99")
  19. lbl_computer = Label(root,text="COMPUTER")
  20. lbl_computer.grid(row=1, column=3)
  21. lbl_computer.config(bg="#99ff99")
  22. lbl_status=Label(root, text="", font=('arial', 8))
  23. lbl_status.config(bg="#99ff99")
  24. player_img.grid(row=2,column=1, padx=30, pady=20)
  25. comp_img.grid(row=2,column=3, pady=20)
  26. lbl_status.grid(row=3, column=2)
  27.  
  28.  
  29.  
  30. #===============================BUTTON WIDGET===================================
  31. rock = Button(root, image=sm_player_rock, command=Rock)
  32. paper = Button(root, image=sm_player_paper, command=Paper)
  33. scissor = Button(root, image=sm_player_scissor, command=Scissor)
  34. btn_quit = Button(root, text="Quit", command=ExitApp)
  35. rock.grid(row=4,column=1, pady=30)
  36. paper.grid(row=4,column=2, pady=30)
  37. scissor.grid(row=4,column=3, pady=30)
  38. btn_quit.grid(row=5, column=2)

Creating The Main Function

This is the Main Code for the Python application. The code contains class that has a several function that will be called. To do that just copy the code below then paste it inside the IDLE text editor.
  1. #===============================METHODS========================================
  2. def Rock():
  3.     global player_choice
  4.     player_choice = 1
  5.     player_img.configure(image=player_rock)
  6.     MatchProcess()
  7.  
  8. def Paper():
  9.     global player_choice
  10.     player_choice = 2
  11.     player_img.configure(image=player_paper)
  12.     MatchProcess()
  13.    
  14. def Scissor():
  15.     global player_choice
  16.     player_choice = 3
  17.     player_img.configure(image=player_scissor)
  18.     MatchProcess()
  19.  
  20. def MatchProcess():
  21.     com_choice = random.randint(1,3)
  22.     if com_choice == 1:
  23.         comp_img.configure(image=com_rock)
  24.         ComputerRock()
  25.     elif com_choice == 2:
  26.         comp_img.configure(image=com_paper)
  27.         ComputerPaper()
  28.        
  29.     elif com_choice == 3:
  30.         comp_img.configure(image=com_scissor)
  31.         CompututerScissor()
  32.  
  33. def ComputerRock():
  34.     if player_choice == 1:
  35.         lbl_status.config(text="Game Tie")
  36.     elif player_choice == 2:
  37.         lbl_status.config(text="Player Win")
  38.     elif player_choice == 3:
  39.         lbl_status.config(text="Computer Win")
  40.            
  41. def ComputerPaper():
  42.     if player_choice == 1:
  43.         lbl_status.config(text="Computer Win")
  44.     elif player_choice == 2:
  45.         lbl_status.config(text="Game Tie")
  46.     elif player_choice == 3:
  47.         lbl_status.config(text="Player Win")
  48.    
  49. def CompututerScissor():
  50.     if player_choice == 1:
  51.         lbl_status.config(text="Player Win")
  52.     elif player_choice == 2:
  53.         lbl_status.config(text="Computer Win")
  54.     elif player_choice == 3:
  55.         lbl_status.config(text="Game Tie")
  56.  
  57. def ExitApp():
  58.     root.destroy()
  59.     exit()

Initializing the Application

This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
  1. #========================================INITIALIZATION===================================
  2. if __name__ == '__main__':
  3.     root.mainloop()
There you have it we just created a Rock, Paper, and Scissor Game Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment