简体   繁体   English

如何在没有 ```screen.fill((0, 0, 0)``` 的情况下清除屏幕?

[英]How to clear screen without ```screen.fill((0, 0, 0)```?

I am making a game where when you touch the wall, you have to click a rectangle to move on.我正在制作一个游戏,当您触摸墙壁时,您必须单击一个矩形才能继续前进。

However, the rectangle is displayed, but so is the previous screen.但是,显示了矩形,但也显示了前一个屏幕。

I can't use screen.fill((0, 0, 0)) because in the loop, it will just keep going.我不能使用screen.fill((0, 0, 0))因为在循环中,它会继续运行。 I want to delete the screen(or withdraw it temporarily. This is preferred).我想删除屏幕(或暂时撤回。这是首选)。

Here is my code:这是我的代码:

import pygame
pygame.init()

clock = pygame.time.Clock()

screen = pygame.display.set_mode((400, 300))
done = False
x = 100

y = 100
#button1 = pygame.draw.rect(screen, (0, 0, 255), (200, 200, 30, 30))

    #if check <= pos - (w/2) and check >= 
pygame.display.set_caption("Auto Maze!")
donk = False
while not done:
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse = event.pos
            try:
                assert button1.collidepoint(mouse)
            except AssertionError:
                pass
            except NameError:
                pass
            else:
                donk = True
    pressed = pygame.key.get_pressed()

    if pressed[pygame.K_w]:
        y -= 5
    elif pressed[pygame.K_s]:
        y += 5
    elif pressed[pygame.K_a]:
        x -= 5
    elif pressed[pygame.K_d]:
        x += 5
    screen.fill((0, 0, 0))

    try:
        assert player.colliderect(wall1)
    except AssertionError:
        pass
    except NameError:
        pass
    else:
        death_screen = pygame.display.set_mode((400, 300))
        button1 = pygame.draw.rect(death_screen, (0, 0, 255), (200, 200, 30, 30))
        if donk:
            break


    player = pygame.draw.rect(screen, (0, 255, 0), (x, y, 60, 60))
    wall1 = pygame.draw.rect(screen, (255, 0, 0), (400, 300, -100, -300))

    clock.tick(60)  
    pygame.display.flip()

quit()

Thanks in advance!!提前致谢!!

I got rid of the try and except and i think it does what you want我摆脱了尝试和例外,我认为它可以满足您的要求

player = pygame.draw.rect(screen, (0, 255, 0), (x, y, 60, 60))
wall1 = pygame.draw.rect(screen, (255, 0, 0), (300, 0, 100, 300))    


if player.colliderect(wall1):
    death_screen = pygame.display.set_mode((400, 300))
    button1 = pygame.draw.rect(death_screen, (0, 0, 255), (200, 200, 30, 30))
    if donk:
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM