繁体   English   中英

在点击python pygame和功能按钮时切换文本

[英]switch text on click python pygame wih a function's button

我正在尝试使用python 2.7和pygame进行游戏/测验,我希望用户单击具有正确答案的按钮。 我做了一个按钮功能,检查鼠标是否在按钮内,然后进行操作。 问题是单击开始后运行程序时,它显示的是第二个屏幕,而不是第一个屏幕。

import pygame

pygame.init()

display_width = 900
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (250,0,0)
green = (0,250,0)
bright_red =(200,0,0)
bright_green = (0,200,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("A MATH'S GAME")


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 text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def messgae_display(text):
    largeText = pygame.font.Font("Arial",60)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    game_loop()

def quitgame():
    pygame.quit()
    quit()

def game_intro():

    intro = True

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

        gameDisplay.fill(white)
        largeText = pygame.font.SysFont("comicsansms",115)
        TextSurf, TextRect = text_objects("A math's game", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        button("GO!",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)

        pygame.display.update()

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)
        largeText = pygame.font.SysFont("comicsansms",48)
        TextSurf, TextRect = text_objects("A math's game", largeText)
        TextRect.center = ((display_width/2), 100)
        gameDisplay.blit(TextSurf, TextRect)
        largeText = pygame.font.SysFont("comicsansms",48)
        TextSurf, TextRect = text_objects("Pick a choice", largeText)
        TextRect.center = ((display_width/2), 200)
        gameDisplay.blit(TextSurf, TextRect)
        largeText = pygame.font.SysFont("comicsansms",48)
        TextSurf, TextRect = text_objects("1 QUESTION", largeText)
        TextRect.center = ((display_width/2), 280)
        gameDisplay.blit(TextSurf, TextRect)

        button1 = button("Option1",120,350,300,150,red,bright_red,option1_loop)
        button2 = button("Option2",520,350,300,150,red,bright_red,None)

        pygame.display.update()

def option1_loop():


    option1Exit = False

    while not option1Exit:

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

        gameDisplay.fill(white)
        largeText = pygame.font.SysFont("comicsansms",48)
        TextSurf, TextRect = text_objects("A math's game", largeText)
        TextRect.center = ((display_width/2), 100)
        gameDisplay.blit(TextSurf, TextRect)
        largeText = pygame.font.SysFont("comicsansms",48)
        TextSurf, TextRect = text_objects("Pick a choice", largeText)
        TextRect.center = ((display_width/2), 200)
        gameDisplay.blit(TextSurf, TextRect)
        largeText = pygame.font.SysFont("comicsansms",48)
        TextSurf, TextRect = text_objects("2 QUESTION", largeText)
        TextRect.center = ((display_width/2), 280)
        gameDisplay.blit(TextSurf, TextRect)

        button1 = button("Option1.1",120,350,300,150,red,bright_red,None)
        button2 = button("Option2.1",520,350,300,150,red,bright_red,None)

       pygame.display.update()


game_intro()
game_loop()
option1_loop()
pygame.quit()
quit()

问题是因为您一直按下pygame.mouse.get_pressed()始终为True 当您在第一个屏幕上按时,它将切换到第二个屏幕,看到该按钮已被按下,它将自动单击按钮并切换到第三个屏幕。

您可以使用event.type = pygame.MOUSEBUTTONDOWN ,该事件仅执行一次-当按钮从未按下状态更改为按下状态时。 但是您必须将button()分为button_check()button_draw()

import pygame

# --- constants --- (UPPER_CASE names)

DISPLAY_WIDTH  = 900
DISPLAY_HEIGHT = 600

BLACK = (  0,  0,  0)
WHITE = (255,255,255)
RED   = (250,  0,  0)
GREEN = (  0,250,  0)
BRIGHT_RED   = (200,  0,  0)
BRIGHT_GREEN = (  0,200,  0)

# --- classes --- (CamelCase names)

# empty

# --- functions --- (lower_case names)

def button_check(button):

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if button['rect'].collidepoint(mouse):
        if click[0] == 1 and button['action']:
            button['action']()         


def button_draw(button):
    font = pygame.font.SysFont("comicsansms", 20)

    mouse = pygame.mouse.get_pos()

    if button['rect'].collidepoint(mouse):
        color = button['ac']
    else:
        color = button['ic']

    pygame.draw.rect(game_display, color, button['rect'])

    image, rect = text_objects(button['msg'], font)
    rect.center = button['rect'].center
    game_display.blit(image, rect)

def text_objects(text, font):
    image = font.render(text, True, BLACK)
    rect  = image.get_rect()
    return image, rect

def message_display(text):
    font = pygame.font.Font("Arial" ,60)

    image, rect = text_objects(text, font)
    rect.center = game_display_rect.center
    game_display.blit(image, rect)

    pygame.display.update()

def quit_game():
    pygame.quit()
    quit()

def game_intro():
    font = pygame.font.SysFont("comicsansms", 115)

    game_display.fill(WHITE)

    image, rect = text_objects("A math's game", font)
    rect.center = game_display_rect.center
    game_display.blit(image, rect)

    buttons = [
        {
            'msg': 'GO!',
            'rect': pygame.Rect(150, 450, 100, 50),
            'ac': GREEN, 
            'ic': BRIGHT_GREEN,
            'action': game_loop,
        },
        {
            'msg': 'Quit',
            'rect': pygame.Rect(550, 450, 100, 50),
            'ac': RED,   
            'ic': BRIGHT_RED,
            'action': game_loop
        }
    ]

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                button_check(buttons[0])
                button_check(buttons[1])

        button_draw(buttons[0])
        button_draw(buttons[1])

        pygame.display.update()

def game_loop():
    font = pygame.font.SysFont("comicsansms",48)

    game_display.fill(WHITE)

    image, rect = text_objects("A math's game", font)
    rect.center = (game_display_rect.centerx, 100)
    game_display.blit(image, rect)

    image, rect = text_objects("Pick a choice", font)
    rect.center = (game_display_rect.centerx, 200)
    game_display.blit(image, rect)

    image, rect = text_objects("1 QUESTION", font)
    rect.center = (game_display_rect.centerx, 280)
    game_display.blit(image, rect)

    buttons = [
        {
            'msg': 'Option1',
            'rect': pygame.Rect(120, 350, 300, 150),
            'ac': RED, 
            'ic': BRIGHT_RED,
            'action': option1_loop,
        },
        {
            'msg': 'Option2',
            'rect': pygame.Rect(520, 350, 300, 150),
            'ac': RED,   
            'ic': BRIGHT_RED,
            'action': None
        }
    ]

    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                button_check(buttons[0])
                button_check(buttons[1])

        button_draw(buttons[0])
        button_draw(buttons[1])

        pygame.display.update()

def option1_loop():
    font = pygame.font.SysFont("comicsansms",48)

    game_display.fill(WHITE)

    image, rect = text_objects("A math's game", font)
    rect.center = (game_display_rect.centerx, 100)
    game_display.blit(image, rect)

    image, rect = text_objects("Pick a choice", font)
    rect.center = (game_display_rect.centerx, 200)
    game_display.blit(image, rect)

    image, rect = text_objects("2 QUESTION", font)
    rect.center = (game_display_rect.centerx, 280)
    game_display.blit(image, rect)

    buttons = [
        {
            'msg': 'Option1.1',
            'rect': pygame.Rect(120, 350, 300, 150),
            'ac': RED, 
            'ic': BRIGHT_RED,
            'action': option1_loop,
        },
        {
            'msg': 'Option2.1',
            'rect': pygame.Rect(520, 350, 300, 150),
            'ac': RED,   
            'ic': BRIGHT_RED,
            'action': None
        }
    ]

    while True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                button_check(buttons[0])
                button_check(buttons[1])

        button_draw(buttons[0])
        button_draw(buttons[1])

        pygame.display.update()

# --- main ---

pygame.init()

game_display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
game_display_rect = game_display.get_rect()

pygame.display.set_caption("A MATH'S GAME")

game_intro()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM