In this article we will see how we can create a rock paper and scissor game using PyQt5. Rock paper scissors is a hand game usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are “rock”, “paper”, and “scissors”. Below is the representation of how the game will look like.
GUI implementation steps :
1. Create a head label that will show the title of the game, set its font and properties
2. Below the head label create a user label that will show the hand sign selected by user
3. Create a computer label that will show hand sign picked by the computer
4. In between the user and the computer label create a label to show text “vs”
5. Create a result label to show the result set font and other properties to it
6. Create three push buttons for rock, paper and scissor respectively
7. Create a reset button to reset the game
Back end implementation steps :
1. Create user choice and counter variable set its value to -1
2. Add actions to the rock, paper and scissor button
3. Inside the actions set the choice value according to the button pressed and set the counter value to 3 and make all the three button disable
4. Create timer object that calls the method after every one second
5. Inside the timer method check if the counter value is -1 then do nothing else set the counter value to the computer label and decrement the counter
6. And check if the counter value is equal to 0 then get a random value from 1 to 3, according to value set the hand symbol to the computer label
7. Call the who_win method to get the result
8. Inside the who_wins method first check if the match is draw else find the winner and set the winner to the result label
9. Add action to the reset button
10. Inside the reset button action, set counter value to -1, enable all the buttons and remove the image from the computer and user label
Below is the implementation
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.