Не знаю, с чего начать с интеграции кнопок - PullRequest
1 голос
/ 02 апреля 2019

У меня проблемы с интеграцией рабочих кнопок в этот код.Кнопки были определены и отображаются в 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()

Программа выдает ошибку о функции положения мыши

1 Ответ

1 голос
/ 02 апреля 2019

Добавьте идентификаторы к вашим кнопкам:

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...