不知道从哪里开始集成按钮

问题描述 投票:1回答:1

我在将工作按钮集成到此代码中时遇到问题。按钮已经定义并显示在pygame中,但我不知道下一步该去哪里。我试图添加鼠标和单击功能,但我真的不知道它们最适合的位置或如何使用它们的功能

import pygame

pygame.init()

screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

BLACK = (0, 0, 0)
BACKGROUND = (200, 230, 234)
WHITE = (255, 255, 255)
HOVER_COLOUR = (50, 70, 90)
# Text Variables 
FONT = pygame.font.SysFont ("Times New Norman", 60)
TEXT = FONT.render ("", True, WHITE)
background_images = pygame.image.load("background.jpg").convert()
screen.blit(background_images, [0,0])
screen.blit(TEXT, (150, 50))
# Text & Rectangles construction
text1 = FONT.render("PlAY", True, WHITE)
text2 = FONT.render("CONTROLS", True, WHITE)
text3 = FONT.render("DIFFICULTY", True, WHITE)
text4 = FONT.render("SCOREBOARD", True, WHITE)

rect1 = pygame.Rect(250,200,300,80)
rect2 = pygame.Rect(250,300,300,80)
rect3 = pygame.Rect(250,400,300,80)
rect4 = pygame.Rect(250,500,300,80)
# The button construction arry. Text and Rectangle 
buttons = [
    [text1, rect1, BACKGROUND],
    [text2, rect2, BACKGROUND],
    [text3, rect3, BACKGROUND],
        [text4, rect4, BACKGROUND],
    ]

def game_intro():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEMOTION:
                for button in buttons:
                    # Uses collisionpoint to detect mouse position collisions
                    if button[1].collidepoint(event.pos):
                        # Set the button's colour to the hover colour.
                        button[2] = HOVER_COLOUR
                    else:
                        # resets the colour to normal.
                        button[2] = BACKGROUND

        # Draws the buttons with their current colours (normal & collisions)
        for text, rect, colour in buttons:
            pygame.draw.rect(screen, colour, rect)
            screen.blit(text, rect)

        pygame.display.flip()
        clock.tick(15)

#Run Game
game_intro()
scene_change()
pygame.quit()

整合时:

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

该程序引发有关鼠标位置功能的错误

python pygame
1个回答
1
投票

将ID添加到按钮:

buttons = [
    [text1, rect1, BACKGROUND, 1],
    [text2, rect2, BACKGROUND, 2],
    [text3, rect3, BACKGROUND, 3], 
    [text4, rect4, BACKGROUND, 4]
]

添加一个处理按钮事件的函数:

def on_button(button):
    print(button[3])

按下按钮时调用该功能:

def game_intro():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEMOTION:
                for button in buttons:
                    # Uses collisionpoint to detect mouse position collisions
                    if button[1].collidepoint(event.pos):
                        # Set the button's colour to the hover colour.
                        button[2] = HOVER_COLOUR
                    else:
                        # resets the colour to normal.
                        button[2] = BACKGROUND
            elif event.type == pygame.MOUSEBUTTONDOWN:
                for button in buttons:
                    # Uses collisionpoint to detect mouse position collisions
                    if button[1].collidepoint(event.pos):
                        on_button(button)

        # Draws the buttons with their current colours (normal & collisions)
        for text, rect, colour, button_id in buttons:
            pygame.draw.rect(screen, colour, rect)
            screen.blit(text, rect)

        pygame.display.flip()
        clock.tick(15)
© www.soinside.com 2019 - 2024. All rights reserved.