-
Notifications
You must be signed in to change notification settings - Fork 2
/
playGame.py
384 lines (310 loc) · 13.1 KB
/
playGame.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import neat.nn
import pygame
from pygame import mixer
import os
from sys import exit
from random import randint
from classes import Floor, Wall, Player
from constants import *
import neat
def reset(screen,floor_image):
global floor_y, floor_yy, FLOOR_VELOCITY, floor_indices, floor_group, START_TIME, FALLING, GAME_ACTIVE
START_TIME = pygame.time.get_ticks() // 1000
FALLING = False
floor_indices = list()
floor_group = []
floor_y = WIN_HEIGHT - 150
floor_yy = WIN_HEIGHT - 150
FLOOR_VELOCITY = 0
bottom_floor = Floor(0, WIN_HEIGHT - 50, WIN_WIDTH)
floor_group.append(bottom_floor)
for i in range(MAX_FLOORS):
f_w = randint(120, 400)
f_x = randint(30, WIN_WIDTH - f_w - 30)
floor = Floor(f_x, floor_yy, f_w)
floor_group.append(floor)
floor_yy -= 100
# GAME_ACTIVE = menu["PlayGame"]
def draw_floor(floor_image, floor_group,screen):
for floor in floor_group:
floor.rect.x = floor.get_x()
# floor.rect.y = floor.get_y()
floor.rect.y += FLOOR_VELOCITY
floor.rectsolid.y += FLOOR_VELOCITY
while floor.rect.x < (floor.get_x() + floor.get_width()):
screen.blit(floor.image, floor.rect)
floor.rect.x += floor_image.get_width()
def welcome(screen,background_image):
SCREEN = screen
pygame.display.set_caption("IcyTower")
tilt = 0
turn = True
font = TEXT_FONT
font.bold = True
font.italic = False
active = True
menu = ["PlayGame", "Instruction", "Profile", "HighScore", "LoadReplay", "Options", "Exit"]
index = 0
menu_dict = {
"PlayGame": True,
"Instruction": False,
"Profile": False,
"HighScore": False,
"LoadReplay": False,
"Options": False,
"Exit": False
}
clock = pygame.time.Clock()
temp_im = pygame.image.load(os.path.join("sprites", "icyMan.png"))
im = pygame.transform.rotozoom(temp_im, 0, 0.5)
menu_img = pygame.image.load(os.path.join("sprites", "menu.png"))
menu_img = pygame.transform.rotozoom(menu_img, 0, 1.3)
menu_rect = menu_img.get_rect(center=(WIN_WIDTH // 2 + 150, WIN_HEIGHT // 2 + 150))
hand_img = pygame.image.load(os.path.join("sprites", "hand.png"))
hand_img = pygame.transform.rotozoom(hand_img, 0, 0.6)
hand_rect = hand_img.get_rect(center=(WIN_WIDTH // 2 - 18, WIN_HEIGHT // 2 + 80))
logo_im = pygame.image.load(os.path.join("sprites", "homescreen1.png")).convert_alpha()
logo_im = pygame.transform.rotozoom(logo_im, 0, 1.2)
logo_rect = logo_im.get_rect(center=(WIN_WIDTH // 2, WIN_HEIGHT // 2 - 130))
while active:
SCREEN.fill((0, 0, 0))
SCREEN.blit(background_image, (0, 0))
SCREEN.blit(logo_im, logo_rect)
rot_image = pygame.transform.rotate(im, tilt)
new_rect = rot_image.get_rect(center=(WIN_WIDTH // 2 - 220, WIN_HEIGHT // 2 + 150))
SCREEN.blit(rot_image, new_rect)
SCREEN.blit(menu_img, menu_rect)
SCREEN.blit(hand_img, hand_rect)
if turn:
tilt += .5
if tilt == 40:
turn = False
else:
tilt -= .5
if tilt == -40:
turn = True
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_ESCAPE) or menu_dict["Exit"]:
pygame.quit()
exit()
if event.key == pygame.K_DOWN and index < (len(menu) - 1):
hand_rect.y += 28.5
menu_dict[menu[index]] = False
menu_dict[menu[index + 1]] = True
index += 1
elif event.key == pygame.K_UP and index > 0:
hand_rect.y -= 28.5
menu_dict[menu[index]] = False
menu_dict[menu[index - 1]] = True
index -= 1
elif event.key == pygame.K_RETURN:
return menu_dict
pygame.display.update()
clock.tick(60)
def handle_move(player, objects):
# decision = output.index(max(output))
# print(decision)decision
keys = pygame.key.get_pressed()
player.x_vel = 0 # If we don't do it player will continue to move untill we press some other key
collide_left = collide(player, objects, -PLAYER_VEL)
collide_right = collide(player, objects, PLAYER_VEL)
if keys[pygame.K_LEFT] and not collide_left:
player.move_left(PLAYER_VEL)
if keys[pygame.K_RIGHT] and not collide_right:
player.move_right(PLAYER_VEL)
# if event.type == pygame.KEYDOWN:
if ((keys[pygame.K_SPACE]) or (keys[pygame.K_UP])) and player.jump_count < 1:
player.current_floor = player.rect.bottom
player.jump()
# Handling horizontal collision
def collide(player, objects, dx):
player.move(dx, 0)
player.update()
collided_object = None
for obj in objects:
if pygame.sprite.collide_mask(player, obj):
collided_object = obj
break
player.move(-dx, 0)
player.update()
return collided_object
def print_text(text, font, screen, pos, color=(202, 241, 222)):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(center=pos)
screen.blit(text_surface, text_rect)
def update_floor(floor, player):
if floor.rect.bottom > WIN_HEIGHT + 200:
# print("Me izz here")
# removed_floor += 1
floor_group.remove(floor)
f_w = randint(120, 400)
f_x = randint(30, WIN_WIDTH - f_w - 30)
floor = Floor(f_x, floor_group[-1].rect.y - 100, f_w)
floor_group.append(floor)
player.highest_floor_index -= 1
player.current_floor_index -= 1
player.old_floor_index -= 1
def check_collision_and_score(floor, player):
global FALLING, FLOOR_VELOCITY
if floor.rectsolid.colliderect(player.rect):
if player.rect.bottom < floor.rectsolid.centery:
if player.y_vel > 0:
player.landed()
player.rect.bottom = floor.rectsolid.top
player.y_vel = 0
player.current_floor_index = floor_group.index(floor)
if player.current_floor_index > player.old_floor_index:
player.score += 10
if player.bonus_y == 4:
player.score += 5
elif player.bonus_y == 8:
player.score += 15
player.old_floor_index = player.current_floor_index
if player.highest_floor_index < player.current_floor_index:
player.highest_floor_index = player.current_floor_index
if (player.rect.top < 100) and not FALLING and FLOOR_VELOCITY == 0:
FLOOR_VELOCITY += 1
FALLING = True
if player.rect.right >= floor.rectsolid.right and player.x_vel != 0:
player.bonus_y = 4
if abs(floor.rectsolid.centerx - floor_group[floor_group.index(floor) + 1].rectsolid.centerx) > 300:
player.bonus_y += 4
elif player.rect.left <= floor.rectsolid.left and player.x_vel != 0:
player.bonus_y = 4
if abs(floor.rectsolid.centerx - floor_group[floor_group.index(floor) + 1].rectsolid.centerx) > 300:
player.bonus_y += 4
else:
player.bonus_y = 0
def draw(floor_image,bg, floor_grp, wallgrp, player, screen):
global scroll, SCROLLING
screen.blit(bg, (0, 0))
draw_floor(floor_image, floor_group, screen)
wallgrp.draw(screen)
# construct_floor(flr,floor_rect,0,WIN_WIDTH)
if player.rect.top <= OFFSET_Y :
if player.y_vel < 0:
# player.y_vel = 0
player.rect.top = OFFSET_Y
scroll = -player.y_vel
# player.move(0,scroll)
else:
scroll = 0
for floor in floor_grp:
# print(floor_indices)
floor.scroll(scroll)
update_floor(floor, player)
check_collision_and_score(floor, player)
player.draw(screen)
def get_high_score():
fr = open("db/score.txt", "r")
highscore = fr.readline()
return int(highscore)
def update_score(score):
f = open("db/score.txt", "r")
old = f.readline()
f.close()
if score > int(old):
fw = open("db/score.txt", "w")
fw.write(str(score))
def gameover(screen, score,menu,background_image):
global GAME_ACTIVE
gameover = pygame.image.load(os.path.join("sprites", "gameover.png"))
gameover = pygame.transform.scale(gameover, (WIN_WIDTH, WIN_HEIGHT))
screen.blit(gameover, (0, 0))
print_text(f"Score: {score}", TEXT_FONT, screen, (WIN_WIDTH // 2 - 100, 25))
print_text(f"High Score: {get_high_score()}", TEXT_FONT, screen, (WIN_WIDTH // 2 + 100, 25))
print_text("Press Space to Play Again", TEXT_FONT, screen, (WIN_WIDTH // 2, 450))
print_text("Press Esc for Main Menu", TEXT_FONT, screen, (WIN_WIDTH // 2, 500))
for event in pygame.event.get():
if event.type == pygame.QUIT or (menu["Exit"]):
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
menu = welcome(screen, background_image)
GAME_ACTIVE = menu["PlayGame"]
if event.key == pygame.K_SPACE:
# tryagain.play()
GAME_ACTIVE = True
def play():
global GAME_ACTIVE, START_TIME, FLOOR_VELOCITY, floor_yy, gen
# Defining pygame screen to draw assets and component
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
# Setting up caption (window title)
pygame.display.set_caption("Icy Tower")
# Loading and setting up window icon
game_icon = pygame.image.load(os.path.join("icons", "icytowericon.png"))
pygame.display.set_icon(game_icon)
# Initialising pygame clock
clock = pygame.time.Clock()
# Loading background and floor image
background_image = pygame.image.load(os.path.join("sprites", 'background.jpg')).convert()
floor_image = pygame.image.load(os.path.join('sprites', 'icy2.png')).convert()
# Getting floor rectangle
floor_rect = floor_image.get_rect(topleft=(0, WIN_HEIGHT - floor_image.get_height()))
# Creating Bottom Floor
bottom_floor = Floor(0, WIN_HEIGHT - 50, WIN_WIDTH)
floor_group.append(bottom_floor)
# Generating n (MAX_FLOORS) number of random floors
for i in range(MAX_FLOORS):
f_w = randint(120, 400) # Generating Random width
f_x = randint(30, WIN_WIDTH - f_w - 30) # Generating Random x position
floor = Floor(f_x, floor_yy, f_w) # Creating a Floor object with these values
floor_group.append(floor) # Adding all our floors to floor_group
floor_yy -= 100 # Decrementing y location for next floor
# Defining wallGroup (sprite group)
wallGroup = pygame.sprite.Group()
# Generating/Constructing Walls
for i in range(3):
wallR = Wall(804, i * 240, "right")
wallL = Wall(45, i * 240, "left")
wallGroup.add(wallR)
wallGroup.add(wallL)
# Loading Music/Sound Assets
mixer.music.load(music_dict["gameBG"])
gameOverSound = mixer.Sound(music_dict["gameOverSound"])
tryagain = mixer.Sound(music_dict["tryagain"])
mixer.music.play(-1) # Playing main game theme
# Creating player object
player = Player(WIN_WIDTH // 2, WIN_HEIGHT - floor_image.get_height() - PLAYER_Y - 20, PLAYER_X, PLAYER_Y)
menu = welcome(screen, background_image)
# Setting GAME_ACTIVE as per user input
GAME_ACTIVE = menu["PlayGame"]
# Getting time from pygame clock at the start of the game
START_TIME = pygame.time.get_ticks() // 1000
while True:
draw(floor_image, background_image, floor_group, wallGroup, player, screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if ((event.key == pygame.K_SPACE)or (event.key == pygame.K_UP)) and player.jump_count < 1:
player.current_floor = player.rect.bottom
player.jump()
if GAME_ACTIVE:
current_time = pygame.time.get_ticks() // 1000 - START_TIME
if current_time % 30 == 0 and current_time != 0 and FLOOR_VELOCITY < 4:
FLOOR_VELOCITY += 1
START_TIME += current_time
player.loop(60)
handle_move(player,wallGroup)
print_text(f"Score: {player.score}", TEXT_FONT, screen, (140, 10))
if player.rect.top > WIN_HEIGHT:
GAME_ACTIVE = False
gameOverSound.play()
update_score(player.score)
# if len(players) < 1 :
# GAME_ACTIVE = False
else:
gameover(screen, player.score, menu, background_image)
reset(screen, floor_image)
# GAME_ACTIVE = True
mixer.Channel(0).set_volume(1)
pygame.display.update()
clock.tick(60)
# reset(screen, floor_image)