示例#1
0
def eval_genomes(pop):
    global WIN

    clock = pygame.time.Clock()
    pipes = [Pipe(700)]
    base = Floor(FLOOR)
    win = WIN
    score = 0

    run = True
    while run:
        clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                quit()
                break

        rem = []
        add_pipe = False
        for pipe in pipes:
            pipe.move()
            pop.check_colision_with_pipe(pipe, win)

            if pipe.x + pipe.PIPE_TOP.get_width() < 0:
                rem.append(pipe)

            if not pipe.passed and pipe.x < pop.pop[0].x:
                pipe.passed = True
                add_pipe = True

        if add_pipe:
            score += 1
            pop.increment_score_by_n(10)
            pipes.append(Pipe(WIN_WIDTH))

        for r in rem:
            pipes.remove(r)

        pipe_ind = 0
        if pop.done() != True:
            if len(pipes) > 1 and pop.pop[
                    0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
                pipe_ind = 1
            pop.increment_score_by_n(0.03)
            pop.update_alive(pipes, pipe_ind, FLOOR)
            base.move()
        else:
            pop.natural_selection()
            run = False

        draw_window(WIN, pop.pop, pipes, base, score, gen, pipe_ind)
示例#2
0
class Game:
    def __init__(self):
        self.blinks_detector = BlinkDetector()
        self.blinks_detector.start()

        pygame.init()
        pygame.event.set_allowed([
            QUIT, KEYDOWN, KEYUP, Pipes.SPAWN_PIPE_EVENT, Bird.BIRD_FLAP_EVENT
        ])

        self.active = True
        self.score = 0

        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
                                              DOUBLEBUF)
        self.screen.set_alpha(None)

        self.clock = pygame.time.Clock()

        self.font = pygame.font.Font('fonts/04B_19.ttf', 40)

        # Game objects
        self.bg_surface = pygame.transform.scale2x(
            pygame.image.load("assets/background-day.png").convert())
        self.gameover_surface = pygame.transform.scale2x(
            pygame.image.load("assets/gameover.png").convert_alpha())
        self.gameover_rect = self.gameover_surface.get_rect(
            center=(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))

        self.floor = Floor()

        self.bird = Bird()
        self.bird.start_flapping()

        self.pipes = Pipes()
        self.pipes.start_spawning()

    def draw_score(self):
        surface = self.font.render(str(int(self.score)), True, (255, 255, 255))
        rect = surface.get_rect(center=(SCREEN_WIDTH / 2, 100))

        self.screen.blit(surface, rect)

    def draw_bg(self):
        self.screen.blit(self.bg_surface, (0, 0))

    def draw_gameover_message(self):
        self.screen.blit(self.gameover_surface, self.gameover_rect)

    def draw_floor(self):
        self.screen.blit(self.floor.surface, (self.floor.x_position, 900))
        self.screen.blit(self.floor.surface,
                         (self.floor.x_position + SCREEN_WIDTH, 900))

    def draw_pipes(self):
        for pipe in self.pipes.pipes_list:
            if pipe.bottom >= SCREEN_HEIGHT:
                self.screen.blit(self.pipes.surface, pipe)
            else:
                flip_pipe = pygame.transform.flip(self.pipes.surface, False,
                                                  True)
                self.screen.blit(flip_pipe, pipe)

    def draw_bird(self):
        self.screen.blit(self.bird.rotated(), self.bird.rect)

    def check_collisions(self):
        return self.pipes.is_collide_with_bird(
            self.bird) or self.bird.is_out_of_bounds()

    def quit(self):
        self.blinks_detector.stop()
        pygame.quit()
        sys.exit()

    def run(self):
        while True:
            if self.blinks_detector.check_blinked() and self.active:
                self.bird.jump()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.quit()

                if event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_SPACE and not self.active:
                        self.active = True
                        self.score = 0
                        self.pipes.clear()
                        self.bird.clear_position()
                        self.bird.jump()

                    if event.key == pygame.K_SPACE and self.active:
                        self.bird.jump()

                if event.type == Bird.BIRD_FLAP_EVENT:
                    self.bird.flap()

                if event.type == Pipes.SPAWN_PIPE_EVENT:
                    if self.active:
                        self.pipes.spawn_new()
                        self.score += 1

            self.draw_bg()

            if self.active:
                self.bird.fall()
                self.draw_bird()

                self.active = not self.check_collisions()

                self.pipes.shift_pipes()
                self.draw_pipes()
            else:
                self.draw_gameover_message()

            self.floor.move()
            self.draw_floor()

            self.draw_score()

            pygame.display.update()
            self.clock.tick(120)
示例#3
0
    def game(self, genomes, config):
        score = 0
        nets = []
        generation = []
        birds = []

        for _, gen in genomes:
            net = neat.nn.FeedForwardNetwork.create(gen, config)
            nets.append(net)
            birds.append(Bird(self.BIRD_IMGS, 100, 200))
            gen.fitness = 0
            generation.append(gen)

        # Game Objects
        bg = Bg(self.BG_IMG)
        floor = Floor(self.FLOOR_IMG, 530)
        pipes = [Pipe(pygame, self.PIPE_IMG, 500)]

        # Pygame Misc
        win = pygame.display.set_mode((self.WIN_WIDTH, self.WIN_HEIGHT))
        clock = pygame.time.Clock()

        run = True
        while run:
            clock.tick(30)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                    pygame.quit()
                    quit()

            pipe_ind = 0
            if (len(birds)) > 0:
                if (len(pipes)) > 1 and birds[
                        0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
                    pipe_ind = 1
            else:
                self.running_generation += 1
                break

            for x, bird in enumerate(birds):
                bird.move()
                generation[x].fitness += 0.1
                outputs = nets[x].activate(
                    (bird.y, abs(bird.y - pipes[pipe_ind].height),
                     abs(bird.y - pipes[pipe_ind].bottom)))
                if outputs[0] > 0.5:
                    bird.flap()

            # Animation
            add_pipe = False
            removed_pipe = []
            for pipe in pipes:
                for x, bird in enumerate(birds):
                    if pipe.collide(pygame, bird):
                        generation[x].fitness -= 1
                        birds.pop(x)
                        nets.pop(x)
                        generation.pop(x)

                    if not pipe.passed and pipe.x < bird.x:
                        pipe.passed = True
                        add_pipe = True

                if pipe.x + pipe.PIPE_TOP.get_width() < 0:
                    removed_pipe.append(pipe)

                pipe.move()

            if add_pipe:
                score += 1
                for g in generation:
                    g.fitness += 5
                pipes.append(Pipe(pygame, self.PIPE_IMG, 400))

            for pipe in removed_pipe:
                pipes.remove(pipe)

            for x, bird in enumerate(birds):
                if bird.y + bird.img.get_height() >= 530 or bird.y < 0:
                    birds.pop(x)
                    nets.pop(x)
                    generation.pop(x)

            floor.move()
            bg.move()
            self.draw_window(win, bg, birds, pipes, floor, score,
                             self.running_generation)

            if score > 30:
                print('Target Acquired! (50++ Flappy Score)')
                with open('best_bird.pkl', 'wb') as file:
                    pickle.dump(nets[0], file)
                    break