【发布时间】:2018-08-27 10:10:57
【问题描述】:
我已阅读此处发布的其他相关问题,但我似乎无法将这些答案放入我的状态机中,可以在此处找到:https://github.com/Aeryes/Demented
我正在尝试使用精灵表或仅通过列表或字典进行迭代来创建动画。到目前为止,我已经设法让列表中的第一个图像作为动画加载,但只要我正在运行的标志变量 = True 为真,它就不会循环遍历 4 个图像的列表。相反,它会从列表中加载第一个图像并将其保留在该图像上,直到 running = False 在这种情况下,它会按预期恢复到静止图像。
这是我当前的播放器类代码,没有失败的动画尝试:
import pygame as pg
from settings import Music_Mixer, loadCustomFont, States, screen
from time import sleep
"""This section contains entity states which are separate from game and menu states."""
class Player():
def __init__(self, x, y):
self.health = 100
self.speed = 1
self.screen = screen
self.imagex = x
self.imagey = y
self.running_right = False
self.running_left = False
self.rect = pg.draw.rect(self.screen, (255, 0, 0), [self.imagex, self.imagey, 20, 45])
self.image = pg.image.load('Images/Animations/PlayerRun/Stickman_stand_still.png').convert_alpha()
self.images = ['Images/Animations/PlayerRun/Stickman_stand_still.png', 'Images/Animations/PlayerRun/Stickman_run_1.png', 'Images/Animations/PlayerRun/Stickman_run_2.png',
'Images/Animations/PlayerRun/Stickman_run_3.png', 'Images/Animations/PlayerRun/Stickman_run_4.png']
#Moves the player and begins the animation phase.
def move_player(self, speed):
self.pressed = pg.key.get_pressed()
#Move left and start left run animation.
if self.pressed[pg.K_a]:
self.imagex -= 5
#Move right and start right run animation
if self.pressed[pg.K_d]:
self.running_right = True
self.imagex += 5
elif not self.pressed[pg.K_d]:
self.running_right = False
#Move up and start jump animation.
if self.pressed[pg.K_w]:
self.imagey -= 5
#Move down and start either crouch.
if self.pressed[pg.K_s]:
self.imagey += 5
#Animates the running movement of the player.
def runAnim(self):
if self.running_right:
pass
elif self.running_right == False:
self.image = pg.image.load('Images/Animations/PlayerRun/Stickman_stand_still.png').convert_alpha()
#draws the player to the screen.
def draw_entity(self):
screen.blit(self.image, (self.imagex, self.imagey))
【问题讨论】:
标签: python-3.x animation pygame