我的游戏在一个while True循环中运行,我希望能够要求用户“再玩一次?”我已经有了弹出文本的rect的代码,但我需要一种方法,让用户单击rect或点击y表示是,然后代码就会自动运行。
发布于 2012-12-21 12:01:45
让循环有它自己的方法,从main()运行中调用它,当游戏结束和结束时,让main()询问用户是否想再玩一次,如果想要,在将所有内容都设置为默认值后调用主循环
或
只需要再次要求清除并重新初始化所有变量作为默认值,并让循环,如果用户想要再次播放
发布于 2013-01-07 09:24:14
要判断用户是否点击了矩形,只需在鼠标位置设置一个1*1的矩形,然后当用户单击时,检查矩形是否发生碰撞。然后,就像DeadChex说的那样,通过让循环成为它自己的函数来再次调用它。
发布于 2013-12-07 16:48:18
运行我专门编写的这个函数。把它调整到你的心意。如果播放器死了(或者发生了什么事情使播放文本再次出现),您只需调用play_again()
import pygame
pygame.init()
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
bigfont = pygame.font.Font(None, 80)
smallfont = pygame.font.Font(None, 45)
def play_again():
text = bigfont.render('Play again?', 13, (0, 0, 0))
textx = SCREEN_WIDTH / 2 - text.get_width() / 2
texty = SCREEN_HEIGHT / 2 - text.get_height() / 2
textx_size = text.get_width()
texty_size = text.get_height()
pygame.draw.rect(screen, (255, 255, 255), ((textx - 5, texty - 5),
(textx_size + 10, texty_size +
10)))
screen.blit(text, (SCREEN_WIDTH / 2 - text.get_width() / 2,
SCREEN_HEIGHT / 2 - text.get_height() / 2))
clock = pygame.time.Clock()
pygame.display.flip()
in_main_menu = True
while in_main_menu:
clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
in_main_menu = False
pygame.display.quit()
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
if x >= textx - 5 and x <= textx + textx_size + 5:
if y >= texty - 5 and y <= texty + texty_size + 5:
in_main_menu = False
break
play_again()
pygame.display.quit()
pygame.quit()我希望这就是你要找的答案。
https://stackoverflow.com/questions/13984066
复制相似问题