-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSnake.py
More file actions
103 lines (77 loc) · 2.56 KB
/
Snake.py
File metadata and controls
103 lines (77 loc) · 2.56 KB
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
__author__ = 'Aaron'
# IMPORTS
import sys
from States import *
from constants import *
from NeuralNet import *
# GLOBAL VARIABLES
clock = pygame.time.Clock()
ann = NeuralNet(NUM_INPUTS, NUM_OUTPUTS, NUM_HIDDEN, NUM_PER_HIDDEN)
# STATE MANAGER
class StateManager(object):
def __init__(self, ann=None):
"""
Initializes the state manager.
Contains "global" variables to hold neural network and score.
"""
self.ann = ann
self.fitness = 0
self.state = None
self.go_to(MenuState())
def go_to(self, state):
self.state = state
self.state.manager = self
# GAME ENGINE
def main():
pygame.init()
pygame.display.set_caption("Snake")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.fill((0, 0, 0))
running = True
manager = StateManager()
while running:
clock.tick(FRAMES_PER_SEC)
if pygame.event.get(QUIT):
sys.exit(0)
manager.state.handle_events(pygame.event.get())
manager.state.update()
manager.state.render(screen)
pygame.display.flip()
# FITNESS FUNCTION
def fitness(weights, headless=1):
"""
Calculate the fitness function.
:param weights: weights representing the ANN.
:return: score of the ANN represented
"""
ann.set_weights(weights)
pygame.init()
pygame.display.set_caption("Snake")
screen = None
if headless == 0:
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.fill((0, 0, 0))
manager = StateManager(ann)
manager.go_to(PlayState())
while not isinstance(manager.state, GameOverState):
if headless == 0:
clock.tick(FRAMES_PER_SEC)
manager.state.update()
if headless == 0:
manager.state.render(screen)
pygame.display.flip()
return manager.fitness
# PROGRAM EXECUTION
if __name__ == '__main__':
main()
"""
# Test ANN produced from GA
best_weights = (
[10.824817911934183, -9.616334653568593, 12.20692015452115, 0.8497712451855408, 12.029237839146205,
8.716560320403946, -16.61271494421796, -9.100878080518749, -4.320295261874485, 11.604731894471593,
-10.79054381793563, -0.142867055094126, 7.9249036931440875, 4.897635836580965, -15.495511421773331,
-0.9093391897555412, 6.365796169512784, 2.1237275249065832, -12.808331156470727, 17.826473388844157,
3.386068137019515, 4.210746286012914, -6.650227121028252, -7.208587155917651])
while 1:
fitness(best_weights, 0)
"""