-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
300 lines (248 loc) · 10.7 KB
/
test.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import random
import neat.nn
import pygame
from pygame import mixer
import os
from sys import exit
from random import randint
from classes import Floor, Wall, Player
from constants import *
import neat
import numpy as np
pygame.font.init()
pygame.init()
def reset(screen,floor_image):
global floor_y, floor_yy, FLOOR_VELOCITY, floor_indices, floor_group, START_TIME, FALLING, GAME_ACTIVE
START_TIME = pygame.time.get_ticks() // 1000
FALLING = False
floor_indices = list()
floor_group = []
floor_y = WIN_HEIGHT - 150
floor_yy = WIN_HEIGHT - 150
FLOOR_VELOCITY = 0
bottom_floor = Floor(0, WIN_HEIGHT - 50, WIN_WIDTH)
floor_group.append([0, bottom_floor])
for i in range(MAX_FLOORS):
f_w = randint(180, 370)
f_x = randint(80, WIN_WIDTH - f_w - 80)
floor = Floor(f_x, floor_yy, f_w)
floor_group.append([i+1,floor])
floor_yy -= 100
# GAME_ACTIVE = menu["PlayGame"]
def draw_floor(floor_image, floor_group,screen):
for floor_info in floor_group:
floor = floor_info[1]
floor.rect.x = floor.get_x()
# floor.rect.y = floor.get_y()
floor.rect.y += FLOOR_VELOCITY
floor.rectsolid.y += FLOOR_VELOCITY
while floor.rect.x < (floor.get_x() + floor.get_width()):
screen.blit(floor.image, floor.rect)
floor.rect.x += floor_image.get_width()
def handle_move(player, output, objects):
decision = output
# decision = output.index(max(output))
# print(decision)decision
indices = [i for i,x in enumerate(output) if x == max(output)]
output_inx = random.choice(indices)
# # keys = pygame.key.get_pressed()
for i in range(len(decision)):
if i == output_inx:
decision[i] = 1
else:
decision[i] = 0
# player.x_vel = 0 # If we don't do it player will
collide_right = False
collide_left = False
if (player.rect.left <= 51):
collide_left = True
player.rect.left = 51
elif(player.rect.right >= WIN_WIDTH-51):
collide_right = True
player.rect.right = WIN_WIDTH - 51
if decision[0] and not collide_left:
player.move_left(PLAYER_VEL)
elif decision[1] and not collide_right:
player.move_right(PLAYER_VEL)
# if decision[2] and player.on_floor:
elif decision[2] and player.on_floor:
player.current_floor = player.rect.bottom
player.jump()
# Handling horizontal collision
def collide(player, objects, dx):
player.move(dx, 0)
player.update()
collided_object = None
for obj in objects:
if pygame.sprite.collide_mask(player, obj):
collided_object = obj
break
player.move(-dx, 0)
player.update()
return collided_object
def print_text(text, font, screen, pos, color=(202, 241, 222)):
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect(center=pos)
screen.blit(text_surface, text_rect)
def update_floor(floor, player):
if floor[1].rect.bottom > WIN_HEIGHT + 200:
# print("Me izz here")
# removed_floor += 1
floor_group.remove(floor)
if (floor_group[-1][0]+1) % 100 == 0:
floor_group.append([floor_group[-1][0]+1,Floor(0, floor_group[-1][1].rect.y - 100, WIN_WIDTH)])
else:
f_w = randint(180, 370)
f_x = randint(80, WIN_WIDTH - f_w - 80)
fr = Floor(f_x, floor_group[-1][1].rect.y - 100, f_w)
floor_group.append([floor_group[-1][0]+1,fr])
def check_collision_and_score(floor, player):
global FALLING, FLOOR_VELOCITY
if floor[1].rectsolid.colliderect(player.rect):
if player.rect.bottom < floor[1].rectsolid.centery:
if player.y_vel > 0:
player.landed()
player.rect.bottom = floor[1].rectsolid.top
player.y_vel = 0
player.current_floor_index = floor[0]
if player.current_floor_index > player.old_floor_index:
player.score_normal = 10*player.current_floor_index
# player_g.fitness += 10
if player.bonus_y == 4:
player.score_bonus += 5
# player_g.fitness += 5
elif player.bonus_y == 8:
player.score_bonus += 15
# player_g.fitness += 15
player.old_floor_index = player.current_floor_index
player.score = player.score_normal + player.score_bonus
if player.highest_floor_index < player.current_floor_index:
player.highest_floor_index = player.current_floor_index
if (player.rect.top < 100) and not FALLING and FLOOR_VELOCITY == 0:
FLOOR_VELOCITY += 1
# FALLING = True
if player.rect.right >= floor[1].rectsolid.right and player.x_vel != 0:
# player_g.fitness += 0.4
player.bonus_y = 4
if abs(floor[1].rectsolid.centerx - floor_group[floor_group.index(floor) + 1][1].rectsolid.centerx) > 300:
# player_g.fitness += 0.4
player.bonus_y += 4
elif player.rect.left <= floor[1].rectsolid.left and player.x_vel != 0:
# player_g.fitness += 0.4
player.bonus_y = 4
if abs(floor[1].rectsolid.centerx - floor_group[floor_group.index(floor) + 1][1].rectsolid.centerx) > 200:
# player_g.fitness += 0.4
player.bonus_y += 4
else:
player.bonus_y = 0
def draw(floor_image,bg, floor_grp, wallgrp, players, screen):
global scroll, SCROLLING
screen.blit(bg, (0, 0))
draw_floor(floor_image, floor_group, screen)
wallgrp.draw(screen)
# construct_floor(flr,floor_rect,0,WIN_WIDTH)
for player in players:
if player.rect.top <= OFFSET_Y:
if player.y_vel < 0:
player.vel_y = 0
player.rect.top = OFFSET_Y
scroll = -player.y_vel
# player.move(0,scroll)
else:
scroll = 0
for floor in floor_grp:
# print(floor_indices)
floor[1].scroll(scroll)
update_floor(floor, player)
check_collision_and_score(floor, player)
player.draw(screen)
def test_ai(winner_net,n):
global GAME_ACTIVE, START_TIME, FLOOR_VELOCITY, floor_yy, gen, floor_group, max_score,gen, floor_group
gen += 1
# Defining pygame screen to draw assets and component
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
# Setting up caption (window title)
pygame.display.set_caption("Icy Tower")
# Loading and setting up window icon
game_icon = pygame.image.load(os.path.join("icons", "icytowericon.png"))
pygame.display.set_icon(game_icon)
# Initialising pygame clock
clock = pygame.time.Clock()
# Loading background and floor image
background_image = pygame.image.load(os.path.join("sprites", 'background.jpg')).convert()
floor_image = pygame.image.load(os.path.join('sprites', 'icy2.png')).convert()
# Getting floor rectangle
floor_rect = floor_image.get_rect(topleft=(0, WIN_HEIGHT - floor_image.get_height()))
floor_group = []
floor_yy = WIN_HEIGHT - 150
# Creating Bottom Floor
bottom_floor = Floor(0, WIN_HEIGHT - 50, WIN_WIDTH)
floor_group.append([0, bottom_floor])
# Generating n (MAX_FLOORS) number of random floors
for i in range(MAX_FLOORS):
# if len(floor_group) < MAX_FLOORS+1 :
f_w = randint(180, 370) # Generating Random width
f_x = randint(80, WIN_WIDTH - f_w - 80)
# Generating Random x position
floor = Floor(f_x, floor_yy, f_w) # Creating a Floor object with these values
floor_group.append([i+1, floor]) # Adding all our floors to floor_group
floor_yy -= 100 # Decrementing y location for next floor
# Defining wallGroup (sprite group)
wallGroup = pygame.sprite.Group()
# Generating/Constructing Walls
for i in range(3):
wallR = Wall(804, i * 240, "right");
wallL = Wall(45, i * 240, "left");
wallGroup.add(wallR);
wallGroup.add(wallL);
# Loading Music/Sound Assets
mixer.music.load(music_dict["gameBG"]);
mixer.music.play(-1) # Playing main game theme
GAME_ACTIVE = True
# Getting time from pygame clock at the start of the game
START_TIME = pygame.time.get_ticks() // 1000
players = []
player = Player(floor_group[1][1].rectsolid.centerx, WIN_HEIGHT - floor_image.get_height() - PLAYER_Y, PLAYER_X, PLAYER_Y)
players.append(player)
while True and len(players)>0:
draw(floor_image, background_image, floor_group, wallGroup, players, screen)
player_height = []
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# break
if GAME_ACTIVE:
current_time = pygame.time.get_ticks() // 1000 - START_TIME
if current_time % 60 == 0 and current_time != 0 and FLOOR_VELOCITY < 4:
FLOOR_VELOCITY += 1
START_TIME += current_time
for x, player in enumerate(players):
player.loop(90)
l = 0
r = 0
floor_data = []
for i in range(len(floor_group)):
if floor_group[i][0] == player.current_floor_index :
for j in range(i,i+n//3-2):
if j == i :
l = player.rect.left - floor_group[j][1].rectsolid.right
r = player.rect.right - floor_group[j][1].rectsolid.left
else:
floor_data.append(floor_group[j][1].rectsolid.left)
floor_data.append(floor_group[j][1].rectsolid.y)
floor_data.append(floor_group[j][1].rectsolid.right)
break
inputs = (FLOOR_VELOCITY,player.on_floor,player.rect.y, player.rect.x, player.x_vel, player.y_vel, *floor_data, player.score,l,r)
output = winner_net.activate(inputs)
handle_move(player,output, wallGroup)
print_text(f"Score: {(player.score)}", TEXT_FONT, screen, (140, 10))
print_text(f"Floors: {(player.highest_floor_index)}", TEXT_FONT, screen, (240, 10))
print_text(f"Gen: {None}", TEXT_FONT, screen, (340, 10))
# print(len(floor_group))
if player.rect.top > WIN_HEIGHT:
players.pop(x)
break
pygame.display.update()
clock.tick(60)
reset(screen, floor_image)