-
Notifications
You must be signed in to change notification settings - Fork 10
/
gluttonous_snake.py
190 lines (140 loc) · 5.41 KB
/
gluttonous_snake.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
# -*- coding: utf-8 -*-
"""
Created on Wed May 16 15:22:20 2018
@author: zou
"""
import pygame, time
from pygame.locals import QUIT
from pygame.locals import KEYDOWN, K_RIGHT, K_LEFT, K_UP, K_DOWN, K_ESCAPE
from game import Game
from search_ai import get_move as search_move
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
green = pygame.Color(0, 200, 0)
bright_green = pygame.Color(0, 255, 0)
red = pygame.Color(200, 0, 0)
bright_red = pygame.Color(255, 0, 0)
blue = pygame.Color(32, 178, 170)
bright_blue = pygame.Color(32, 200, 200)
yellow = pygame.Color(255, 205, 0)
bright_yellow = pygame.Color(255, 255, 0)
game = Game()
rect_len = game.settings.rect_len
snake = game.snake
pygame.init()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((game.settings.width*15, game.settings.height*15))
pygame.display.set_caption('Gluttonous')
crash_sound = pygame.mixer.Sound('./sound/crash.wav')
def text_objects(text, font, color = black):
text_surface = font.render(text, True, color)
return text_surface, text_surface.get_rect()
def message_display(text, x, y, color = black):
large_text = pygame.font.SysFont('comicsansms', 50)
text_surf, text_rect = text_objects(text, large_text, color)
text_rect.center = (x, y)
screen.blit(text_surf, text_rect)
pygame.display.update()
def button(msg, x, y, w, h, inactive_color, active_color, action = None, parameter = None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, active_color, (x, y, w, h))
if click[0] == 1 and action != None:
if parameter != None:
action(parameter)
else:
action()
else:
pygame.draw.rect(screen, inactive_color, (x, y, w, h))
smallText = pygame.font.SysFont('comicsansms', 20)
TextSurf, TextRect = text_objects(msg, smallText)
TextRect.center = (x + (w / 2), y + (h / 2))
screen.blit(TextSurf, TextRect)
def quitgame():
pygame.quit()
quit()
def crash():
pygame.mixer.Sound.play(crash_sound)
message_display('crashed', game.settings.width/2*15, game.settings.height/3*15, white)
time.sleep(1)
def initial_interface():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill(white)
message_display('Gluttonous', game.settings.width/2*15, game.settings.height/4*15)
button('Go!', 80, 210, 80, 40, green, bright_green, game_loop, 'human')
button('Quit', 270, 210, 80, 40, red, bright_red, quitgame)
button('AI 1', 80, 280, 80, 40, blue, bright_blue, game_loop, 'search_ai')
button('AI 2', 270, 280, 80, 40, yellow, bright_yellow, DQN)
pygame.display.update()
pygame.time.Clock().tick(15)
def game_loop(player, fps = 30):
game.restart_game()
while not game.game_end():
pygame.event.pump()
if player == 'search_ai':
move = search_move(game)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
else:
move = human_move()
fps = 10
game.do_move(move)
screen.fill(black)
game.snake.blit(rect_len, screen)
game.strawberry.blit(screen)
game.blit_score(white, screen)
pygame.display.flip()
fpsClock.tick(fps)
crash()
def human_move():
direction = snake.facing
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'):
direction = 'right'
if event.key == K_LEFT or event.key == ord('a'):
direction = 'left'
if event.key == K_UP or event.key == ord('w'):
direction = 'up'
if event.key == K_DOWN or event.key == ord('s'):
direction = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
move = game.direction_to_int(direction)
return move
def DQN():
import tensorflow as tf
from DQN import DeepQNetwork
import numpy as np
game.restart_game()
tf.reset_default_graph()
sess = tf.Session()
dqn = DeepQNetwork(sess, game)
game_state = game.current_state()
start_state = np.concatenate((game_state, game_state, game_state, game_state), axis=2)
s_t = start_state
while not game.game_end():
# choose an action epsilon greedily
_, action_index = dqn.choose_action(s_t)
move = action_index
game.do_move(move)
pygame.event.pump()
game_state = game.current_state()
s_t = np.append(game_state, s_t[:, :, :-2], axis=2)
screen.fill(black)
game.snake.blit(rect_len, screen)
game.strawberry.blit(screen)
game.blit_score(white, screen)
pygame.display.flip()
fpsClock.tick(15)
crash()
if __name__ == "__main__":
initial_interface()