-
Notifications
You must be signed in to change notification settings - Fork 0
/
jogo.py
229 lines (197 loc) · 8.88 KB
/
jogo.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
import pygame
import random
from enum import Enum
from collections import namedtuple
import numpy as np #pacote NumPy para cálculos numéricos
pygame.init()
font = pygame.font.Font('games-italic.ttf',25)
image = pygame.image.load('bg2.jpg')
#font = pygame.font.SysFont('arial', 25)
class Direction(Enum):
RIGHT = 1
LEFT = 2
UP = 3
DOWN = 4
Point = namedtuple('Point', 'x, y')
random_color=list(np.random.choice(range(255),size=3))
random_color2=list(np.random.choice(range(255),size=3))
random_color3=list(np.random.choice(range(255),size=3))
# rgb colors
PRETO = (0,0,0)
BRANCO = (255,255,255)
COR_COMIDA = (random_color)
COR_QUAD = (random_color2)
COR_STROKE = (random_color3)
DIMENSAO_QUAD = 20
SPEED = 50
class JogoCobraIA:
def __init__(self):
self.display = pygame.display.set_mode((480,480))
pygame.display.set_caption('Trabalho de IA- Jogo da cobra com IA')
self.clock = pygame.time.Clock()
self.reset()
def reset(self):
#ordena reinicializacao cabeca, corpo, ponto e comida
self.direction = Direction.RIGHT
self.cabeca = Point(480/2, 480/2)
self.cobra = [self.cabeca,
Point(self.cabeca.x-DIMENSAO_QUAD, self.cabeca.y),
Point(self.cabeca.x-(2*DIMENSAO_QUAD), self.cabeca.y)]
self.pontuacao = 0
self.comida = None
self._lugar_comida()
self.frame_iteration = 0
def _lugar_comida(self):
#local da comida e sua modificacao
x = random.randint(0, (480-DIMENSAO_QUAD )//DIMENSAO_QUAD )*DIMENSAO_QUAD
y = random.randint(0, (480-DIMENSAO_QUAD )//DIMENSAO_QUAD )*DIMENSAO_QUAD
self.comida = Point(x, y)
if self.comida in self.cobra:
self._lugar_comida()
def play_step(self, acao):
self.frame_iteration += 1
#1. ver entrada do jogo
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#2. movimento
self._move(acao) # atualiza cabeca
self.cobra.insert(0, self.cabeca)
# 3 verifica se o jogo acabou
recompensa = 0 #valor de recompensa
fim_de_jogo = False
if self._ocorre_colisao() or self.frame_iteration > 100*len(self.cobra):
fim_de_jogo = True
recompensa = -10 #valor de recompensa
return recompensa, fim_de_jogo, self.pontuacao
# 4.adc novos alimentos ou movendo
if self.cabeca == self.comida:
self.pontuacao += 1
recompensa = 10
self._lugar_comida()
pygame.mixer.music.load('rapai.wav')
pygame.mixer.music.play()
else:
self.cobra.pop()
# 5. atualiza interface
self._update_ui()
self.clock.tick(60)
# 6. retorna game over e pontuacao
return recompensa, fim_de_jogo, self.pontuacao
def _ocorre_colisao(self, pt=None):
if pt is None:
pt = self.cabeca
# atinge o limite
if pt.x > 480 - DIMENSAO_QUAD or pt.x < 0 or pt.y > 480 - DIMENSAO_QUAD or pt.y < 0:
pygame.mixer.music.load('Tome.wav')
pygame.mixer.music.play()
return True
# atinge a si mesma
if pt in self.cobra[1:]:
pygame.mixer.music.load('ui.wav')
pygame.mixer.music.play()
return True
return False
def _update_ui(self):
self.display.blit(image, (0,0))
for pt in self.cobra:
#Desenho do Corpo
pygame.draw.rect(self.display, PRETO, pygame.Rect(pt.x, pt.y, DIMENSAO_QUAD, DIMENSAO_QUAD))
pygame.draw.rect(self.display, COR_STROKE, pygame.Rect(pt.x+2, pt.y+2, 16, 16))
for pt in self.cabeca:
#Desenho da Cabeça
pygame.draw.rect(self.display, (PRETO), pygame.Rect(self.cabeca.x, self.cabeca.y, DIMENSAO_QUAD, DIMENSAO_QUAD))
pygame.draw.polygon(self.display,PRETO,
[(self.cabeca.x-5, self.cabeca.y+11),
(self.cabeca.x-5, self.cabeca.y+9),
(self.cabeca.x+8, self.cabeca.y-4),
(self.cabeca.x+10, self.cabeca.y-4),
(self.cabeca.x+23, self.cabeca.y+9),
(self.cabeca.x+23, self.cabeca.y+11),
(self.cabeca.x+10, self.cabeca.y+24),
(self.cabeca.x+8, self.cabeca.y+24)])
pygame.draw.polygon(self.display,COR_STROKE,
[(self.cabeca.x-2, self.cabeca.y+11),
(self.cabeca.x-2, self.cabeca.y+9),
(self.cabeca.x+8, self.cabeca.y-1),
(self.cabeca.x+10, self.cabeca.y-1),
(self.cabeca.x+20, self.cabeca.y+9),
(self.cabeca.x+20, self.cabeca.y+11),
(self.cabeca.x+10, self.cabeca.y+21),
(self.cabeca.x+8, self.cabeca.y+21)])
pygame.draw.rect(self.display, COR_STROKE, pygame.Rect(self.cabeca.x+2, self.cabeca.y+2, 16, 16))
pygame.draw.polygon(self.display,PRETO,
[(self.cabeca.x+3, self.cabeca.y+11),
(self.cabeca.x+3, self.cabeca.y+9),
(self.cabeca.x+8, self.cabeca.y+4),
(self.cabeca.x+10, self.cabeca.y+4),
(self.cabeca.x+15, self.cabeca.y+9),
(self.cabeca.x+15, self.cabeca.y+11),
(self.cabeca.x+10, self.cabeca.y+16),
(self.cabeca.x+8, self.cabeca.y+16)])
#Desenho da comida (Tema do Orgulho)
#pygame.draw.rect(self.display, (255,255,255), pygame.Rect(self.comida.x, self.comida.y, DIMENSAO_QUAD+1, DIMENSAO_QUAD+1))
pygame.draw.polygon(self.display,(255,0,0),
[(self.comida.x+1, self.comida.y+8),
(self.comida.x+2, self.comida.y+4),
(self.comida.x+4, self.comida.y+2),
(self.comida.x+5, self.comida.y+2),
(self.comida.x+6, self.comida.y+2),
(self.comida.x+10, self.comida.y+6),
(self.comida.x+14, self.comida.y+2),
(self.comida.x+15, self.comida.y+2),
(self.comida.x+16, self.comida.y+2),
(self.comida.x+18, self.comida.y+4),
(self.comida.x+19, self.comida.y+8),
(self.comida.x+10, self.comida.y+19)])
pygame.draw.polygon(self.display,(252,138,0,99),
[(self.comida.x+1, self.comida.y+8),
(self.comida.x+2, self.comida.y+6),
(self.comida.x+10, self.comida.y+6),
(self.comida.x+18, self.comida.y+6),
(self.comida.x+19, self.comida.y+8),
(self.comida.x+10, self.comida.y+19)])
pygame.draw.polygon(self.display,(255,241,48,100),
[(self.comida.x+1, self.comida.y+8),
(self.comida.x+19, self.comida.y+8),
(self.comida.x+10, self.comida.y+19)])
pygame.draw.polygon(self.display,(0,255,0,100),
[(self.comida.x+2, self.comida.y+10),
(self.comida.x+18, self.comida.y+10),
(self.comida.x+10, self.comida.y+19)])
pygame.draw.polygon(self.display,(0,2,255,100),
[(self.comida.x+4, self.comida.y+13),
(self.comida.x+16, self.comida.y+13),
(self.comida.x+10, self.comida.y+19)])
pygame.draw.polygon(self.display,(153,0,255,100),
[(self.comida.x+6, self.comida.y+15),
(self.comida.x+14, self.comida.y+15),
(self.comida.x+10, self.comida.y+19)])
text = font.render("Pontos: " + str(self.pontuacao), True, BRANCO)
self.display.blit(text, [0, 0])
pygame.display.flip()
def _move(self, acao):
#[Continuar reto, Virar a direita, Virar a esquerda]
clock_wise = [Direction.RIGHT, Direction.DOWN, Direction.LEFT, Direction.UP]
idx = clock_wise.index(self.direction) #A função index() retorna o índice da primeira ocorrência que encontra a partir do índice 0
if np.array_equal(acao, [1, 0, 0]): #A função numpy array_equal() retorna True se os arrays são iguais e False se os arrays não são iguais
new_dir = clock_wise[idx] #Sem mudança
elif np.array_equal(acao, [0, 1, 0]):
next_idx = (idx + 1) % 4
new_dir = clock_wise[next_idx] #Virar a direita -> Descer -> Virar a esquerda -> Subir
else: # [0, 0, 1]
next_idx = (idx - 1) % 4
new_dir = clock_wise[next_idx] #Virar a esquerda -> Subir -> Virar a direita -> Descer
self.direction = new_dir
x = self.cabeca.x
y = self.cabeca.y
if self.direction == Direction.RIGHT:
x += DIMENSAO_QUAD
elif self.direction == Direction.LEFT:
x -= DIMENSAO_QUAD
elif self.direction == Direction.DOWN:
y += DIMENSAO_QUAD
elif self.direction == Direction.UP:
y -= DIMENSAO_QUAD
self.cabeca = Point(x, y)