亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

pygame 按鈕無法運行

pygame 按鈕無法運行

郎朗坤 2021-12-29 20:18:05
(英語不是我的第一語言,所以請原諒任何錯誤。)(謝謝大家?。。┊斘尹c擊按鈕“1”時,BE01 將出現,但如果我不點擊,scene01Img 將返回。我嘗試在 def BE01(): 中使用 gameExit = true,但它不起作用。pygame.init()clock = pygame.time.Clock()def button(msg,x,y,w,h,ic,ac,action=None):    mouse = pygame.mouse.get_pos()    click = pygame.mouse.get_pressed()    if x+w > mouse[0] > x and y+h > mouse[1] > y:        pygame.draw.rect(gameDisplay, ac,(x,y,w,h))        if click[0] == 1 and action != None:            action()             else:        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))    smallText = pygame.font.SysFont("comicsansms",20)    textSurf, textRect = text_objects(msg, smallText)    textRect.center = ( (x+(w/2)), (y+(h/2)) )    gameDisplay.blit(textSurf, textRect)def BE01():    gameDisplay.fill(white)    gameDisplay.blit(BE01Img,(0,0))    button("BACK",350,450,100,50,black,gray,game_intro)def game_intro():    intro = True    while intro:        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                quit()        gameDisplay.fill(white)        gameDisplay.blit(introImg,(0,0))        button("START",350,450,100,50,black,gray,game_loop)        pygame.display.update()        clock.tick(15)    def quitgame():    pygame.quit()    quit()   def game_loop():    gameExit = False    while not gameExit:        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                quit()        gameDisplay.fill(white)        gameDisplay.blit(scene01Img,(0,0))        button("1",200,450,100,50,black,gray,BE01)        pygame.display.update()        clock.tick(60)game_intro()game_loop()pygame.quit()quit()單擊“1”按鈕后,BE01 將出現并運行另一個程序,scene01 不應出現。
查看完整描述

1 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

每個場景都應該有自己的loop,當你按下按鈕時,你應該去新的loop。


如果您想在一個循環中更改元素,則將舊元素放入某個函數(即draw_intro),并將新元素放入其他函數(即draw_other) - 在開始時draw_intro在循環中使用,當您按下按鈕時,將此函數替換為draw_other


在 Python 中,您可以將函數名稱(不帶())分配給變量


show = print

然后使用此名稱(使用())


show("Hello World")

你可以用draw_intro,做同樣的事情draw_other。


在游戲循環內(之前while)您可以設置


global draw


draw = draw_intro 

并在里面使用它 while


draw()

當您按下按鈕時,它應該替換功能


draw = draw_other

這是完整的工作代碼 - 我只刪除了所有代碼blit(image)以便在沒有圖像的情況下輕松運行它。


import pygame


black = (0,0,0)

white = (255,255,255)

gray  = (128,128,128)

red   = (255,0,0)


pygame.init()

gameDisplay = pygame.display.set_mode( (800,600))

clock = pygame.time.Clock()



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

    mouse = pygame.mouse.get_pos()

    click = pygame.mouse.get_pressed()


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

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

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

            action()         

    else:

        pygame.draw.rect(gameDisplay, ic, (x,y,w,h))


    smallText = pygame.font.SysFont("comicsansms",20)

    textSurf = smallText.render(msg, True, red)

    textRect = textSurf.get_rect()

    textRect.center = ( (x+(w/2)), (y+(h/2)) )

    gameDisplay.blit(textSurf, textRect)


def change_draw(new_draw):

    global draw

    draw = new_draw


def draw_BE01():

    gameDisplay.fill(white)

    #gameDisplay.blit(BE01Img,(0,0))

    button("BACK",350,450,100,50,black,gray,lambda:change_draw(draw_intro))


def draw_intro():

    gameDisplay.fill(white)

    #gameDisplay.blit(introImg,(0,0))

    button("START",350,450,100,50,black,gray,lambda:change_draw(draw_other))


def draw_other():

    gameDisplay.fill(white)

    #gameDisplay.blit(scene01Img,(0,0))

    button("1",200,450,100,50,black,gray,lambda:change_draw(draw_BE01))


def quitgame():

    pygame.quit()

    quit()   


def game_loop():

    global draw


    draw = draw_intro


    gameExit = False


    while not gameExit:


        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                pygame.quit()

                quit()


        draw()


        pygame.display.update()

        clock.tick(60)


game_loop()

pygame.quit()

quit()

編輯:


如果您之前沒有使用lamda過,那么您可以不使用它,lambda但是您將需要change_draw_to_intro, change_draw_to_other,change_draw_to_BE01而不是單個change_draw


import pygame


black = (0,0,0)

white = (255,255,255)

gray  = (128,128,128)

red   = (255,0,0)


pygame.init()

gameDisplay = pygame.display.set_mode( (800,600))

clock = pygame.time.Clock()


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

    mouse = pygame.mouse.get_pos()

    click = pygame.mouse.get_pressed()


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

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

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

            action()         

    else:

        pygame.draw.rect(gameDisplay, ic, (x,y,w,h))


    smallText = pygame.font.SysFont("comicsansms",20)

    textSurf = smallText.render(msg, True, red)

    textRect = textSurf.get_rect()

    textRect.center = ( (x+(w/2)), (y+(h/2)) )

    gameDisplay.blit(textSurf, textRect)


def change_draw_to_intro():

    global draw

    draw = draw_intro


def change_draw_to_other():

    global draw

    draw = draw_other


def change_draw_to_BE01():

    global draw

    draw = draw_BE01


def draw_BE01():

    gameDisplay.fill(white)

    #gameDisplay.blit(BE01Img,(0,0))

    button("BACK",350,450,100,50,black,gray,change_draw_to_intro)


def draw_intro():

    gameDisplay.fill(white)

    #gameDisplay.blit(introImg,(0,0))

    button("START",350,450,100,50,black,gray,change_draw_to_other)


def draw_other():

    gameDisplay.fill(white)

    #gameDisplay.blit(scene01Img,(0,0))

    button("1",200,450,100,50,black,gray,change_draw_to_BE01)


def quitgame():

    pygame.quit()

    quit()   


def game_loop():

    global draw


    draw = draw_intro


    gameExit = False


    while not gameExit:


        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                pygame.quit()

                quit()


        draw()


        pygame.display.update()

        clock.tick(60)


game_loop()

pygame.quit()

quit()



查看完整回答
反對 回復 2021-12-29
  • 1 回答
  • 0 關注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號