Ejemplo n.º 1
0
class PlantSprite(AnimalSprite):
    # Constants for the initial state of all PlantSprites
    IMAGE = pygame.image.load(os.path.join(sprites_dir, "plant.png"))
    HEALTH_BAR = HealthBar(100)
    AVG_SPEED = 0.2
    VISION = 4

    def __init__(self, world_map, GRID_LOCK, coordinates=None):
        """
        Create a PlantSprite object
        :param world_map: WorldMap Object
        :param coordinates: Array of coordinates [x,y]
        :param GRID_LOCK: Lock for screen (for threading)
        """
        ''' Take parameters, and Sprite Constants '''
        super(PlantSprite,
              self).__init__(world_map, PlantSprite.IMAGE, GRID_LOCK,
                             PlantSprite.HEALTH_BAR, PlantSprite.AVG_SPEED,
                             PlantSprite.VISION, coordinates)

        self.type = "plant"
        self.movable_terrain = world_map.get_all_land_tile_types()
        self.is_pollinated = False
        self.pollinate_timer = 0

    def spawn(self):
        """
        @Override
        :return:
        """
        AnimalSprite.spawn(self)
        self.tile.ignore_contents = True

    def move(self, target=None):
        """
        @Override
        Plants do not move, move changes plant pollination.
        :param target: meaningless to plants, just there to suppress warning
        :return: changes plant pollination on a timer right now
        """
        self.pollinate_timer += 1
        if self.pollinate_timer % 25 == 0:
            self.pollinate()
        self.tile.set_sprite(self)
        self.tile.ignore_contents = True
        self.display(self.tile)

    def pollinate(self):
        """
        Checks for pollination and either pollinates or de-pollinates accordingly
        """
        if not self.is_pollinated:
            self.image = pygame.image.load(
                os.path.join(sprites_dir, "plantpollinated.png"))
            self.is_pollinated = True
        elif self.is_pollinated:
            self.image = pygame.image.load(
                os.path.join(sprites_dir, "plant.png"))
            self.is_pollinated = False
Ejemplo n.º 2
0
class BearSprite(AnimalSprite):
    # Constants for the initial state of all BearSprites
    IMAGE = pygame.image.load(os.path.join(sprites_dir, "bear.png"))
    HEALTH_BAR = HealthBar(100)
    AVG_SPEED = 0.2
    VISION = 4
    PREY = ["fish"]

    def __init__(self, world_map, GRID_LOCK, coordinates=None):
        """
        Create a BearSprite object
        :param world_map: WorldMap Object
        :param coordinates: Array of coordinates [x,y]
        :param GRID_LOCK: Lock for screen (for threading)
        """
        ''' Take parameters, and Sprite Constants '''
        super(BearSprite,
              self).__init__(world_map, BearSprite.IMAGE, GRID_LOCK,
                             BearSprite.HEALTH_BAR, BearSprite.AVG_SPEED,
                             BearSprite.VISION, coordinates)

        self.type = "bear"
        self.prey = ["fish"]

    def move(self, target=None):
        """
        @Override
            Move sprite to a specified target tile (target).
            Otherwise, moves sprite to a random adjacent tile.
        :param target: Target tile to move sprite.
        """
        visible_tiles = vision.vision(8, self.world_map, self.tile)
        target_tile = vision.find_target(visible_tiles, self.prey)
        if target_tile:
            move_to_tile = vision.approach(self.tile, target_tile,
                                           self.world_map)
            if self.is_movable_terrain(
                    move_to_tile) and self.not_contains_sprite(
                        move_to_tile, self.prey):
                if move_to_tile == target_tile:
                    move_to_tile.contains_sprite.die()
                AnimalSprite.move(self, move_to_tile)
            else:
                AnimalSprite.move(self)
        else:
            AnimalSprite.move(self)

    def run(self):
        """
        @Override
            Runs the BearSprites's thread
        """
        self.spawn()
        while self.is_alive:
            self.move()
            if self.tile.name.startswith("water"):
                time.sleep(self.speed * 2)
            else:
                time.sleep(self.speed)
Ejemplo n.º 3
0
class BeesSprite(AnimalSprite):
    # Constants for the initial state of all BeesSprites
    IMAGE = pygame.image.load(os.path.join(sprites_dir, "bees.png"))
    HEALTH_BAR = HealthBar(100)
    AVG_SPEED = 0.2
    VISION = 4

    def __init__(self, world_map, GRID_LOCK, coordinates=None):
        """
        Create a BeesSprite object
        :param world_map: WorldMap Object
        :param coordinates: Array of coordinates [x,y]
        :param GRID_LOCK: Lock for screen (for threading)
        """
        ''' Take parameters, and Sprite Constants '''
        super(BeesSprite,
              self).__init__(world_map, BeesSprite.IMAGE, GRID_LOCK,
                             BeesSprite.HEALTH_BAR, BeesSprite.AVG_SPEED,
                             BeesSprite.VISION, coordinates)

        self.type = "bees"
        self.prey = ["plant"]

    def move(self, target=None):
        """
        @Override
            Move sprite to a specified target tile (target).
            Otherwise, moves sprite to a random adjacent tile.
        :param target: Target tile to move sprite.
        """
        visible_tiles = vision.vision(15, self.world_map, self.tile)
        visible_tiles = filter(BeesSprite.pollinated_filter, visible_tiles)
        target_tile = vision.find_target(visible_tiles, self.prey)
        if target_tile:
            move_to_tile = vision.approach(self.tile, target_tile,
                                           self.world_map)
            if self.is_movable_terrain(move_to_tile) and \
                    self.not_contains_sprite(move_to_tile, self.prey):
                if move_to_tile == target_tile:
                    move_to_tile.contains_sprite.pollinate()
                AnimalSprite.move(self, move_to_tile)
            else:
                AnimalSprite.move(self)
        else:
            AnimalSprite.move(self)

    @staticmethod
    def pollinated_filter(tile):
        """
        Determines if there is an unpollinated plant sprite on the given tile
        :param tile: Given tile in WorldMap object
        :return: False: if plant is pollinated
                 True: if plant is not pollinated
        """
        current_sprite = tile.contains_sprite
        if current_sprite and current_sprite.type == "plant" and current_sprite.is_pollinated:
            return False
        else:
            return True
Ejemplo n.º 4
0
 def prep_health(self):
     self.healths = Group()
     for health_left in range(self.stats.ship_health):
         print("health added")
         health = HealthBar(self.a_s_game)
         health.rect.x = 10 + health_left * health.rect.width
         health.rect.y = 80
         self.healths.add(health)
Ejemplo n.º 5
0
class HareSprite(AnimalSprite):
    # Constants for the initial state of all HareSprites
    IMAGE = pygame.image.load(os.path.join(sprites_dir, "hare.png"))
    HEALTH_BAR = HealthBar(100)
    AVG_SPEED = 0.1
    VISION = 2

    def __init__(self, world_map, GRID_LOCK, coordinates=None):
        """
        Create a HareSprite object
        :param world_map: WorldMap Object
        :param coordinates: Array of coordinates [x,y]
        :param GRID_LOCK: Lock for screen (for threading)
        """
        ''' Take parameters, and Sprite Constants '''
        super(HareSprite,
              self).__init__(world_map, HareSprite.IMAGE, GRID_LOCK,
                             HareSprite.HEALTH_BAR, HareSprite.AVG_SPEED,
                             HareSprite.VISION, coordinates)

        self.type = "hare"
        self.predators = ["wolf", "lynx"]
        self.prey = ["plant"]
        self.movable_terrain = world_map.get_all_land_tile_types()

    def move(self, target=None):
        visible_tiles = vision.vision(4, self.world_map, self.tile)
        flight_tile = vision.find_target(visible_tiles, self.predators)
        target_tile = vision.find_target(visible_tiles, self.prey)
        if flight_tile:
            move_to_tile = vision.flee(self.tile, flight_tile, self.world_map)
            if self.is_movable_terrain(
                    move_to_tile) and self.not_contains_sprite(move_to_tile):
                AnimalSprite.move(self, move_to_tile)
            else:
                AnimalSprite.move(self)
        elif target_tile:
            move_to_tile = vision.approach(self.tile, target_tile,
                                           self.world_map)
            if self.is_movable_terrain(
                    move_to_tile) and self.not_contains_sprite(
                        move_to_tile, self.prey):
                if move_to_tile == target_tile:
                    move_to_tile.contains_sprite.die()
                AnimalSprite.move(self, move_to_tile)
            else:
                AnimalSprite.move(self)
        else:
            AnimalSprite.move(self)
Ejemplo n.º 6
0
def main():
    """
    Sprite Implementation Example
    """
    from widgets.widget___tiled_map import WorldMap
    from properties import sprites_dir
    from health_bar import HealthBar
    import pygame
    import os, threading
    from threading import Thread
    pygame.init()

    # Map Setup
    screen = pygame.display.set_mode((800, 800))
    world_map = WorldMap("map2.tmx", (23, 23))
    world_map.render_entire_map()

    # Threading
    GRID_LOCK = threading.Lock()

    # Sprite Setup
    image_path = os.path.join(sprites_dir, "deer.png")
    image = pygame.image.load(image_path)

    # Create Healthbar
    health_bar = HealthBar(15)
    sprite = AnimalSprite(world_map, screen, image, (50, 50), GRID_LOCK,
                          health_bar, 5, 4)

    # Create Thread
    t = Thread(target=sprite.run)
    t.daemon = True

    # Run Sprite
    t.start()

    # Loop until Pygame exits
    done = False
    while not done:
        for event in pygame.event.get():  # User did something
            if event.type == pygame.QUIT:  # If user clicked close
                done = True
Ejemplo n.º 7
0
class SalmonSprite(AnimalSprite):
    # Constants for the initial state of all SalmonSprites
    IMAGE = pygame.image.load(os.path.join(sprites_dir, "salmon.png"))
    HEALTH_BAR = HealthBar(100)
    AVG_SPEED = 0.2
    VISION = 4

    def __init__(self, world_map, GRID_LOCK, coordinates=None):
        """
        Create a SalmonSprite object
        :param world_map: WorldMap Object
        :param coordinates: Array of coordinates [x,y]
        :param GRID_LOCK: Lock for screen (for threading)
        """
        ''' Take parameters, and Sprite Constants '''
        super(SalmonSprite,
              self).__init__(world_map, SalmonSprite.IMAGE, GRID_LOCK,
                             SalmonSprite.HEALTH_BAR, SalmonSprite.AVG_SPEED,
                             SalmonSprite.VISION, coordinates)

        self.type = "fish"
        self.movable_terrain = world_map.get_all_water_tile_types()
Ejemplo n.º 8
0
class TickSprite(AnimalSprite):
    # Constants for the initial state of a DeerSprite
    IMAGE = pygame.image.load(os.path.join(sprites_dir, "tick_corner.png"))
    HEALTH_BAR = HealthBar(100)
    AVG_SPEED = 0.2
    VISION = 4

    def __init__(self, world_map, GRID_LOCK, coordinates=None):
        """
        Create a TickSprite object
        :param world_map: WorldMap Object
        :param coordinates: Array of coordinates [x,y]
        :param GRID_LOCK: Lock for screen (for threading)
        """

        ''' Take parameters, and Sprite Constants '''
        super(TickSprite, self).__init__(world_map, TickSprite.IMAGE,
                                        GRID_LOCK, TickSprite.HEALTH_BAR,
                                        TickSprite.AVG_SPEED, TickSprite.VISION, coordinates)

        self.type = "tick"
        self.prey = ["deer"]
        self.movable_terrain = world_map.get_all_land_tile_types()

    def move(self):
        """
        :return:
        """
        visible_tiles = vision.vision(4, self.world_map, self.tile)
        target_tile = vision.find_target(visible_tiles, self.prey)
        if target_tile:
            move_to_tile = vision.approach(self.tile, target_tile, self.world_map)
            if self.is_movable_terrain(move_to_tile) and self.not_contains_sprite(move_to_tile, self.prey):
                AnimalSprite.move(self, move_to_tile)
            else:
                AnimalSprite.move(self)
        else:
            AnimalSprite.move(self)
Ejemplo n.º 9
0
    def __init__(self, screen, asteroidController,
                 bulletController, ship, stats):

        self.screen = screen

        self.asteroidController = asteroidController
        self.bulletController = bulletController
        self.ship = ship
        self.gameStats = stats

        self.visuals = []

        # Add bullet guage visual.
        self.visuals.append(BulletGuage(self.screen, self.bulletController))

        # Add health bar visual.
        self.visuals.append(HealthBar(self.screen, self.ship))

        # Add scoreboard.
        self.visuals.append(Scoreboard(self.screen, self.gameStats))

        # Add level text.
        self.visuals.append(LevelText(self.screen, self.gameStats))
Ejemplo n.º 10
0
    def __init__(self):
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode(
            (self.settings.screen_width, self.settings.screen_height))

        pygame.display.set_caption("Alien Shooter")

        # Create an instance to store game statistics
        self.stats = GameStats(self)
        self.sb = Scoreboard(self)

        self.ship = Ship(self)
        self.health_bar = HealthBar(self)
        self.bullets = pygame.sprite.Group()
        self.aliens = pygame.sprite.Group()

        # Set the background color
        self.bg_color = (230, 230, 230)
        self._create_fleet()

        # Make the play Button
        self.play_button = Button(self, "Play Game")
Ejemplo n.º 11
0
class EagleSprite(AnimalSprite):
    # Constants for the initial state of all EagleSprites
    IMAGE = pygame.image.load(os.path.join(sprites_dir, "eagle.png"))
    SHADOW_IMAGE = pygame.image.load(
        os.path.join(sprites_dir, "eagle_shadow.png"))
    HEALTH_BAR = HealthBar(100)
    AVG_SPEED = 0.2
    VISION = 15
    PREY = ["fish"]

    def __init__(self, world_map, GRID_LOCK, coordinates=None):
        """
        Create a EagleSprite object
        :param world_map: WorldMap Object
        :param coordinates: Array of coordinates [x,y]
        :param GRID_LOCK: Lock for screen (for threading)
        """
        ''' Take parameters, and Sprite Constants '''
        super(EagleSprite,
              self).__init__(world_map, EagleSprite.IMAGE, GRID_LOCK,
                             EagleSprite.HEALTH_BAR, EagleSprite.AVG_SPEED,
                             EagleSprite.VISION, coordinates)

        self.type = "eagle"
        self.prey = ["fish"]
        self.movable_terrain = world_map.tile_types
        self.shadow = self.SHADOW_IMAGE
        self.shadow_tile = self.world_map.get_tile_by_index(
            (self.tile.location_t[1] + 1, self.tile.location_t[0]))

    def move(self, target=None):
        """
        @Override
            Move sprite to a specified target tile (target).
            Otherwise, moves sprite to a random adjacent tile.
        :param target: Target tile to move sprite.
        """
        visible_tiles = vision.vision(self.vision, self.world_map, self.tile)
        target_tile = vision.find_target(visible_tiles, self.prey)
        if target_tile:
            move_to_tile = vision.approach(self.tile, target_tile,
                                           self.world_map)
            if self.is_movable_terrain(
                    move_to_tile) and self.not_contains_sprite(
                        move_to_tile, self.prey):
                if move_to_tile == target_tile:
                    move_to_tile.contains_sprite.die()
                AnimalSprite.move(self, move_to_tile)
            else:
                AnimalSprite.move(self)
        else:
            AnimalSprite.move(self)

    def run(self):
        """
        @Override
            Runs the EagleSprites's thread
        """
        self.spawn()
        while self.is_alive:
            self.move()
            time.sleep(.2)
Ejemplo n.º 12
0
class WolfSprite(AnimalSprite):
    # Constants for the initial state of a DeerSprite
    IMAGE = pygame.image.load(os.path.join(sprites_dir, "wolf.png"))
    HEALTH_BAR = HealthBar(100)
    AVG_SPEED = 0.2
    VISION = 4

    def __init__(self, world_map, GRID_LOCK, coordinates=None):
        """
        Create a WolfSprite object
        :param world_map: WorldMap Object
        :param coordinates: Array of coordinates [x,y]
        :param GRID_LOCK: Lock for screen (for threading)
        """
        ''' Take parameters, and Sprite Constants '''
        super(WolfSprite,
              self).__init__(world_map, WolfSprite.IMAGE, GRID_LOCK,
                             WolfSprite.HEALTH_BAR, WolfSprite.AVG_SPEED,
                             WolfSprite.VISION, coordinates)

        self.type = "wolf"
        self.prey = ["deer", "hare"]
        self.movable_terrain = world_map.get_all_land_tile_types()
        self.is_leader = False

    def move(self, target=None):
        """

        :return:
        """
        visible_tiles = vision.vision(10, self.world_map, self.tile)
        target_tile = vision.find_target(visible_tiles, self.prey)
        if target_tile:
            move_to_tile = vision.approach(self.tile, target_tile,
                                           self.world_map)
            if self.is_movable_terrain(
                    move_to_tile) and self.not_contains_sprite(
                        move_to_tile, self.prey):
                if move_to_tile == target_tile:
                    move_to_tile.contains_sprite.die()
                AnimalSprite.move(self, move_to_tile)
            elif not self.is_leader:
                leader_tile = self.find_leader()
                if leader_tile:
                    move_to_tile = vision.approach(self.tile, leader_tile,
                                                   self.world_map)
                    if self.is_movable_terrain(
                            move_to_tile) and self.not_contains_sprite(
                                move_to_tile, self.prey):
                        AnimalSprite.move(self, move_to_tile)
                else:
                    AnimalSprite.move(self)
            else:
                AnimalSprite.move(self)
        elif not self.is_leader:
            leader_tile = self.find_leader()
            if leader_tile:
                chance = random.randint(0, 10)
                if chance == 0:
                    AnimalSprite.move(self)
                else:
                    move_to_tile = vision.approach(self.tile, leader_tile,
                                                   self.world_map)
                    if self.is_movable_terrain(
                            move_to_tile) and self.not_contains_sprite(
                                move_to_tile, self.prey):
                        AnimalSprite.move(self, move_to_tile)
            else:
                AnimalSprite.move(self)
        else:
            AnimalSprite.move(self)

    def find_leader(self):
        for row in self.world_map.tiles:
            for tile in row:
                sprite = tile.contains_sprite
                if sprite is not None:
                    if sprite.type == "wolf" and sprite.is_leader:
                        tiles = self.world_map.get_surrounding_movable_tiles(
                            tile)
                        index = random.randint(0, len(tiles) - 1)
                        return tiles[index]
        return False