如何实现主菜单的按钮交互(Pygame)

2024-10-01 09:29:40 发布

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

我一直在看一堆教程和新的pygame和一般的编码。我看的教程中没有一个对我有帮助。我的目标是,当用户将鼠标悬停在按钮上时,按钮可以改变为不同的颜色。下面是我目前为止主菜单的全部代码。在

import pygame
import time
import random

pygame.init()

size = (800, 600)
screen = pygame.display.set_mode(size)

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)


pygame.display.set_caption("Basketball Shootout")
clock = pygame.time.Clock()

#GAME INTRO

def game_intro():

    intro = True

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

#Font's and text
font = pygame.font.SysFont ("Times New Norman", 60)
text = font.render ("", True, WHITE)

#Background Image
background_images = pygame.image.load("greybackground.jpg").convert()
screen.blit(background_images, [0,0])
screen.blit(text, (150, 50))

#Background Music
pygame.mixer.music.load('game.ogg')
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play()
pygame.display.update()
clock.tick(15)

#BUTTONS
screen.blit(text, (150, 50))
pygame.draw.rect(screen,(0,0,0),(300,300,205,80));
pygame.draw.rect(screen,(0,0,0),(300,400,205,80));
pygame.draw.rect(screen,(0,0,0),(300,500,205,80));

font = pygame.font.SysFont ("Times New Norman, Arial", 30)
text = font.render ("START", True, WHITE)
screen.blit(text, (340, 320))
font = pygame.font.SysFont ("Times New Norman, Arial", 30)
text = font.render ("OPTIONS", True, WHITE)
screen.blit(text, (340, 420))
font = pygame.font.SysFont ("Times New Norman, Arial", 30)
text = font.render ("ABOUT", True, WHITE)
screen.blit(text, (340, 520))

pygame.display.flip();

#Quit Pygame
game_intro()
pygame.quit

Tags: texteventtruenewdisplayrenderscreenpygame
1条回答
网友
1楼 · 发布于 2024-10-01 09:29:40

我建议将按钮数据(文本表面、矩形、颜色)存储在列表列表中。当鼠标移动时,pygame.MOUSEMOTION事件被添加到事件队列中。在事件循环中,检查鼠标是否移动,然后在按钮上迭代,并将colliding buttons的颜色设置为悬停颜色,将其他颜色重置为黑色。在for循环中Blit矩形和文本表面。在

import pygame


pygame.init()

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

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
HOVER_COLOR = (50, 70, 90)
# Don't define new font objects in your while loop (that's inefficient).
FONT = pygame.font.SysFont ("Times New Norman", 60)
# If the text surfaces and button rects don't change,
# you can define them once outside of the while loop.
text1 = FONT.render("START", True, WHITE)
text2 = FONT.render("OPTIONS", True, WHITE)
text3 = FONT.render("ABOUT", True, WHITE)
rect1 = pygame.Rect(300,300,205,80)
rect2 = pygame.Rect(300,400,205,80)
rect3 = pygame.Rect(300,500,205,80)
# The buttons consist of a text surface, a rect and a color.
buttons = [
    [text1, rect1, BLACK],
    [text2, rect2, BLACK],
    [text3, rect3, BLACK],
    ]

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:
                    # button[1] is the rect. Use its collidepoint method with
                    # the `event.pos` (mouse position) to detect collisions.
                    if button[1].collidepoint(event.pos):
                        # Set the button's color to the hover color.
                        button[2] = HOVER_COLOR
                    else:
                        # Otherwise reset the color to black.
                        button[2] = BLACK

        screen.fill((20, 50, 70))

        # Draw the buttons with their current colors at their rects.
        # You can unpack the button lists directly in the head of the loop.
        for text, rect, color in buttons:
            pygame.draw.rect(screen, color, rect)
            screen.blit(text, rect)

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


game_intro()
pygame.quit()

实际上,我建议定义一个Button类,但我不确定您是否已经熟悉类/面向对象编程。在

按钮还需要回调函数来实际执行某些操作。看看我的答案:https://stackoverflow.com/a/47664205/6220679

相关问题 更多 >