Example #1
0
    def __init__(self, playerNumber=1):
        """Init HalfDisplaySprite.

        Instance variables:
            spriteSheet: The SpriteSheet object for the display sprite sheet image.he bonus sprite sheet image.
            image: The current image to be drawn for the sprite. Always is the one image from spriteSheet.
            coordinates: A tuple location to blit the sprite on the screen.
        """
        super().__init__(c.displayGroup)
        spriteSheet = SpriteSheet("display.png")
        if isinstance(PlayerSprite.currentLevel, lvl.BoardTwoLevel):
            self.image = spriteSheet.getSheetImage(250, 680, 250, 146)
        elif isinstance(PlayerSprite.currentLevel, lvl.BoardThreeLevel):
            self.image = spriteSheet.getSheetImage(500, 680, 250, 146)
        elif isinstance(PlayerSprite.currentLevel, lvl.BoardFourLevel):
            self.image = spriteSheet.getSheetImage(0, 826, 250, 146)
        elif isinstance(PlayerSprite.currentLevel, lvl.BoardFiveLevel):
            self.image = spriteSheet.getSheetImage(250, 826, 250, 146)
        else:
            self.image = spriteSheet.getSheetImage(0, 680, 250, 146)

        if playerNumber == 1:
            self.coordinates = (4, 484)
        elif playerNumber == 2:
            self.coordinates = (260, 484)
        elif playerNumber == 3:
            self.coordinates = (4, 708)
        else:
            self.coordinates = (260, 708)
        self.image.set_colorkey(c.RED)
Example #2
0
    def __init__(self):
        """Init GoldSprite.

        Instance variables:
            spriteSheet: The SpriteSheet object for the gold sprite sheet image.
                If the current level is an instance of the BonusLevel class, uses the bonus sprite sheet image.
            animationFrames: A list of 8 Surface objects from the SpriteSheet object.
            coordinates: A tuple location to blit the sprite on the screen.
            goldState: An OtherStates Enum instance of the current state of the sprite.
                Used to determine which methods get called and when.
            passingDirection: A Directions Enum instance of the direction the player was facing upon colliding
                with this sprite.
                Should only be one of the four cardinal directions. Setting this to CLOCKWISE or COUNTER will
                cause unexpected and undesired results.
            isHorizontal: A boolean indicating if the sprite should be rotated 270 degrees or not.
            alreadyRevealed: A boolean indicating if the sprite has already been revealed by a player.
                Players only earn points from colliding with gold sprites that have not already been revealed.
            frameCount: An integer that increases whenever the update method is called.
            animationCount: An integer tracking from where in animationFrame the sprite should take its next
                image.
            flashImage: A Surface object, showing the gold flashing brightly.
            pointsImage: A Surface object, showing the 100 points earned from first revealing a gold sprite.
                The GoldSprite class never uses this image directly, but passes it to the PointsSprite class
                when creating a new instance of the latter.
            emptyImage: A Surface object, showing a fully-transparent blank image.
                Used when the sprite should not be visibly drawn onscreen.
            image: The current image to be drawn for the sprite.
                Defaults to the emptyImage.
            rect: A rect object for the sprite.
            collisionRect: A smaller rect object used for checking collision between this sprite and others.
                This creates a better visual for collision than using the main rect object.
        """
        super().__init__(c.goldGroup)
        if isinstance(PlayerSprite.currentLevel, BonusLevel):
            spriteSheet = SpriteSheet("gold_bonus.png")
        else:
            spriteSheet = SpriteSheet("gold.png")
        self.animationFrames = []

        self.coordinates = (0, 0)
        self.goldState = c.OtherStates.OFF_SCREEN
        self.passingDirection = c.Directions.RIGHT
        self.isHorizontal = self.alreadyRevealed = False
        self.frameCount = self.animationCount = 0

        self.animationFrames.extend(spriteSheet.getStripImages(0, 0, 34, 34))
        self.animationFrames.extend(spriteSheet.getStripImages(0, 34, 34, 34))
        self.flashImage = spriteSheet.getSheetImage(0, 68, 34, 34)
        self.pointsImage = spriteSheet.getSheetImage(34, 68, 34, 34)
        self.emptyImage = spriteSheet.getSheetImage(34, 102, 34, 34)

        self.image = self.emptyImage
        self.image.set_colorkey(c.BLACK)
        self.rect = self.image.get_rect()
        self.collisionRect = pg.rect.Rect((0, 0), (16, 32))
Example #3
0
    def __init__(self, playerNumber=1):
        """Init GameOverTextSprite using the integer playerNumber.

        Instance variables:
            spriteSheet: The SpriteSheet object for the display sprite sheet image.
            image: The current image to be drawn for the sprite. Always is the one image from spriteSheet.
            coordinates: A tuple location to blit the sprite on the screen.
            frameCount: An integer that increases whenever the update method is called.
                Used to control when other methods should be called.
        """
        super().__init__(c.textGroup)
        spriteSheet = SpriteSheet("display.png")
        self.image = spriteSheet.getSheetImage(404, 536, 62, 32)
        self.image.set_colorkey(c.RED)
        self.coordinates = (20, 478)
        self.playerNumber = playerNumber
        self.frameCount = 0
Example #4
0
    def __init__(self):
        """Init RubberTrapSprite.

        Instance variables:
            spriteSheet: The SpriteSheet object for the trap sprite sheet image.
            animationFrames: A list of 4 Surface objects from the SpriteSheet object
            coordinates: A tuple location to blit the sprite on the screen.
            trapState: An OtherStates Enum instance of the current state of the sprite.
                Used to determine which methods get called and when.
            collidingPlayer: An instance of the PlayerSprite class that collides with this sprite.
                Is a None type variable until the trap sprite overlaps with a player sprite.
            isHorizontal: A boolean indicating if the sprite should be rotated 90 degrees or not.
            flipTrigger: A boolean indicating if the trap is in a step in its animation where it is triggered and
                its image should be flipped.
            frameCount: An integer that increases whenever the animateTrap method is called.
            emptyImage: A Surface object, showing a fully-transparent blank image.
                Used when the sprite should not be visibly drawn onscreen.
            image: The current image to be drawn for the sprite.
                Defaults to the emptyImage.
            rect: A rect object for the sprite.
            collisionRect: A smaller rect object used for checking collision between this sprite and others.
                This creates a better visual for collision than using the main rect object.
        """
        super().__init__(c.rubberGroup)
        spriteSheet = SpriteSheet("trap.png")
        self.animationFrames = []

        self.coordinates = (0, 0)
        self.trapState = c.OtherStates.OFF_SCREEN
        self.collidingPlayer = None
        self.isHorizontal = self.flipTrigger = False
        self.frameCount = 0

        self.animationFrames.extend(spriteSheet.getStripImages(0, 0, 60, 56, 4, key=c.RED))
        self.emptyImage = spriteSheet.getSheetImage(0, 240, 60, 56, key=c.RED)

        self.image = self.emptyImage
        self.image.set_colorkey(c.BLACK)
        self.rect = self.image.get_rect()
        self.collisionRect = pg.rect.Rect((0, 0), (16, 32))
Example #5
0
class DemoSprite(pg.sprite.Sprite):
    """Create an instance of a sprite for the demo.

    This class should not be called directly. Only call its subclasses.

    Attributes:
        coordinates: A tuple location to blit the sprite on the screen.
    """

    def __init__(self, coordinates=(0, 0)):
        """Init DemoSprite using the tuple coordinates.

        Instance variables:
            spriteSheet: The SpriteSheet object for the demo sprite sheet image.
            animationFrames: An empty list. Subclasses replace this with a list of Surface objects from the
                SpriteSheet object.
            frameCount: An integer that increases whenever the update method is called in the subclasses.
                Used to control when other methods should be called.
            emptyImage: A Surface object, showing a fully-transparent blank image.
                Used when the sprite should not be visibly drawn onscreen.
            image: The current image to be drawn for the sprite.
                Defaults to the emptyImage.
            rect: A rect object for the sprite.
        """
        super().__init__(c.demoGroup)
        self.spriteSheet = SpriteSheet("demo.png")
        self.animationFrames = []
        self.coordinates = coordinates
        self.frameCount = 0
        self.emptyImage = self.spriteSheet.getSheetImage(546, 416, 32, 32)
        self.image = self.emptyImage
        self.image.set_colorkey(c.BLACK)
        self.rect = self.image.get_rect()

    def update(self):
        """Not to be called directly, but inherited by subclasses."""
        pass

    def setCoordinates(self):
        """Change the sprite's coordinates.

        If the demo player sprite is currently swinging, this sprite's coordinates change based on how they are
        swinging.
        Otherwise, it changes based on which direction the demo player sprite is facing.
        """
        if DemoPlayerSprite.swingValue != (0, 0):
            self.coordinates = (self.coordinates[0] + DemoPlayerSprite.swingValue[0],
                                self.coordinates[1] + DemoPlayerSprite.swingValue[1])
        elif not DemoPlayerSprite.paused:
            if DemoPlayerSprite.facingDirection == c.Directions.UP:
                offsets = (0, 4)
            elif DemoPlayerSprite.facingDirection == c.Directions.DOWN:
                offsets = (0, -4)
            elif DemoPlayerSprite.facingDirection == c.Directions.LEFT:
                offsets = (4, 0)
            else:
                offsets = (-4, 0)
            self.coordinates = (self.coordinates[0] + offsets[0], self.coordinates[1] + offsets[1])
        self.rect.topleft = self.coordinates

    def setMonochromeImage(self):
        """Not to be called directly, but inherited by subclasses."""
        pass
Example #6
0
    def __init__(self):
        """Init UrchinSprite.

        Instance variables:
            spriteSheet: The SpriteSheet object for the urchin sprite sheet image.
            coordinates: A tuple location to blit the sprite on the screen.
            enemyState: An EnemyStates Enum instance of the current state of the sprite.
                Used to determine which methods get called and when.
            color: A tuple representing the urchin's current color.
                Blue urchins are active, whereas yellow urchins are stunned.
            facingDirection: A Directions Enum instance of the current direction the sprite is facing.
                Used to determine which direction the sprite moves and whether its image is flipped.
                Should only be one of the four cardinal directions. Setting this to CLOCKWISE or COUNTER will
                cause unexpected and undesired results.
            bouncingOff: A boolean indicating if the sprite is currently colliding with any objects that would
                prevent it from moving forwards.
            running: A boolean indicating if the sprite is currently moving at double speed.
            frameCount: An integer that increases whenever the update method is called.
            animationCount: An integer that increases whenever the sprite changes its animation image under
                specific circumstances.
                This is tracked for the purposes of controlling when to change the enemyState.
            delayCount: An integer representing how long the sprite must wait at an intersection before it can
                move.
            imageDict: A dict associating the keys BLUE and YELLOW with sub-dictionaries. These sub-dictionaries
                associate keywords with lists of Surface objects from the SpriteSheet object.
            emptyImage: A Surface object, showing a fully-transparent blank image.
                Used when the sprite should not be visibly drawn onscreen.
            image: The current image to be drawn for the sprite.
                Defaults to the emptyImage.
            rect: A rect object for the sprite.
            collisionRect: A smaller rect object used for checking collision between this sprite and others.
                This creates a better visual for collision than using the main rect object.
        """
        super().__init__(c.enemyGroup)
        spriteSheet = SpriteSheet("urchin.png")
        self.coordinates = (0, 0)
        self.enemyState = c.EnemyStates.SMALL_BALL
        self.color = c.BLUE
        self.facingDirection = c.Directions.RIGHT
        self.bouncingOff = self.running = False
        self.frameCount = self.animationCount = self.delayCount = 0
        self.audioCount = 1

        self.imageDict = {c.BLUE: {}, c.YELLOW: {}}
        self.imageDictKeys = ["horizontal", "vertical", "ball"]

        # xValue is used to prevent repetition, as we need to call getStripImages three times in total with very
        # similar arguments, differing only by xValue each time, and only by 34 each time.
        # In this case, we want (0, 0, 34, 34); (0, 34, 34, 34); (0, 68, 34, 34)
        xValue = 0
        for key in self.imageDictKeys:
            stripImages = spriteSheet.getStripImages(0, xValue, 34, 34)
            self.imageDict[c.BLUE][key] = [stripImages[0], stripImages[1]]
            self.imageDict[c.YELLOW][key] = [stripImages[2], stripImages[3]]
            xValue += 34
        self.imageDict[c.BLUE]["death"] = self.imageDict[c.YELLOW]["death"]\
            = spriteSheet.getStripImages(0, 102, 34, 34)
        self.emptyImage = spriteSheet.getSheetImage(0, 136, 34, 34)

        self.image = self.emptyImage
        self.image.set_colorkey(c.BLACK)
        self.rect = self.image.get_rect()
        self.collisionRect = pg.rect.Rect((0, 0), (18, 18))
Example #7
0
    def __init__(self, playerNumber=1):
        """Init PlayerSprite using the integer playerNumber.

        Instance variables:
            spriteSheet: The SpriteSheet object for the player sprite sheet image.
            lives: An integer representing the player's current remaining number of lives.
            baseCoordinates: A tuple location to blit the sprite upon starting a level or after losing a life.
                This should be updated whenever the player begins a new level, and at no other point.
            coordinates: A tuple location to blit the sprite on the screen.
            swingingArmCoordinates: A tuple location of the center point of the player's arm sprite when it grabs
                onto a post.
                This should be updated whenever the arm sprite grabs onto a new post, and at no other point.
            playerState: A PlayerStates Enum instance of the current state of the sprite.
                Used to determine which methods get called and when.
            facingDirection: A Directions Enum instance of the current direction the sprite is facing.
                Used to determine which direction the sprite moves and whether its image is flipped.
                Should only be one of the four cardinal directions. Setting this to CLOCKWISE or COUNTER will
                cause unexpected and undesired results.
            initialSwingDirection: A Directions Enum instance of which direction the sprite was facing
                immediately before entering the SWINGING PlayerStates Enum.
                Used to determine if a player should fall into a black hole sprite if they finish swinging while
                their rects overlap.
                Should only be one of the four cardinal directions. Setting this to CLOCKWISE or COUNTER will
                cause unexpected and undesired results.
            swingingDirection: A Directions Enum instance indicating if the sprite is swinging clockwise or
                counter-clockwise.
                Should only be one of the two rotation directions. Setting this to a cardinal direction will
                cause unexpected and undesired results.
            bouncingOffWall: A boolean indicating if the sprite is currently colliding with any of the level
                boundary's rect objects.
            bouncingOffPlayer: A boolean indicating if the sprite is currently colliding with any other Player
                sprites.
            isFrozen: A boolean indicating if the sprite is currently unable to move due to the methods of an
                ItemClock sprite.
            killedUrchinCount: An integer tracking how many Urchin sprites the player has killed in the current
                level.
            goldCollectedCount: An integer tracking how many Gold sprites the player has revealed in the current
                level.
            self.score: An integer tracking the player's total cumulative score.
            frameCount: An integer that increases whenever the update method is called.
                Used to control when other methods should be called.
            currentAngle: A float that tracks the current angle between the player sprite's center point and
                swingingArmCoordinates.
                Should only be updated or referenced while the player is swinging.
            imageDict: A dict associating keys with lists of Surface objects from the SpriteSheet object.
                As the player's image appears different when moving or squishing based on if they are moving
                horizontally or vertically, those keys take dicts of Surface objects instead of lists.
            emptyImage: A Surface object, showing a fully-transparent blank image.
                Used when the sprite should not be visibly drawn onscreen.
            image: The current image to be drawn for the sprite.
                Defaults to the emptyImage.
            rect: A rect object for the sprite.
            collisionRect: A smaller rect object used for checking collision between this sprite and others.
                This creates a better visual for collision than using the main rect object.
        """
        super().__init__(c.playerGroup)
        spriteSheet = SpriteSheet("player{}.png".format(playerNumber))
        self.playerNumber = playerNumber
        self.lives = 5
        self.baseCoordinates = (0, 0)
        self.coordinates = (0, 0)
        self.swingingArmCoordinates = (0, 0)
        self.playerState = c.PlayerStates.OFF_SCREEN
        self.facingDirection = c.Directions.RIGHT
        self.initialSwingDirection = c.Directions.RIGHT
        self.swingingDirection = c.Directions.CLOCKWISE
        self.bouncingOffWall = self.bouncingOffPlayer = self.isFrozen = False
        self.killedUrchinCount = self.goldCollectedCount = self.score = self.frameCount = 0
        self.currentAngle = 0.0

        self.imageDict = {
            "arm": [],
            "ball": [],
            "end": [],
            "death": [],
            "turn": [],
            "fall": [],  # #######
            "move": {},
            "squish": {}
        }
        armImageList = spriteSheet.getStripImages(152, 0, 16, 16, 2)
        armImageList.append(spriteSheet.getSheetImage(184, 0, 14, 14))
        self.imageDict["arm"] = armImageList
        self.imageDict["ball"] = spriteSheet.getStripImages(0, 0, 34, 34, 2)
        self.imageDict["end"] = spriteSheet.getStripImages(68, 0, 42, 32, 2)
        self.imageDict["death"] = spriteSheet.getStripImages(0, 34, 34, 34, 4)
        self.imageDict["turn"] = spriteSheet.getStripImages(136, 34, 34, 34)
        self.imageDict["fall"] = spriteSheet.getStripImages(0, 68, 34, 34, 4)
        self.imageDict["move"]["vertical"] = spriteSheet.getStripImages(
            0, 102, 32, 38, 4)
        self.imageDict["squish"]["vertical"] = spriteSheet.getStripImages(
            128, 102, 48, 38)
        self.imageDict["move"]["horizontal"] = spriteSheet.getStripImages(
            0, 140, 34, 34, 4)
        self.imageDict["squish"]["horizontal"] = spriteSheet.getStripImages(
            136, 140, 30, 52, 3)
        self.emptyImage = spriteSheet.getSheetImage(136, 68, 34, 34)

        self.image = self.emptyImage
        self.image.set_colorkey(c.BLACK)
        self.rect = self.image.get_rect()
        self.collisionRect = pg.rect.Rect((0, 0), (16, 16))