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)