我用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)它基本上只是检查鼠标在某个区域内的点击,如果它检测到它执行代码。
现在在我的启动屏幕上,我有一个按钮start,我想让它结束game_intro()循环,但是尝试intro = False不会产生任何结果。尝试break不起作用,因为它必须在循环本身中。
所以我的问题是:如何让这个start按钮真正结束game_intro()循环?
附言:我是Python的新手
发布于 2020-05-25 21:28:33
据我所知,您在函数button中设置了intro = False,但在game_intro函数中也有一个名为intro的变量。尽管它们具有相同的名称,但Python将它们视为不同的变量。
为了使从一个函数到另一个函数的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()发布于 2020-05-25 21:29:24
你可以做一些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在实例中定义的,因此可以在类中的任何位置调用它。
发布于 2020-05-25 21:21:28
尽管我已经有一段时间没有做python了,你可以试着把你的循环作为一个新的线程运行,然后把它作为一个线程手动结束,老实说,将to设置为false应该已经起作用了,我会先做一些调试,检查一下to是否被设置为false。尽管在进一步阅读您的代码时,我注意到您实际上调用了game_intro()函数两次,这会导致您循环,因为变量intro是在函数的一侧定义的,所以调用函数game_intro()会重置循环,因此可能会删除末尾的调用,因为您已经在while循环中使用了它。另一种选择是在函数cheers的外部定义变量intro
https://stackoverflow.com/questions/62003167
复制相似问题