Pygame按钮不起作用

时间:2018-04-21 18:53:14

标签: python python-3.x pygame

def button(text, x, y, width, height, inactive_color, active_color,action = None):
    cur = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    #click1 = pygame.MOUSEBUTTONDOWN()

    if x + width > cur[0] > x and y+height > cur[1] > y:
        pygame.draw.rect(gameDisplay, active_color, (x,y,width,height))
        if click[0] == 1 and action !=None:
            if action == "start": #actions define what each thing does. modify the actions for it to do different things.
                game_intro()
                pygame.display.update()
            if action == "play":
                scene1()
                pygame.display.update()
            if action == "next":
                scene1_p2()
                pygame.display.update()
            if action == "next1":
                scene1_p3()
                pygame.display.update()
            if action == "next2":
                scene2()
                pygame.display.update()
            if action == "next3":
                scene2_p2()

                pygame.display.update()




    else:
        x + width > cur[0] > x and y+height > cur[1] > y
        pygame.draw.rect(gameDisplay, inactive_color, (x,y,width,height))
    text_to_button(text,black,x,y,width,height)



def game_title():
    title = True

    while title:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()


        gameDisplay.fill(white)
        gameDisplay.blit(opimg, [0, 0])
        message_to_screen("TBN", red, -100, size='large')
        pygame.draw.rect(gameDisplay, blue, (380, 500, 230, 50))
        text_to_button("Press to Start", red, 450, 500, 100, 50)

        mouse = pygame.mouse.get_pos()
        #print(mouse)
        if 380+230 > mouse[0] > 380 and 500+50 > mouse[1] > 500:
            button("Press to Start",380, 500, 230, 50,blue,light_blue,"start")
            '''
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.MOUSEBUTTONDOWN:
                    title = False
                    game_intro()
            '''
        pygame.display.update()


def game_intro():


    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)

        message_to_screen("Welcome to TBN", red, -100, size='large')
        message_to_screen('This is a story based RPG',
                          black,
                          10)
        message_to_screen('By Stephen Wang',
                          black,
                          50)
        message_to_screen('Assets used not owned by me but by their respective owners.',
                          black,
                          100)
        button("Play", 420, 550, 150, 50, blue, light_blue,action="play")



        pygame.display.update()

def scene1():
    scene1= True
    mouse_click=False

    while scene1 == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(black)
        button("I can't hear anything...", 0,700 , 1000, 100, blue,blue,action="next")



        pygame.display.update()



def scene1_p2():
    scene1= True

    while scene1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(black)
        button("Where am I?", 0,700 , 1000, 100, blue, blue,action="next1")

        pygame.display.update()

def scene1_p3():
    scene1= True

    while scene1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(black)
        button("Slowly, I opened my eyes.", 0,700 , 1000, 100, blue, blue,action="next2")

        pygame.display.update()

def scene2():
    scene2 = True

    while scene2:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

    gameDisplay.fill(white)
    gameDisplay.blit(forest, [0, 0])
    button("Nearby, I could hear the sounds of battle.", 0, 700, 1000, 100, blue, blue, action="next3")
    pygame.display.update()
def scene2_p2():
    scene2 = True

    while scene2:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

    gameDisplay.fill(white)
    gameDisplay.blit(forest, [0, 0])
    button("Nearby, I could hear the sounds of battle.", 0, 700, 1000, 100, blue, blue, action="next3")
    pygame.display.update()

game_title()
game_intro()

这就是我拥有的代码。我目前正在为学校选择自己的冒险类游戏。由于我还不熟悉Pygame,因此我不知道如何处理文本框,因此我为每次更新创建了一个新屏幕。我使用该按钮作为文本框,按下该按钮后,它将进入下一个屏幕。 然而,当我尝试添加更多屏幕时,按钮直接上升根本不起作用。请帮忙!

0 个答案:

没有答案