首页 > 解决方案 > Pygame - 即使我正在更新我的图像,也会出现奇怪的踪迹。这是怎么回事?

问题描述

正在发生的事情的图像

大家好,所以我是 pygame 的新手,我正在尝试做一些相当简单的事情。只需在屏幕上移动图像。我以前做过,没有遇到过这个问题。我所做的唯一更改是我现在将 1 个图像放在另一个图像之上。

出于某种奇怪的原因,这会导致留下痕迹。

我试过清除屏幕并重新绘制,但没有得到积极的结果。

任何帮助,将不胜感激。

def main():

    pygame.init()
    screen = pygame.display.set_mode((700,680),0,32)
    clock = pygame.time.Clock()

    bgd_image = pygame.image.load("Grid.png").convert()
    #X Motor Pieces
    MotorXbase = pygame.image.load("MotorXBase.png").convert()
    MotorXbase.set_colorkey((34,177,76))
    MotorXMovePiece = pygame.image.load("MotorXMovePiece.png").convert()
    MotorXMovePiece.set_colorkey((34,177,76))


    #Y Motor Pieces
    MotorYbase = pygame.image.load("MotorYBase.png").convert()
    MotorYbase.set_colorkey((34,177,76))
    MotorYMovePiece = pygame.image.load("MotorYMovePiece.png").convert()
    MotorYMovePiece.set_colorkey((34,177,76))

    screen.fill([34,177,76])
    black = (0,0,0)
    running = True
    xpos = 16
    ypos = 14
    xstep = 1
    ystep = 1
    while running:
        screen.blit(bgd_image,(0,0))
        screen.blit(MotorXbase, [50,550])
        MotorXbase.blit(MotorXMovePiece,[xpos,18])

        screen.blit(MotorYbase, [550,50])
        MotorYbase.blit(MotorYMovePiece,[20,ypos])


        clock.tick(60)
        pygame.display.update()
        screen.fill(black)

        xpos += xstep
        ypos += ystep
        if xpos >399 or xpos <16:
            xstep = -xstep

        if ypos > 397 or ypos < 14:
            ystep = -ystep





    # event handling, gets all event from the eventqueue
        for event in pygame.event.get():
            # only do something if the event is of type QUIT
            if event.type == pygame.QUIT:
                # change the value to False, to exit the main loop
                running = False

标签: pythonpygamemotionblit

解决方案


来自评论:

好吧,您将片段图像传送到基本图像上,而不是直接到屏幕上。这是一个破坏性操作,您必须保留基本映像的未修改副本,或者从磁盘重新加载它们,以撤消 blit。


推荐阅读