Skip to main content
 首页 » 编程设计

python之行走动画速度

2024年11月01日9落叶无声

这是一道概念题,请注意。

我的动画工作正常,我循环播放图像数组,并在适当的时候显示它们。 我唯一的问题是让迭代变慢(现在图像以 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 的帧即可。