【问题标题】:Walking Animation Speed行走动画速度
【发布时间】:2014-05-01 22:08:51
【问题描述】:

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

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

为清晰起见进行了编辑。

【问题讨论】:

  • 大声笑,我知道你已经看到了这一点,但是代码示例会非常有帮助。
  • 由于某种原因,代码在代码框中无法正确显示。
  • 大声笑,你们太可笑了!你真的删除“谢谢”?真是一群强迫症。

标签: python performance animation pygame


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2013-11-05
    • 2017-01-18
    • 1970-01-01
    相关资源
    最近更新 更多