为什么运动图像在停止和启动太快时会留下自己的重复


def function():
    import pygame
    pygame.init()
    blue = 0,255,255
    red = 255,0,0
    white = 255,255,255
    Location = 136
    Level = 432
    SKY = 0
    space1 = 200
    space1 = 200
    WaterLevel = 452
    Left_Rect = pygame.Rect(0,452,135,100)
    Right_Rect = pygame.Rect(137,452,135,100)
    CLOCK = pygame.time.Clock()
    FPS = 30
    gameDisplay = pygame.display.set_mode((272,552))
    pygame.display.set_caption('Boat Game')
    boat = pygame.image.load("FinalBoat.png").convert_alpha()
    boat = pygame.transform.scale(boat, (40,25))
    stop = False
    is_Left = False
    is_Right = False
    while not stop:

####### Background ###############################################
        pygame.draw.rect(gameDisplay, white,(0,SKY,272,552))
        SKY -= 1
####### Controles #################################################
        pygame.draw.rect(gameDisplay, red, Left_Rect)
        pygame.draw.rect(gameDisplay, red, Right_Rect)
####### Boat #################################################
        gameDisplay.blit(boat, (Location, Level))
####### Water ################################################
        pygame.draw.rect(gameDisplay,blue,(0,WaterLevel,272,100))
####### Movement ##############################################

        pygame.display.update()
        for event in pygame.event.get():
            print event
            if event.type == pygame.MOUSEBUTTONDOWN:
                is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos())
                is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos())
            if event.type == pygame.MOUSEBUTTONUP:
                is_Left = False
                is_Right = False
            if Location == 0:
                is_Left = False
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        if is_Left:
            Location -= 5
        elif is_Right:
            Location += 5
        CLOCK.tick(FPS)
        pygame.display.update()
function()

这很难解释,但我有一个游戏,其中船的图像在屏幕上水平移动,并通过单击底角的矩形来控制。当我停止和开始太快或太突然地改变方向时,图像会在屏幕上以轨迹复制自己。踪迹不会消失。我希望有人能解释这种奇怪的异常情况。

出现轨迹,因为屏幕应清除每一帧。您是通过绘制白色背景来执行此操作的。但是在您的代码中是将白色矩形向上移动的行SKY -= 1,现在青色按钮区域和天空之间有一个间隙,不会每帧都清除。现在,当船移动时,从之前的框架中移动的图片将不清楚。

编辑:我不熟悉pygame,但有两个pygame.display.update(),我认为你只需要一个。

最新更新