在pygame中移动图像后如何删除它?

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

我试图在移动后删除上一张图像。这是我如何移动图像的代码(unit0

def Move(x, y):
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            Background.blit(RedInfantry,(-x*64, 0))
        if event.key == pygame.K_RIGHT:
            Background.blit(RedInfantry,(x*64, 0))
        if event.key == pygame.K_UP:
            Background.blit(RedInfantry,(0, -y*64))
        if event.key == pygame.K_DOWN:
            Background.blit(RedInfantry,(0, y*64))

起始位置在左上角,而不是图像所在的位置。此外,一旦移动,我也不知道如何删除该图像。如果我在不同方向上移动了两次,则会创建分开的图像,而不是删除最后一个图像。This is the image when the down and right button is pressed

如何删除上一张图像?

python image pygame grid
1个回答
0
投票

根据先前的一个问题(Image loading using pygame,您必须更改图像在网格中的位置:

if event.type == pygame.KEYDOWN:

    new_x, new_y = x, y
    if event.key == pygame.K_LEFT:
        new_x -= 1
    if event.key == pygame.K_RIGHT:
        new_x += 1
    if event.key == pygame.K_UP:
        new_y -= 1
    if event.key == pygame.K_DOWN:
        new_x += 1

    grid[new_x][new_y] = grid[x][y]
    grid[x][y] = None

当然,您必须在每一帧中重新绘制整个场景(网格和图像)。(如Image loading using pygame的答案)

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