-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
256 lines (197 loc) · 7.58 KB
/
game.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
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import random
from pygame.locals import *
class Labirinto():
def __init__(self, largura, altura):
self.largura = largura
self.altura = altura
def gera_labirinto(self):
# Inicializa o labirinto com paredes
labirinto = [[1] * self.largura for _ in range(self.altura)]
# Inicializa a matriz de visitados
visitados = [[False] * self.largura for _ in range(self.altura)]
# Escolhe uma célula inicial aleatória com coordenadas ímpares
x, y = random.randint(0, (self.largura - 3) // 2) * 2 + 1, random.randint(0, (self.altura - 3) // 2) * 2 + 1
# Função recursiva para realizar a busca em profundidade
def dfs(x, y):
# Marca a célula atual como visitada
visitados[y][x] = True
labirinto[y][x] = 0
# Embaralha as direções para tornar o labirinto mais aleatório
direcoes = [(0, 2), (2, 0), (0, -2), (-2, 0)]
random.shuffle(direcoes)
for dx, dy in direcoes:
nx, ny = x + dx, y + dy
if 0 < nx < self.largura - 1 and 0 < ny < self.altura - 1 and not visitados[ny][nx]:
labirinto[(y + ny) // 2][(x + nx) // 2] = 0
dfs(nx, ny)
dfs(x, y)
return labirinto
def desenha_labirinto(self, labirinto):
degrade = 1
for i in range(len(labirinto)):
for j in range(len(labirinto[i])):
if labirinto[i][j] == 1:
cor_labirinto = [0.5, 0.5, 0.8]
#indice_escolhido = random.randint(0,2)
#cor_labirinto[indice_escolhido] = degrade
cor_labirinto = tuple(cor_labirinto)
glColor3f(cor_labirinto[0], cor_labirinto[1], degrade)
glBegin(GL_QUADS)
glVertex2f(j, i)
glVertex2f(j + 1, i)
glVertex2f(j + 1, i + 1)
glVertex2f(j, i + 1)
glEnd()
degrade -= 0.0007
class Player():
vel_move = 0.03
dx, dy = 0, 0
def __init__(self, x, y, tamanho, vidas):
self.vidas = vidas
self.x = x
self.y = y
self.tamanho = tamanho
def desenha_vidas(self):
tamanho = 0.2
y = 0.2
x = 0.2
for _ in range(self.vidas):
glColor3f(1, 0, 0)
glBegin(GL_QUADS)
glVertex2f(x, y)
glVertex2f(x + tamanho, y)
glVertex2f(x + tamanho, y + tamanho)
glVertex2f(x, y + tamanho)
glEnd()
x += 0.5
def desenha_player(self):
glColor3f(0, 0, 0)
glBegin(GL_QUADS)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.tamanho, self.y)
glVertex2f(self.x + self.tamanho, self.y + self.tamanho)
glVertex2f(self.x, self.y + self.tamanho)
glEnd()
def colisao(self, x, y, tamanho,labirinto):
for i in range(int(y), int(y + tamanho + 1)):
for j in range(int(x), int(x + tamanho + 1)):
if labirinto[i][j] == 1:
return True
def colide_inimigo(self, x_inimigo, y_inimigo):
if round(self.x, 0) == round(x_inimigo, 0) and round(self.y, 0) == round(y_inimigo, 0):
return True
else:
return False
def move(self , keys, labirinto):
if keys[K_LEFT]:
self.dx = -self.vel_move
if keys[K_RIGHT]:
self.dx = self.vel_move
if keys[K_UP]:
self.dy = -self.vel_move
if keys[K_DOWN]:
self.dy = self.vel_move
if not self.colisao(self.x + self.dx, self.y, self.tamanho, labirinto):
self.x += self.dx
if not self.colisao(self.x, self.y + self.dy, self.tamanho, labirinto):
self.y += self.dy
class Inimigo():
vel_move = 0.02
tamanho = 0.4
dx = random.uniform(-vel_move, vel_move)
dy = random.uniform(-vel_move, vel_move)
def __init__(self, x, y):
self.padrao_move = random.randint(1,4)
self.x = x
self.y = y
def desenha_player(self):
glColor3f(1, 0, 0)
glBegin(GL_QUADS)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.tamanho, self.y)
glVertex2f(self.x + self.tamanho, self.y + self.tamanho)
glVertex2f(self.x, self.y + self.tamanho)
glEnd()
def colisao(self, x, y, tamanho,labirinto):
for i in range(int(y), int(y + tamanho + 1)):
for j in range(int(x), int(x + tamanho + 1)):
if labirinto[i][j] == 1:
return True
return False
def move(self , player_x, player_y,labirinto):
#pesquisar sistema de movimentação automática
if self.padrao_move == 1:
self.vel_move = 0.005
# persegue o player
if player_x > self.x:
self.dx = self.vel_move
if player_x < self.x:
self.dx = -self.vel_move
if player_y > self.y:
self.dy = self.vel_move
if player_y < self.y:
self.dy = -self.vel_move
else:
# Colide e troca a direção
if self.colisao(self.x + self.dx, self.y, self.tamanho, labirinto):
self.dx *= -1
if self.colisao(self.x, self.y + self.dy, self.tamanho, labirinto):
self.dy *= -1
if not self.colisao(self.x + self.dx, self.y, self.tamanho, labirinto):
self.x += self.dx
if not self.colisao(self.x, self.y + self.dy, self.tamanho, labirinto):
self.y += self.dy
class Portal():
contador_animacao = 25
pulsacao = 0.005
tamanho = 0.9
liberado = False
def __init__(self, labirinto):
self.x = len(labirinto) - 2
self.y = len(labirinto) - 2
#self.x = 1
#self.y = 2
def desenha_portal(self):
if self.liberado:
glColor3f(0.0, 1, 0.0)
else:
glColor3f(0.7, 0.7, 0.1)
glBegin(GL_QUADS)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.tamanho, self.y)
glVertex2f(self.x + self.tamanho, self.y + self.tamanho)
glVertex2f(self.x, self.y + self.tamanho)
glEnd()
def animacao(self):
if self.liberado:
self.contador_animacao -=1
self.tamanho += self.pulsacao
if self.contador_animacao <= 0:
self.contador_animacao = 25
self.pulsacao *= -1
def colide_player(self, x_player, y_player):
if round(self.x, 0) == round(x_player, 0) and round(self.y, 0) == round(y_player, 0):
if self.liberado:
return True
else: return False
class Chave():
tamanho = 0.2
def __init__(self, labirinto):
self.x = 1.5
self.y = len(labirinto) - 1.5
def desenha_chave(self):
glColor3f(0.8, 0.6, 0.2)
glBegin(GL_QUADS)
glVertex2f(self.x, self.y)
glVertex2f(self.x + self.tamanho, self.y)
glVertex2f(self.x + self.tamanho, self.y + self.tamanho)
glVertex2f(self.x, self.y + self.tamanho)
glEnd()
def colide_player(self, x_player, y_player):
if round(self.x, 0) == round(x_player, 0) and round(self.y, 0) == round(y_player, 0):
return True
else:
return False