Esempio n. 1
0
class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        pygame.display.set_caption(TITLE)
        self.clock = pygame.time.Clock()
        self.run = True

    # method to call
    def mainLoop(self):
        self.setUp()
        while self.run:
            self.clock.tick(FPS)
            self.events()
            pygame.display.update()

    # setting up all game objects
    def setUp(self):
        self.score = 0
        self.base = Base(BASE_Y)
        self.bird = Bird(int(WIDTH / 2), int(HEIGHT / 2))

    # function contains eg. collision detection
    def events(self):
        self.update()
        self.controls()
        self.movements()

    # this method will handle all obj movements
    def movements(self):
        # Base
        self.base.move()

    """Drawings"""

    def update(self):
        self.drawBg()
        self.drawBase()
        self.drawBird()

    def drawBird(self):
        self.bird.draw(self.screen)

    def drawBg(self):
        self.screen.blit(BG_IMGS[0], (0, 0))

    def drawBase(self):
        self.base.draw(self.screen)

    # key controls
    def controls(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.run = False
Esempio n. 2
0
def main(genomes, config):
    global GEN
    BASE_POINT = 700
    GEN += 1

    nets = []
    gens = []
    birds = []

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

    base = Base(BASE_POINT)
    pipes = [Pipe(600)]
    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_idx = 0
        if len(birds) > 0:
            if len(pipes) > 1 and birds[
                    0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():
                pipe_idx = 1
        else:
            run = False
            break

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

            output = nets[birds.index(bird)].activate(
                (bird.y, abs(bird.y - pipes[pipe_idx].height),
                 abs(bird.y - pipes[pipe_idx].bottom)))

            if output[
                    0] > 0.5:  # use a tanh activation function so result will be between -1 and 1. if over 0.5 jump
                bird.jump()

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

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

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

            pipe.move()

        if add_pipe:
            score += 1

            for g in gens:
                g.fitness += 5

            pipes.append(Pipe(600))

        for pipe in remove:
            pipes.remove(pipe)

        for i, bird in enumerate(birds):
            if bird.y + bird.img.get_height() >= BASE_POINT or bird.y < 0:
                birds.pop(i)
                nets.pop(i)
                gens.pop(i)

        base.move()
        draw_window(win, birds, pipes, base, score, GEN)
Esempio n. 3
0
def main(genomes, config):
    nets = []
    ge = []
    birds = []
    global GEN
    GEN += 1

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

    base = Base(730)
    pipes = [Pipe(600)]
    win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    score = 0
    #clock = pygame.time.Clock()

    run = True
    while run:
        #time.sleep(0.2)
        #clock.tick(3000) #pour donner un tempo au jeu quelque soit l'OS mais ne marche pas
        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:
            run = False
            break

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

            output = nets[x].activate(
                (bird.y, abs(bird.y - pipes[pipe_ind].height),
                 abs(bird.y - pipes[pipe_ind].bottom)))

            if output[0] > 0.5:
                bird.jump(
                )  # we use a tanh activation function so result will be between -1 and 1. if over 0.5 jump

        add_pipe = False
        rem = []
        for pipe in pipes:
            for x, bird in enumerate(birds):
                if pipe.collide(bird):
                    ge[x].fitness -= 1
                    birds.pop(x)
                    nets.pop(x)
                    ge.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:
                rem.append(pipe)
            pipe.move()
        if add_pipe:
            score += 1
            for g in ge:
                g.fitness += 5
            pipes.append(Pipe(600))

        for r in rem:
            pipes.remove(r)

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

        base.move()
        draw_window(win, birds, pipes, base, score, GEN)