如何在不更改游戏FPS的情况下更改动画速度?

时间:2019-01-20 16:08:17

标签: python animation pygame sprite frame-rate

我终于想出了如何为我的精灵设置动画,但是现在我遇到了一个新问题。以我想要的FPS(60)运行游戏时,角色动画太快了。动画在10FPS左右时看起来很流畅,但在该帧速率下游戏看起来很不稳定。我的游戏有可能以60FPS运行,而动画以单独的FPS(例如10)运行吗?任何帮助表示赞赏!

Images and Sound FX Dowload

我的代码:

import pygame
import random
import time
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 20)
pygame.init()

SIZE = W, H = 400, 700
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# colours
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BACKGROUND = (94, 194, 222)
STRIPE = (60, 160, 190)
LANELINE = (255, 255, 255)

x1 = 30
x2 = 330
lane1 = 30
lane2 = 130
lane3 = 230
lane4 = 330
y = 530
width = 40
height = 64

toggle1 = 0
toggle2 = 0

target_x1 = 30
target_x2 = 330
vel_x = 10

def drawScene():
    screen.fill(BACKGROUND)
    pygame.draw.polygon(screen, STRIPE, ((200, 700), (300, 700), (400, 600), (400, 500)))
    pygame.draw.polygon(screen, STRIPE, ((0, 700), (100, 700), (400, 400), (400, 300)))
    pygame.draw.polygon(screen, STRIPE, ((0, 500), (0, 600), (400, 200), (400, 100)))
    pygame.draw.polygon(screen, STRIPE, ((0, 300), (0, 400), (400, 0), (300, 0)))
    pygame.draw.polygon(screen, STRIPE, ((0, 100), (0, 200), (200, 0), (100, 0)))
    pygame.draw.line(screen, LANELINE, (100, 0), (100, 700), 2)
    pygame.draw.line(screen, LANELINE, (200, 0), (200, 700), 4)
    pygame.draw.line(screen, LANELINE, (300, 0), (300, 700), 2)


mainsheet = pygame.image.load("dolphinSheet.png").convert()
sheetSize = mainsheet.get_size()
horiz_cells = 6
vert_cells = 1
cell_width = int(sheetSize[0] / horiz_cells)
cell_height = int(sheetSize[1] / vert_cells)

cellList = []
for vert in range(0, sheetSize[1], cell_height):
    for horz in range(0, sheetSize[0], cell_width):
        surface = pygame.Surface((cell_width, cell_height))
        surface.blit(mainsheet, (0, 0),
                     (horz, vert, cell_width, cell_height))
        colorkey = surface.get_at((0, 0))
        surface.set_colorkey(colorkey)
        cellList.append(surface)

cellPosition = 0

# main loop

while True:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                pygame.mixer.music.load('percussiveHit.mp3')
                pygame.mixer.music.play()
                toggle1 += 1
                if toggle1 % 2 == 1:
                    target_x1 += 100
                else:
                    target_x1 -= 100
            elif event.key == pygame.K_d:
                pygame.mixer.music.load('percussiveHit.mp3')
                pygame.mixer.music.play()
                toggle2 += 1
                if toggle2 % 2 == 1:
                    target_x2 -= 100
                else:
                    target_x2 += 100

    if x1 < target_x1:
        x1 = min(x1 + vel_x, target_x1)
    else:
        x1 = max(x1 - vel_x, target_x1)

    if x2 < target_x2:
        x2 = min(x2 + vel_x, target_x2)
    else:
        x2 = max(x2 - vel_x, target_x2)

    if cellPosition < len(cellList) - 1:
        cellPosition += 1
    else:
        cellPosition = 0

    drawScene()
    pygame.draw.rect(screen, RED, (x1, y, width, height))
    pygame.draw.rect(screen, RED, (x2, y, width, height))

    screen.blit(cellList[cellPosition], (x1 + 4, y - 1))
    screen.blit(cellList[cellPosition], (x2 + 4, y - 1))
    # players
    # screen.blit(playerImg, (x1 + 4, y - 5))
    # screen.blit(playerImg, (x2 + 4, y - 5))
    pygame.display.update()

pygame.quit()

1 个答案:

答案 0 :(得分:0)

根据实时毫秒延迟而不是每帧更新图像。使用pygame.time.get_ticks()(返回自pygame.init()起的毫秒数)来基于特定时间更新图像。

例如:

MS_FRAME_TIME = 100  # Mow many milliseconds a frame is shown for

...

last_paint_at = 0    # Start condition to ensure first paint

...

while True:
    clock.tick(60)

    ticks = pygame.time.get_ticks()  # millliseconds since start

    # If enough milliseconds have elapsed since the last frame, update!
    if ( ticks - last_paint_at > MS_FRAME_TIME ):
        last_paint_at = ticks

        if ( cellPosition < len(cellList) - 1 ):
            cellPosition += 1
        else:
            cellPosition = 0

        screen.blit(cellList[cellPosition], (x1 + 4, y - 1))
        screen.blit(cellList[cellPosition], (x2 + 4, y - 1))

如果它适合您的代码,则还可以简单地查看选择动画帧的时间模数。