Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (111 loc) · 4.07 KB
/
Copy pathmain.py
File metadata and controls
139 lines (111 loc) · 4.07 KB
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
from maze_generator import *
class Food:
def __init__(self):
self.img = pygame.image.load('img/money.png').convert_alpha()
self.img = pygame.transform.scale(self.img, (TILE - 6, TILE - 6))
self.rect = self.img.get_rect()
self.set_pos()
def set_pos(self):
self.rect.topleft = randrange(cols) * TILE + 5, randrange(rows) * TILE + 5
def draw(self):
game_surface.blit(self.img, self.rect)
def is_collide(x, y):
tmp_rect = player_rect.move(x, y)
if tmp_rect.collidelist(walls_collide_list) == -1:
return False
return True
def eat_food():
for food in food_list:
if player_rect.collidepoint(food.rect.center):
food.set_pos()
return True
return False
def is_game_over():
global time, score, record, FPS
if time < 0:
pygame.time.wait(700)
player_rect.center = TILE // 2, TILE // 2
[food.set_pos() for food in food_list]
set_record(record, score)
def get_record():
try:
with open('record') as f:
return f.readline()
except FileNotFoundError:
with open('record', 'w') as f:
f.write('0')
return 0
def set_record(record, score):
rec = max(int(record), score)
with open('record', 'w') as f:
f.write(str(rec))
FPS = 60
pygame.init()
game_surface = pygame.Surface(RES)
score_surface = pygame.display.set_mode((WIDTH +0, HEIGHT))
clock = pygame.time.Clock()
# images
bg_game = pygame.image.load('img/bg_1.jpg').convert()
bg_game = pygame.transform.scale(bg_game,RES)
bg = pygame.image.load('img/bg_main.jpg').convert()
# get maze
maze = generate_maze()
# player settings
player_speed = 4
player_img = pygame.image.load('img/shark.png').convert_alpha()
player_img = pygame.transform.scale(player_img, (TILE - 2 * maze[0].thickness, TILE - 2 * maze[0].thickness))
player_rect = player_img.get_rect()
player_rect.center = TILE // 2, TILE // 2
directions = {'LEFT': (-player_speed, 0), 'RIGHT': (player_speed, 0), 'UP': (0, -player_speed), 'DOWN': (0, player_speed)}
keys = {'LEFT': pygame.K_LEFT, 'RIGHT': pygame.K_RIGHT, 'UP': pygame.K_UP, 'DOWN': pygame.K_DOWN}
direction = (0, 0)
# food settings
food_list = [Food() for i in range(4)]
# collision list
walls_collide_list = sum([cell.get_rects() for cell in maze], [])
# timer, score, record
pygame.time.set_timer(pygame.USEREVENT, 1000)
time = 60
score = 0
record = get_record()
# fonts
font = pygame.font.SysFont('Impact', 150)
text_font = pygame.font.SysFont('Impact', 80)
while True:
score_surface.blit(bg, (WIDTH - 300, 0))
score_surface.blit(game_surface,(0,0))
game_surface.blit(bg_game, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.USEREVENT:
time -= 1
# controls and movement
pressed_key = pygame.key.get_pressed()
for key, key_value in keys.items():
if pressed_key[key_value] and not is_collide(*directions[key]):
direction = directions[key]
break
if not is_collide(*direction):
player_rect.move_ip(direction)
# draw maze
[cell.draw(game_surface) for cell in maze]
# gameplay
if eat_food():
FPS += 60
score += 1
is_game_over()
# draw player
game_surface.blit(player_img, player_rect)
# draw food
[food.draw() for food in food_list]
# draw stats
score_surface.blit(text_font.render('TIME', True, pygame.Color('cyan'), True), (WIDTH - 230, 30))
score_surface.blit(font.render(f'{time}', True, pygame.Color('cyan')), (WIDTH - 230, 130))
score_surface.blit(text_font.render('score:', True, pygame.Color('forestgreen'), True), (WIDTH - 230, 350))
score_surface.blit(font.render(f'{score}', True, pygame.Color('forestgreen')), (WIDTH - 230, 430))
score_surface.blit(text_font.render('record:', True, pygame.Color('magenta'), True), (WIDTH - 250, 620))
score_surface.blit(font.render(f'{record}', True, pygame.Color('magenta')), (WIDTH - 230, 700))
# print(clock.get_fps())
pygame.display.flip()
clock.tick(FPS)