Pygame复制图形

2022-12-01 04:35:58 发布

您现在位置:Python中文网/ 问答频道 /正文

移动“player1”时,屏幕上仍会显示以前的所有位置,留下痕迹。如何使屏幕仅在当前位置应用图像

我使用的是Windows10和Python2.7,我查看了其他人的代码,没有发现与播放器图形有任何不同

应该发生的是,玩家应该能够移动,而不会自己弄脏地图

PLAYER = pygame.image.load('player1.png')
player_position = [16,0]

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == KEYDOWN:
            # right arrow key
            if (event.key == K_RIGHT) and (player_position[0] < MAPWIDTH - 1):
                # player move right
                player_position[0] +=1
            # left arrow key
            elif (event.key == K_LEFT) and (player_position[0] > 0):
                # player move left
                player_position[0] -=1
            # up arrow key
            elif (event.key == K_UP) and (player_position[1] > 0):
                # player move up
                player_position[1] -=1
            # down arrow key
            elif (event.key == K_DOWN) and (player_position[1] < MAPHEIGHT - 1):
                # player move down
                player_position[1] +=1
    #load the player graphics
    view.blit(PLAYER, (player_position[0]*TILESIZE, 
    player_position[1]*TILESIZE))
    pygame.display.update()

Tags: andkeyeventmoveif屏幕typepositionloadpygameplayerelifarrowplayer1
1条回答
网友
1楼 · 发布于 2022-12-01 04:35:58

将其替换为以下内容:

PLAYER = pygame.image.load('player1.png')
player_position = [16,0]

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == KEYDOWN:
            # right arrow key
            if (event.key == K_RIGHT) and (player_position[0] < MAPWIDTH - 1):
                # player move right
                player_position[0] +=1
            # left arrow key
            elif (event.key == K_LEFT) and (player_position[0] > 0):
                # player move left
                player_position[0] -=1
            # up arrow key
            elif (event.key == K_UP) and (player_position[1] > 0):
                # player move up
                player_position[1] -=1
            # down arrow key
            elif (event.key == K_DOWN) and (player_position[1] < MAPHEIGHT - 1):
                # player move down
                player_position[1] +=1

    view.fill((0,0,0))
    #load the player graphics
    view.blit(PLAYER, (player_position[0]*TILESIZE, 
    player_position[1]*TILESIZE))
    pygame.display.update()

添加:view.fill((0,0,0))