-
Notifications
You must be signed in to change notification settings - Fork 3
/
Snake.py
87 lines (68 loc) · 2.34 KB
/
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
from pygame.math import Vector2
from Fruit import Fruit
from NN import NeuralNework
import pickle
class Snake:
def __init__(self, hidden=8):
self.body = [Vector2(5, 8), Vector2(4, 8), Vector2(3, 8)]
self.fruit = Fruit()
self.score = 0
self.fitness = 0
self.life_time = 0
self.steps = 0
self.hidden = hidden
self.network = NeuralNework(5, self.hidden, 3)
def save_model(self, network, name):
with open(name, "wb") as file:
pickle.dump(network, file)
def load_model(self, name):
with open(name, 'rb') as file:
self.network = pickle.load(file)
def reset(self):
self.body = [Vector2(5, 8), Vector2(4, 8), Vector2(3, 8)]
self.fruit.reset_seed()
self.score = 0
self.fitness = 0
self.steps = 0
self.network = NeuralNework(5, self.hidden, 3)
def get_x(self):
return self.body[0].x
def get_y(self):
return self.body[0].y
def get_fruit(self):
return self.fruit.position
def ate_fruit(self):
if self.fruit.position == self.body[0]:
self.score += 1
self.life_time -= 40
return True
return False
def create_fruit(self):
self.fruit.generate_fruit()
def move_ai(self, x, y):
self.life_time += 1
self.steps += 1
for i in range(len(self.body)-1, 0, -1):
self.body[i].x = self.body[i-1].x
self.body[i].y = self.body[i-1].y
self.body[0].x = x
self.body[0].y = y
def add_body_ai(self):
last_indx = len(self.body) - 1
tail = self.body[-1]
before_last = self.body[-2]
if tail.x == before_last.x:
if tail.y < before_last.y:
self.body.append(Vector2(tail.x, tail.y-1))
else:
self.body.append(Vector2(tail.x, tail.y+1))
elif tail.y == before_last.y:
if tail.x < before_last.x:
self.body.append(Vector2(tail.x - 1, tail.y))
else:
self.body.append(Vector2(tail.x + 1, tail.y))
def ate_body(self):
for body in self.body[1:]:
if self.body[0] == body:
return True
return False