Example #1
0
def initialize_game_elements():
    """
    Creates all class instances needed for the game, then saves all instances into a dictionary

    :return: type: dict
    A dictionary containing all the class instances needed for the game to function
    """
    # Initialize first & second base
    base1 = Base(0, DISPLAY_HEIGHT - Base.height)
    base2 = Base(Base.width, DISPLAY_HEIGHT - Base.height)

    # Initialize bird
    bird = Bird((DISPLAY_WIDTH / 2) - Bird.width, DISPLAY_HEIGHT / 2)

    # Initialize pipes
    pipe1 = Pipe(DISPLAY_WIDTH * 2)
    pipe2 = Pipe(pipe1.x + Pipe.interval)

    # Initialize score
    score = Score()

    return {
        "base": [base1, base2],
        "bird": bird,
        "pipe": [pipe1, pipe2],
        "score": score
    }
Example #2
0
    def __init__(self, window_width, window_height, square_side,\
            grid, fps=10):
        self.window_width = window_width
        self.window_height = window_height
        self.square_side = square_side
        self.fps = fps
        self.clock = pygame.time.Clock()
        self.game_window = pygame.display.set_mode(
            (window_width, window_height))
        #self.pygame.display.set_caption("Automating Mechanical Engineering")

        self.grid = grid
        self.pipe = Pipe((2, 2), (2, 5), self.grid)
        self.wall = Wall(self.grid)
        self.grid.update()
        self.wall.update(self.game_window, self.grid)
        self.state = torch.zeros([self.grid.size_x, self.grid.size_y])
Example #3
0
 def start_objects(self):
     self.high_score = max(self.high_score, self.score)
     self.score = 0
     self.distance = 0
     self.pipes = [Pipe(WIDTH + i * 230) for i in range(2)]
     self.pipe = self.pipes[0]
     self.birds_dead = [0 for _ in self.birds]
     for bird in self.birds:
         bird.alive = True
Example #4
0
def create_pipe(pipes_s, last_obstacle, gameSpeed):
  if len(last_obstacle) == 0:
    new_pipe = Pipe(gameSpeed, 0.6)
    pipes_s.append(new_pipe)
    last_obstacle.add(new_pipe)
    new_pipe = Pipe(gameSpeed, 0.6, True)
    pipes_s.append(new_pipe)
    last_obstacle.add(new_pipe)
  else:
    for l in last_obstacle:
      if l.rect.right < WIN_WIDTH * 0.4:
        last_obstacle.empty()
        rand = random.randrange(35, 60) / 100
        new_pipe = Pipe(gameSpeed, rand)
        pipes_s.append(new_pipe)
        last_obstacle.add(new_pipe)
        new_pipe = Pipe(gameSpeed, rand, True)
        pipes_s.append(new_pipe)
        last_obstacle.add(new_pipe)
        break
Example #5
0
 def pipe_move(self):
     # move pipes to left
     for uPipe, lPipe in zip(self.upperPipes, self.lowerPipes):
         uPipe['x'] += self.pipeVelX
         lPipe['x'] += self.pipeVelX
     # add new pipe when first pipe is about to touch left of screen
     if 0 < self.upperPipes[0]['x'] < 5:
         new_pipe = Pipe.get_random_pipe()
         self.upperPipes.append(new_pipe[0])
         self.lowerPipes.append(new_pipe[1])
     # remove first pipe if its out of the screen
     if self.upperPipes[0]['x'] < -PIPE_WIDTH:
         self.upperPipes.pop(0)
         self.lowerPipes.pop(0)
Example #6
0
class MyGame():
    def __init__(self, window_width, window_height, square_side,\
            grid, fps=10):
        self.window_width = window_width
        self.window_height = window_height
        self.square_side = square_side
        self.fps = fps
        self.clock = pygame.time.Clock()
        self.game_window = pygame.display.set_mode(
            (window_width, window_height))
        #self.pygame.display.set_caption("Automating Mechanical Engineering")

        self.grid = grid
        self.pipe = Pipe((2, 2), (2, 5), self.grid)
        self.wall = Wall(self.grid)
        self.grid.update()
        self.wall.update(self.game_window, self.grid)
        self.state = torch.zeros([self.grid.size_x, self.grid.size_y])

    def time_step(self, action):
        #while self.pipe.done == False:
        #_ = input()
        self.clock.tick(self.fps)

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

        reward = self.pipe.update(action, self.game_window)

        # pipe update needs an action
        #self.state = self.pipe.update(self.game_window)
        #self.wall.update(self.game_window, self.grid)
        #self.pipe.draw(self.game_window)
        pygame.display.flip()
        #return len(self.pipe), self.pipe.illegal_moves
        return reward
Example #7
0
def initialize_game_elements(genomes):
    """
    Creates all class instances needed for the game, then saves all instances into a dictionary

    Also creates the birds, networks and genomes list needed for the NEAT algorithm recording purposes
    :param genomes: type list
    List containing the genomes for every bird

    :return: type: dict
    A dictionary containing all the class instances needed for the game to function
    """
    # Initialize first & second base
    base1 = Base(0, DISPLAY_HEIGHT - Base.height)
    base2 = Base(Base.width, DISPLAY_HEIGHT - Base.height)

    # Initialize bird, network and genome list
    # These list will record the surviving birds respective network and genomes
    birds_list = []
    networks_list = []
    genomes_list = []
    ranking = {}
    for genome_id, genome, model_name in genomes:
        # Create network for bird
        # Setup network using genome & config
        network = neat.nn.FeedForwardNetwork.create(genome, config)
        networks_list.append(network)

        # Create bird
        bird = Bird((DISPLAY_WIDTH / 2) - Bird.width, DISPLAY_HEIGHT / 2)
        birds_list.append(bird)

        # Define starting fitness
        genome.fitness = 0
        genomes_list.append((genome_id, genome))

        # Add model names into ranking board
        ranking[bird] = {
            "model name": model_name,
            "fitness score": None,
            "pipe score": None
        }

    # Initialize pipes
    pipe1 = Pipe(DISPLAY_WIDTH * 2)
    pipe2 = Pipe(pipe1.x + Pipe.interval)
    # Get pipe index
    pipe_x_list = [pipe.x for pipe in [pipe1, pipe2]]
    pipe_index = pipe_x_list.index(min(pipe_x_list))

    # Initialize score
    score = Score()

    return {
        "base": [base1, base2],
        "birds": birds_list,
        "networks": networks_list,
        "genomes": genomes_list,
        "ranking": ranking,
        "pipe": [pipe1, pipe2],
        "pipe_index": pipe_index,
        "score": score
    }
Example #8
0
def initialize_game_elements(genomes):
    """
    Creates all class instances needed for the game, then saves all instances into a dictionary

    Also creates the birds, networks and genomes list needed for the NEAT algorithm recording purposes
    :param genomes: type list
    List containing the genomes for every bird

    :return: type: dict
    A dictionary containing all the class instances needed for the game to function
    """
    # Initialize first & second base
    base1 = Base(0, DISPLAY_HEIGHT - Base.height)
    base2 = Base(Base.width, DISPLAY_HEIGHT - Base.height)

    # Initialize bird, network and genome list
    # These list will record the surviving birds respective network and genomes
    birds_list = []
    networks_list = []
    genomes_list = []
    for genome_id, genome in genomes:
        # Create network for bird
        # Setup network using genome & config
        network = neat.nn.FeedForwardNetwork.create(genome, config)
        networks_list.append(network)

        # Create bird
        birds_list.append(
            Bird((DISPLAY_WIDTH / 2) - Bird.width, DISPLAY_HEIGHT / 2))

        # Define starting fitness
        genome.fitness = 0
        genomes_list.append((genome_id, genome))

    # Initialize pipes
    pipe1 = Pipe(DISPLAY_WIDTH * 2)
    pipe2 = Pipe(pipe1.x + Pipe.interval)
    # Get pipe index
    pipe_x_list = [pipe.x for pipe in [pipe1, pipe2]]
    pipe_index = pipe_x_list.index(min(pipe_x_list))

    # Initialize score
    score = Score()

    # Initialize bird counter textbox
    bird_counter = Textbox("white", "arialbd", 16, (DISPLAY_WIDTH * (1 / 4)),
                           20)

    # Initialize generation counter textbox
    generation_counter = Textbox("white", "arialbd", 16,
                                 (DISPLAY_WIDTH * (3 / 4)), 20)

    return {
        "base": [base1, base2],
        "birds": birds_list,
        "networks": networks_list,
        "genomes": genomes_list,
        "pipe": [pipe1, pipe2],
        "pipe_index": pipe_index,
        "score": score,
        "bird_counter": bird_counter,
        "generation_counter": generation_counter
    }
Example #9
0
 def __init__(self):
     Pipe.__init__(self)
     Player.__init__(self)
     Base.__init__(self)
     self.score = self.playerIndex = self.loopIter = 0