Example #1
0
class Dynamite(Object):
    ANIMATION_FRAME_COUNT = 3
    ANIMATION_FRAME_TIME = 0.25  # in seconds
    texture = None
    stageManager = None
    range = 0
    explosionTime = 0.

    def __init__(self, tilePos):
        Object.__init__(self, tilePos * Dynamite.stageManager.stage.tileSize, Dynamite.texture)
        self.currTime = Dynamite.explosionTime
        self.tilePos = tilePos
        self.exploded = False

        animationInfo = AnimationInfo()
        animationInfo.firstFrameRect = Rectangle((0, 0), (self.stageManager.stage.tileSize.x,
                                                          self.stageManager.stage.tileSize.y))
        animationInfo.frameCount = Dynamite.ANIMATION_FRAME_COUNT
        animationInfo.frameTime = Dynamite.ANIMATION_FRAME_TIME
        animationInfo.frameSize = self.stageManager.stage.tileSize
        animationInfo.sprite = self.sprite

        self.animation = Animation(animationInfo).start()

    def update(self, deltaTime):
        self.animation.update(deltaTime)
        self.currTime -= deltaTime
        self.exploded = self.currTime <= 0.
        if self.exploded:
            self.stageManager.clearDynamite(self)
            self.stageManager.addExplosion(Explosion(self.tilePos, Dynamite.range))
Example #2
0
    def __init__(self, centerTilePos, radiusInTile):
        Object.__init__(self, centerTilePos * self.stage.tileSize, Explosion.texture)
        self.spritesPos = [[], [], [], []]
        self.tilesPos = []
        self.sideSprites = (sf.Sprite, sf.Sprite)
        self.currTime = 0.
        self.over = False

        row = lambda tilePos, offSet: (tilePos.x +
                                       tilePos.y *
                                       self.stage.tilesCount.x +
                                       offSet)
        column = lambda tilePos, offSet: (tilePos.x +
                                          (tilePos.y + offSet) *
                                          self.stage.tilesCount.x)

        isObstacle = lambda tile: utils.checkBit(tile.bits, Tile.Obstacle) == 1

        spritePos = lambda tilePos: tilePos * self.stage.tileSize

        tileOffSet = 1
        while radiusInTile >= tileOffSet and not isObstacle(self.stage.tiles[row(centerTilePos, tileOffSet)]):
            self.spritesPos[0].append(spritePos(sf.Vector2(centerTilePos.x + tileOffSet,
                                                        centerTilePos.y)))
            tileOffSet += 1

        animationInfo = AnimationInfo()
        animationInfo.firstFrameRect = sf.Rectangle((0, 0), (48, 48))
        animationInfo.sprite = self.sprite
        animationInfo.frameSize = 48
        animationInfo.frameTime = 0.1
        animationInfo.frameCount = 3

        self.centerAnimation = Animation(animationInfo).start()
Example #3
0
    def __init__(self, tilePos):
        Object.__init__(self, tilePos * Dynamite.stageManager.stage.tileSize, Dynamite.texture)
        self.currTime = Dynamite.explosionTime
        self.tilePos = tilePos
        self.exploded = False

        animationInfo = AnimationInfo()
        animationInfo.firstFrameRect = Rectangle((0, 0), (self.stageManager.stage.tileSize.x,
                                                          self.stageManager.stage.tileSize.y))
        animationInfo.frameCount = Dynamite.ANIMATION_FRAME_COUNT
        animationInfo.frameTime = Dynamite.ANIMATION_FRAME_TIME
        animationInfo.frameSize = self.stageManager.stage.tileSize
        animationInfo.sprite = self.sprite

        self.animation = Animation(animationInfo).start()
Example #4
0
    def __initAnimations(self):
        upAnimationInfo = AnimationInfo()
        upAnimationInfo.firstFrameRect = sf.Rectangle((0, 48), (48, 48))
        upAnimationInfo.sprite = self.sprite
        upAnimationInfo.frameSize = 48
        upAnimationInfo.frameTime = 0.1
        upAnimationInfo.frameCount = 3
        downAnimationInfo = copy.deepcopy(upAnimationInfo)
        downAnimationInfo.firstFrameRect.top = 96
        leftAnimationInfo = copy.deepcopy(upAnimationInfo)
        leftAnimationInfo.firstFrameRect.top = 0
        rightAnimationInfo = copy.deepcopy(leftAnimationInfo)
        rightAnimationInfo.firstFrameRect.width = -48

        self.__upAnimation = Animation(upAnimationInfo)
        self.__downAnimation = Animation(downAnimationInfo)
        self.__leftAnimation = Animation(leftAnimationInfo)
        self.__rightAnimation = Animation(rightAnimationInfo)
        self.__currAnimation = self.__downAnimation.start()
Example #5
0
class Player(Object):
    def __init__(self, tilePos, texture, stageManager):
        Object.__init__(self, tilePos * stageManager.stage.tileSize, texture)
        self.__isMoving = False
        self.__stageManager = stageManager
        self.tile = tilePos
        self.nextPos = sf.Vector2(0., 0.)
        self.velocity = sf.Vector2(0., 0.)
        self.currDirection = Direction.STAY
        self.nextPos = sf.Vector2(48., 48.)
        self.__speed = 150
        self.__directions = [Direction.STAY]
        self.__initAnimations()

    def __initAnimations(self):
        upAnimationInfo = AnimationInfo()
        upAnimationInfo.firstFrameRect = sf.Rectangle((0, 48), (48, 48))
        upAnimationInfo.sprite = self.sprite
        upAnimationInfo.frameSize = 48
        upAnimationInfo.frameTime = 0.1
        upAnimationInfo.frameCount = 3
        downAnimationInfo = copy.deepcopy(upAnimationInfo)
        downAnimationInfo.firstFrameRect.top = 96
        leftAnimationInfo = copy.deepcopy(upAnimationInfo)
        leftAnimationInfo.firstFrameRect.top = 0
        rightAnimationInfo = copy.deepcopy(leftAnimationInfo)
        rightAnimationInfo.firstFrameRect.width = -48

        self.__upAnimation = Animation(upAnimationInfo)
        self.__downAnimation = Animation(downAnimationInfo)
        self.__leftAnimation = Animation(leftAnimationInfo)
        self.__rightAnimation = Animation(rightAnimationInfo)
        self.__currAnimation = self.__downAnimation.start()

    def __getTileMove(self):
        return {
            Direction.UP: sf.Vector2(0, -1),
            Direction.DOWN: sf.Vector2(0, 1),
            Direction.LEFT: sf.Vector2(-1, 0),
            Direction.RIGHT: sf.Vector2(1, 0),
            Direction.STAY: sf.Vector2(0, 0)
        }

    def __checkPosition(self):
        return {
            Direction.UP: self.position.y < self.nextPos.y,
            Direction.DOWN: self.position.y > self.nextPos.y,
            Direction.LEFT: self.position.x < self.nextPos.x,
            Direction.RIGHT: self.position.x > self.nextPos.x
        }

    def __getOneAxisPos(self, direction):
        if direction is Direction.LEFT or direction is Direction.RIGHT:
            return sf.Vector2(self.nextPos.x, self.position.y)
        else:
            return sf.Vector2(self.position.x, self.nextPos.y)

    def __getInvVecAxis(self, vec1, vec2):
        if self.currDirection is Direction.LEFT or self.currDirection is Direction.RIGHT:
            return vec1.y, vec2.y, (Direction.DOWN, Direction.UP)
        else:
            return vec1.x, vec2.x, (Direction.RIGHT, Direction.LEFT)

    def __getVecAxis(self, vector):
        if self.currDirection is Direction.LEFT or self.currDirection is Direction.RIGHT:
            return vector.x
        else:
            return vector.y

    def __getVelocity(self, direction):
        return self.__getTileMove()[direction] * self.__speed

    def __moveInvertedAxis(self, deltaTime):
        invertedVec = self.__getInvVecAxis(self.position, self.nextPos)
        direction = Direction.STAY
        if invertedVec[0] < invertedVec[1]:
            direction = invertedVec[2][0]
        elif invertedVec[0] > invertedVec[1]:
              direction = invertedVec[2][1]
        if direction is not Direction.STAY:
            self.move(self.__getVelocity(direction) * deltaTime)
            if self.__checkPosition()[direction]:
                self.position = self.__getOneAxisPos(direction)

    def __getAnimation(self):
        return {
            Direction.UP: self.__upAnimation.start,
            Direction.DOWN: self.__downAnimation.start,
            Direction.LEFT: self.__leftAnimation.start,
            Direction.RIGHT: self.__rightAnimation.start
        }.get(self.currDirection, lambda: ())

    def __pushDirection(self, direction):
        if not direction in self.__directions:
            self.__directions.append(direction)
            self.currDirection = self.__directions[-1]
            self.velocity = self.__getVelocity(self.currDirection)
            self.__currAnimation = self.__getAnimation()()

    def __popDirection(self, direction):
        if direction in self.__directions:
            self.__directions.remove(direction)
            self.currDirection = self.__directions[-1]
            self.velocity = self.__getVelocity(self.currDirection)
            self.__currAnimation = self.__getAnimation()()

    def handleEvent(self, event):
        if type(event) is sf.KeyEvent:
            if event.pressed:
                if event.code == sf.Keyboard.RIGHT:
                    self.__pushDirection(Direction.RIGHT)
                elif event.code == sf.Keyboard.LEFT:
                    self.__pushDirection(Direction.LEFT)
                elif event.code == sf.Keyboard.UP:
                    self.__pushDirection(Direction.UP)
                elif event.code == sf.Keyboard.DOWN:
                    self.__pushDirection(Direction.DOWN)
                elif event.code == sf.Keyboard.SPACE:
                    self.__stageManager.addDynamite(Dynamite(self.tile))
            elif event.released:
                if event.code == sf.Keyboard.RIGHT:
                    self.__popDirection(Direction.RIGHT)
                elif event.code == sf.Keyboard.LEFT:
                    self.__popDirection(Direction.LEFT)
                elif event.code == sf.Keyboard.UP:
                    self.__popDirection(Direction.UP)
                elif event.code == sf.Keyboard.DOWN:
                    self.__popDirection(Direction.DOWN)

    def update(self, deltaTime = 1.0):
        if self.currDirection is not Direction.STAY:
            self.__currAnimation.update(deltaTime)
            offSet = self.velocity * deltaTime
            self.position += self.velocity * deltaTime
            if self.__checkPosition()[self.currDirection] and not self.__stageManager.checkMove(self, self.__getTileMove()[self.currDirection]):
                self.position -= offSet
            else:
                delta = self.position - self.nextPos
                if math.fabs(self.__getVecAxis(delta)) > 24:
                    self.nextPos = self.__stageManager.move(self, self.__getTileMove()[self.currDirection])
            if not self.__getVecAxis(self.position) == self.__getVecAxis(self.nextPos):
                self.__moveInvertedAxis(deltaTime)