python - 如何在没有```screen.fill((0, 0, 0)```的情况下清除屏幕?
问题描述
我正在制作一个游戏,当您触摸墙壁时,您必须单击一个矩形才能继续前进。
然而,矩形被显示,但前一个屏幕也是如此。
我不能使用screen.fill((0, 0, 0))
,因为在循环中,它会继续运行。我想删除屏幕(或暂时撤回。这是首选)。
这是我的代码:
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()
提前致谢!!
解决方案
我摆脱了尝试和除外,我认为它可以满足您的需求
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
推荐阅读
- javascript - 更改粘贴文本的颜色
- python-3.x - 跨多维数组、基于文本的游戏的“移动”问题
- logging - Pycharm 无法识别日志格式
- c# - 我如何从 texboxes 的名称中获取值
- java - 使用 Jsoup 抓取网站数据时遇到问题
- powershell - 无法在带有 LSP 的 Sublime Text 3 中使用 Powershell 智能感知
- go - 连接 3 个或更多切片的最简洁方法
- javascript - 检测所选文本无法正常工作
- c++ - ROS 和 OpenCV4 的 DNN 模块的命名空间错误
- awesome-wm - 在当前标签中启动应用程序的快捷方式,或者如果已经启动,则在真棒 wm 中引入当前标签