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()

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它可以在类中的任何地方调用。

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

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()
添加回答
举报