本文整理汇总了Python中sprite.Sprite.changeSprite方法的典型用法代码示例。如果您正苦于以下问题:Python Sprite.changeSprite方法的具体用法?Python Sprite.changeSprite怎么用?Python Sprite.changeSprite使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sprite.Sprite
的用法示例。
在下文中一共展示了Sprite.changeSprite方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from sprite import Sprite [as 别名]
# 或者: from sprite.Sprite import changeSprite [as 别名]
class Perso:
def __init__(self, x, y, path, difficulty):
self.HIT_DURATION = 0.8
self.HURT_DURATION = 0.8
self.HURT_POWER = 300
self.hitTime = 0
self.hurtTime = 0
self.state = "normal"
self.sprite = Sprite(path)
self.sprite.rect.x = x
self.sprite.rect.y = y
self.pos = [x,y]
self.speed = difficulty * 1.5 + 6
self.life = 1
self.pushedVector = [0,0]
def update(self, timeElapsed):
if self.state == "hit":
self.hitTime += timeElapsed
if self.hitTime >= self.HIT_DURATION:
self.normal()
self.hitTime = 0
if self.state == "hurt":
self.hurtTime += timeElapsed
if self.hurtTime >= self.HURT_DURATION:
self.normal()
self.hurtTime = 0
self.updatePos(timeElapsed)
self.sprite.update(timeElapsed)
def updatePos(self, timeElapsed):
if self.pos[0] < 25:
self.pos[0] = 25
self.pushedVector[0] = -self.pushedVector[0]
elif self.pos[0] > 725:
self.pos[0] = 725
self.pushedVector[0] = -self.pushedVector[0]
if self.pos[1] < 200:
self.pos[1] = 200
self.pushedVector[1] = -self.pushedVector[1]
elif self.pos[1] > 525:
self.pos[1] = 525
self.pushedVector[1] = -self.pushedVector[1]
self.pos[0] = self.pos[0] + self.pushedVector[0] * timeElapsed / self.HURT_DURATION
self.pos[1] = self.pos[1] + self.pushedVector[1] * timeElapsed / self.HURT_DURATION
self.pushedVector[0] = self.pushedVector[0] - self.pushedVector[0] * timeElapsed / self.HURT_DURATION
self.pushedVector[1] = self.pushedVector[1] - self.pushedVector[1] * timeElapsed / self.HURT_DURATION
def draw(self, screen):
self.sprite.draw(screen, self.pos)
def move(self, direction):
if direction == "up":
self.pos[1] -= self.speed
elif direction == "left":
self.pos[0] -= self.speed
elif direction == "right":
self.pos[0] += self.speed
elif direction == "down":
self.pos[1] += self.speed
if self.state != "hit" and self.state != "hurt":
self.sprite.changeSprite(direction)
def normal(self):
self.state = "normal"
self.sprite.changeSprite("normal")
def hit(self):
if self.state != "hit" and self.state != "hurt":
self.state = "hit"
self.sprite.changeSprite("action")
return True
else:
return False
def isHurt(self):
return self.state == "hurt"
def hurt(self, pos):
newPos = [self.pos[0] - pos[0], self.pos[1] - pos[1]]
if newPos[0] == 0 and newPos[1] == 0:
newPos[0] = 1
newPos[1] = 1
max = abs(newPos[0]) if (abs(newPos[0]) > abs(newPos[1])) else abs(newPos[1])
self.pushedVector = [self.HURT_POWER * newPos[0] / max, self.HURT_POWER * newPos[1] / max]
print self.pushedVector
self.state = "hurt"
self.sprite.changeSprite("action")