本文整理汇总了Python中sprite.Sprite.setCenterPos方法的典型用法代码示例。如果您正苦于以下问题:Python Sprite.setCenterPos方法的具体用法?Python Sprite.setCenterPos怎么用?Python Sprite.setCenterPos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sprite.Sprite
的用法示例。
在下文中一共展示了Sprite.setCenterPos方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Enemy
# 需要导入模块: from sprite import Sprite [as 别名]
# 或者: from sprite.Sprite import setCenterPos [as 别名]
class Enemy():
def __init__(self, enemyType, **kwargs):
self.canvas = InstructionGroup()
self.enemyType = enemyType
self.sprite = Sprite()
if self.enemyType == 'normal':
self.setOffsetTheta(0)
self.sprite.color.r = 0.0
self.sprite.color.g = 0.2
self.sprite.color.b = 0.5
else:
self.setOffsetTheta(math.pi / 2)
self.sprite.color.r = 0.5
self.sprite.color.g = 0.1
self.sprite.color.b = 0.1
self.health = 100
self.pos = (0, 0)
self.velocity = [0, 0]
self.updateAppearance()
self.canvas.add(self.sprite.canvas)
self.shouldRemove = False
def setOffsetTheta(self, offsetTheta):
self.offsetTheta = offsetTheta
self.otcos = math.cos(self.offsetTheta)
self.otsin = math.sin(self.offsetTheta)
def reset(self, isRespawned):
sample = random.random()
theta = math.pi * 2 * sample
speed = 0.1
self.isRespawned = isRespawned
self.velocity = [math.cos(theta) * speed,
math.sin(theta) * speed]
def setWorld(self, world):
self.world = world
def setCenterPos(self, centerPos):
self.pos = centerPos
self.sprite.setCenterPos(centerPos)
def decrement(self, beamState):
delta = 0
if beamState == 1:
self.health -= 10
if self.enemyType == 'normal':
delta = 100
else:
delta = -500
if self.health <= 0:
self.shouldRemove = True
else:
self.updateAppearance()
return delta
def updateAppearance(self):
baseSize = 30
if not self.enemyType == 'normal':
baseSize = 15
factor = math.log(100 - self.health + 1)
self.sprite.setSizeScalar(baseSize + factor * 10)
def update(self, dt):
worldVector = (self.world.direction[0] * self.world.speed,
self.world.direction[1] * self.world.speed)
worldOffset = (worldVector[0] * self.otcos - worldVector[1] * self.otsin,
worldVector[0] * self.otsin - worldVector[1] * self.otcos)
centerPos = (self.pos[0] + self.velocity[0] + worldOffset[0],
self.pos[1] + self.velocity[1] + worldOffset[1])
if self.isRespawned:
self.isRespawned = False
if centerPos[0] < self.world.left:
self.shouldRemove = True
if centerPos[0] > self.world.right:
self.shouldRemove = True
if centerPos[1] < self.world.left:
self.shouldRemove = True
if centerPos[1] > self.world.right:
self.shouldRemove = True
#.........这里部分代码省略.........
示例2: Player
# 需要导入模块: from sprite import Sprite [as 别名]
# 或者: from sprite.Sprite import setCenterPos [as 别名]
class Player():
def __init__(self, playerCode, **kwargs):
self.canvas = InstructionGroup()
self.sprite = Sprite()
self.playerCode = playerCode
self.frameNum = 0
self.bottomLeft = (0, 0)
self.topRight = (700, 500)
self.pos = (0, 0)
self.direction = (0, 0)
self.speed = 0
self.targetDirection = (0, 0)
self.targetSpeed = 0
self.isTweeningDirection = False
self.isTweeningSpeed = False
self.newDirectionSince = 0
self.newSpeedSince = 0
self.directionChange = (0, 0)
if playerCode == 'p2':
self.sprite.color.r = 1
if playerCode == 'enemy1':
self.sprite.color.b = 1
self.canvas.add(self.sprite.canvas)
def reset(self):
self.frameNum = 0
def setBounds(self, bottomLeft, topRight):
self.bottomLeft = bottomLeft
self.topRight = topRight
def setCenterPos(self, centerPos):
self.pos = centerPos
self.sprite.setCenterPos(self.pos)
def setPlayerKeyReport(self, playerKeyReport):
self.playerKeyReport = playerKeyReport
def update(self, dt):
self.frameNum += 1
self.updateDynamics()
self.updateTweening()
self.updatePosition()
def updateDynamics(self):
dX = 0
dY = 0
scale = 1
speed = 0
if self.playerKeyReport.up:
dY = 1
elif self.playerKeyReport.down:
dY = -1
if self.playerKeyReport.left:
dX = -1
elif self.playerKeyReport.right:
dX = 1
if not dY == 0 and not dX == 0:
scale = SQRT_2_DIV_2
if not dY == 0 or not dX == 0:
speed = TOP_SPEED
targetDirection = (dX * scale, dY * scale)
wasNullDirection = (self.targetDirection[0] == 0 and self.targetDirection[1] == 0)
isNullDirection = (targetDirection[0] == 0 and targetDirection[1] == 0)
isNewTargetDirection = (not self.targetDirection[0] == targetDirection[0] or
not self.targetDirection[1] == targetDirection[1])
if not isNullDirection and isNewTargetDirection:
if wasNullDirection:
self.targetDirection = targetDirection
self.direction = targetDirection
else:
self.isTweeningDirection = True
self.newDirectionSince = self.frameNum
self.targetDirection = targetDirection
self.calculateDirectionVector()
isNewTargetSpeed = not self.targetSpeed == speed
if isNewTargetSpeed:
self.isTweeningSpeed = True
self.newSpeedSince = self.frameNum
self.targetSpeed = speed
def updateTweening(self):
self.updateSpeedTweening()
self.accountForZeroSpeed()
self.updateDirectionTweening()
def updateSpeedTweening(self):
#.........这里部分代码省略.........