-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacman.py
171 lines (148 loc) · 7.31 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
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600), 0)
font = pygame.font.SysFont("arial", 20, True, False)
AMARELO = (255, 255, 0)
PRETO = (0, 0, 0)
AZUL = (0, 0, 255)
VELOCIDADE = 1
class Cenario:
def __init__(self, tamanho, pac):
self.pacman = pac
self.pontos = 0
self.tamanho = tamanho
self.matriz = [
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2],
[2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2],
[2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2],
[2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2],
[2, 1, 1, 1, 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x = self.tamanho * 30
pontos_img = font.render("Score {}".format(self.pontos), True, AMARELO)
tela.blit(pontos_img, (pontos_x, 50))
def pintar_linha(self, tela, numero_linha, linha):
for numero_coluna, coluna in enumerate(linha):
x = numero_coluna * self.tamanho
y = numero_linha * self.tamanho
half = self.tamanho // 2
cor = PRETO
if coluna == 2:
cor = AZUL
pygame.draw.rect(tela, cor, (x, y, self.tamanho, self.tamanho), 0)
if coluna == 1:
pygame.draw.circle(tela, AMARELO, (x + half, y + half),
self.tamanho // 10, 0)
def pintar(self, tela):
for numero_linha, linha in enumerate(self.matriz):
self.pintar_linha(tela, numero_linha, linha)
self.pintar_pontos(tela)
def calcular_regras(self):
col = self.pacman.coluna_intencao
lin = self.pacman.linha_intencao
if 0 <= col < 28 and 0 <= lin < 29:
if self.matriz[lin][col] != 2:
self.pacman.aceitar_movimento()
if self.matriz[lin][col] == 1:
self.pontos += 1
self.matriz[lin][col] = 0
print(self.pontos)
class Pacman:
def __init__(self, tamanho):
self.coluna = 1
self.linha = 1
self.centro_x = 400
self.centro_y = 300
self.tamanho = tamanho
self.vel_x = 0
self.vel_y = 0
self.raio = self.tamanho // 2
self.coluna_intencao = self.coluna
self.linha_intencao = self.linha
def calcular_regras(self):
self.coluna_intencao = self.coluna + self.vel_x
self.linha_intencao = self.linha + self.vel_y
self.centro_x = int(self.coluna * self.tamanho + self.raio)
self.centro_y = int(self.linha * self.tamanho + self.raio)
def pintar(self, tela):
# Desenhar o corpo do Pacman
pygame.draw.circle(tela, AMARELO, (self.centro_x, self.centro_y), self.raio, 0)
# Desenho da boca do Pacman
canto_boca = (self.centro_x, self.centro_y)
labio_superior = (self.centro_x + self.raio, self.centro_y - self.raio)
labio_inferior = (self.centro_x + self.raio, self.centro_y)
pontos = [canto_boca, labio_superior, labio_inferior]
pygame.draw.polygon(tela, PRETO, pontos, 0)
# Olho do Pacman
olho_x = int(self.centro_x + self.raio / 3)
olho_y = int(self.centro_y - self.raio * 0.70)
olho_raio = int(self.raio / 10)
pygame.draw.circle(tela, PRETO, (olho_x, olho_y), olho_raio, 0)
def processar_eventos(self, eventos):
for e in eventos:
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_RIGHT:
self.vel_x = VELOCIDADE
elif e.key == pygame.K_LEFT:
self.vel_x = -VELOCIDADE
elif e.key == pygame.K_UP:
self.vel_y = -VELOCIDADE
elif e.key == pygame.K_DOWN:
self.vel_y = VELOCIDADE
elif e.type == pygame.KEYUP:
if e.key == pygame.K_RIGHT:
self.vel_x = 0
elif e.key == pygame.K_LEFT:
self.vel_x = 0
elif e.key == pygame.K_UP:
self.vel_y = 0
elif e.key == pygame.K_DOWN:
self.vel_y = 0
def aceitar_movimento(self):
self.linha = self.linha_intencao
self.coluna = self.coluna_intencao
if __name__ == "__main__":
size = 600 // 30
pacman = Pacman(size)
cenario = Cenario(size, pacman)
while True:
# Calcular as regras
pacman.calcular_regras()
cenario.calcular_regras()
# Pintar a tela
screen.fill(PRETO)
cenario.pintar(screen)
pacman.pintar(screen)
pygame.display.update()
pygame.time.delay(100)
# Captura os eventos
eventos = pygame.event.get()
for e in eventos:
if e.type == pygame.QUIT:
exit()
pacman.processar_eventos(eventos)