Esempio n. 1
0
    def update(self, delta_time):
        """Update weapon."""
        self.time_since_last_shot += delta_time

        if self.firing:
            self.elapsed_t += delta_time

        if self.elapsed_t >= self.firerate:
            self.elapsed_t -= self.firerate
            if self.ammo > 0:
                if self.owner.snake.heading is not None:
                    head = None
                    heading = None
                    if self.emitter == HEAD:
                        head = self.owner.snake[0]
                        heading = self.owner.snake.heading
                    elif self.emitter == TAIL:
                        head = self.owner.snake[len(self.owner.snake.body)-1]
                        heading = (-self.owner.snake.heading[0],
                                   -self.owner.snake.heading[1])

                    if (add_vecs(head, mul_vec(heading, 1)) not in
                            self.game.tilemap.tiles and head not in
                            self.game.tilemap.tiles):
                        self.ammo -= 1
                        self.game.shot_manager.create_shot(
                            add_vecs(head, mul_vec(heading, 2)),
                            heading, self.owner.snake.head_tag,
                            self.shot)
                    self.time_since_last_shot = 0
            else:
                self.firing = False
Esempio n. 2
0
    def update(self, delta_time):
        """Update weapon."""
        self.time_since_last_shot += delta_time

        if self.firing:
            self.elapsed_t += delta_time

        if self.elapsed_t >= self.firerate:
            self.elapsed_t -= self.firerate
            if self.ammo > 0:
                if self.owner.snake.heading is not None:
                    head = None
                    heading = None
                    if self.emitter == HEAD:
                        head = self.owner.snake[0]
                        heading = self.owner.snake.heading
                    elif self.emitter == TAIL:
                        head = self.owner.snake[len(self.owner.snake.body) - 1]
                        heading = (-self.owner.snake.heading[0],
                                   -self.owner.snake.heading[1])

                    if (add_vecs(head, mul_vec(
                            heading, 1)) not in self.game.tilemap.tiles
                            and head not in self.game.tilemap.tiles):
                        self.ammo -= 1
                        self.game.shot_manager.create_shot(
                            add_vecs(head, mul_vec(heading, 2)), heading,
                            self.owner.snake.head_tag, self.shot)
                    self.time_since_last_shot = 0
            else:
                self.firing = False
Esempio n. 3
0
    def __init__(self, tilemap):
        self.rows = tilemap.height
        self.cols = tilemap.width
        self.blocked = set(tilemap.tiles)
        self.tilemap = tilemap
        self.open_lst = list()
        self.clsd_lst = set()
        self.nodes = [None] * self.cols
        self.nodes = [[None] * self.rows for _ in self.nodes]

        penalty = defaultdict(int)

        for tile in tilemap.tiles:
            for adjacent in get_adjacent(tile, self.cols, self.rows):
                if adjacent in self.blocked:
                    continue

                spread_dir = sub_vecs(adjacent, tile)
                spread_count = 1
                spread = add_vecs(tile, spread_dir)

                while (spread not in tilemap.tiles
                       and not tilemap.on_edge(spread)
                       and spread_count < WALL_PENALTY_MAX_SPREAD):
                    penalty[spread] += 12 / spread_count

                    spread = add_vecs(spread, spread_dir)
                    spread_count += 1

        for xpos in range(self.cols):
            for ypos in range(self.rows):
                pos = (xpos, ypos)
                pen = penalty.get((xpos, ypos), 0)
                self.nodes[xpos][ypos] = Node(pos, pos in self.blocked, pen)
Esempio n. 4
0
    def __init__(self, tilemap):
        self.rows = tilemap.height
        self.cols = tilemap.width
        self.blocked = set(tilemap.tiles)
        self.tilemap = tilemap
        self.open_lst = list()
        self.clsd_lst = set()
        self.nodes = [None] * self.cols
        self.nodes = [[None] * self.rows for _ in self.nodes]

        penalty = defaultdict(int)

        for tile in tilemap.tiles:
            for adjacent in get_adjacent(tile, self.cols, self.rows):
                if adjacent in self.blocked:
                    continue

                spread_dir = sub_vecs(adjacent, tile)
                spread_count = 1
                spread = add_vecs(tile, spread_dir)

                while (spread not in tilemap.tiles
                       and not tilemap.on_edge(spread)
                       and spread_count < WALL_PENALTY_MAX_SPREAD):
                    penalty[spread] += 12 / spread_count

                    spread = add_vecs(spread, spread_dir)
                    spread_count += 1

        for xpos in range(self.cols):
            for ypos in range(self.rows):
                pos = (xpos, ypos)
                pen = penalty.get((xpos, ypos), 0)
                self.nodes[xpos][ypos] = Node(pos, pos in self.blocked, pen)
Esempio n. 5
0
 def draw(self, tex_name, pos, gridcoords=True, offset=(0, PANEL_H)):
     """Draw a texture."""
     if tex_name not in self.textures:
         raise Exception('No such texture: {0}'.format(tex_name))
     if gridcoords:
         self.surf.blit(self.textures[tex_name],
                        add_vecs(mul_vec(pos, CELL_SIZE), offset))
     else:
         self.surf.blit(self.textures[tex_name], add_vecs(pos, offset))
Esempio n. 6
0
    def draw(self, pos, width):
        WidgetBase.draw(self, pos, width)

        offset = (self.brd_thickness, self.brd_thickness)
        offset = add_vecs(offset, (100, 10))

        for snake_pos, skin_area in self.fake_snake:
            self.game.graphics.draw(self.skin, add_vecs(snake_pos, pos),
                                    gridcoords=False, area=skin_area,
                                    offset=offset)
Esempio n. 7
0
    def draw(self, offset):
        """Draw snake and UI."""
        self.snake.draw()
        self.game.draw_string('Player{0}'.format(self.pid),
                              add_vecs((2, 2), offset), self.color)
        self.game.draw_string('{0:.2f}'.format(self.snake.speed),
                              add_vecs((56, 2), offset), WHITE)
        self.game.draw_string('Points: {0}'.format(self.points),
                              add_vecs((2, 18), offset), WHITE)

        pygame.draw.rect(self.game.screen, ORANGE,
                         pygame.Rect(add_vecs((100, 2), offset), (104, 20)))

        pygame.draw.rect(self.game.screen, RED,
                         pygame.Rect(add_vecs((102, 4), offset), (int(
                             self.snake.hitpoints /
                             float(MAX_HITPOINTS) * 100), 7)))

        pygame.draw.rect(self.game.screen, BLUE,
                         pygame.Rect(add_vecs((102, 13), offset), (int(
                             self.boost / float(MAX_BOOST) * 100), 7)))

        self.game.draw_string('{0} {1}'.format(self.weapons[0].wtype,
                                               self.weapons[0].ammo),
                              add_vecs((208, 2), offset), WHITE)

        for i in range(self.lifes):
            self.game.graphics.draw('life16x16', add_vecs((100, 24), offset),
                                    gridcoords=False, offset=(i*18, 0))
Esempio n. 8
0
    def handle_collisions(self):
        """Handle collisions."""
        for player in self.players:
            if player.snake[0] in self.tilemap.tiles:
                player.snake.take_damage(20,
                                         WALL_TAG,
                                         True,
                                         True,
                                         1,
                                         shrink=0,
                                         slowdown=0.07)
                break

        for shot in self.game.shot_manager.shot_pool:
            if shot.pos in self.tilemap.tiles:
                shot.hit()
            if shot.pos in self.tilemap.portals:
                shot.heading = self.tilemap.portals[shot.pos][1]
                shot.pos = add_vecs(self.tilemap.portals[shot.pos][0],
                                    shot.heading)

        for entries in self.spatialhash.values():
            if len(entries) > 1:
                for player in self.players:
                    for entry in entries:
                        if entry[0] == player.snake.head_tag:
                            player.coll_check_head(entries)
                        if entry[0] == player.snake.body_tag:
                            player.coll_check_body(entries)
Esempio n. 9
0
 def coll_check_head(self, collobjs):
     """Handle collisions for the snake's head."""
     for tag, obj in collobjs:
         if (tag.endswith('-body') or tag.endswith('-head')) and \
                 tag != self.snake.head_tag:
             obj.take_damage(35,
                             self.snake.head_tag,
                             False,
                             True,
                             0.7,
                             shrink=1,
                             slowdown=0.03)
         elif tag == PORTAL_TAG:
             self.snake.heading = obj[1]
             self.snake[0] = add_vecs(obj[0], self.snake.heading)
         elif tag == PWRUP_TAG:
             for action in obj.actions:
                 target = self.pwrup_targets[action['target']]
                 if '.' in target:
                     target1, target2 = target.split('.')
                     attr = getattr(getattr(self, target1), target2)
                     setattr(getattr(self, target1), target2,
                             attr + action['value'])
                 else:
                     attr = getattr(self, target)
                     setattr(self, target, attr + action['value'])
             obj.collect()
         elif tag == SHOT_TAG:
             self.handle_shot(obj)
Esempio n. 10
0
def main():
    parser = argparse.ArgumentParser(description='Pack skin textures')
    parser.add_argument('src')
    parser.add_argument('dst')
    parser.add_argument('-u',
                        dest='unpack',
                        const=True,
                        type=bool,
                        default=False,
                        nargs="?")

    args = parser.parse_args()

    src_img = image.load(args.src)

    if args.unpack:
        dst_img = Surface((10, 50), flags=SRCALPHA)

        dst_img.blit(src_img, (0, 00), HEAD)
        dst_img.blit(src_img, (0, 10), STRAIGHT1)
        dst_img.blit(src_img, (0, 20), STRAIGHT2)
        dst_img.blit(src_img, (0, 30), TAIL)
        dst_img.blit(src_img, (0, 40), TURN)
    else:
        dst_img = Surface((40, 40), flags=SRCALPHA)

        single_tile = Surface((10, 10), flags=SRCALPHA)

        seq = ((HEAD, (0, 0)), (TAIL, (0, 30)), (TURN, (0, 40)))
        for tile, tile_pos in seq:
            single_tile.fill((0, 0, 0, 0))
            single_tile.blit(src_img, (0, 0), Rect(tile_pos, (10, 10)))

            for rot, offset in ROT_OFFSET.items():
                pos = add_vecs(tile.topleft, offset)
                dst_img.blit(transform.rotate(single_tile, rot), pos)

        for tile, tile_pos in ((STRAIGHT1, (0, 10)), (STRAIGHT2, (0, 20))):
            single_tile.fill((0, 0, 0, 0))
            single_tile.blit(src_img, (0, 0), Rect(tile_pos, (10, 10)))

            dst_img.blit(single_tile, tile)

            pos = add_vecs(tile.topleft, (0, 10))
            dst_img.blit(transform.rotate(single_tile, -90), pos)

    image.save(dst_img, args.dst)
Esempio n. 11
0
 def draw(self,
          tex_name,
          pos,
          gridcoords=True,
          offset=(0, PANEL_H),
          area=None):
     """Draw a texture."""
     if tex_name not in self.textures:
         raise Exception('No such texture: {0}'.format(tex_name))
     if gridcoords:
         self.surf.blit(self.textures[tex_name],
                        add_vecs(mul_vec(pos, CELL_SIZE), offset),
                        area=area)
     else:
         self.surf.blit(self.textures[tex_name],
                        add_vecs(pos, offset),
                        area=area)
Esempio n. 12
0
def main():
    parser = argparse.ArgumentParser(description='Pack skin textures')
    parser.add_argument('src')
    parser.add_argument('dst')
    parser.add_argument('-u', dest='unpack', const=True,
                        type=bool, default=False, nargs="?")

    args = parser.parse_args()

    src_img = image.load(args.src)

    if args.unpack:
        dst_img = Surface((10, 50), flags=SRCALPHA)

        dst_img.blit(src_img, (0, 00), HEAD)
        dst_img.blit(src_img, (0, 10), STRAIGHT1)
        dst_img.blit(src_img, (0, 20), STRAIGHT2)
        dst_img.blit(src_img, (0, 30), TAIL)
        dst_img.blit(src_img, (0, 40), TURN)
    else:
        dst_img = Surface((40, 40), flags=SRCALPHA)

        single_tile = Surface((10, 10), flags=SRCALPHA)

        seq = ((HEAD, (0, 0)), (TAIL, (0, 30)), (TURN, (0, 40)))
        for tile, tile_pos in seq:
            single_tile.fill((0, 0, 0, 0))
            single_tile.blit(src_img, (0, 0), Rect(tile_pos, (10, 10)))

            for rot, offset in ROT_OFFSET.items():
                pos = add_vecs(tile.topleft, offset)
                dst_img.blit(transform.rotate(single_tile, rot), pos)

        for tile, tile_pos in ((STRAIGHT1, (0, 10)),
                               (STRAIGHT2, (0, 20))):
            single_tile.fill((0, 0, 0, 0))
            single_tile.blit(src_img, (0, 0), Rect(tile_pos, (10, 10)))

            dst_img.blit(single_tile, tile)

            pos = add_vecs(tile.topleft, (0, 10))
            dst_img.blit(transform.rotate(single_tile, -90), pos)

    image.save(dst_img, args.dst)
Esempio n. 13
0
    def draw(self, pos, width):
        WidgetBase.draw(self, pos, width)

        num_chars = width / self.game.graphics.get_size('a')[0]

        xpos, ypos = add_vecs(pos,
                              (self.brd_thickness, self.brd_thickness))
        for line in textwrap.wrap(self.text, width=num_chars):
            self.game.graphics.draw_string((xpos, ypos), line, WHITE)
            ypos += self.game.graphics.get_height()
Esempio n. 14
0
    def update(self, delta_time):
        """Update shot."""
        self.elapsed_t += delta_time
        self.elapsed_blink += delta_time
        self.elapsed_lifetime += delta_time

        if self.elapsed_t >= self.speed:
            self.elapsed_t -= self.speed
            self.pos = add_vecs(self.pos, self.heading)
            self.pos = self.game.tilemap.wrap_around(self.pos)

        if self.elapsed_blink >= self.blinkrate:
            self.elapsed_blink -= self.blinkrate
            self.isvisible = not self.isvisible

        if self.elapsed_lifetime >= self.lifetime:
            self.hit()
Esempio n. 15
0
    def draw(self, pos, width):
        border_color = self.brd_color

        if not self.enabled:
            border_color = WIDGET_DISABLED_BORDER_COLOR

        # Draw border
        border = pygame.Rect(pos, (width, self.height))
        pygame.draw.rect(self.game.screen, border_color, border)

        # Draw background
        bg_size = (border.width - self.brd_thickness * 2,
                   border.height - self.brd_thickness * 2)
        bg_pos = add_vecs(pos,
                         (self.brd_thickness, self.brd_thickness))
        background = pygame.Rect(bg_pos, bg_size)
        pygame.draw.rect(self.game.screen, self.bg_color, background)
Esempio n. 16
0
    def update(self, delta_time):
        """Update shot."""
        self.elapsed_t += delta_time
        self.elapsed_blink += delta_time
        self.elapsed_lifetime += delta_time

        if self.elapsed_t >= self.speed:
            self.elapsed_t -= self.speed
            self.pos = add_vecs(self.pos, self.heading)
            self.pos = self.game.tilemap.wrap_around(self.pos)

        if self.elapsed_blink >= self.blinkrate:
            self.elapsed_blink -= self.blinkrate
            self.isvisible = not self.isvisible

        if self.elapsed_lifetime >= self.lifetime:
            self.hit()
Esempio n. 17
0
    def draw(self, pos, width):
        WidgetBase.draw(self, pos, width)

        text_color = self.text_color

        if not self.enabled:
            text_color = TEXT_WIDGET_DISABLED_TEXT_COLOR
        elif self.focus_enabled and self.focus:
            text_color = self.text_color_focus

        size = self.game.graphics.get_size(self.text, big=self.big_text)

        center = (width / 2 - size[0] / 2,
                  self.height / 2 - size[1] / 2)
        self.game.graphics.draw_string(add_vecs(center, pos),
                                       self.text,
                                       text_color,
                                       big=self.big_text)
Esempio n. 18
0
    def draw(self, offset):
        """Draw snake and UI."""
        if not self.snake:
            return

        self.snake.draw()

        gfx = self.game.graphics

        gfx.draw_string(add_vecs((2, 2), offset), 'Player{0}'.format(self.pid),
                        self.color)

        gfx.draw_string(add_vecs((56, 2), offset),
                        '{0:.2f}'.format(self.snake.speed), WHITE)

        gfx.draw_string(add_vecs((2, 18), offset),
                        'Points: {0}'.format(self.points), WHITE)

        pygame.draw.rect(self.game.screen, ORANGE,
                         pygame.Rect(add_vecs((100, 2), offset), (104, 20)))

        # Draw life bar. TODO: Make this some kinda class.
        width = int(self.snake.hitpoints / float(MAX_HITPOINTS) * 100)
        rect = pygame.Rect(add_vecs((102, 4), offset), (width, 7))
        pygame.draw.rect(self.game.screen, RED, rect)

        if self.boost_enabled:
            width = int(self.boost / float(MAX_BOOST) * 100)
            rect = pygame.Rect(add_vecs((102, 13), offset), (width, 7))
            pygame.draw.rect(self.game.screen, BLUE, rect)

        gfx.draw_string(
            add_vecs((208, 2), offset),
            '{0} {1}'.format(self.weapons[0].wtype,
                             self.weapons[0].ammo), WHITE)

        for index in range(self.lifes):
            gfx.draw('life16x16',
                     add_vecs((100, 24), offset),
                     gridcoords=False,
                     offset=(index * 18, 0))
Esempio n. 19
0
    def draw(self, offset):
        """Draw snake and UI."""
        if not self.snake:
            return

        self.snake.draw()

        gfx = self.game.graphics

        gfx.draw_string(add_vecs((2, 2), offset),
                        'Player{0}'.format(self.pid), self.color)

        gfx.draw_string(add_vecs((56, 2), offset),
                        '{0:.2f}'.format(self.snake.speed), WHITE)

        gfx.draw_string(add_vecs((2, 18), offset),
                        'Points: {0}'.format(self.points), WHITE)

        pygame.draw.rect(self.game.screen, ORANGE,
                         pygame.Rect(add_vecs((100, 2), offset),
                                     (104, 20)))

        # Draw life bar. TODO: Make this some kinda class.
        width = int(self.snake.hitpoints / float(MAX_HITPOINTS) * 100)
        rect = pygame.Rect(add_vecs((102, 4), offset), (width, 7))
        pygame.draw.rect(self.game.screen, RED, rect)

        if self.boost_enabled:
            width = int(self.boost / float(MAX_BOOST) * 100)
            rect = pygame.Rect(add_vecs((102, 13), offset), (width, 7))
            pygame.draw.rect(self.game.screen, BLUE, rect)

        gfx.draw_string(add_vecs((208, 2), offset),
                        '{0} {1}'.format(self.weapons[0].wtype,
                                         self.weapons[0].ammo), WHITE)

        for index in range(self.lifes):
            gfx.draw('life16x16', add_vecs((100, 24), offset),
                     gridcoords=False, offset=(index*18, 0))
Esempio n. 20
0
    def move(self):
        """Move snake."""
        if not self.ismoving:
            return

        self.body[0] = self.game.tilemap.wrap_around(self.body[0])
        # Move Snake
        if self.elapsed_t >= 1. / (self._speed + self._speed_bonus):
            self.prev = self.body[:]
            self.elapsed_t -= 1. / (self._speed + self._speed_bonus)
            self.body.insert(0, add_vecs(self.body[0], self.heading))
            if self.grow == 0:
                self.body.pop()
            elif self.grow > 0:
                self.grow -= 1
            elif self.grow < 0:
                for _ in range(-self.grow+1):
                    if len(self.body) == 2:
                        break
                    self.body.pop()
                self.grow = 0
Esempio n. 21
0
    def move(self):
        """Move snake."""
        if not self.ismoving:
            return

        self.body[0] = self.game.tilemap.wrap_around(self.body[0])
        # Move Snake
        if self.elapsed_t >= 1. / (self._speed + self._speed_bonus):
            self.prev = self.body[:]
            self.elapsed_t -= 1. / (self._speed + self._speed_bonus)
            self.body.insert(0, add_vecs(self.body[0], self.heading))
            if self.grow == 0:
                self.body.pop()
            elif self.grow > 0:
                self.grow -= 1
            elif self.grow < 0:
                for _ in range(-self.grow + 1):
                    if len(self.body) == 2:
                        break
                    self.body.pop()
                self.grow = 0
Esempio n. 22
0
    def find_path(self, start_pos, dest_pos):
        tilemap = self.game.tilemap

        for island in tilemap.islands:
            if island.contains_target(start_pos, dest_pos):
                portal1 = island.get_closest_portal(start_pos)
                portal2 = tilemap.portals[portal1][0]
                offset2 = add_vecs(portal2, tilemap.portals[portal1][1])

                blocked = set(tilemap.portals.keys())
                blocked -= {portal1}

                path1 = self.astar.find_path(start_pos, portal1)
                path2 = self.astar.find_path(offset2, dest_pos, blocked)

                path2.append(offset2)

                return path2 + path1

        blocked = set(tilemap.portals.keys())

        return self.astar.find_path(start_pos, dest_pos, blocked)
Esempio n. 23
0
    def find_path(self, start_pos, dest_pos):
        tilemap = self.game.tilemap

        for island in tilemap.islands:
            if island.contains_target(start_pos, dest_pos):
                portal1 = island.get_closest_portal(start_pos)
                portal2 = tilemap.portals[portal1][0]
                offset2 = add_vecs(portal2, tilemap.portals[portal1][1])

                blocked = set(tilemap.portals.keys())
                blocked -= {portal1}

                path1 = self.astar.find_path(start_pos, portal1)
                path2 = self.astar.find_path(offset2, dest_pos, blocked)

                path2.append(offset2)

                return path2 + path1

        blocked = set(tilemap.portals.keys())

        return self.astar.find_path(start_pos, dest_pos, blocked)
Esempio n. 24
0
    def handle_collisions(self):
        """Handle collisions."""
        for player in self.players:
            if player.snake[0] in self.tilemap.tiles:
                player.snake.take_damage(20, WALL_TAG, True, True, 1,
                                         shrink=0, slowdown=0.07)
                break

        for shot in self.game.shot_manager.shot_pool:
            if shot.pos in self.tilemap.tiles:
                shot.hit()
            if shot.pos in self.tilemap.portals:
                shot.heading = self.tilemap.portals[shot.pos][1]
                shot.pos = add_vecs(self.tilemap.portals[shot.pos][0],
                                    shot.heading)

        for entries in self.spatialhash.values():
            if len(entries) > 1:
                for player in self.players:
                    for entry in entries:
                        if entry[0] == player.snake.head_tag:
                            player.coll_check_head(entries)
                        if entry[0] == player.snake.body_tag:
                            player.coll_check_body(entries)
Esempio n. 25
0
 def coll_check_head(self, collobjs):
     """Handle collisions for the snake's head."""
     for tag, obj in collobjs:
         if (tag.endswith('-body') or tag.endswith('-head')) and \
                 tag != self.snake.head_tag:
             obj.take_damage(35, self.snake.head_tag, False, True,
                             0.7, shrink=1, slowdown=0.03)
         elif tag == PORTAL_TAG:
             self.snake.heading = obj[1]
             self.snake[0] = add_vecs(obj[0], self.snake.heading)
         elif tag == PWRUP_TAG:
             for action in obj.actions:
                 target = self.pwrup_targets[action['target']]
                 if '.' in target:
                     target1, target2 = target.split('.')
                     attr = getattr(getattr(self, target1), target2)
                     setattr(getattr(self, target1),
                             target2, attr + action['value'])
                 else:
                     attr = getattr(self, target)
                     setattr(self, target, attr + action['value'])
             obj.collect()
         elif tag == SHOT_TAG:
             self.handle_shot(obj)