作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道如何使用 Pyganim,它相当简单。但我的问题在于我如何构建游戏,它每秒启动 pyganim 的动画 60 次,因此它根本不会动画(根据人眼)。我需要帮助来了解如何判断动画当前是否正在播放,然后调用播放或不播放?
我的代码:
class gameStart(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.background = pygame.image.load("BG" + str(level) + ".png")
self.player = pygame.image.load("2_scaled.png")
self.icon = pygame.image.load("1_scaled.png")
self.background_S = pygame.transform.scale(self.background, (width, height)) #Scale the background to match the screen resolution
screen.blit(self.background_S, (0,0))
screen.blit(self.player, (0, height/2))
screen.blit(self.icon, (0,0))
self.position = self.player.get_rect()
self.health = 100
#Setup the players Idle, Attack, Attack 2 and Death Animations
self.PlayerIdleAnim = pyganim.PygAnimation([('2_scaled.png', 1), ('3_scaled.png', 1), ('4_scaled.png', 1), ('5_scaled.png', 1), ('6_scaled.png', 1)])
updateDisplay()
def move(self):
global rightPressed
global leftPressed
global facing
global frame
leftPressed = False
rightPressed = False
if keyPressed(K_a):
leftPressed = True
elif keyPressed(K_d):
rightPressed = True
elif not keyPressed(K_a):
leftPressed = False
elif not keyPressed(K_d):
rightPressed = False
if rightPressed and (self.position.x < width - 200):
self.position.x += moveSpeed
screen.blit(self.background_S, (0,0))
self.PlayerIdleAnim.stop()
screen.blit(self.player, (self.position.x, height/2))
if not facing:
self.player = pygame.transform.flip(self.player, True, False)
facing = True
updateDisplay()
elif leftPressed and (self.position.x > 20):
self.position.x += -moveSpeed
screen.blit(self.background_S, (0,0))
self.PlayerIdleAnim.stop()
if facing:
self.player = pygame.transform.flip(self.player, True, False)
facing = False
screen.blit(self.player, (self.position.x, height/2))
updateDisplay()
elif not leftPressed and not rightPressed:
if not facing:
self.PlayerIdleAnim.flip(True, False)
self.PlayerIdleAnim.play()
else:
self.PlayerIdleAnim.play()
"""elif rightPressed and not (self.position.x < width - 200):
rightPressed = False
elif leftPressed and not (self.position.x > 200):
leftPressed = False"""
game = gameStart()
while not gameQuit:
for event in pygame.event.get():
if event.type == QUIT:
gameQuit = True
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
gameQuit = True
game.move()
updateDisplay()
fpsClock.tick(fps)
最佳答案
从技术上讲,动画永远不会运行,因为它总是卡在第一帧。但是,如果您确实想知道如果您给它时间切换帧,它是否会运行,只需仅在按下让动画运行的键时才激活循环。类似...
def anim_test():
clock = pg.time.Clock()
while run == true:
pg.display.update()
clock.tick(60)
关于python - Pyganim 在 Sidescroller 游戏中的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33071152/
我知道如何使用 Pyganim,它相当简单。但我的问题在于我如何构建游戏,它每秒启动 pyganim 的动画 60 次,因此它根本不会动画(根据人眼)。我需要帮助来了解如何判断动画当前是否正在播放,然
这有什么问题吗? $("body").mousewheel(function(event, delta) { if(delta>0){ this.animate({scro
我是一名优秀的程序员,十分优秀!