Ejemplo n.º 1
0
class FlappyBirdUser:
    WIN_WIDTH = 500
    WIN_HEIGHT = 800

    def __init__(self):
        self.running = False
        self.window = pygame.display.set_mode(
            (self.WIN_WIDTH, self.WIN_HEIGHT))
        self.clock = pygame.time.Clock()
        self.score = 0
        self.bird = Bird(230, 350)
        self.ground = Ground(730)
        self.pipes = [Pipe(600)]

    def run(self):
        self.running = True
        while self.running:
            self.loop()
        pygame.quit()
        quit()

    def loop(self):
        self.clock.tick(30)
        self.handle_events()
        add_pipe = False
        pipes_to_remove = []
        for pipe in self.pipes:
            if pipe.collide(self.bird):
                # self.running = False
                pass
            if not pipe.passed and pipe.x < self.bird.x:
                pipe.passed = True
                add_pipe = True
            if pipe.is_out_of_map():
                pipes_to_remove.append(pipe)

        if add_pipe:
            self.score += 1
            self.pipes.append(Pipe(600))

        for pipe_to_remove in pipes_to_remove:
            self.pipes.remove(pipe_to_remove)

        if self.bird.y + self.bird.animation.image.get_height(
        ) >= 730 or self.bird.y < 0:
            self.running = False

        self.update()
        self.draw_window()

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                self.bird.jump()

    def update(self):
        for pipe in self.pipes:
            pipe.move()
        self.bird.move()
        self.ground.move()

    def draw_window(self):
        self.window.blit(BACKGROUND_IMAGE, (0, 0))
        for pipe in self.pipes:
            pipe.draw(self.window)
        self.ground.draw(self.window)
        self.bird.draw(self.window)
        text_score = STAT_FONT.render("Score: " + str(self.score), 1,
                                      (255, 255, 255))
        self.window.blit(text_score,
                         (self.WIN_WIDTH - 10 - text_score.get_width(), 10))

        pygame.display.update()
Ejemplo n.º 2
0
def run_generation(genomes, config):
    if 'generation_number' not in run_generation.__dict__:
        run_generation.generation_number = 0
    run_generation.generation_number += 1
    nets = []
    ge = []
    birds = []

    for _, genome in genomes:
        net = neat.nn.FeedForwardNetwork.create(genome, config)
        nets.append(net)
        birds.append(Bird(230, 350))
        genome.fitness = 0
        ge.append(genome)

    base = Ground(730)
    pipes = [Pipe(600)]
    window = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    clock = pygame.time.Clock()
    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()

        pipe_index = 0
        if len(birds) > 0:
            if len(pipes) > 1 and birds[
                    0].x > pipes[0].x + pipes[0].PIPE_IMAGE_TOP.get_width():
                pipe_index = 1
        else:
            run = False
            break

        for bird_index, bird in enumerate(birds):
            bird.move()
            ge[bird_index].fitness += 0.1

            output = nets[bird_index].activate(
                (bird.y, abs(bird.y - pipes[pipe_index].height),
                 abs(bird.y - pipes[pipe_index].bottom)))
            if output[0] > 0.5:
                bird.jump()

        add_pipe = False
        pipes_to_remove = []
        for pipe in pipes:
            for index, bird in enumerate(birds):
                if pipe.collide(bird):
                    ge[index].fitness -= 1
                    birds.pop(index)
                    nets.pop(index)
                    ge.pop(index)

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

            if pipe.x + pipe.PIPE_IMAGE_TOP.get_width() < 0:
                pipes_to_remove.append(pipe)
            pipe.move()
        if add_pipe:
            score += 1
            for g in ge:
                g.fitness += 5
            pipes.append(Pipe(600))

        for pipe_to_remove in pipes_to_remove:
            pipes.remove(pipe_to_remove)

        for bird_index, bird in enumerate(birds):
            if bird.y + bird.animation.image.get_height() >= 730 or bird.y < 0:
                birds.pop(bird_index)
                nets.pop(bird_index)
                ge.pop(bird_index)

        base.move()
        draw_window(window, birds, pipes, base, score,
                    run_generation.generation_number)
def main():
    population = Population(25, 0.01)

    ground = Ground()
    pipes = [Pipe()]

    run = True
    win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    pygame.font.init()
    clock = pygame.time.Clock()
    max_score = 0
    score = 0

    while run:
        #clock.tick(30)
        ground.move() # moves ground

        # Pipes
        removed_pipes = []
        flag_add_pipe = False
        for pipe in pipes:
            if pipe.x + pipe.PIPE_TOP.get_width() < 0:  # if pipe leaves screen
                removed_pipes.append(pipe)

            pipe.move()  # moves pipe

            for bird in population.individuals:
                if pipe.collide(bird) and bird.is_alive: # checks collision
                    bird.is_alive = False

                if not pipe.passed: # measures distance between bird and next pipe
                    bird.distance_x_next_pipe = (pipe.x - bird.x)
                    bird.distance_y_next_pipe = (pipe.height - bird.y)

                # checks if pipe has already been crossed
                if not pipe.passed and pipe.x + pipe.PIPE_BOTTOM.get_width()/2 < bird.x - bird.img.get_width()/2:
                    bird.fitness += 100
                    pipe.passed = True
                    flag_add_pipe = True
                    score += 1


        if flag_add_pipe:
            pipes.append(Pipe()) # adds pipe

        for removed_pipe in removed_pipes:
            pipes.remove(removed_pipe) # removes pipe


        for bird in population.individuals:
            bird.move()  # move pássaro
            if bird.y + bird.img.get_height() >= 350 and bird.is_alive: # if bird hits the ground
                bird.is_alive = False
                bird.y = 350

            if bird.y < 0: # if bird hits the ceiling
                bird.y = 0

            if bird.is_alive:
                bird.fitness += 1

        draw_window(win, population.individuals, pipes, ground, max_score, score) # draws objects modifications on screen

        if population.is_all_dead():
            pipes = [Pipe()]
            population.reproduct()
            if score > max_score:
                max_score = score
            score = 0

        # Exit game
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

    pygame.quit()
    quit()