def gameplay(window, surface): global SIZE, screen SIZE = window screen = surface background_image_1 = pygame.image.load( os.path.join(PATH, "images", "Background2.jpg")).convert() background_image_1 = pygame.transform.smoothscale(background_image_1, SIZE) all_sprites_group = pygame.sprite.Group() player_list = [] block_group_list = [pygame.sprite.Group(), pygame.sprite.Group()] coin_group = pygame.sprite.Group() block_width = 150 tolerance = 200 vertical_distance = 60 block_x = SIZE[0] // 2 block_y = [SIZE[1] - 50, SIZE[1] - 50] new_block = [ Block([block_x // 2, block_y[0]], block_width), Block([block_x // 2 + SIZE[0] // 2, block_y[1]], block_width) ] start_block = Block([0, SIZE[1] - 20], SIZE[0]) block_group_list[0].add(new_block[0]) block_group_list[1].add(new_block[1]) all_sprites_group.add(start_block, new_block) while max(block_y) > 70: block_x, block_y = set_block(block_x, block_y, block_width, tolerance) new_block1 = Block([block_x // 2, block_y[0]], block_width) new_block2 = Block([block_x // 2 + SIZE[0] // 2, block_y[1]], block_width) block_group_list[0].add(new_block1) block_group_list[1].add(new_block2) if random.choice([True, False]): coin_group.add(new_block1.coin, new_block2.coin) #player1 = Player(round(SIZE[0]*0.25)) player_database = shelve.open("test_database") player1_net = player_database["test"] player1 = ComputerPlayer() player1.second = False player2 = Player(round(SIZE[0] * 0.75), True) player_list += [player1, player2] all_sprites_group.add(player1, player2, coin_group, block_group_list) game_over = False clock = pygame.time.Clock() count = 0 high_score = 0 max_score = 0 score_font = pygame.font.Font("freesansbold.ttf", SIZE[1] // 36) # -------------- Main Program Loop ---------------- # while not game_over: count += 1 for event in pygame.event.get(): if event.type == pygame.QUIT: return 'gameover' elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return 'gameover' elif event.key == pygame.K_SPACE: pause = pause_menu(SIZE, screen) if pause == "gameover": return 'gameover' elif pause == 'main_menu': return 'lost', [player1.score, player2.score] keys = pygame.key.get_pressed() if keys[pygame.K_RIGHT]: player2.move('right') elif keys[pygame.K_LEFT]: player2.move('left') player1.move() distance1 = player1.check_directions(SIZE, block_group_list[0]) distance2 = player1.check_quadrants(SIZE, block_group_list[0]) nn_output = player1_net.activate(distance1 + distance2) position = nn_output.index(max(nn_output)) if position == 0 and nn_output[0] > 0.5: player1.horizontal_direction = 'right' player1.none_score = 0 elif position == 1 and nn_output[1] > 0.5: player1.horizontal_direction = 'left' player1.none_score = 0 elif player1.horizontal_direction != 'none': player1.none_score = 0 player1.horizontal_direction = 'none' else: player1.none_score += 1 for i, player in enumerate(player_list): player.set_score(start_block) if player.score > high_score: high_score = player.score max_score = i if player1.alive: player1.collision(block_group_list[0]) if player2.alive: player2.collision(block_group_list[1]) if max_score >= len(player_list): max_score = len(player_list) - 1 if player_list[max_score].rect.y < SIZE[1] // 3 and max(block_y) > -10: block_x, block_y = set_block(block_x, block_y, block_width, tolerance) block_type = choose_block_type(player_list[max_score].score) if block_type == 'move': new_block = [ SideBlock([block_x // 2, block_y[0]], block_width), SideBlock([block_x // 2 + SIZE[0] // 2, block_y[1]], block_width) ] elif block_type == 'still': new_block = [ Block([block_x // 2, block_y[0]], block_width), Block([block_x // 2 + SIZE[0] // 2, block_y[1]], block_width) ] elif block_type == 'vertical': block_y = list( map(lambda x: x - vertical_distance // 2, block_y)) new_block = [ VerticalBlock([block_x // 2, block_y[0]], block_width, vertical_distance), VerticalBlock([block_x // 2 + SIZE[0] // 2, block_y[1]], block_width, vertical_distance) ] block_y = list( map(lambda x: x - vertical_distance // 2, block_y)) new_block[0].coin = None new_block[1].coin = None elif block_type == 'onetime': new_block = [ OneTimeBlock([block_x // 2, block_y[0]], block_width), OneTimeBlock([block_x // 2 + SIZE[0] // 2, block_y[1]], block_width) ] block_group_list[0].add(new_block[0]) block_group_list[1].add(new_block[1]) all_sprites_group.add(new_block) if random.choice([True, False]) and new_block[0].coin != None: coin_group.add(new_block[0].coin, new_block[1].coin) all_sprites_group.add(new_block[0].coin, new_block[1].coin) coin1_hit_list = pygame.sprite.spritecollide(player_list[0], coin_group, True) player_list[0].score += len(coin1_hit_list * 800) if len(player_list) > 1: coin2_hit_list = pygame.sprite.spritecollide( player_list[1], coin_group, True) player_list[1].score += len(coin2_hit_list * 800) if count % 15 == 0: block_width = int( round(-60 / (1 + 1.0003**(5000 - high_score)) + 160)) if count % 13 == 0: tolerance = int(round(60 / (1 + 1.0003**(5000 - high_score)) + 190)) block_y = scroll(all_sprites_group, block_y, player1, 1) block_y = scroll(all_sprites_group, block_y, player2, 2) for block_group in block_group_list: for block in block_group: if block.rect.y > SIZE[1] + 30 or (block.type == "onetime" and block.hit > 0): block.kill() for player in player_list: if player.rect.top > SIZE[1]: player_list.pop(player_list.index(player)) if player.second: for block in block_group_list[1]: block.kill() for coin in coin_group: if coin.rect.centerx > SIZE[0] // 2: coin.kill() background_image_1 = pygame.image.load( os.path.join(PATH, "images", "Background2_Right_Blur.jpg")).convert() background_image_1 = pygame.transform.smoothscale( background_image_1, SIZE) else: for block in block_group_list[0]: block.kill() for coin in coin_group: if coin.rect.centerx < SIZE[0] // 2: coin.kill() background_image_1 = pygame.image.load( os.path.join(PATH, "images", "Background2_Left_Blur.jpg")).convert() background_image_1 = pygame.transform.smoothscale( background_image_1, SIZE) player.kill() if not player_list: game_over_double(SIZE, screen, [player1.score, player2.score]) return None screen.blit(background_image_1, (0, 0)) all_sprites_group.update() if player1.alive: screen.blit(player1.image, player1.rect) if player2.alive: screen.blit(player2.image, player2.rect) block_group_list[0].draw(screen) block_group_list[1].draw(screen) coin_group.draw(screen) screen.blit( score_font.render("Player 1 Score: " + str(player1.score), True, BLACK), (SIZE[0] // 20, SIZE[1] // 20)) screen.blit( score_font.render("Player 2 Score: " + str(player2.score), True, BLACK), (SIZE[0] // 20 + SIZE[0] // 2, SIZE[1] // 20)) pygame.display.flip() clock.tick(60)
def gameplay(genomes, config): global game_over, main_menu if game_over: for genome_id, genome in genomes: genome.fitness = 0 return global generation, high_net, high_score, slow background_image_1 = pygame.image.load( os.path.join(PATH, "images", "Background.jpg")).convert() background_image_1 = pygame.transform.smoothscale(background_image_1, SIZE) generation += 1 score_font = pygame.font.Font("freesansbold.ttf", 30) player_list = [] genome_list = [] nn_list = [] block_group = pygame.sprite.Group() block_count = 0 tolerance = 220 vertical_distance = 60 for genome_id, genome in genomes: genome.fitness = 0 net = neat.nn.FeedForwardNetwork.create(genome, config) nn_list.append(net) player_list.append(Computer()) genome_list.append(genome) block_width = 130 start_block = Block([0, SIZE[1] - 20], SIZE[0], block_count) block_count += 1 block_x, block_y = round(SIZE[0] * 0.25), SIZE[1] - 50 new_block = Block([block_x, block_y], block_width, block_count) block_count += 1 block_group.add(new_block) while block_y > 70: block_x, block_y = set_block(block_x, block_y, block_width, tolerance) new_block = Block([block_x, block_y], block_width, block_count) block_count += 1 block_group.add(new_block) clock = pygame.time.Clock() current_score = 0 max_index = 0 max_score = 0 count = 0 # -------------- Main Program Loop ---------------- # while not game_over and len(player_list) > 0: count += 1 for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: game_over = True return elif event.key == pygame.K_s: slow = not slow elif event.key == pygame.K_SPACE: pause = pause_menu(SIZE, screen) if pause == "gameover": game_over = True return elif pause == 'main_menu': game_over = True main_menu = True return screen.blit(background_image_1, (0, 0)) current_score = 0 for i, player in enumerate(player_list): player.score_count += 1 player.set_score(start_block) genome_list[i].fitness = player.fitness player.move() player.collision(block_group) if player.score > current_score: current_score = player.score max_index = i max_net = nn_list[i] if player.score > max_score: max_score = player.score if player.direction == "up": direction_num = 5 elif player.direction == "down": direction_num = -5 nn_output = nn_list[i].activate( player.set_inputs(block_group) + [direction_num]) position = nn_output.index(max(nn_output)) if position == 0 and nn_output[0] > 0.5: player.horizontal_direction = 'right' player.none_score = 0 elif position == 1 and nn_output[1] > 0.5: player.horizontal_direction = 'left' player.none_score = 0 elif player.horizontal_direction != 'none': player.none_score = 0 player.horizontal_direction = 'none' else: player.none_score += 1 if max_index >= len(player_list): max_index = len(player_list) - 1 if player_list[max_index].rect.y < SIZE[0] // 5: if block_y > -10: block_x, block_y = set_block(block_x, block_y, block_width, tolerance) block_type = choose_block_type(player_list[max_index].score) if block_type == 'move': new_block = SideBlock([block_x, block_y], block_width, block_count) elif block_type == 'still': new_block = Block([block_x, block_y], block_width, block_count) elif block_type == 'vertical': block_y -= vertical_distance // 2 new_block = VerticalBlock([block_x, block_y], block_width, vertical_distance, block_count) block_y -= vertical_distance // 2 block_count += 1 block_group.add(new_block) if count % 15 == 0: block_width = int( round(-60 / (1 + 1.0001**(9000 - max_score)) + 160)) if count % 13 == 0: tolerance = int(round(60 / (1 + 1.0003**(10000 - max_score)) + 190)) if player_list[max_index].rect.y < 0: block_y = move(11, block_y, block_group, player_list, start_block) elif player_list[max_index].rect.y < SIZE[1] // 6 and player_list[ max_index].rect.y > 0: block_y = move(7, block_y, block_group, player_list, start_block) elif player_list[max_index].rect.y < SIZE[1] // 3 and player_list[ max_index].rect.y > SIZE[1] // 6: block_y = move(4, block_y, block_group, player_list, start_block) print(player_list[max_index].none_score, player_list[max_index].fitness, player_list[max_index].block_hit_list) for block in block_group: if block.rect.y > SIZE[1] * 2: block.kill() for player in player_list: if player.rect.bottom > SIZE[1] * 2 or player.score_count > 450: genome_list[player_list.index(player)].fitness = player.fitness nn_list.pop(player_list.index(player)) genome_list.pop(player_list.index(player)) player_list.pop(player_list.index(player)) screen.blit(background_image_1, (0, 0)) block_group.update() block_group.draw(screen) for player in player_list: player.update() screen.blit(player.image, player.rect) score_display = score_font.render("Max Score " + str(max_score), True, BLACK) screen.blit(score_display, (SIZE[0] // 20, SIZE[1] // 20)) generation_display = score_font.render("Generation " + str(generation), True, BLACK) screen.blit(generation_display, (SIZE[0] // 20, SIZE[1] // 10)) high_score_display = score_font.render("Highscore " + str(high_score), True, BLACK) screen.blit(high_score_display, (SIZE[0] // 20, SIZE[1] // 7)) pygame.display.flip() if slow == True: clock.tick(60) if max_score > high_score: high_score = max_score high_net = max_net max_score_list.append(max_score)
def gameplay(window, surface): global SIZE, screen SIZE = window screen = surface background_image_1 = pygame.image.load( os.path.join(PATH, "images", "Background.jpg")).convert() background_image_1 = pygame.transform.smoothscale(background_image_1, SIZE) all_sprites_group = pygame.sprite.Group() block_group = pygame.sprite.Group() coin_group = pygame.sprite.Group() block_width = 150 tolerance = 200 vertical_distance = 60 block_x, block_y = SIZE[0] // 2, SIZE[1] - 50 new_block = Block([block_x, block_y], block_width) start_block = Block([0, SIZE[1] - 20], SIZE[0]) block_group.add(new_block) all_sprites_group.add(start_block, new_block) while block_y > 70: block_x, block_y = set_block(block_x, block_y, block_width, tolerance) new_block = Block([block_x, block_y], block_width) block_group.add(new_block) if new_block.coin != None: coin_group.add(new_block.coin) all_sprites_group.add(block_group, coin_group) player1 = Player(SIZE[0] // 2 - 10) all_sprites_group.add(player1) game_over = False clock = pygame.time.Clock() count = 0 high_score = 0 max = 0 score_font = pygame.font.Font("freesansbold.ttf", SIZE[0] // 64) # -------------- Main Program Loop ---------------- # while not game_over: count += 1 for event in pygame.event.get(): if event.type == pygame.QUIT: return 'gameover', [] elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return 'gameover', [] elif event.key == pygame.K_SPACE: pause = pause_menu(SIZE, screen) if pause == "gameover": return 'gameover', [] elif pause == 'main_menu': return None keys = pygame.key.get_pressed() if keys[pygame.K_RIGHT]: player1.move('right') elif keys[pygame.K_LEFT]: player1.move('left') player1.set_score(start_block) player1.collision(block_group) if player1.score > high_score: high_score = player1.score if player1.rect.y < SIZE[1] // 3 and block_y > -10: block_x, block_y = set_block(block_x, block_y, block_width, tolerance) block_type = choose_block_type(player1.score) if block_type == 'move': new_block = SideBlock([block_x, block_y], block_width) elif block_type == 'still': new_block = Block([block_x, block_y], block_width) elif block_type == 'vertical': block_y -= vertical_distance // 2 new_block = VerticalBlock([block_x, block_y], block_width, vertical_distance) block_y -= vertical_distance // 2 new_block.coin = None elif block_type == 'onetime': new_block = OneTimeBlock([block_x, block_y], block_width) block_group.add(new_block) all_sprites_group.add(new_block) if new_block.coin != None: coin_group.add(new_block.coin) all_sprites_group.add(new_block.coin) coin_hit_list = pygame.sprite.spritecollide(player1, coin_group, True) player1.score += len(coin_hit_list * 800) if count % 15 == 0: block_width = int( round(-60 / (1 + 1.0003**(5000 - high_score)) + 160)) if count % 13 == 0: tolerance = int(round(60 / (1 + 1.0003**(5000 - high_score)) + 190)) if player1.rect.y < 0: block_y = move(15, block_y, all_sprites_group) elif player1.rect.y < SIZE[1] // 6 and player1.rect.y > 0: block_y = move(7, block_y, all_sprites_group) elif player1.rect.y < SIZE[1] // 3 and player1.rect.y > SIZE[1] // 6: block_y = move(4, block_y, all_sprites_group) for block in block_group: if block.rect.y > SIZE[1] + 30: block.kill() if block.type == 'onetime' and block.hit > 0: block.kill() if player1.rect.top > SIZE[1]: game_over = True game_over_single(SIZE, screen, player1.score) return None screen.blit(background_image_1, (0, 0)) all_sprites_group.update() screen.blit(player1.image, player1.rect) block_group.draw(screen) coin_group.draw(screen) score1_display = score_font.render("Score: " + str(player1.score), True, BLACK) screen.blit(score1_display, (SIZE[0] // 20, SIZE[1] // 20)) pygame.display.flip() clock.tick(60)