为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 python 中的循环外部结束循环

如何从 python 中的循环外部结束循环

慕尼黑5688855 2022-12-06 15:31:15
我正在用 python 3.8.3 制作一个 pygame 游戏,在我的游戏中我有一个开始屏幕,在这个 while 循环中:def game_intro():     intro = True     mixer.music.load("musicIntro.mp3")    mixer.music.set_volume(0.02)     mixer.music.play(-1)     while intro:        for event in pygame.event.get():            if event.type == pygame.QUIT:                 pygame.quit()                quit()            if event.type == pygame.KEYDOWN:                if event.key == pygame.K_RETURN:                    intro = False        screen.blit(startscherm_img, bordrect)         clock.tick(30)        button("Start",302,517,94,44,donkerOranje,lichtOranje,"start")        button("Music",553,517,94,44,donkerOranje,lichtOranje,"toggleMusic")        button("Quit",803,517,94,44,donkerOranje,lichtOranje,"quit")        pygame.display.flip() game_intro()我正在调用上面定义的按钮功能:def button(msg,x,y,w,h,ic,ac,action=None):     mousePos = pygame.mouse.get_pos()     click = pygame.mouse.get_pressed()     if x+w > mousePos[0] > x and y+h > mousePos[1] > y:         pygame.draw.rect(screen, ac, (x,y,w,h))         if click[0] == 1 and action != None:             if action == "toggleMusic":                 PAUSE.toggle()                time.sleep(0.3)            elif action == "quit":                 pygame.quit()                time.sleep(0.3)            elif action == "start":                 time.sleep(0.3)                intro = False    else:        pygame.draw.rect(screen, ic, (x,y,w,h))     smallText = pygame.font.SysFont("Bauhaus 93",30)    textSurf, textRect = text_objecten(msg, smallText)    textRect.center = ( (x+(w/2)), (y+(h/2)) )    screen.blit(textSurf, textRect)它基本上只是检查特定区域内的鼠标点击,以及是否检测到它执行代码。现在在我的开始屏幕上我有一个按钮开始,我希望它结束 game_intro() 循环,但是尝试intro = False不会产生任何结果。尝试break不起作用,因为它必须在循环本身中。所以我的问题是:如何让这个开始按钮真正结束 game_intro() 循环?PS 我对 Python 很陌生
查看完整描述

4 回答

?
当年话下

TA贡献1890条经验 获得超9个赞

据我所知,您是intro = False在 function 内部设置的,但是您在函数内部button也有一个名为的变量。尽管它们具有相同的名称,但 Python 将它们视为不同的变量。introgame_intro


为了使intro从一个函数到另一个函数的变量更改生效,您需要将intro变量设置为全局变量,或者将intro变量作为两个函数之间的参数传递。


选项1:


intro = True


def button(msg,x,y,w,h,ic,ac,action=None): 

    global intro

    mousePos = pygame.mouse.get_pos() 

    click = pygame.mouse.get_pressed() 

    if x+w > mousePos[0] > x and y+h > mousePos[1] > y: 

        pygame.draw.rect(screen, ac, (x,y,w,h)) 

        if click[0] == 1 and action != None: 

            if action == "toggleMusic": 

                PAUSE.toggle()

                time.sleep(0.3)

            elif action == "quit": 

                pygame.quit()

                time.sleep(0.3)

            elif action == "start": 

                time.sleep(0.3)

                intro = False


def game_intro(): 

    global intro

    mixer.music.load("musicIntro.mp3")

    mixer.music.set_volume(0.02) 

    mixer.music.play(-1) 

    while intro:

        for event in pygame.event.get():

            if event.type == pygame.QUIT: 

                pygame.quit()

                quit()

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_RETURN:

                    intro = False

        screen.blit(startscherm_img, bordrect) 

        clock.tick(30)

        button("Start",302,517,94,44,donkerOranje,lichtOranje,"start")

        button("Music",553,517,94,44,donkerOranje,lichtOranje,"toggleMusic")

        button("Quit",803,517,94,44,donkerOranje,lichtOranje,"quit")

        pygame.display.flip() 

game_intro()

选项 2:


def button(msg,x,y,w,h,ic,ac, intro, action=None): 

    mousePos = pygame.mouse.get_pos() 

    click = pygame.mouse.get_pressed() 

    if x+w > mousePos[0] > x and y+h > mousePos[1] > y: 

        pygame.draw.rect(screen, ac, (x,y,w,h)) 

        if click[0] == 1 and action != None: 

            if action == "toggleMusic": 

                PAUSE.toggle()

                time.sleep(0.3)

            elif action == "quit": 

                pygame.quit()

                time.sleep(0.3)

            elif action == "start": 

                time.sleep(0.3)

                intro = False

    return intro


def game_intro(): 

    intro = True

    mixer.music.load("musicIntro.mp3")

    mixer.music.set_volume(0.02) 

    mixer.music.play(-1) 

    while intro:

        for event in pygame.event.get():

            if event.type == pygame.QUIT: 

                pygame.quit()

                quit()

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_RETURN:

                    intro = False

        screen.blit(startscherm_img, bordrect) 

        clock.tick(30)

        intro = button("Start",302,517,94,44,donkerOranje,lichtOranje,intro,"start")

        intro = button("Music",553,517,94,44,donkerOranje,lichtOranje,intro,"toggleMusic")

        intro = button("Quit",803,517,94,44,donkerOranje,lichtOranje,intro,"quit")

            pygame.display.flip() 

game_intro()


查看完整回答
反对 回复 2022-12-06
?
桃花长相依

TA贡献1860条经验 获得超8个赞

你可以做一些OOP。尝试在这样的类中创建它:


class Intro:


  def __init__(self):

    self.intro = True


  def game_intro(self): 

    mixer.music.load("musicIntro.mp3")

    mixer.music.set_volume(0.02) 

    mixer.music.play(-1) 

    while self.intro:

        for event in pygame.event.get():

            if event.type == pygame.QUIT: 

                pygame.quit()

                quit()

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_RETURN:

                    self.intro = False

        screen.blit(startscherm_img, bordrect) 

        clock.tick(30)

        button("Start",302,517,94,44,donkerOranje,lichtOranje,"start")

        button("Music",553,517,94,44,donkerOranje,lichtOranje,"toggleMusic")

        button("Quit",803,517,94,44,donkerOranje,lichtOranje,"quit")

        pygame.display.flip() 


  def button(self, msg,x,y,w,h,ic,ac,action=None): 

    mousePos = pygame.mouse.get_pos() 

    click = pygame.mouse.get_pressed() 

    if x+w > mousePos[0] > x and y+h > mousePos[1] > y: 

        pygame.draw.rect(screen, ac, (x,y,w,h)) 

        if click[0] == 1 and action != None: 

            if action == "toggleMusic": 

                PAUSE.toggle()

                time.sleep(0.3)

            elif action == "quit": 

                pygame.quit()

                time.sleep(0.3)

            elif action == "start": 

                time.sleep(0.3)

                self.intro = False



obj = Intro()

start = obj.gameintro()

intro是在一个实例中定义的,所以self它可以在类中的任何地方调用。


查看完整回答
反对 回复 2022-12-06
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

尽管我已经有一段时间没有使用 python 了,但你可以尝试将你的循环作为一个新线程运行,然后在所有诚实设置 intro 为 false 的情况下手动将其终止为一个线程,我会先进行一些调试以实际检查 intro被设置为假。尽管在进一步阅读您的代码后,我注意到您实际上调用了函数 game_intro() 两次,这会导致您循环,因为变量 intro 是在函数的一侧定义的,因此调用函数 game_intro() 会重置循环,因此可能会删除最后调用,因为你已经在 while 循环中了。另一种选择是在函数 cheers 的外面定义变量 intro



查看完整回答
反对 回复 2022-12-06
?
噜噜哒

TA贡献1784条经验 获得超7个赞

线程事件


分配一个全局范围变量(在两个函数之外)作为threading.Event实例。


然后,在您想要控制循环时设置和清除该事件。


import threading



do_intro_loop = threading.Event



def button(action=None):

  if action == "start":

    do_intro_loop.clear()


def into():

  do_intro_loop.set()

  while do_intro_loop.is_set():

    # show do intro stuff here

    pass


intro()


查看完整回答
反对 回复 2022-12-06
  • 4 回答
  • 0 关注
  • 176 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号