def __init__(self): self.logger: Logger = getLogger(__name__) self.imageRotation = 0 GamePiece.__init__(self, filename=Enterprise.FILENAME) SmoothMotion.__init__(self, imageRotation=125)
def __init__(self, filename: str, coordinates: Coordinates, moveInterval: int = NEVER_MOVE_INTERVAL, scale: float = 1.0, imageRotation: int = SmoothMotion.IMAGE_ROTATION): """ Args: filename: The image file name coordinates: The game sector coordinates within the quadrant moveInterval: How often this enemy moves scale: How to scale the image imageRotation: How much to rotate image when the enemy is shooting at Captain Kirk. """ GamePiece.__init__(self, filename=filename, scale=scale) SmoothMotion.__init__(self, imageRotation=imageRotation) self.gameCoordinates = coordinates self._moveInterval: int = moveInterval self._id: EnemyId = EnemyId('IdNotSet') self._power: float = cast(float, None) self._firingInterval: int = BaseEnemy.NEVER_FIRE_INTERVAL self._lastTimeCheck: int = 0 self._timeSinceMovement: float = 0.0
def __init__(self, speed: float = 3.0): GamePiece.__init__(self, filename=PhotonTorpedo.FILENAME, speed=speed) SmoothMotion.__init__(self, imageRotation=0) self.logger: Logger = getLogger(__name__) self._id: PhotonTorpedoId = PhotonTorpedoId( f'Torpedo-{PhotonTorpedo.nextId}') self._firedAt: EnemyId = cast(EnemyId, None) PhotonTorpedo.nextId += 1
def _placeMiss(self, quadrant: Quadrant, torpedoDud: GamePiece, miss: BaseMiss): """ Convert to game coordinates Then to game point in order to get miss center as a sector coordinates Args: miss: The appropriate "miss" sprite """ gameCoordinates: Coordinates = self._computer.computeSectorCoordinates( x=torpedoDud.center_x, y=torpedoDud.center_y) arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition( gameCoordinates=gameCoordinates) miss.center_x = arcadePoint.x miss.center_y = arcadePoint.y if isinstance(miss, KlingonTorpedoMiss): sectorType: SectorType = SectorType.KLINGON_TORPEDO_MISS else: sectorType = SectorType.ENTERPRISE_TORPEDO_MISS miss.gameCoordinates = gameCoordinates self.__placeMissInQuadrant(quadrant, gameCoordinates, sectorType) self._missesMediatorLogger.info( f'Placed miss at: {gameCoordinates=} {arcadePoint=}')
def __init__(self, filename: str, torpedoId: EnemyTorpedoId, speed: float = 3, scale: float = 1.0): GamePiece.__init__(self, filename=filename, speed=speed, scale=scale) SmoothMotion.__init__(self) self._baseEnemyTorpedoLogger: Logger = getLogger(__name__) self._id: EnemyTorpedoId = torpedoId self._firedBy: EnemyId = cast(EnemyId, None) self._firedFromPosition: Coordinates = cast(Coordinates, None) self._followers: SpriteList = cast(SpriteList, None) self._computer: Computer = Computer()
def testGamePositionToScreenPositionZeroNine(self): sectorCoordinates: Coordinates = Coordinates(x=0, y=9) arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition( gameCoordinates=sectorCoordinates) self.logger.info(f'{arcadePoint=}') expectedPoint: ArcadePoint = ArcadePoint(x=32.0, y=222.0) self.assertEqual(expectedPoint, arcadePoint, 'Calculation must have changed')
def _toArcadePoint(self, enemy: BaseEnemy, newPosition: Coordinates): """ Sets the arcade points for the input enemy. Args: enemy: The enemy that moved newPosition: The enemy new game position """ arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition( newPosition) enemy.center_x = arcadePoint.x enemy.center_y = arcadePoint.y
def __init__(self, coordinates: Coordinates, moveInterval: int): super().__init__(filename=SuperCommander.FILENAME, coordinates=coordinates, moveInterval=moveInterval, scale=0.055, imageRotation=45) self.logger: Logger = getLogger(__name__) self.id = EnemyId(f'SuperCommander-{self.gameCoordinates}') # Compute at creation; Mediator will move the commander arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition(coordinates) self.center_x = arcadePoint.x self.center_y = arcadePoint.y
def update(self, quadrant: Quadrant): enterprise: Enterprise = quadrant.enterprise arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition(quadrant.enterpriseCoordinates) arcadeX: float = arcadePoint.x arcadeY: float = arcadePoint.y if enterprise.inMotion is True: self.logger.debug(f'Enterprise arcade position: ({arcadeX},{arcadeY})') enterprise.destinationPoint = ArcadePoint(x=arcadeX, y=arcadeY) enterprise.update() else: enterprise.center_x = arcadeX enterprise.center_y = arcadeY
def impulse(self, quadrant: Quadrant, arcadePoint: ArcadePoint): targetCoordinates: Coordinates = self._computer.computeSectorCoordinates(x=arcadePoint.x, y=arcadePoint.y) enterpriseCoordinates: Coordinates = self._gameState.currentSectorCoordinates if targetCoordinates == enterpriseCoordinates: self._messageConsole.displayMessage("WTF. You are already here!") self._soundMachine.playSound(SoundType.UnableToComply) else: startingPoint: ArcadePoint = GamePiece.gamePositionToScreenPosition(enterpriseCoordinates) endPoint: ArcadePoint = arcadePoint results: LineOfSightResponse = self._doWeHaveLineOfSight(quadrant=quadrant, startingPoint=startingPoint, endPoint=endPoint) if results.answer is True: self._doImpulseMove(quadrant=quadrant, enterpriseCoordinates=enterpriseCoordinates, targetCoordinates=targetCoordinates) else: self._doBlockedImpulseMove(quadrant=quadrant, enterpriseCoordinates=enterpriseCoordinates, results=results)
def __init__(self, sectorCoordinates: Coordinates): self.logger: Logger = getLogger(__name__) super().__init__(filename=StarBase.FILENAME, scale=0.25) # Compute these once since StarBase's don't move self.gameCoordinates = sectorCoordinates arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition( sectorCoordinates) self.center_x = arcadePoint.x self.center_y = arcadePoint.y self._id: str = f'StarBase-{StarBase.nextId}' StarBase.nextId += 1
def __init__(self, coordinates: Coordinates, moveInterval: int = 0): """ Args: coordinates: Current Game Position moveInterval: Only set at advanced game levels """ super().__init__(filename=Klingon.FILENAME, coordinates=coordinates) self.id = EnemyId(f'Klingon-{self.gameCoordinates}') self.moveInterval = moveInterval # Compute at creation; Mediator will move the Klingon at advance game conditions arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition( coordinates) self.center_x = arcadePoint.x self.center_y = arcadePoint.y
def __init__(self, planetType: PlanetType, sectorCoordinates: Coordinates): self.logger: Logger = getLogger(__name__) bareFileName: str = f'{planetType.value}.png' super().__init__(filename=bareFileName, scale=0.35) # Compute these values once since planets don't move self.gameCoordinates = sectorCoordinates arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition( sectorCoordinates) self.center_x = arcadePoint.x self.center_y = arcadePoint.y self._type: PlanetType = planetType self._id: str = f'{self._type.value} type planet'
def _doImpulseMove(self, quadrant: Quadrant, enterpriseCoordinates: Coordinates, targetCoordinates: Coordinates): """ Handle impulse move if we are not blocked by any obstacles. Args: quadrant: The current quadrant enterpriseCoordinates: Then enterprise sector coordinates targetCoordinates: Where the player indicated we were moving """ self.__updateQuadrant(quadrant=quadrant, currentCoordinates=enterpriseCoordinates, targetCoordinates=targetCoordinates) quadrant.enterprise.destinationPoint = GamePiece.gamePositionToScreenPosition(gameCoordinates=targetCoordinates) quadrant.enterpriseCoordinates = targetCoordinates quadrant.enterprise.inMotion = True self._gameEngine.impulse(newCoordinates=targetCoordinates, quadrant=quadrant, enterprise=quadrant.enterprise) self._soundMachine.playSound(SoundType.Impulse) if quadrant.klingonCount > 0 or quadrant.commanderCount > 0 or quadrant.superCommanderCount > 0: self._gameState.shipCondition = ShipCondition.Red else: self._gameState.shipCondition = ShipCondition.Green
def _addEnemyToTestGrid( self, enemy: BaseEnemy, x: float, y: float, ) -> bool: added: bool = True gameCoordinates: Coordinates = self._computer.computeCoordinates(x=x, y=y) if gameCoordinates.valid() is True: # Recompute a 'centered' arcade point arcadePoint: ArcadePoint = GamePiece.gamePositionToScreenPosition( gameCoordinates) enemy.gameCoordinates = gameCoordinates enemy.center_x = arcadePoint.x enemy.center_y = arcadePoint.y else: added = False return added
def doMotion(self, gamePiece: GamePiece, destinationPoint: ArcadePoint, angleDiffRadians: float, actualAngleRadians: float): destinationX: float = destinationPoint.x destinationY: float = destinationPoint.y gamePiece.angle = degrees( actualAngleRadians) + self.imageRotation # Convert back to degrees # Are we close to the correct angle? If so, move forward. if abs(angleDiffRadians) < pi / 4: gamePiece.change_x = cos(actualAngleRadians) * gamePiece.speed gamePiece.change_y = sin(actualAngleRadians) * gamePiece.speed # Fine-tune our change_x/change_y if we are really close to destinationPoint # point and just need to set to that location. traveling = False self._smoothMotionLogger.debug( f'{gamePiece.center_x=} {destinationX=} {gamePiece.center_y=} {destinationY=}' ) if abs(gamePiece.center_x - destinationX) < abs(gamePiece.change_x): gamePiece.center_x = destinationX else: gamePiece.center_x += gamePiece.change_x traveling = True if abs(gamePiece.center_y - destinationY) < abs(gamePiece.change_y): gamePiece.center_y = destinationY else: gamePiece.center_y += gamePiece.change_y traveling = True # self._smoothMotionLogger.debug(f'({gamePiece.center_x},{gamePiece.center_y})') # If we have arrived, then we are not in motion if not traveling: # self.destinationPoint = None # Leave this set for klingon torpedo hit computation self._inMotion = False gamePiece.angle = 0