-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.py
206 lines (181 loc) · 7.08 KB
/
pacman.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
import pygame as pg
from timer import *
class Pacman:
def __init__(self, screen, graph, settings):
self.node = None
self.screen = screen
self.graph = graph
self.settings = settings
self.node = self.graph.nodes[len(self.graph.nodes) - 4]
self.target = self.node.neighbors["LEFT"]
self.x = self.node.x
self.y = self.node.y
self.move_speed = 0.5
self.direction = "LEFT"
self.target_direction = "LEFT"
self.onNode = True
self.radius = 20
self.spritesheet = Spritesheet("./assets/images/pacman-spritesheet.png")
self.images = {"LEFT": None, "RIGHT": None, "DOWN": None, "UP": None}
self.setImages()
self.deadImages = []
self.setDeadImages()
self.dead_timer = Timer(self.deadImages, is_loop=False)
self.image = self.left_timer.image()
self.dead = False
self.lives = 3
self.lives_x = 780
self.score_from_death = 0
self.index = 0
def setDeadImages(self):
self.deadImages = self.spritesheet.images_at(
[
(0, 193, 32, 32),
(33, 193, 32, 32),
(65, 193, 32, 32),
(97, 193, 32, 32),
(129, 193, 32, 32),
(161, 193, 32, 32),
(193, 193, 32, 32),
(225, 193, 32, 32),
(257, 193, 32, 32),
(289, 193, 32, 32),
(321, 193, 32, 32),
]
)
def setImages(self):
self.images["LEFT"] = self.spritesheet.images_at(
[(0, 0, 32, 32), (0, 33, 32, 32)], colorkey=(255, 255, 255)
)
self.images["RIGHT"] = self.spritesheet.images_at(
[(33, 0, 32, 32), (33, 33, 32, 32)], colorkey=(255, 255, 255)
)
self.images["DOWN"] = self.spritesheet.images_at(
[(65, 0, 32, 32), (65, 33, 32, 32)], colorkey=(255, 255, 255)
)
self.images["UP"] = self.spritesheet.images_at(
[(97, 0, 32, 32), (97, 33, 32, 32)], colorkey=(255, 255, 255)
)
self.left_timer = Timer(self.images["LEFT"])
self.right_timer = Timer(self.images["RIGHT"])
self.down_timer = Timer(self.images["DOWN"])
self.up_timer = Timer(self.images["UP"])
def move(self):
self.change_direction()
self.update_node()
self.teleport_node()
if self.direction == "UP" and self.node.neighbors["UP"] is not None:
self.y += self.move_speed * -1
self.x = self.node.x
self.target = self.node.neighbors["UP"]
if self.direction == "DOWN" and self.node.neighbors["DOWN"] is not None:
self.y += self.move_speed * 1
self.x = self.node.x
self.target = self.node.neighbors["DOWN"]
if self.direction == "LEFT" and self.node.neighbors["LEFT"] is not None:
self.x += self.move_speed * -1
self.y = self.node.y
self.target = self.node.neighbors["LEFT"]
if self.direction == "RIGHT" and self.node.neighbors["RIGHT"] is not None:
self.x += self.move_speed * 1
self.y = self.node.y
self.target = self.node.neighbors["RIGHT"]
# print(self.x, ", ", self.y)
def update_node(self):
for node in self.graph.nodes:
if node.circle.collidepoint(self.x, self.y):
self.node = node
self.onNode = True
break
else:
self.onNode = False
def change_direction(self):
if self.onNode:
self.direction = self.target_direction
# allows you to turn around
if self.target and self.target.neighbors[self.target_direction] == self.node:
self.direction = self.target_direction
temp = self.node
self.node = self.target
self.target = temp
def teleport_node(self):
if self.x < 1 and self.direction == "LEFT":
self.x = self.graph.node_right.x
self.node = self.graph.node_right
if self.x > 845 and self.direction == "RIGHT":
self.x = self.graph.node_left.x
self.node = self.graph.node_left
def draw(self):
if self.dead:
self.image = self.dead_timer.image()
self.direction = "STOP"
if self.dead_timer.is_expired():
self.dead_timer.index = 0
self.reset()
elif self.direction == "LEFT":
self.image = self.left_timer.image()
elif self.direction == "RIGHT":
self.image = self.right_timer.image()
elif self.direction == "DOWN":
self.image = self.down_timer.image()
elif self.direction == "UP":
self.image = self.up_timer.image()
rect = self.image.get_rect()
rect.left = self.x - 16
rect.top = self.y - 16
self.screen.blit(self.image, rect)
# pg.draw.circle(self.screen, (255, 255, 0), (self.x, self.y), self.radius)
def drawLives(self, framerate):
if self.lives == 0:
self.settings.game.game_over = True
self.settings.game.playable = False
if self.settings.game.game_over_frame == 0:
self.settings.game.game_over_frame = framerate
if self.settings.game.game_over_frame + 800 == framerate:
self.settings.game.cur_menu = "menu"
self.settings.game.fullReset()
if self.lives > 0:
image = self.images["LEFT"][0]
rect = image.get_rect()
rect.left = self.lives_x
rect.top = 947
self.screen.blit(image, rect)
if self.lives > 1:
image2 = self.images["LEFT"][0]
rect2 = image2.get_rect()
rect2.left = self.lives_x - 32
rect2.top = 947
self.screen.blit(image2, rect2)
if self.lives > 2:
image3 = self.images["LEFT"][0]
rect3 = image3.get_rect()
rect3.left = self.lives_x - 64
rect3.top = 947
self.screen.blit(image3, rect3)
def reset(self):
if self.lives > 0:
self.lives -= 1
self.node = self.graph.nodes[len(self.graph.nodes) - 4]
self.target = None
self.x = self.node.x
self.y = self.node.y
self.direction = "STOP"
self.target_direction = "STOP"
self.onNode = True
self.image = self.left_timer.image()
self.dead = False
self.score_from_death = 0
self.settings.fruit.spawn = False
for ghost in self.settings.ghosts:
ghost.reset()
if self.lives == 0:
self.node = self.graph.nodes[len(self.graph.nodes) - 4]
self.target = None
self.x = self.node.x
self.y = self.node.y
self.direction = "STOP"
self.target_direction = "STOP"
self.onNode = True
self.image = self.left_timer.image()
self.dead = False
self.score_from_death = 0