pygame按钮无法运行

2024-10-03 19:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

(英语不是我的母语,请原谅我的错误。) (谢谢大家!!)

当我点击按钮“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


Tags: eventgamegetifdefbuttonpygamequit
1条回答
网友
1楼 · 发布于 2024-10-03 19:24:01

每个场景都应该有自己的loop,当您按下按钮时,应该转到新的loop

如果要更改一个循环中的元素,请将旧元素放在某个函数中(即draw_intro),将新元素放在另一个函数中(即draw_other)-开始时在循环中使用draw_intro,然后按按钮将此函数替换为draw_other

在Python中,可以将函数名(不带())赋给变量

show = print

然后使用这个名称(使用()

show("Hello World")

您可以对draw_introdraw_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_introchange_draw_to_otherchange_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()

相关问题 更多 >