-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
233 lines (183 loc) · 8.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import pygame
import random
import time
from winner_decider import decide_winner
from button import Button
pygame.init()
# Setting up the game display resolution
screen_res = (420, 420)
win = pygame.display.set_mode(screen_res)
# Setting up the hands
hand_x = 170
hand_y = 260
hand_width = 80
hand_height = 150
computer_x = 175
computer_y = 0
class StartPage():
def __init__(self, player_hand_coords, computer_hand_coords, hands_res, win):
self.hand_x, self.hand_y = player_hand_coords
self.computer_x, self.computer_y = computer_hand_coords
self.hand_width, self.hand_height = hands_res
self.win = win
quit_game = False
while not quit_game:
start_button = Button((0, 255, 0), 90, 160, 250, 50, "Start game")
start_button.draw(self.win, (0, 0, 255))
instructions_button = Button(
(255, 0, 255), 90, 210, 250, 50, "How to play")
instructions_button.draw(self.win, (0, 0, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game = True
if event.type == pygame.MOUSEBUTTONDOWN:
if start_button.isOver(pygame.mouse.get_pos()):
self.start_game()
if instructions_button.isOver(pygame.mouse.get_pos()):
self.go_to_inst_page()
pygame.display.update()
def start_game(self):
return GamePage((self.hand_x, self.hand_y), (self.computer_x, self.computer_y), (self.hand_width, self.hand_height), self.win).start_game()
def go_to_inst_page(self):
return HowToPlay(self.win, (self.hand_width, self.hand_height))
class HowToPlay():
def __init__(self, win, hands_res):
self.win = win
self.res = (hands_res[0]-20, hands_res[1]-20)
self.on_this_page = True
while self.on_this_page:
self.win.fill((0, 0, 0))
self.hand = pygame.image.load("Game Pictures/hand.png")
self.hand = pygame.transform.scale(self.hand, self.res)
self.rock = pygame.image.load("Game Pictures/rock.png")
self.rock = pygame.transform.scale(self.rock, self.res)
self.scissors = pygame.image.load("Game Pictures/scissors.png")
self.scissors = pygame.transform.scale(self.scissors, self.res)
self.win.blit(self.scissors, (50, 50))
self.win.blit(self.rock, (180, 50))
self.win.blit(self.hand, (300, 50))
self.left_click = pygame.image.load("Game Pictures/left_click.png")
self.left_click = pygame.transform.scale(
self.left_click, (self.res[0]+35, self.res[1]))
self.middle_click = pygame.image.load("Game Pictures/middle_click.png")
self.middle_click = pygame.transform.scale(
self.middle_click, (self.res[0]+20, self.res[1]))
self.right_click = pygame.image.load("Game Pictures/right_click.png")
self.right_click = pygame.transform.scale(
self.right_click, (self.res[0]+30, self.res[1]))
self.win.blit(self.left_click, (40, 220))
self.win.blit(self.middle_click, (165, 220))
self.win.blit(self.right_click, (285, 220))
self.back_button = Button(
(230, 185, 200), 80, 365, 250, 50, "Go back")
self.back_button.draw(self.win, (0, 0, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN:
if self.back_button.isOver(pygame.mouse.get_pos()):
self.win.fill((0, 0, 0))
self.on_this_page = False
pygame.display.update()
class GamePage():
def __init__(self, player_hand_coords, computer_hand_coords, hands_res, window):
self.hand_x, self.hand_y = player_hand_coords
self.computer_x, self.computer_y = computer_hand_coords
self.hand_width, self.hand_height = hands_res
self.win = window
def start_game(self):
my_hand = pygame.image.load("Game Pictures/rock.png")
my_hand = pygame.transform.scale(
my_hand, (self.hand_width, self.hand_height))
computer_hand = pygame.image.load("Game Pictures/rock.png")
computer_hand = pygame.transform.scale(
my_hand, (self.hand_width, self.hand_height))
computer_hand = pygame.transform.rotate(computer_hand, 180)
rock_hand = pygame.image.load("Game Pictures/rock.png")
rock_hand = pygame.transform.scale(
rock_hand, (self.hand_width, self.hand_height))
scissors_hand = pygame.image.load("Game Pictures/scissors.png")
scissors_hand = pygame.transform.scale(
scissors_hand, (self.hand_width, self.hand_height))
hand_hand = pygame.image.load("Game Pictures/hand.png")
hand_hand = pygame.transform.scale(
hand_hand, (self.hand_width, self.hand_height))
options = [rock_hand, scissors_hand, hand_hand]
game = True
while game:
start_game = False
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# Getting mouse button states
if event.type == pygame.MOUSEBUTTONDOWN:
start_game = True
if pygame.mouse.get_pressed()[0]:
my_hand = scissors_hand
my_hand_name = "scissors"
elif pygame.mouse.get_pressed()[1]:
my_hand = rock_hand
my_hand_name = "rock"
elif pygame.mouse.get_pressed()[2]:
my_hand = hand_hand
my_hand_name = "hand"
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
game = False
# Giving the ability to move the mouse
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and self.hand_x < 340:
self.hand_x += 15
self.computer_x += 15
elif keys[pygame.K_LEFT] and self.hand_x > 0:
self.hand_x -= 15
self.computer_x -= 15
elif keys[pygame.K_UP] and self.hand_y > 209:
self.hand_y -= 15
self.computer_y += 15
elif keys[pygame.K_DOWN] and self.hand_y < 270:
self.hand_y += 15
self.computer_y -= 15
# Filling the background with black to avoid error
self.win.fill((0, 0, 0))
# Displaying the hand on the screen
self.win.blit(my_hand, (self.hand_x, self.hand_y))
# Displaying the computer hand
self.win.blit(computer_hand, (self.computer_x, self.computer_y))
# A mouse press starts the game
if start_game:
self.hand_x = 170
self.hand_y = 260
computer_hand = random.choice(options)
if computer_hand == rock_hand:
computer_hand_name = "rock"
elif computer_hand == scissors_hand:
computer_hand_name = "scissors"
else:
computer_hand_name = "hand"
computer_hand = pygame.transform.rotate(computer_hand, 180)
self.win.blit(
computer_hand, (self.computer_x, self.computer_y))
self.computer_x = 175
self.computer_y = 0
winner, color = decide_winner(my_hand_name, computer_hand_name)
if color != (0, 0, 255):
winner_text = f"The winner is {winner}"
else:
winner_text = "It is a freakin' DRAW!!!!!"
# Showing up a message to display the winner
font = pygame.font.Font("freesansbold.ttf", 35)
text = font.render(winner_text, True, color, (0, 0, 128))
textRect = text.get_rect()
self.win.blit(text, (-2, 180))
pygame.display.update()
time.sleep(1)
pygame.display.update()
self.win.fill((0, 0, 0))
class MyGame():
def run(self):
return StartPage((hand_x, hand_y), (computer_x, computer_y), (hand_width, hand_height), win)
if __name__ == "__main__":
MyGame().run()
pygame.quit()