python - 我的暂停显示如何在 Pygame 中正常工作?
问题描述
因此,在我单击继续所有内容的按钮后,我的暂停显示会不断返回。
这包含在代码中:“def paused(): global pause clock = pygame.time.Clock()”
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.fill(0)
screen.blit(intropic, (0, 0))
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
# print(mouse)
if 270 + 120 > mouse[0] > 270 and 590 + 50 > mouse[1] > 590:
pygame.draw.rect(screen, (0, 0, 0), (268, 588, 124, 54))
pygame.draw.rect(screen, (0, 255, 0), (270, 590, 120, 50))
if click[0] == 1:
pause = False
else:
pygame.draw.rect(screen, (0, 0, 0), (268, 588, 124, 54))
pygame.draw.rect(screen, (100, 255, 100), (270, 590, 120, 50))
if 770 + 150 > mouse[0] > 770 and 590 + 50 > mouse[1] > 590:
pygame.draw.rect(screen, (0, 0, 0), (768, 588, 154, 54))
pygame.draw.rect(screen, (255, 0, 0), (770, 590, 150, 50))
if click[0] == 1:
pygame.quit()
exit()
else:
pygame.draw.rect(screen, (0, 0, 0), (768, 588, 154, 54))
pygame.draw.rect(screen, (255, 100, 100), (770, 590, 150, 50))
healthfont = pygame.font.Font(None, 40)
start = healthfont.render("Weiter", True, (0, 0, 0))
textRect1 = start.get_rect()
textRect1.topright = [370, 603]
screen.blit(start, textRect1)
healthfont = pygame.font.Font(None, 40)
end = healthfont.render("Beenden", True, (0, 0, 0))
textRect1 = end.get_rect()
textRect1.topright = [900, 603]
screen.blit(end, textRect1)
pausefont = pygame.font.Font(None, 80)
pausetxt = pausefont.render("Pause", True, (0, 0, 0))
textRect1 = pausetxt.get_rect()
textRect1.center = [800, 300]
screen.blit(pausetxt, textRect1)
pygame.display.flip()
如果您按 P,这是代码:
if keys[4]:
pause = True
paused()
还有一个全局暂停 = False
任何帮助深表感谢
解决方案
暂停或不暂停,它只是一个布尔值。你的程序必须决定这意味着什么。
也许这意味着屏幕没有更新,也许输入处理不同。
下面是您的代码的重新工作,如果global_paused
设置了暂停标志,屏幕将停止绘制除“*** PAUSED ***”横幅之外的所有内容。
注意pyagme.Rect
存储矩形的使用,颜色的常量,更简单的碰撞测试。字体只需要加载一次,因此它们已移到主循环之外。
global_paused = False
BLACK = (0, 0, 0)
RED = (0, 255, 0)
GREENISH = (100, 255, 100)
area1 = pygame.Rect( 268, 588, 124, 54 )
area2 = pygame.Rect( 270, 590, 120, 50 )
area3 = pygame.Rect(768, 588, 154, 54)
area4 = pygame.Rect(770, 590, 150, 50)
healthfont = pygame.font.Font(None, 40)
start = healthfont.render("Weiter", True, BLACK )
textRect1 = start.get_rect()
textRect1.topright = [370, 603]
healthfont = pygame.font.Font(None, 40)
end = healthfont.render("Beenden", True, BLACK )
textRect1 = end.get_rect()
textRect1.topright = [900, 603]
pausefont = pygame.font.Font(None, 80)
pausetxt = pausefont.render("Pause", True, BLACK )
textRect1 = pausetxt.get_rect()
textRect1.center = [800, 300]
pause_mode = pausefont.render( "*** PAUSED ***", True, RED, BLACK )
clock = pygame.time.Clock()
exiting = False
while not exiting:
mouse_click = None
mouse_pos = pygame.mouse.get_pos()
# Handle Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif ( event.type == pygame.MOUSEBUTTONUP ):
mouse_click = pygame.mouse.get_pressed()
if ( area1.collidepoint( event.pos ) ):
global_paused = not global_paused
elif ( area3.collidepoint( event.pos ) ):
exiting = True
elif ( event.type == pygame.KEYUP ):
if ( event.key == pygame.K_p ):
global_paused = not global_paused
# draw the screen
screen.fill( BLACK )
if ( not global_paused ):
screen.blit(intropic, (0, 0))
pygame.draw.rect(screen, BLACK, area1 )
if ( area1.collidepoint( mouse_pos ) ):
pygame.draw.rect(screen, RED, area2 )
else:
pygame.draw.rect(screen, GREENISH, area2 )
pygame.draw.rect(screen, BLACK, area3 )
pygame.draw.rect(screen, RED, area4 )
else:
pygame.draw.rect(screen, GREENISH, area4 )
screen.blit(start, textRect1)
screen.blit(end, textRect1)
screen.blit(pausetxt, textRect1)
else:
# everything is paused
screen.blit( pause_mode, ( 0, 0 ) )
pygame.display.flip()
clock.tick( 60 )
pygame.quit()
推荐阅读
- javascript - Firebase以选择形式显示数据 - 角度
- testing - 如何在 Leiningen 中声明测试范围依赖项?
- node.js - 我突然无法使用 Typescript 在 node.js 中安装 Express
- airflow - 在 AirflowUI 触发 DAG 时传递参数或配置
- c++ - 声明成员对象而不调用其默认构造函数
- dart - 在 Dart 语言中使用实验性扩展方法时的类型推断
- php - 如何在 Laravel 生成的 registerController 验证器方法中应用验证
- python - 如何在 form_valid 中获取对象 ID?
- php - 在 try-catch 块中继续当前迭代 | PHP
- java - 如何使用二进制改造上传图像?