python - 行走动画速度

标签 python performance animation pygame

这是一个概念问题,因此请注意。

我的动画工作正常,我循环遍历图像数组,并在适当的时候显示它们。 我唯一的问题是让迭代速度减慢(现在图像以 28 FPS 的速度变化,太快了)。我不是在寻找具体的代码,只是寻找总体思路,然后我会弄清楚如何实现它。

import pygame
import os
import sys
import time
import random

cameraX, cameraY = (0,0)
width, height = 600, 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Frigid Development")
sprite_list = pygame.sprite.Group()
clock = pygame.time.Clock()
tileSize = 32


class Player(pygame.sprite.Sprite):
    def __init__(self, cameraX, cameraY):
        pygame.sprite.Sprite.__init__(self)
        self.images = [pygame.image.load("C:\Users\Software Development\Desktop\Test00.png"), pygame.image.load("C:\Users\Software Development\Desktop\Test01.png"), pygame.image.load("C:\Users\Software Development\Desktop\Test02.png")]
        self.index = 0
        self.image = self.images[self.index]
        self.rect = self.images[self.index].get_rect()
        self.rect.x, self.rect.y, = 200, 300
    def update(self):
        isint = isinstance(self.index, (int, long))
        if self.index < 0 and isint:
            self.index = 2
        if self.index >= len(self.images) and isint:
            self.index = 0
        if isint:
            self.image = self.images[self.index]
            self.rect.x, self.rect.y = (self.rect.x - cameraX), (self.rect.y - cameraY)
            screen.blit(self.image, (self.rect.x, self.rect.y))
        else:
            pass

class MapControl(object):
    def __init__(self, cameraX, cameraY):
        self.tile_dict = {0: pygame.image.load("C:\Users\Software Development\Desktop\Tile00.png"), 1: pygame.image.load("C:\Users\Software Development\Desktop\Tile01.png")}
        self.tileX = 0
        self.tileY = 0

    def LoadMap(self, map, cameraX, cameraY):
        self.tileX = cameraX
        self.tileY = cameraY
        for x in map:
            for tile in x:
                    if tile == '0':
                        screen.blit(self.tile_dict[0], (self.tileX, self.tileY))
                        self.tileX = self.tileX+32
                    if tile == '1':
                        screen.blit(self.tile_dict[1], (self.tileX, self.tileY))
                        self.tileX = self.tileX+32
                    if tile == '\n':
                        self.tileX = cameraX
                        self.tileY += 32
            self.tileX = cameraX
            self.tileY = cameraY

    def CalcMapBorders(self, map):
        self.length = 0
        self.count = 0
        i = True
        while i:
            for ba in map:
                with open('C:\Users\Software Development\Desktop\Map00L0.txt', 'r') as f:
                    for line in f:
                        self.length += 1
                        self.count = len(list(line.strip('\n')))
                    i = False   
file = open('C:\Users\Software Development\Desktop\Map00L0.txt', 'r')   
map00ly0 = list(file.read())    
map = [map00ly0]
def Loop(screen, map, cameraX, cameraY):
    cameraX, cameraY = 0,0
    player = Player(cameraX, cameraY)
    mapcontrol = MapControl(cameraX, cameraY)
    while True:
        sprite_list.add(player) 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
        key = pygame.key.get_pressed()
        if key[pygame.K_RIGHT]:
            player.rect.x += 3
        if key[pygame.K_LEFT]:
            player.rect.x -= 3
        if not key[pygame.K_UP] and not key[pygame.K_DOWN]:
            player.index = 0
        if key[pygame.K_DOWN]:
            player.rect.y += 3
            player.index -= 1
        if key[pygame.K_UP]:
            player.rect.y -= 3
            player.index += 1

        screen.fill((0,0,0))
        mapcontrol.CalcMapBorders(map)
        mapcontrol.LoadMap(map, cameraX, cameraY)
        if player.rect.y > height - 15:
            player.rect.y -= 3
            cameraY -= 3
        if player.rect.y < 0:
            player.rect.y += 3
            cameraY += 3
        if player.rect.x > width - 15:
            player.rect.x -= 3
            cameraX -= 3
        if player.rect.x < 0:
            player.rect.x += 3
            cameraX += 3
        if player.rect.y < cameraY:
            cameraY -= 3
        if player.rect.x < cameraX:
            cameraX -= 3
        if player.rect.y > (cameraY + (mapcontrol.length*32) - 20):
            cameraY += 3
        if player.rect.x > (cameraX + (mapcontrol.count*32) - 20):
            cameraX += 3
        sprite_list.update()
        sprite_list.draw(screen)
        pygame.display.update()
        clock.tick(29)
Loop(screen, map, cameraX, cameraY)

为清楚起见进行了编辑。

最佳答案

如果您想让游戏以更高的 FPS 运行,但以您想要的任何速率运行动画,您必须简单地根据耗时计算要使用的帧。

举个简单的例子,假设您的行走动画有 10 帧,一步应持续 1 秒。然后,您只需在 0.1 秒过去后更改行走 Sprite 上的帧即可。

关于python - 行走动画速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23417483/

相关文章:

ios - 如何沿圆形路径为 UIView 设置动画?

python - 使用 plt.imshow 更快的刷新率

python - 按键(整数)对字典进行有效排序,返回排序的值列表

mysql - 主键会加速索引吗?

c - 为什么添加线程会使这个简单的套接字服务器更快?

ios - 动画 UIWebView 在动画期间不响应触摸

python - 在遍历列表时如果字符串不在项目中则计算一次 - python 3.x

python - OpenCV:增强视频流源 .png 未按预期呈现透明度

python - 具有不可迭代对象的 python 类中的错误

WPF - 颜色动画完成事件