-1

我该如何减慢速度?现在它立即向下/向左/向上/向右。我知道它与时间模块有关,但我不知道如何实现它。基本上我想把这个过程从 10 年减慢到 490 年。不要添加延迟,而是减慢过程,就像让它慢动作一样。我什至尝试了 120fps 的 pygame 时钟,但它不起作用

import time
import pygame
from pygame.locals import *

clock = pygame.time.Clock()
fps = 120

pygame.init()
surface = pygame.display.set_mode((600, 600))
background = pygame.image.load('back.png')
surface.blit(background, (0, 0))
block = pygame.image.load('block.png').convert()
block_y = 0
block_x = 0
surface.blit(block, (block_x, block_y))

def draw():
    surface.blit(background, (0, 0))
    surface.blit(block, (block_x, block_y))
    pygame.display.flip()

pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            if event.key == K_UP:
                block_y -= 10
                draw()
            if event.key == K_DOWN:
                block_y += 10
                draw()
            if event.key == K_LEFT:
                block_x -= 10
                draw()
            if event.key == K_RIGHT:
                block_x += 10
                draw()
        elif event.type == QUIT:
            running = False
        elif event.type == KEYUP:
            clock.tick(fps)
            if event.key == K_UP:
                block_y -= 10
                draw()
            if event.key == K_DOWN:
                block_y += 490
                draw()
            if event.key == K_LEFT:
                block_x -= 100
                draw()
            if event.key == K_RIGHT:
                block_x += 510
                draw()
    ```
4

1 回答 1

0

这可能不是一个很好的解决方案,因为它不是独立于帧速率的(请参阅 FaranAiki 的评论),但是您可以使用(更改speed为,呃,更改速度。您可能还想降低帧速率):

import time
import pygame
from pygame.locals import *

clock = pygame.time.Clock()
fps = 120
speed=1
pygame.init()
surface = pygame.display.set_mode((600, 600))
background = pygame.image.load('back.png')
surface.blit(background, (0, 0))
block = pygame.image.load('block.png').convert()
block_y = 0
block_x = 0
surface.blit(block, (block_x, block_y))

def draw():
    surface.blit(background, (0, 0))
    surface.blit(block, (block_x, block_y))
    pygame.display.flip()

pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            if event.key == K_UP:
                block_y -= speed
                draw()
            if event.key == K_DOWN:
                block_y += speed
                draw()
            if event.key == K_LEFT:
                block_x -= speed
                draw()
            if event.key == K_RIGHT:
                block_x += speed
                draw()
        elif event.type == QUIT:
            running = False
        elif event.type == KEYUP:
            clock.tick(fps)
            if event.key == K_UP:
                block_y -= 10
                draw()
            if event.key == K_DOWN:
                block_y += 490
                draw()
            if event.key == K_LEFT:
                block_x -= 100
                draw()
            if event.key == K_RIGHT:
                block_x += 510
                draw()
于 2021-07-02T07:05:49.143 回答