-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_copy.py
237 lines (190 loc) · 6.14 KB
/
env_copy.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
import sys
import multiprocessing
sys.path.append('.')
import pygame
import random
from time import sleep
from Population import build_population
from Player import Player
from NN import Brain
from Natural_Selection import new_population
from Mutation import mutation
pops = []
make_pops = build_population(pops)
pops = make_pops.make_pop()
n = len(pops)
players = []
height = 30
score = 0
dino_image = pygame.image.load(r'C:\Users\dino.png')
one = pygame.image.load(r'C:\Users\one_small.png')
two = pygame.image.load(r'C:\Users\one.png')
three = pygame.image.load(r'C:\Users\many.png')
one = pygame.transform.scale(one, (15, height))
two = pygame.transform.scale(two, (30, height))
three = pygame.transform.scale(three, (60, height))
class Walls:
def __init__(self, counter):
self.counter = counter
self.t = 0
def draw_rect(self, x):
self.t = x
# pygame.draw.rect(screen, red, (x, 400 - height, 15 * self.counter, height))
if self.counter == 1:
screen.blit(one, (x, 400 - height))
elif self.counter == 2:
screen.blit(two, (x, 400 - height))
else:
screen.blit(three, (x, 400 - height))
def get(self):
return self.t, self.t + (15 * self.counter)
def get_width(self):
return self.counter
def get_speed():
if score < 4000:
return 10
elif score < 8000:
return 12
elif score < 12000:
return 14
elif score < 20000:
return 16
else:
return 20
pygame.init()
screen = pygame.display.set_mode((600, 600))
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
x_coord = 600
best_score = 0
def get_the_fitness(popu):
running = True
score = 0
x_coord = random.randint(200, 600)
brr = x_coord
for i in range(n):
players.append(Player(0, 400, False, screen))
brain_player = []
for i in range(n):
brain_player.append((players[i], popu[i]))
mat = [-1, -1, -1, 0, 0, 1]
dead = 0
dead_players = [int(0) for u in range(n)]
tt = 0
while running:
score = score + 1
screen.fill((173, 216, 210))
if x_coord >=brr:
obstacle = Walls(random.randint(1, 4))
mat[1] = obstacle.get_width() * 15
mat[0] = x_coord - 50
mat[2] = get_speed()
#print(mat)
actions = []
for item in brain_player:
actions.append(item[1].get_action(mat))
#print(actions)
ground = pygame.draw.rect(screen, black, (0, 400, 600, 5))
idx = -1
#print("Players alive : " + str(n - dead))
for item in brain_player:
idx = idx + 1
if dead_players[idx] != 0:
continue
dino = item[0]
if dino.get_made_jump():
# pygame.draw.rect(screen, black, (50, 400 - height, 15, 30))
screen.blit(dino_image, (50, 400 - height))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
tt = 1
if actions[idx]:
if dino.get_made_jump():
dino.update_made_jump(dino.make_the_jump())
if not dino.get_made_jump():
dino.update_made_jump(dino.make_the_jump())
obstacle.draw_rect(x_coord)
pygame.display.update()
#sleep(0.003)
if x_coord > 0:
got = obstacle.get()
if dino.collided(got[0], got[1]):
dino.update_score(score)
#print("Players alive : " + str(n - 1 - dead))
dead_players[idx] = score
dead = dead + 1
sleep(0.005)
if dead == n:
running = False
x_coord = x_coord - get_speed()
if x_coord < -100:
brr = random.randint(200, 600)
x_coord = brr
# print(score)
if score > 20000:
break
print(score)
if tt == 1:
exit(0)
return [], False
# pygame.quit()
print("BEST SCORE ==> " + str(score))
if score > 20000:
print("Reached the goal")
found_the_best = True
print("ALL SCORES : ")
for i in range(len(dead_players)):
if dead_players[i] == 0:
dead_players[i] = 100000000
print(dead_players)
for i in range(n):
brain_player[i] = (dead_players[i], brain_player[i][0], brain_player[i][1])
for i in range(n):
for j in range(i + 1, n):
if brain_player[i][0] < brain_player[j][0]:
ddr1 = brain_player[i]
ddr2 = brain_player[j]
temp = ddr1
ddr1 = ddr2
ddr2 = temp
brain_player[i] = ddr1
brain_player[j] = ddr2
#print(brain_player)
# pygame.quit()
return brain_player, True
print("ALL SCORES : ")
print(dead_players)
for i in range(n):
brain_player[i] = (dead_players[i], brain_player[i][0], brain_player[i][1])
for i in range(n):
for j in range(i+1, n):
if brain_player[i][0] < brain_player[j][0]:
ddr1 = brain_player[i]
ddr2 = brain_player[j]
temp = ddr1
ddr1 = ddr2
ddr2 = temp
brain_player[i] = ddr1
brain_player[j] = ddr2
#print(brain_player)
return brain_player, False
found_the_best = False
mr = 0
while True:
mr, done = get_the_fitness(pops)
if done:
break
new_pops = new_population(mr)
mutated_pops = mutation(new_pops)
populated = build_population(mutated_pops)
pops = populated.make_pop()
#print(pops)
for i in range(len(mr)):
if mr[i][0] == 0:
mr[i] = (100000000, mr[i][1], mr[i][2])
print(mr)
pygame.quit()