当我按住方向时,pygame 中的玩家会闪烁(初学者代码)

问题描述 投票:0回答:1

我在 pygame 中的玩家是一个矩形,当朝一个方向连续移动时会变黑,直到我松开按键,所以动画非常卡顿

我尝试在 screen.fill 上移动,看看它是否可以修复任何问题,但我似乎无法让它工作

            import pygame
            from pygame.locals import (K_w,K_a,K_s,K_d,QUIT,KEYDOWN,K_ESCAPE)
            pygame.init()

            class Player:
                def __init__(self) -> None:
                    self.surf = pygame.Surface((20,50))
                    self.surf.fill((255,255,255))
                    self.x = 0
                    self.y = 0


                def update(self,keypressed):
                    if keypressed[K_d]:
                        self.x += 10
                        screen.fill((0,0,0))
                        if self.x >= length:
                            self.x = 0

                    elif keypressed[K_s]:
                        self.y += 10
                        screen.fill((0,0,0))
                        if self.y >= length:
                            self.y = 0


                    elif keypressed[K_w]:
                        self.y -= 10
                        screen.fill((0,0,0))
                        if self.y <= 0:
                            self.y = 0


                    elif keypressed[K_a]:
                        self.x -= 10
                        screen.fill((0,0,0))
                        if self.x <= 0:
                            self.x = 0
            length, width = 800,800
            screen = pygame.display.set_mode((length,width))

            player = Player()
            clock = pygame.time.Clock()
            loop = True
            while loop == True:
                clock.tick(40)
                for event in pygame.event.get():
                    if event.type == KEYDOWN:
                        if event.key == K_ESCAPE:
                            loop = False
                    elif event.type == QUIT:
                        loop = False
                
                screen.blit(player.surf,(player.x,player.y))
                keypressed = pygame.key.get_pressed()
                player.update(keypressed)
                pygame.display.flip()
python pygame sprite
1个回答
0
投票

您仅以 40 FPS 运行游戏 -clock.tick(40)。要么将其更改为更高的数字(例如 60 或 144),要么使用增量时间。

© www.soinside.com 2019 - 2024. All rights reserved.