forked from maurock/snake-ga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakeClass.py
231 lines (184 loc) · 7.97 KB
/
snakeClass.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
import pygame
from random import randint
from DQN import DQNAgent
import numpy as np
from keras.utils import to_categorical
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Set options to activate or deactivate the game view, and its speed
display_option = False
speed = 0
pygame.font.init()
class Game:
def __init__(self, game_width, game_height):
pygame.display.set_caption('SnakeGen')
self.game_width = game_width
self.game_height = game_height
self.gameDisplay = pygame.display.set_mode((game_width, game_height+60))
self.bg = pygame.image.load("img/background.png")
self.crash = False
self.player = Player(self)
self.food = Food()
self.score = 0
class Player(object):
def __init__(self, game):
x = 0.45 * game.game_width
y = 0.5 * game.game_height
self.x = x - x % 20
self.y = y - y % 20
self.position = []
self.position.append([self.x, self.y])
self.food = 1
self.eaten = False
self.image = pygame.image.load('img/snakeBody.png')
self.x_change = 20
self.y_change = 0
def update_position(self, x, y):
if self.position[-1][0] != x or self.position[-1][1] != y:
if self.food > 1:
for i in range(0, self.food - 1):
self.position[i][0], self.position[i][1] = self.position[i + 1]
self.position[-1][0] = x
self.position[-1][1] = y
def do_move(self, move, x, y, game, food,agent):
move_array = [self.x_change, self.y_change]
if self.eaten:
self.position.append([self.x, self.y])
self.eaten = False
self.food = self.food + 1
if np.array_equal(move ,[1, 0, 0]):
move_array = self.x_change, self.y_change
elif np.array_equal(move,[0, 1, 0]) and self.y_change == 0: # right - going horizontal
move_array = [0, self.x_change]
elif np.array_equal(move,[0, 1, 0]) and self.x_change == 0: # right - going vertical
move_array = [-self.y_change, 0]
elif np.array_equal(move, [0, 0, 1]) and self.y_change == 0: # left - going horizontal
move_array = [0, -self.x_change]
elif np.array_equal(move,[0, 0, 1]) and self.x_change == 0: # left - going vertical
move_array = [self.y_change, 0]
self.x_change, self.y_change = move_array
self.x = x + self.x_change
self.y = y + self.y_change
if self.x < 20 or self.x > game.game_width-40 or self.y < 20 or self.y > game.game_height-40 or [self.x, self.y] in self.position:
game.crash = True
eat(self, food, game)
self.update_position(self.x, self.y)
def display_player(self, x, y, food, game):
self.position[-1][0] = x
self.position[-1][1] = y
if game.crash == False:
for i in range(food):
x_temp, y_temp = self.position[len(self.position) - 1 - i]
game.gameDisplay.blit(self.image, (x_temp, y_temp))
update_screen()
else:
pygame.time.wait(300)
class Food(object):
def __init__(self):
self.x_food = 240
self.y_food = 200
self.image = pygame.image.load('img/food2.png')
def food_coord(self, game, player):
x_rand = randint(20, game.game_width - 40)
self.x_food = x_rand - x_rand % 20
y_rand = randint(20, game.game_height - 40)
self.y_food = y_rand - y_rand % 20
if [self.x_food, self.y_food] not in player.position:
return self.x_food, self.y_food
else:
self.food_coord(game,player)
def display_food(self, x, y, game):
game.gameDisplay.blit(self.image, (x, y))
update_screen()
def eat(player, food, game):
if player.x == food.x_food and player.y == food.y_food:
food.food_coord(game, player)
player.eaten = True
game.score = game.score + 1
def get_record(score, record):
if score >= record:
return score
else:
return record
def display_ui(game, score, record):
myfont = pygame.font.SysFont('Segoe UI', 20)
myfont_bold = pygame.font.SysFont('Segoe UI', 20, True)
text_score = myfont.render('SCORE: ', True, (0, 0, 0))
text_score_number = myfont.render(str(score), True, (0, 0, 0))
text_highest = myfont.render('HIGHEST SCORE: ', True, (0, 0, 0))
text_highest_number = myfont_bold.render(str(record), True, (0, 0, 0))
game.gameDisplay.blit(text_score, (45, 440))
game.gameDisplay.blit(text_score_number, (120, 440))
game.gameDisplay.blit(text_highest, (190, 440))
game.gameDisplay.blit(text_highest_number, (350, 440))
game.gameDisplay.blit(game.bg, (10, 10))
def display(player, food, game, record):
game.gameDisplay.fill((255, 255, 255))
display_ui(game, game.score, record)
player.display_player(player.position[-1][0], player.position[-1][1], player.food, game)
food.display_food(food.x_food, food.y_food, game)
def update_screen():
pygame.display.update()
def initialize_game(player, game, food, agent):
state_init1 = agent.get_state(game, player, food) # [0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0]
action = [1, 0, 0]
player.do_move(action, player.x, player.y, game, food, agent)
state_init2 = agent.get_state(game, player, food)
reward1 = agent.set_reward(player, game.crash)
agent.remember(state_init1, action, reward1, state_init2, game.crash)
agent.replay_new(agent.memory)
def plot_seaborn(array_counter, array_score):
sns.set(color_codes=True)
ax = sns.regplot(np.array([array_counter])[0], np.array([array_score])[0], color="b", x_jitter=.1, line_kws={'color':'green'})
ax.set(xlabel='games', ylabel='score')
plt.show()
def run():
pygame.init()
agent = DQNAgent()
counter_games = 0
score_plot = []
counter_plot =[]
record = 0
while counter_games < 150:
# Initialize classes
game = Game(440, 440)
player1 = game.player
food1 = game.food
# Perform first move
initialize_game(player1, game, food1, agent)
if display_option:
display(player1, food1, game, record)
while not game.crash:
#agent.epsilon is set to give randomness to actions
agent.epsilon = 80 - counter_games
#get old state
state_old = agent.get_state(game, player1, food1)
#perform random actions based on agent.epsilon, or choose the action
if randint(0, 200) < agent.epsilon:
final_move = to_categorical(randint(0, 2), num_classes=3)
else:
# predict action based on the old state
prediction = agent.model.predict(state_old.reshape((1,11)))
final_move = to_categorical(np.argmax(prediction[0]), num_classes=3)
#perform new move and get new state
player1.do_move(final_move, player1.x, player1.y, game, food1, agent)
state_new = agent.get_state(game, player1, food1)
#set treward for the new state
reward = agent.set_reward(player1, game.crash)
#train short memory base on the new action and state
agent.train_short_memory(state_old, final_move, reward, state_new, game.crash)
# store the new data into a long term memory
agent.remember(state_old, final_move, reward, state_new, game.crash)
record = get_record(game.score, record)
if display_option:
display(player1, food1, game, record)
pygame.time.wait(speed)
agent.replay_new(agent.memory)
counter_games += 1
print('Game', counter_games, ' Score:', game.score)
score_plot.append(game.score)
counter_plot.append(counter_games)
agent.model.save_weights('weights.hdf5')
plot_seaborn(counter_plot, score_plot)
run()