-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
executable file
·242 lines (179 loc) · 5.12 KB
/
Main.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
import pygame
import numpy as np
import Birds_Brain as brain
import pickle
import Neural_Net as nn
import sys
import Matrixs
#Globals
screen_width = 800
screen_height = 600
tick_length = 100
population = 100
counter = 1
gen = 0
pillars = []
birds = []
dead_birds = []
exit = False
pillar_speed = 6
RED = (255,0,0)
BLACK = (0,0,0)
WHITE = (255,255,255)
#Initialise pygame
pygame.init()
screen = pygame.display.set_mode([screen_width,screen_height])
pygame.display.set_caption('Flappy Bird')
clock = pygame.time.Clock()
###My Classes
#class for the bird
class _Bird:
def __init__(self):
self.x = screen_width/3
self.y = screen_height/2
self.velocity = 0
self.gravity = 1
self.neural_net = nn.Neural_Network(3,4,1)
self.colour = (np.random.randint(255),np.random.randint(255),np.random.randint(255))
def update(self):
self.velocity+=self.gravity
self.y+=self.velocity
def draw(self):
pygame.draw.ellipse(screen, self.colour, [self.x, self.y, 20, 20])
def jump(self):
self.velocity = -15
#The folliwing two methods are for the neural net
def decide(self):
data = self.find_pole()
data.append(int(self.y))
choice = self.neural_net.predict(data)[0]
if (choice > 0.5):
self.jump()
def find_pole(self):
for i in range(len(pillars)):
if (pillars[i].x>self.x):
x_dist = int(pillars[i].x-self.x)
y_dist = int((pillars[i].top+(pillars[i].gap/2)) - self.y)
return [(x_dist), (y_dist)]
def return_neural_network_weights(self):
return self.neural_net.return_weights()
def set_neural_network_weights(self, weights):
self.neural_net.change_weights(weights)
#Class to create pillars
class pillar:
def __init__(self):
self.top = np.random.randint(screen_height - ((screen_height/5)*3), screen_height - ((screen_height/5)))
self.gap = 100
self.bottom = screen_height - (self.top+self.gap)
self.width = 30
self.x = screen_width
self.move_rate = pillar_speed
self.passed = False
######NEED MORE ELEGANT SOLUTION FOR CHECKING TO MAKE SURE THE GAME DOESNT GO TOO SLOW
def move(self):
self.x -= self.move_rate
if(self.x < screen_width/3 and self.x+self.width > screen_width/3):
check_collision(self.top, self.gap)
if(self.passed == False and self.x < (screen_width/2)):
add_pillar()
self.passed = True
def draw(self):
pygame.draw.rect(screen, BLACK, [self.x,0, self.width, self.top])
pygame.draw.rect(screen, BLACK, [self.x, screen_height-self.bottom, self.width, self.bottom])
###My Methods
def check_collision(top, gap):
global birds, dead_birds
for index, bird in enumerate(birds):
if (bird.y < top or bird.y> top+gap):
dead_birds.append(birds.pop(index))
#updates generation
def make_new_generation():
#intitiate all the variables
global birds, dead_birds
fill_bird_array()
#this gives top %10 of previous population
num = int(population / 10)
good_birds = dead_birds[-num:]
best_bird = dead_birds[population-1]
for i in range(population-int(num/2)):
if (i< num):
weights = good_birds[i].return_neural_network_weights()
#mutates the first bunch of birds
elif(i<population/2):
weights = best_bird.return_neural_network_weights()
weights[0] = Matrixs.mutate_2D(weights[0])
weights[1] = Matrixs.mutate_1D(weights[1])
#combines random birds
else:
weights1 = best_bird.return_neural_network_weights()
weights2 = good_birds[np.random.randint(num)].return_neural_network_weights()
return_arr1 = Matrixs.add_arrays(weights1[0],weights2[0])
return_arr2 = Matrixs.add_arrays(weights1[1],weights2[1])
weights = [return_arr1, return_arr2]
birds[i].set_neural_network_weights(weights)
#Resets game to the start
def set_game():
global pillars, gen, counter, pillar_speed
print("Generation: ", gen, " Score: ", counter)
make_new_generation()
pillars = []
add_pillar()
pillar_speed = 7
counter = 0
gen += 1
if(not exit):
loop()
#fills bird array
def fill_bird_array():
global birds
for _ in range(population):
birds.append(_Bird())
#adds pillar
def add_pillar():
pillars.append(pillar())
#pygame loop
def loop():
global counter, exit, pillar_speed
end = False
while not end:
#gets inputs - for when a user is playing
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
end = True
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
birds[0].jump()
#this is to do some checks every once in a while
if (counter % 50 == 0):
#get rid of any pillars to the left so the list doesnt get massive
if (pillars[0].x<0):
del pillars[0]
#check if all the birds are dead
if (len(birds) == 0):
end = True
break
#Decide if the birds should jump
for i in birds:
i.decide()
##All the stuff here is the main loop for printing to the pygame window and moving objects
screen.fill(WHITE)
#update bird
for i in birds:
i.update()
i.draw()
#update pillars
for i in pillars:
i.move()
i.draw()
pygame.display.flip()
if (counter % 1000 == 0 ):
pillar_speed+=1
print("Num birds alive: ",len(birds))
counter += 1
clock.tick(tick_length)
set_game()
fill_bird_array()
add_pillar()
loop()