我需要清除 Pygame 中的屏幕,但是一旦我松开按钮,窗口就会恢复到原始状态

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

我已将我认为存在的问题用斜体字表示。按下的按钮是“start_rect”,这当然是开始按钮。尝试将“如果”更改为“何时”会导致游戏冻结。网上查了一下,似乎没有太多关于按下按钮时清除的内容。有什么办法可以解决这个问题吗?

import pygame
from sys import exit

pygame.init()

boot = True
start = False
clear = False

screen = pygame.display.set_mode((750,450))
pygame.display.set_caption('Appalachia: Chapter 1')


title_font = pygame.font.Font('chapter1/Pixeltype.ttf',150)
sub_font = pygame.font.Font('chapter1/Pixeltype.ttf',80)


title_surface1 = title_font.render('x', True, 'white')
title_rect1 = title_surface1.get_rect(topleft = (125,60))

title_surface2 = sub_font.render('y', True, 'white')
title_rect2 = title_surface2.get_rect(topleft = (170,170))

start_surface = sub_font.render('z', True, 'white')
start_rect = start_surface.get_rect(topleft = (100,320))

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.QUIT()
            exit
    if boot:
        screen.fill('black')
        screen.blit(title_surface1,title_rect1)
        screen.blit(title_surface2,title_rect2)
        screen.blit(start_surface,start_rect)

        *if event.type == pygame.MOUSEBUTTONDOWN:
         if start_rect.collidepoint(event.pos):
          clear = True

        if clear:
         screen.fill ('black')
         clear = False*



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

就像我说的,我尝试将“如果”切换为“何时”,但在那之后就卡住了。

python pygame fill
1个回答
0
投票

感谢评论者的帮助,我能够为我自己的项目重新调整一些代码的用途。我需要创建一个仅运行一次的“current_screen”变量。主菜单是屏幕 1,当按下按钮时,我会转到屏幕 2。

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