4 回答

TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
據(jù)我所知,您是intro = False在 function 內(nèi)部設(shè)置的,但是您在函數(shù)內(nèi)部button也有一個(gè)名為的變量。盡管它們具有相同的名稱,但 Python 將它們視為不同的變量。introgame_intro
為了使intro從一個(gè)函數(shù)到另一個(gè)函數(shù)的變量更改生效,您需要將intro變量設(shè)置為全局變量,或者將intro變量作為兩個(gè)函數(shù)之間的參數(shù)傳遞。
選項(xiàng)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()
選項(xiàng) 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貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
你可以做一些OOP。嘗試在這樣的類中創(chuàng)建它:
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是在一個(gè)實(shí)例中定義的,所以self它可以在類中的任何地方調(diào)用。

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊
盡管我已經(jīng)有一段時(shí)間沒(méi)有使用 python 了,但你可以嘗試將你的循環(huán)作為一個(gè)新線程運(yùn)行,然后在所有誠(chéng)實(shí)設(shè)置 intro 為 false 的情況下手動(dòng)將其終止為一個(gè)線程,我會(huì)先進(jìn)行一些調(diào)試以實(shí)際檢查 intro被設(shè)置為假。盡管在進(jìn)一步閱讀您的代碼后,我注意到您實(shí)際上調(diào)用了函數(shù) game_intro() 兩次,這會(huì)導(dǎo)致您循環(huán),因?yàn)樽兞?intro 是在函數(shù)的一側(cè)定義的,因此調(diào)用函數(shù) game_intro() 會(huì)重置循環(huán),因此可能會(huì)刪除最后調(diào)用,因?yàn)槟阋呀?jīng)在 while 循環(huán)中了。另一種選擇是在函數(shù) cheers 的外面定義變量 intro

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
線程事件
分配一個(gè)全局范圍變量(在兩個(gè)函數(shù)之外)作為threading.Event實(shí)例。
然后,在您想要控制循環(huán)時(shí)設(shè)置和清除該事件。
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()
添加回答
舉報(bào)