[英]How to slow down sprite animation while maintaining 60 fps in pygame?
[英]How Do I Slow Down Sprite FPS in Pygame?
所以我在这里有我的精灵,我想知道如何在不减慢游戏速度的情况下减慢速度? 正如你在视频中看到的我的精灵的视频它移动得很快,我想放慢它我怎么能这样做
class snow:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.games = [
pygame.image.load("key1.png"),
pygame.image.load("key2.png"),
pygame.image.load("key3.png"),
pygame.image.load("key4.png"),
pygame.image.load("key5.png"),
pygame.image.load("key6.png"),
pygame.image.load("key7.png")]
self.anim_index = 0
self.rect = pygame.Rect(x,y,height,width)
self.direction = "idk"
def draw(self):
self.rect.topleft = (self.x,self.y)
if self.direction == "idk":
window.blit(self.games[self.anim_index],self.rect)
self.anim_index += 1
if self.anim_index == len(self.games):
self.anim_index = 0
如果要减慢 animation 的速度,则必须将 animation 索引除以某个数字。 添加一个属性 ( anim_frames
),该属性定义 animation 图像的帧数,并将anim_index
除以anim_frames
。 商是当前图像索引。 如果超过最大索引,则重新启动 animation ( anim_index = 0
)。 因此anim_frames
控制 animation 的速度。 例如:
class snow:
def __init__(self,x,y,height,width,color):
# [...]
self.anim_index = 0
self.anim_frames = 10
# [...]
def draw(self):
self.rect.topleft = (self.x,self.y)
if self.direction == "idk":
img_index = self.anim_index // self.anim_frames
if img_index >= len(self.games):
img_index = 0
self.anim_index = 0
self.anim_index += 1
window.blit(self.games[img_index], self.rect)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.