行走动画速度

时间:2014-05-01 22:08:52

标签: 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)

为了清晰而编辑。

1 个答案:

答案 0 :(得分:2)

如果你想让游戏以更高的FPS运行但是以任何你想要的速度运行动画,你必须根据经过的时间计算要使用的帧。

举一个简单的例子,假设你的步行动画中有10帧,一步应该持续1秒。然后你只需在经过0.1秒后改变行走精灵上的帧。