我试图在 pygame 上解决这个不流畅和不稳定的动画和动作,我一直在尝试一切,但没有任何效果

Im trying to solve this un-smooth and choppy animations and movements on pygame, i've been trying everything but nothing works

这里是动画的视频https://www.youtube.com/watch?v=uhPdN3v8vg0

其他 pygame 代码不是这样的,只有这个特定的,所以我很确定它不是硬件问题

import sys
import time
import pygame
import os
from pygame import mixer
 pygame.init()
pygame.mixer.init()
width,height=(900,500)

border= pygame.Rect(0,0,6,height)
WIN=pygame.display.set_mode((width,height))
bg= pygame.image.load(os.path.join('','pixel_bg.jpg'))
bg=pygame.transform.scale(bg,(width,height))
person1=pygame.image.load(os.path.join('','mario.png'))
p1_width, p1_height = (50,60)
person1=pygame.transform.scale(person1,(p1_width,p1_height))
black=(0,0,0)
p1_rect = pygame.Rect(50,340,p1_width,p1_height)
pygame.display.set_caption("rpg game")
Velocity= 4
jump_height = 50
FPS = 60
mixer.music.load("adventurebeat.mp3")
mixer.music.play(-1)

def draw():
    WIN.blit(bg, (0, 0))
    WIN.blit(person1, (p1_rect.x,p1_rect.y))
    pygame.display.update()
def p1_movement(keys_pressed, p1_rect):
    if keys_pressed[pygame.K_a] and p1_rect.x - Velocity > border.x + border.width:  # left
        p1_rect.x -= Velocity
    if keys_pressed[pygame.K_d] and p1_rect.x + Velocity + p1_rect.width < width:  # right
        p1_rect.x += Velocity
    if keys_pressed[pygame.K_w] and p1_rect.y - Velocity > 0:  # up
        p1_rect.y -= Velocity
    if keys_pressed[pygame.K_SPACE] and p1_rect.y - Velocity > 0:  # up
        p1_rect.y -= jump_height
    if keys_pressed[pygame.K_s] and p1_rect.y + Velocity + p1_rect.height < height:  # down
        p1_rect.y += Velocity

def main():

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick_busy_loop(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                sys.exit()
            draw()
        keys_pressed = pygame.key.get_pressed()
        p1_movement(keys_pressed,p1_rect)
main()

我已经尝试将 clock.tick_busy_loop() 更改为 clock.tick() 但它仍然不起作用:/

您需要draw应用程序循环而不是事件循环中的对象:

def main():

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick_busy_loop(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                sys.exit()
            
            # draw()   <--- DELETE
   
        keys_pressed = pygame.key.get_pressed()
        p1_movement(keys_pressed,p1_rect)

        draw()       # <--- INSERT

应用程序循环在每一帧中执行,但事件循环仅在事件发生时执行。