def calc(self): fonter = functions.font_chooser(self.font, self.fsize) y = 0 x = 0 if len(self.images) > 0: self.images = [] self.post = [] for item in self.listt: self.images.append(functions.write_text(fonter, item, self.fcolor)) if y > 0: self.post.append(self.images[y].get_rect().size[1] + 1 + (y * (self.images[y - 1].get_rect().size[1]))) else: self.post.append(self.images[y].get_rect().size[1] + 1) y += 1 altercolor = [0, 0, 0] for i in range(0, 3): if self.color[i] >= 200: altercolor[i] = (self.color[i] - 100) elif self.color[i] >= 100: altercolor[i] = (self.color[i] - 50) self.hcolor = tuple(altercolor) altercolor = [0, 0, 0] for i in range(0, 3): if self.color[i] == 0: altercolor[i] = 30 elif self.color[i] < 60: altercolor[i] = (self.color[i] + 100) elif self.color[i] >= 60 and self.color[i] < 150: altercolor[i] = (self.color[i] + 50) elif self.color[i] >= 150: altercolor[i] = self.color[i] self.scolor = tuple(altercolor) self.xffset()
def type_(self, event): string = pygame.key.name(event.key) if string.lower() == 'space': string = ' ' self.string += string self.list_text.append(string) self.string_toDisplay = self.string i = 1 while functions.write_text( self.ffont, self.string_toDisplay, self.fcolor).get_rect().size[0] + 4 > self.size[0]: self.string_toDisplay = self.string[i:] i += 1 self.imagetxt = (functions.write_text(self.ffont, self.string_toDisplay, self.fcolor)) return elif string.lower() == "right shift" or string.lower() == "left shift": self.caps = True elif string.lower() == "return": self.clicked = False elif string.lower() == "caps lock": print(self.cap2) if self.cap2: self.cap2 = False else: self.cap2 = True elif string.lower() == 'backspace': string = '' self.string += string self.string = self.string[:-1] self.string_toDisplay = self.string i = 1 while functions.write_text( self.ffont, self.string_toDisplay, self.fcolor).get_rect().size[0] + 4 > self.size[0]: self.string_toDisplay = self.string[i:] i += 1 self.imagetxt = (functions.write_text(self.ffont, self.string_toDisplay, self.fcolor)) return else: if self.caps or self.cap2 == True: string = string.upper() self.string += string self.list_text.append(string) self.string_toDisplay = self.string i = 1 while functions.write_text( self.ffont, self.string_toDisplay, self.fcolor).get_rect().size[0] + 4 > self.size[0]: self.string_toDisplay = self.string[i:] i += 1 self.imagetxt = (functions.write_text(self.ffont, self.string_toDisplay, self.fcolor)) return
def calc(self): self.ffont = functions.font_chooser(self.font, self.fsize) self.size = (self.size[0], functions.write_text(self.ffont, "hello", self.fcolor).get_rect().size[1] + 4)
track.draw(line_color, bg_color, None) #background_image) # Initialize population param_list = calculate_parameters(input_size, output_size, layer_dimensions) population = initialize_population(param_list, population_size, initial_weight_range) cars = [] models = [] for generation in range(n_generations): pygame.event.pump() win.blit(track.surface, (0, 0)) write_text(win, generation + 1) decode_population(population, cars, models, track, car_image, win, input_size, output_size, layer_dimensions, param_list) fitness_list, draw_vision_lines = evaluate_population( cars, models, track, win, dt, draw_vision_lines, generation + 1) best_individual_index = np.argmax(fitness_list) best_individual = copy.deepcopy(population[best_individual_index]) temp_population = copy.deepcopy(population) for i in range(population_size, 2): selected1 = tournament_select(fitness_list, p_tournament, tournament_size) selected2 = tournament_select(fitness_list, p_tournament,
def evaluate_population(cars, models, track, win, dt, draw_vision_lines, generation_number): pygame.display.update() pause = False running = True while running: #pygame.time.delay(dt) for event in pygame.event.get(): if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: running = False if event.type == pygame.KEYDOWN and event.key == pygame.K_v: draw_vision_lines = not draw_vision_lines if event.type == pygame.KEYDOWN and event.key == pygame.K_p: pause = True if event.type is pygame.QUIT: pygame.quit() quit() pause = check_pause(pause, win) # Draw track win.blit(track.surface, (0, 0)) for model, icar in zip(models, cars): if icar.running: # Update car position and vision icar.vision() vision_lines, intersection_points, distance = wall_distance( icar, track) distance = distance.reshape((1, 5)) # Propagate through network nn_output = model.predict(distance) take_action(nn_output, icar) icar.update(dt=dt) crash_detection(icar, track) fitness(icar, distance, dt) draw_vision(draw_vision_lines, win, vision_lines, intersection_points) icar.draw_model(win) running_list = [icar.running for icar in cars] if not any(running_list): running = False write_text(win, generation_number) pygame.display.update() fitness_list = [icar.fitness for icar in cars] return fitness_list, draw_vision_lines
icar = car(x=track.start[0],y=track.start[1], car_image = car_image) #Crash text font = pygame.font.Font('freesansbold.ttf', 32) text = font.render('CRASH!', True, green, blue) textRect = text.get_rect() textRect.center = (1080 // 2, 720 // 2) draw_vision_lines = True running = True pause = False while running: #pygame.time.delay(dt) win.blit(track.surface,(0,0)) write_text(win, 0) for event in pygame.event.get(): if event.type == pygame.KEYDOWN and event.key == pygame.K_v: draw_vision_lines = not draw_vision_lines if event.type == pygame.KEYDOWN and event.key == pygame.K_p: pause = True if event.type is pygame.QUIT: pygame.quit() quit() keyes = pygame.key.get_pressed() if keyes[pygame.K_LEFT]:
#%% pygame.init() win = pygame.display.set_mode((1280, 720)) pygame.display.set_caption('CJ Racing Game') car_image = pygame.image.load('car40_20.png') #Load track track = track('test_track.csv') background_image = pygame.image.load("track_bg.png").convert() track.draw(line_color, bg_color, background_image) # Initialize population param_list = calculate_parameters(input_size, output_size, layer_dimensions) population = [best_individual] cars = [] models = [] pygame.event.pump() win.blit(track.surface, (0, 0)) write_text(win, None) decode_population(population, cars, models, track, car_image, win, input_size, output_size, layer_dimensions, param_list) fitness_list, draw_vision_lines = evaluate_population(cars, models, track, win, dt, draw_vision_lines, None) pygame.quit()