-
Notifications
You must be signed in to change notification settings - Fork 3
/
GameGUI.py
295 lines (225 loc) · 10 KB
/
GameGUI.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import pygame
from Constants import *
from Menu import *
from GameController import GameController
from GA import *
import sys
class GameGUI:
def __init__(self):
pygame.init()
self.clock = pygame.time.Clock()
self.SCREEN_UPDATE = pygame.USEREVENT
self.speed = 110
self.speed_up = 80
pygame.time.set_timer(self.SCREEN_UPDATE, self.speed)
self.controller = GameController()
self.running, self.playing = True, False
self.UPKEY, self.DOWNKEY, self.START, self.BACK = False, False, False, False
self.SIZE = CELL_SIZE * NO_OF_CELLS
self.display = pygame.Surface((self.SIZE, self.SIZE))
self.window = pygame.display.set_mode((self.SIZE, self.SIZE))
self.font_name = 'SquareAntiqua-Bold.ttf'
self.main_menu = MainMenu(self)
self.GA = GAMenu(self, self.controller)
self.curr_menu = self.main_menu
self.load_model = False
self.view_path = False
def game_loop(self):
while self.playing:
self.event_handler()
if self.BACK:
self.playing = False
self.display.fill(WINDOW_COLOR)
if self.controller.algo != None:
self.draw_elements()
self.window.blit(self.display, (0, 0))
pygame.display.update()
self.clock.tick(60)
self.reset_keys()
def draw_elements(self):
# draw banner and stats
self.draw_banner()
self.draw_game_stats()
if self.curr_menu.state != 'GA' or self.controller.model_loaded: # Path Ai or trained GA
fruit = self.controller.get_fruit_pos()
snake = self.controller.snake
self.draw_fruit(fruit)
self.draw_snake(snake)
self.draw_score()
if not self.controller.model_loaded:
self.draw_path() # only path Ai has a path
else: # training a GA model
self.draw_all_snakes_GA()
def draw_game_stats(self):
if self.curr_menu.state != 'GA': # path Ai algo
instruction = 'Space to view Ai path, W to speed up, Q to go back'
elif self.controller.model_loaded: # trained model
instruction = 'W to speed up, Q to go back'
else: # training model GA algo
instruction = 'Space to hide all snakes, W to speed up, Q to go back'
curr_gen = str(self.controller.curr_gen())
best_score = str(self.controller.best_GA_score())
stats_gen = f'Generation: {curr_gen}/{GA.generation}'
stats_score = f'Best score: {best_score}'
stats_hidden_node = f'Hidden nodes {Population.hidden_node}'
# draw stats
self.draw_text(
stats_gen, size=20,
x=3*CELL_SIZE, y=CELL_SIZE - 10,
)
self.draw_text(
stats_score, size=20,
x=3*CELL_SIZE, y=CELL_SIZE + 20,
)
self.draw_text(
stats_hidden_node, size=20,
x=self.SIZE / 2, y=CELL_SIZE - 30,
color=SNAKE_COLOR
)
# instruction
self.draw_text(
instruction, size=20,
x=self.SIZE/2, y=(CELL_SIZE * NO_OF_CELLS) - NO_OF_CELLS,
color=WHITE
)
# current Algo Title
self.draw_text(
self.curr_menu.state, size=30,
x=self.SIZE/2, y=CELL_SIZE,
)
def draw_all_snakes_GA(self):
if not self.view_path: # have all snakes visible by default
for snake in self.controller.snakes: # for each snake in list
self.draw_snake(snake)
# fruit of each snake
self.draw_fruit(snake.get_fruit())
def draw_path(self):
if self.controller.algo != None and self.view_path:
for path in self.controller.algo.path: # for each {x,y} in path
x = int(path.x * CELL_SIZE)
y = int(path.y * CELL_SIZE)
path_rect = pygame.Rect(x, y, CELL_SIZE, CELL_SIZE)
shape_surf = pygame.Surface(path_rect.size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, PATHCOLOR, shape_surf.get_rect())
pygame.draw.rect(self.display, BANNER_COLOR, path_rect, 1)
self.display.blit(shape_surf, path_rect)
def draw_snake_head(self, snake):
head = snake.body[0]
self.draw_rect(head, color=SNAKE_HEAD_COLOR)
def draw_snake_body(self, body):
self.draw_rect(body, color=SNAKE_COLOR, border=True)
def draw_rect(self, element, color, border=False):
x = int(element.x * CELL_SIZE)
y = int(element.y * CELL_SIZE)
body_rect = pygame.Rect(x, y, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(self.display, color, body_rect)
if border:
pygame.draw.rect(self.display, WINDOW_COLOR, body_rect, 3)
def draw_snake(self, snake):
self.draw_snake_head(snake) # draw head
for body in snake.body[1:]:
self.draw_snake_body(body) # draw body
def draw_fruit(self, fruit):
x = int(fruit.x * CELL_SIZE)
y = int(fruit.y * CELL_SIZE)
fruit_rect = pygame.Rect(x, y, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(self.display, FRUIT_COLOR, fruit_rect)
def draw_banner(self):
banner = pygame.Rect(0, 0, self.SIZE, BANNER_HEIGHT * CELL_SIZE)
pygame.draw.rect(self.display, BANNER_COLOR, banner)
def draw_score(self):
score_text = 'Score: ' + str(self.controller.get_score())
score_x = self.SIZE - (CELL_SIZE + 2*len(score_text))
score_y = CELL_SIZE
self.draw_text(score_text, 20, score_x, score_y, WINDOW_COLOR)
def game_over(self):
again = False
while not again:
for event in pygame.event.get():
if self.is_quit(event):
again = True
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
again = True
break
if event.key == pygame.K_s:
again = True
self.controller.save_model()
break
self.display.fill(MENU_COLOR)
# training model results
if self.curr_menu.state == 'GA' and self.controller.model_loaded == False:
best_score = self.controller.best_GA_score()
best_gen = self.controller.best_GA_gen()
high_score = f'Best snake Score: {best_score} in generation {best_gen}'
save = 'Press S to save best snake'
self.draw_text(
save, size=30,
x=self.SIZE/2, y=self.SIZE/2 + 3*CELL_SIZE,
color=FRUIT_COLOR
)
else:
# Path ai or trained model results
high_score = f'High Score: {self.controller.get_score()}'
to_continue = 'Enter to Continue'
self.draw_text(
high_score, size=35,
x=self.SIZE/2, y=self.SIZE/2,
)
self.draw_text(
to_continue, size=30,
x=self.SIZE/2, y=self.SIZE/2 + 2*CELL_SIZE,
color=WHITE
)
self.window.blit(self.display, (0, 0))
pygame.display.update()
self.controller.reset()
def is_quit(self, event):
# user presses exit icon
if event.type == pygame.QUIT:
self.running, self.playing = False, False
self.curr_menu.run_display = False
return True
return False
def event_handler(self):
for event in pygame.event.get():
if self.is_quit(event):
print('Bye :)')
pygame.quit()
sys.exit()
# user event that runs every self.speed milisec
elif self.playing and event.type == pygame.USEREVENT:
if self.load_model: # user load model
self.controller.load_model()
self.load_model = False
self.controller.ai_play(self.curr_menu.state) # play
if self.controller.end == True: # Only path ai and trained model
self.playing = False
self.game_over() # show game over stats
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN: # on Enter
self.START = True
self.view_path = False
elif event.key == pygame.K_q: # on q return
self.BACK = True
self.controller.reset()
elif event.key == pygame.K_SPACE: # space view path or hide training snakes
self.view_path = not self.view_path
elif event.key == pygame.K_DOWN:
self.DOWNKEY = True
elif event.key == pygame.K_UP:
self.UPKEY = True
elif event.key == pygame.K_w: # speed up/down by self.speed_up
self.speed_up = -1 * self.speed_up
self.speed = self.speed + self.speed_up
pygame.time.set_timer(self.SCREEN_UPDATE, self.speed)
def reset_keys(self):
self.UPKEY, self.DOWNKEY, self.START, self.BACK = False, False, False, False
def draw_text(self, text, size, x, y, color=WINDOW_COLOR):
font = pygame.font.Font(self.font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
self.display.blit(text_surface, text_rect)