繁体   English   中英

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

[英]How to end a loop, from outside the loop in python

我正在使用 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()

我正在调用我在上面定义的按钮 function:

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很陌生

据我所见,您在 function button内设置了intro = False ,但在game_intro function 内还有一个名为intro的变量。 尽管它们具有相同的名称,但 Python 将它们视为不同的变量。

为了使intro变量从一个 function 更改为另一个有效,您需要将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()

你可以做一些OOP。 尝试在 class 中创建它,如下所示:

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的实例内部定义的,因此可以在 class 内的任何位置调用它。

尽管我已经有一段时间没有完成 python 了,但是您可以尝试将循环作为新线程运行,然后在作为线程手动完成后将其杀死被设置为假。 Although upon reading your code further I noticed that you actually call the function game_intro() twice which would cause you to loop as the variable intro is defined in side of the function so calling the function game_intro() would reset the loop so maybe remove the最后调用,因为您已经在 while 循环中拥有它。 另一种选择是在 function 欢呼声中定义变量 intro

线程事件

将全局 scope 变量(在两个函数之外)分配为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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM