-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePage.py
306 lines (183 loc) · 10.7 KB
/
GamePage.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import pygame
import sys
import random
import os
import time
from HighScore import HighScore
class GamePlay():
def __init__(self,args):
self.count = 0
self.selected = args
# Initialized resource path for cross platform compatibility
self.BASE_DIR = os.path.dirname(__file__)
self.RESOURCE_PATH = os.path.join(self.BASE_DIR, 'res')
self.IMAGE_PATH = os.path.join(self.RESOURCE_PATH, 'Images')
# Initializing pygame.
pygame.init()
# Initializing the width and height of the window to be 680 and 1250 pixels respectively.
self.display_height = 680
self.display_width = 1250
# Setting the title and dimensions of the window
pygame.display.set_caption('Train BRAIN')
self.gameDisplay = pygame.display.set_mode((self.display_width, self.display_height))
pygame.mixer.music.load(os.path.join(self.RESOURCE_PATH, 'Songs', 'bgScore.wav'))
pygame.mixer.music.play(-1)
# Defining the clock to set the FPS rates
self.clock = pygame.time.Clock()
# Relatively small sized images to be placed in the main menu
self.witch_image = pygame.image.load(os.path.join(self.IMAGE_PATH, 'ghost.png'))
self.basket_image = pygame.image.load(os.path.join(self.IMAGE_PATH, 'basket.png'))
if self.selected is "Fruits":
self.object_images_raw = ['apple.png', 'bananas.png', 'grapes.png', 'orange.png', 'pineapple.png', 'strawberry.png',
'watermelon.png', 'chilli.png', 'mango.png', 'blueberry.png', 'cherries.png',
'halloween.png','avocado.png']
elif self.selected is "Vegetables":
self.object_images_raw = ['tomato.png', 'beet.png', 'carrot.png', 'cucumber.png', 'eggplant.png',
'potato.png',
'spinach.png', 'chilli.png', 'garlic.png', 'halloween.png', 'cauliflower.png',
'corn.png','okra.png']
elif self.selected is "Animals":
self.object_images_raw = ['cow.png', 'sheep.png', 'dog.png', 'lion.png', 'giraffe.png',
'elephant.png',
'horse.png', 'chicken.png', 'pig.png', 'tiger.png', 'zebra.png',
'cat.png', 'okra.png']
self.object_images = [os.path.join(self.IMAGE_PATH, i) for i in self.object_images_raw]
self.bomb_image = pygame.image.load(self.object_images[7])
self.basket = pygame.image.load(os.path.join(self.IMAGE_PATH, 'basket.png'))
self.minion = pygame.image.load(self.object_images[11])
self.explosion = pygame.image.load(os.path.join(self.IMAGE_PATH, 'ghost.png'))
# Initializing the speed variable which denotes the speed with which the images move.
self.image_speed = 3
# Intialiaing the score variable.
self.score = 0
# Pixels being moved by the basket under keystrokes (10 px)
self.unit = 4
self.user_clicked = False
# Setting white background to the game window
self.gameDisplay.fill((255, 255, 255))
# X and Y co-ordinates of the basket.
self.basket_x = self.display_width / 2 - 200
self.basket_y = self.display_height - 270
# For changing the X co-ordinate of the basket when appropriate keys are pressed.
self.x_change = 0
# Random positions for fruits
self.x = random.randint(0, self.display_width - 250)
self.y = -150
# Randomly loading fruit images
self.random_images = self.object_images[random.randint(0, 12)]
self.random_fruits = pygame.image.load(self.random_images)
def setText(self,text, font_size, position, foreground_color, background_color=None, font_family="Times New Roman"):
# Setting the font and related font features as per the parameters supplied.
font = pygame.font.SysFont(font_family, font_size)
text_to_display = font.render(text, 1, foreground_color, background_color)
self.gameDisplay.blit(text_to_display, position)
pygame.display.update()
# Defining functions for file operations.
def file_open_write(self,high_score):
f = open(os.path.join(self.RESOURCE_PATH, 'File', 'high_score.txt'), 'w')
f.write(high_score)
return f
def file_open_read(self):
f = open(os.path.join(self.RESOURCE_PATH, 'File', 'high_score.txt'), 'r')
best_score = f.read()
return f, best_score
def file_close(self,file):
file.close()
def play(self):
self.play_clicked = True
while self.play_clicked:
# New game window is created on clicking the play button with the same dimensions.
self.play_window = pygame.display.set_mode((1250, 680))
pygame.display.set_caption('Play')
self.play_window.fill((255, 255, 255))
self.play_clock = pygame.time.Clock()
self.play_window.blit(self.random_fruits, (self.x, self.y))
# Horizontal line carrying the basket.
pygame.draw.line(self.play_window, (175, 115, 0), (0, self.display_height - 20), (self.display_width, self.display_height - 20))
# Color beneath the line.
pygame.draw.rect(self.play_window, (243, 128, 12), (0, self.display_height - 18, self.display_width, self.display_height))
for self.event in pygame.event.get():
# Event handling
if self.event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif self.event.type == pygame.KEYDOWN:
if self.event.key == pygame.K_RIGHT:
self.x_change = self.unit
elif self.event.key == pygame.K_LEFT:
self.x_change = -self.unit
elif self.event.key == pygame.K_DOWN:
self.x_change = 0
self.basket_x += self.x_change
self.y += self.image_speed
# Placing the Basket in position
self.play_window.blit(self.basket, (self.basket_x, self.basket_y))
# Placing score on the game window.
self.setText("You collected "+str(self.count)+" items and Your Score is " + str(self.score), 40, (0, 0), (107, 20, 99), (128, 255, 255))
# Checking fruit and basket crossover.
if self.y + 80 >= self.basket_y and self.y + 80 <= self.basket_y + 15:
if self.x >= self.basket_x - 40 and self.x + 100 <= self.basket_x + self.display_width / 2 - 240:
# Checks collision with bomb and minion image
if self.random_images == self.minion or self.random_images == self.object_images[7]:
self.score -= 5
self.setText("Your Score:" + str(self.score), 40, (0, 0), (107, 20, 99), (128, 255, 255))
# Checking whether the current score is greater than the best score.
self.file, self.current_best_score = self.file_open_read()
self.file_close(self.file)
#if self.score > int(self.current_best_score):
#self.file = self.file_open_write(str(self.score))
#self.file_close(self.file)
self.setText("Crashed", 150, (self.display_width / 2 - 240, 35), (0, 0, 0))
self.setText(None, 40, (0, 0), (255, 255, 255))
self.play_window.blit(self.explosion, (self.basket_x, self.basket_y - 80))
pygame.display.update()
time.sleep(3)
for k in range(0, self.display_width + 1, 5):
self.setText("Your Score:" + str(self.score), 40, (k, 0), (107, 20, 99), (128, 255, 255))
pygame.time.wait(20)
time.sleep(2)
pygame.quit()
highScore = HighScore(self.score,self.count)
break
#game_over = True
#play_clicked = False
# Makes the fruit disappear !
self.y = self.display_height
# Incrementing the score appropriately.
if self.random_images == self.object_images[6]:
self.count += 1
self.score += 1
elif self.random_images == self.object_images[0] or self.random_images == self.object_images[1] or self.random_images == self.object_images[
3]:
self.count += 1
self.score += 3
elif self.random_images == self.object_images[4] or self.random_images == self.object_images[5] or self.random_images == self.object_images[
8]:
self.count += 1
self.score += 5
elif self.random_images == self.object_images[2]:
self.count += 1
self.score += 10
# Checking whether the fruit image had crossed the floor.
if self.y >= self.display_height + 200:
# Random positions for fruits
self.x = random.randint(0, self.display_width - 250)
self.y = -150
# Randomly loading fruit images
self.random_images = self.object_images[random.randint(0, 9)]
self.random_fruits = pygame.image.load(self.random_images)
# Increasing the speed in which the basket moves both the directions.
if self.score > 75:
self.unit += 1
# Increasing the speed in which the images moves down.
if self.score > 75:
self.image_speed += 0.5
# Restricting the basket within the width of the Game window
if self.basket_x <= 0:
self.basket_x = 0
elif self.basket_x >= self.display_width - 300:
self.basket_x = self.display_width - 300
pygame.display.update()
self.play_clock.tick(60)
def setEvent(self,gestureEvent):
self.event = gestureEvent