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

时间:2015-04-05 00:59:38

标签: python python-2.7 pygame

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()

很难解释,但我有一个游戏,船的图像在屏幕上水平移动,并通过点击底角的矩形进行控制。当我停止并快速启动或突然改变方向时,图像会在屏幕上的轨迹中重复。小道不会消失。我希望有人可以解释这个奇怪的异常现象。

1 个答案:

答案 0 :(得分:1)

显示路径,因为屏幕应清除每一帧。你是通过绘制白色背景这样做的。但是在你的代码中是行SKY -= 1,它将白色矩形向上移动,现在青色按钮区域和天空之间有一个间隙,每个帧都不会被清除。现在,当船只移动之前从框架移动的图片时,将无法清除。

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