首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame重启?

Pygame重启?
EN

Stack Overflow用户
提问于 2012-12-21 11:58:21
回答 4查看 22.2K关注 0票数 4

我的游戏在一个while True循环中运行,我希望能够要求用户“再玩一次?”我已经有了弹出文本的rect的代码,但我需要一种方法,让用户单击rect或点击y表示是,然后代码就会自动运行。

EN

回答 4

Stack Overflow用户

发布于 2012-12-21 12:01:45

让循环有它自己的方法,从main()运行中调用它,当游戏结束和结束时,让main()询问用户是否想再玩一次,如果想要,在将所有内容都设置为默认值后调用主循环

只需要再次要求清除并重新初始化所有变量作为默认值,并让循环,如果用户想要再次播放

票数 1
EN

Stack Overflow用户

发布于 2013-01-07 09:24:14

要判断用户是否点击了矩形,只需在鼠标位置设置一个1*1的矩形,然后当用户单击时,检查矩形是否发生碰撞。然后,就像DeadChex说的那样,通过让循环成为它自己的函数来再次调用它。

票数 0
EN

Stack Overflow用户

发布于 2013-12-07 16:48:18

运行我专门编写的这个函数。把它调整到你的心意。如果播放器死了(或者发生了什么事情使播放文本再次出现),您只需调用play_again()

代码语言:javascript
复制
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()

我希望这就是你要找的答案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13984066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档