コード例 #1
0
    def __init__(self, surface):
        Setup.__init__(self, surface)

        platformW = int(params.WIDTH * 0.10)
        platformH = int(params.HEIGHT * 0.30)
        platformX = int(params.WIDTH * 0.3 - platformW * 0.5)
        platformY = int(params.HEIGHT - platformH)
        platform1 = objects.Obstacle(surface=self.surface,
                                     color=objects.OBSTACLE_COLOR,
                                     rect=(platformX, platformY, platformW,
                                           platformH))

        self.diamondX = platformX - objects.DIAMOND_RADIUS  ## Initial diamond x
        self.diamondY = params.HEIGHT - objects.DIAMOND_RADIUS  ## Initial diamond y
        diamond1 = objects.Diamond(surface=self.surface,
                                   color=objects.DIAMOND_COLOR,
                                   x=self.diamondX,
                                   y=self.diamondY,
                                   radius=objects.DIAMOND_RADIUS)

        platformW = int(params.WIDTH * 0.10)
        platformH = int(params.HEIGHT * 0.30)
        platformX = int(params.WIDTH * 0.7 - platformW * 0.5)
        platformY = int(params.HEIGHT - platformH)
        platform2 = objects.Obstacle(surface=self.surface,
                                     color=objects.OBSTACLE_COLOR,
                                     rect=(platformX, platformY, platformW,
                                           platformH))

        self.diamondX = platformX + platformW + objects.DIAMOND_RADIUS  ## Initial diamond x
        self.diamondY = params.HEIGHT - objects.DIAMOND_RADIUS  ## Initial diamond y
        diamond2 = objects.Diamond(surface=self.surface,
                                   color=objects.DIAMOND_COLOR,
                                   x=self.diamondX,
                                   y=self.diamondY,
                                   radius=objects.DIAMOND_RADIUS)

        obstacles = [platform1, platform2]

        ballX = random.randint(params.WIDTH * 0.5, params.WIDTH * 0.55)
        ballY = params.HEIGHT * 0.95 - objects.BALL_RADIUS
        dotAngle = 0.0
        ball = objects.Ball(surface=self.surface,
                            color=objects.BALL_COLOR,
                            x=ballX,
                            y=ballY,
                            radius=objects.BALL_RADIUS,
                            dotAngle=dotAngle,
                            obstacles=obstacles)

        diamonds = [diamond1, diamond2]

        self.obstacles, self.ball, self.diamonds = (obstacles, ball, diamonds)

        self.maxCollectTime = 9  ## Ideal max completion time in seconds to collect all diamond
コード例 #2
0
    def make_map(self):
        """Makes a map of the level."""
        # edges of the field
        self.obstacles = [
            objects.Obstacle(self.all_sprites, WINDOW_SIZE, self.map_data[2])
        ]
        # obstacles on the field
        for i, obstacle in enumerate(self.map_data[3]):
            self.obstacles.append(
                objects.Obstacle(self.all_sprites,
                                 WINDOW_SIZE,
                                 self.map_data[3][i],
                                 fill_color=pygame.Color("white")))

        self.draw_on_field()
コード例 #3
0
 def on_char_v2(self, char, x, y, layer):
     if char not in self.config:
         return
     x = x * self.tile_width
     y = y * self.tile_height
     if char[7:8] == '1':  # obstacle
         objects.Obstacle(self.game, x, y, self.config[char]).sides = [
             True, True, True, True
         ]
     else:
         objects.Scenery(self.game, x, y, self.config[char])
コード例 #4
0
    def make_map(self, level):
        """
        Creates all game objects. Then calls draw_on_field method to blit them to field. After that calls save_map
        function to save the map.
        """
        self.ball = objects.Ball(self.all_sprites, 10, self.map_data[0])
        self.cue = objects.Cue(self.all_sprites, self.ball.pos, max_vel=15)
        self.pocket = objects.Pocket(self.all_sprites, 10, self.map_data[1])
        # edges of the field
        self.obstacles = [
            objects.Obstacle(self.all_sprites, WINDOW_SIZE, self.map_data[2])
        ]
        # obstacles on the field
        for i, obstacle in enumerate(self.map_data[3]):
            self.obstacles.append(
                objects.Obstacle(self.all_sprites,
                                 WINDOW_SIZE,
                                 self.map_data[3][i],
                                 fill_color=pygame.Color("white")))

        self.draw_on_field()
        data.save_map(self.field.subsurface(self.obstacles[0].polygon_rect),
                      level)
コード例 #5
0
 def on_char(self, char, x, y, layer):
     gx, gy = x, y
     x = x * self.tile_width
     y = y * self.tile_height
     if char == "GRS":
         objects.Scenery(self.game, x, y, "grass.png")
     if char == "WTR":
         objects.Scenery(self.game, x, y, "water.png")
     if char == "SND":
         objects.Scenery(self.game, x, y, "sand.png")
     if char == "TRT":
         self.set_tile(objects.Scenery(self.game, x, y, "tree-top.png"))
     if char == "TRM":
         self.set_tile(objects.Scenery(self.game, x, y, "tree-middle.png"))
     if char == "TRB":
         self.set_tile(objects.Scenery(self.game, x, y, "tree-bottom.png"))
     if char == "HLL":
         objects.Scenery(self.game, x, y, "hill.png")
     if char == "BRV":
         objects.Obstacle(self.game, x, y, "bridge-vertical.png").sides = [
             False, True, False, True
         ]
     if char == "BRH":
         objects.Obstacle(
             self.game, x, y,
             "bridge-horizontal.png").sides = [True, False, True, False]
     if char == "CSL":
         objects.Obstacle(self.game, x, y, "castle.png")
     if char == "MTN":
         objects.Obstacle(self.game, x, y, "mountain-center.png")
     if char == "MNT":
         objects.Obstacle(self.game, x, y, "mountain-top.png")
     if char == "MNB":
         objects.Obstacle(self.game, x, y, "mountain-bottom.png")
     if char == "MNL":
         objects.Obstacle(self.game, x, y, "mountain-left.png")
     if char == "MNR":
         objects.Obstacle(self.game, x, y, "mountain-right.png")
     if char == "MTL":
         objects.Obstacle(self.game, x, y, "mountain-topleft.png")
     if char == "MTR":
         objects.Obstacle(self.game, x, y, "mountain-topright.png")
     if char == "MBL":
         objects.Obstacle(self.game, x, y, "mountain-bottomleft.png")
     if char == "MBR":
         objects.Obstacle(self.game, x, y, "mountain-bottomright.png")
     if char == "SHT":
         objects.Obstacle(self.game, x, y, "shore-top.png")
     if char == "SHB":
         objects.Obstacle(self.game, x, y, "shore-bottom.png")
     if char == "SHL":
         objects.Obstacle(self.game, x, y, "shore-left.png")
     if char == "SHR":
         objects.Obstacle(self.game, x, y, "shore-right.png")
     if char == "STL":
         objects.Obstacle(self.game, x, y, "shore-topleft.png")
     if char == "STR":
         objects.Obstacle(self.game, x, y, "shore-topright.png")
     if char == "SBL":
         objects.Obstacle(self.game, x, y, "shore-bottomleft.png")
     if char == "SBR":
         objects.Obstacle(self.game, x, y, "shore-bottomright.png")
     if char == "CLB":
         objects.Obstacle(self.game, x, y, "cliff-bottom.png")
     if char == "CLL":
         objects.Obstacle(self.game, x, y, "cliff-left.png").sides = [
             False, True, False, False
         ]
     if char == "CLR":
         objects.Obstacle(self.game, x, y, "cliff-right.png").sides = [
             False, False, False, True
         ]
     if char == "CTL":
         objects.Obstacle(self.game, x, y, "cliff-topleft.png").sides = [
             True, True, False, False
         ]
     if char == "CTR":
         objects.Obstacle(self.game, x, y, "cliff-topright.png").sides = [
             True, False, False, True
         ]
     if char == "CLT":
         objects.Obstacle(self.game, x, y, "cliff-top.png").sides = [
             True, False, False, False
         ]
     if char.startswith("CH"):
         id = int(char[-1:])
         objects.Chest(self.game, x, y, id)
     if char.startswith("NP"):
         id = int(char[-1:])
         name = self.info.npcs[id][0]
         npc = objects.NPC(self.game, x, y, name)
         if npc.name in self.game.npc_cache:
             npc.set_pos_to(self.game.npc_cache[npc.name])
             self.game.npc_cache[npc.name] = npc
         else:
             self.game.npc_cache[npc.name] = npc
     if char.startswith("IT"):
         id = int(char[-1:])
         name, id = self.info.items[id]
         if name in self.game.inventory:
             if self.game.inventory[name][0] == id:
                 return
         objects.Item(self.game, x, y, name, id)
コード例 #6
0
    def update(self, events):
        """Handles the events."""
        for event in events:
            if event.type == pygame.MOUSEMOTION:
                if self.stage == 0:
                    if len(self.obstacles) > self.obstacle_number \
                            and len(self.obstacles[self.obstacle_number].vertices) > 0:
                        start_pos = [
                            self.obstacles[self.obstacle_number].vertices[-1],
                            self.obstacles[self.obstacle_number].vertices[0]
                        ]
                        self.line_pos = [[start_pos[0], event.pos],
                                         [start_pos[1], event.pos]]
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.pos[1] < WINDOW_HEIGHT - 50 * 3 // 2:
                    if event.button == 1:
                        if self.stage == 0:
                            if len(self.obstacles) <= self.obstacle_number:
                                self.obstacles.append(
                                    objects.Obstacle(
                                        self.all_sprites, WINDOW_SIZE,
                                        np.array(event.pos, ndmin=2)))
                            else:
                                if self.obstacle_number:
                                    fill_color = pygame.Color("white")
                                else:
                                    fill_color = pygame.Color("#0060ff")
                                new_vertices = np.concatenate(
                                    (self.obstacles[self.obstacle_number].
                                     vertices, np.array(event.pos, ndmin=2)),
                                    axis=0)
                                self.obstacles[
                                    self.obstacle_number] = objects.Obstacle(
                                        self.all_sprites,
                                        WINDOW_SIZE,
                                        new_vertices,
                                        fill_color=fill_color)
                        elif self.stage == 1:
                            self.pocket = objects.Pocket(
                                self.all_sprites, 10, event.pos)
                        elif self.stage == 2:
                            self.ball = objects.Ball(self.all_sprites, 10,
                                                     event.pos)
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    if self.stage == 0 and len(self.obstacles) > 0 or \
                            self.stage == 1 and self.pocket is not None or\
                            self.stage == 2 and self.ball is not None:
                        self.stage += 1
                elif event.key == pygame.K_LEFT:
                    if self.stage >= 1:
                        self.stage -= 1
                elif event.key == pygame.K_SPACE:
                    if self.stage == 0 and len(
                            self.obstacles) > self.obstacle_number:
                        if len(self.obstacles[self.obstacle_number].vertices
                               ) > 2:  # if obstacle is at least a line
                            self.obstacle_number += 1
                            self.line_pos = []
                        else:
                            self.obstacles.pop()

        if self.stage == 3:
            data.save_level_data(self)
            data.save_map(
                self.field.subsurface(self.obstacles[0].polygon_rect),
                self.level)
            data.make_level_button_theme(self.level)
コード例 #7
0
ファイル: main.py プロジェクト: ernestchu/ACO
initialize(objects.num_ants, ants, foods, nests)
foods.draw(surface)
ants.draw(surface)
nests.draw(surface)
pg.display.update()

while True:
    for event in pg.event.get():
        if event.type == QUIT:
            pg.quit()
            sys.exit()
        elif event.type == pg.KEYDOWN:  #press blankspace to start/pause
            if event.key == pg.K_SPACE:
                start = not start
        elif event.type == pg.MOUSEBUTTONDOWN:
            obstacles.add(objects.Obstacle(event.pos, 50))
        elif event.type == pg.MOUSEMOTION:
            if event.buttons[0] == 1:
                obstacles.add(objects.Obstacle(event.pos, 50))
    if start:
        surface.blit(background, (0, 0))
        ants.update(foods, nests, pheromone_food, pheromone_nest, obstacles)
        pheromone_food.update(ants, 'found')
        pheromone_nest.update(ants, 'finding')
        ants.clear(surface, background)
        draw_pheromone(pheromone_food, surface, (0, 255, 0))
        draw_pheromone(pheromone_nest, surface, (255, 0, 0))
        foods.draw(surface)
        ants.draw(surface)
        nests.draw(surface)
        obstacles.draw(surface)