Skip to content

Commit 3dccc00

Browse files
authored
Merge pull request #3 from AyushSolanki-17/RockPaperScissor
Added Rock Paper Scissors game with GUI
2 parents 471bea6 + 6d7d34b commit 3dccc00

File tree

11 files changed

+132
-0
lines changed

11 files changed

+132
-0
lines changed
48.8 KB
Loading
8.14 KB
Loading
326 KB
Binary file not shown.
8.99 KB
Loading
12.1 KB
Loading
8.69 KB
Loading
8.93 KB
Loading
12.2 KB
Loading
8.73 KB
Loading

RockPaperScissor(GUI)/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from rpsEngine import GameScreen
2+
3+
if __name__ == '__main__':
4+
window = GameScreen()
5+
window.button_panel()
6+
window.game_panel()
7+
window.mainloop()
8+

RockPaperScissor(GUI)/rpsEngine.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
from tkinter import *
2+
from tkinter.font import Font
3+
from tkinter import messagebox
4+
import random
5+
6+
7+
class GameScreen(Tk):
8+
def __init__(self):
9+
super().__init__()
10+
self.user_points = 0
11+
self.comp_points = 0
12+
self.choices = ['rock', 'paper', 'scissor']
13+
self.user_choice = 'default'
14+
self.comp_choice = 'default'
15+
self.bgcolor = '#eb4034'
16+
self.title("Rock Paper Scissor")
17+
self.photo = PhotoImage(file="./img/RockPaperScissor.png")
18+
self.iconphoto(False, self.photo)
19+
self.geometry("1000x550")
20+
self.resizable(False, False)
21+
self.configure(background=self.bgcolor)
22+
self.tframe = Frame(self,bg=self.bgcolor)
23+
self.tframe.pack(side=TOP)
24+
self.heading = Label(self.tframe, text='Rock Paper Scissor by Ayush Solanki', font=Font(size=25),
25+
bg=self.bgcolor)
26+
self.heading.pack()
27+
28+
def button_panel(self):
29+
self.lframe = Frame(self, bg=self.bgcolor)
30+
self.lframe.pack(side=LEFT, padx=(25,0))
31+
self.left_label = Label(self.lframe, text='Choose here', width=15, font=Font(size=15,), bg= self.bgcolor)
32+
self.left_label.pack(pady=5)
33+
self.left_rock = Button(self.lframe, text='Rock', width=15, font=Font(size=15,), bg='#61cf7b',
34+
activebackground='#f2ce55', command=self.selectrock)
35+
self.left_rock.pack(pady=5)
36+
self.left_paper = Button(self.lframe, text='Paper', width=15, font=Font(size=15,), bg='#61cf7b',
37+
activebackground='#f2ce55', command=self.selectpaper)
38+
self.left_paper.pack(pady=5)
39+
self.left_scissor = Button(self.lframe, text='Scissor', width=15, font=Font(size=15,), bg='#61cf7b',
40+
activebackground='#f2ce55', command=self.selectscissor)
41+
self.left_scissor.pack(pady=5)
42+
43+
def game_panel(self):
44+
self.rframe = Frame(self, bg=self.bgcolor)
45+
self.rframe.pack(side=RIGHT)
46+
self.lblframe = LabelFrame(self.rframe,width=500, bg=self.bgcolor, highlightthickness=0, border=0)
47+
self.lblframe.pack(side=TOP)
48+
self.user_label = Label(self.lblframe, text='User: 0', font=Font(size=25,), bg=self.bgcolor,fg='#ffffff')
49+
self.user_label.pack(side=LEFT, padx=(25,100))
50+
self.computer_label = Label(self.lblframe, text='Computer: 0', font=Font(size=25), bg=self.bgcolor,fg='#ffffff')
51+
self.computer_label.pack(side=RIGHT, padx=(100,25))
52+
self.user_canvas = Canvas(self.rframe, width=200, height=200, highlightthickness=0, bg='#32a852')
53+
self.user_canvas.create_image(0, 0, anchor='nw', image=self.photo)
54+
self.user_canvas.pack(side=LEFT, padx=(25,225),pady=0)
55+
self.comp_canvas = Canvas(self.rframe, width=200, height=200, highlightthickness=0, bg='#32a852')
56+
self.comp_canvas.create_image(0, 0, anchor='nw', image=self.photo)
57+
self.comp_canvas.pack(padx=(25,100))
58+
59+
def selectrock(self):
60+
self.user_choice = 'rock'
61+
self.gameplay()
62+
def selectpaper(self):
63+
self.user_choice = 'paper'
64+
self.gameplay()
65+
def selectscissor(self):
66+
self.user_choice = 'scissor'
67+
self.gameplay()
68+
def gameplay(self):
69+
if self.user_points < 5 and self.comp_points < 5:
70+
self.comp_choice = random.choice(self.choices)
71+
user_image = PhotoImage(file=f"img/left/{self.user_choice}.png")
72+
comp_image = PhotoImage(file=f"img/right/{self.comp_choice}.png")
73+
self.user_canvas.delete("all")
74+
self.user_canvas.create_image(0, 0, anchor='nw', image=user_image)
75+
self.user_canvas.image = user_image
76+
self.comp_canvas.delete("all")
77+
self.comp_canvas.create_image(0, 0, anchor='nw', image=comp_image)
78+
self.comp_canvas.image = comp_image
79+
if self.comp_choice != self.user_choice:
80+
if self.user_choice == 'rock':
81+
if self.comp_choice == 'paper':
82+
self.comp_points += 1
83+
elif self.comp_choice == 'scissor':
84+
self.user_points += 1
85+
elif self.user_choice == 'paper':
86+
if self.comp_choice == 'scissor':
87+
self.comp_points += 1
88+
elif self.comp_choice == 'rock':
89+
self.user_points += 1
90+
elif self.user_choice == 'scissor':
91+
if self.comp_choice == 'rock':
92+
self.comp_points += 1
93+
elif self.comp_choice == 'paper':
94+
self.user_points += 1
95+
self.user_label.configure(text=f'User: {self.user_points}')
96+
self.computer_label.configure(text=f'Computer: {self.comp_points}')
97+
98+
if self.user_points >=5:
99+
res = messagebox.askyesno("User Won!!", "User wins the game :) want to play again?")
100+
if res:
101+
self.resetgame()
102+
elif self.comp_points >=5:
103+
res = messagebox.askyesno("Computer Won!!", "Computer wins the game :( want to play again?")
104+
if res:
105+
self.resetgame()
106+
else:
107+
pass
108+
109+
def resetgame(self):
110+
self.user_points = 0
111+
self.comp_points = 0
112+
self.user_choice = 'default'
113+
self.comp_choice = 'default'
114+
user_image = PhotoImage(file=f"img/RockPaperScissor.png")
115+
comp_image = PhotoImage(file=f"img/RockPaperScissor.png")
116+
self.user_canvas.delete("all")
117+
self.user_canvas.create_image(0, 0, anchor='nw', image=user_image)
118+
self.user_canvas.image = user_image
119+
self.comp_canvas.delete("all")
120+
self.comp_canvas.create_image(0, 0, anchor='nw', image=comp_image)
121+
self.comp_canvas.image = comp_image
122+
self.user_label.configure(text=f'User: {self.user_points}')
123+
self.computer_label.configure(text=f'Computer: {self.comp_points}')
124+

0 commit comments

Comments
 (0)