Python 主菜单帮助(pygame)

Python 主菜单帮助(pygame),python,menu,pygame,rect,Python,Menu,Pygame,Rect,我已经创建了鼠标单击功能,当我单击这些按钮时可以打印(开始、选项和关于)。我只是不知道如何进入下一阶段,一旦点击这些按钮,就可以打开一个新页面。我试着在点击按钮时使用screen.fill,但只会持续几秒钟,按钮就会出现在它前面 我对pygame还相当陌生。 这是我到目前为止的代码 import pygame from pygame import * pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pyg

我已经创建了鼠标单击功能,当我单击这些按钮时可以打印(开始、选项和关于)。我只是不知道如何进入下一阶段,一旦点击这些按钮,就可以打开一个新页面。我试着在点击按钮时使用screen.fill,但只会持续几秒钟,按钮就会出现在它前面

我对pygame还相当陌生。 这是我到目前为止的代码

import pygame
from pygame import *


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)

#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)

#Background
bg = pygame.image.load("greybackground.png")


#Fonts
FONT = pygame.font.SysFont ("Times New Norman", 60)


text1 = FONT.render("START", True, WHITE)
text2 = FONT.render("OPTIONS", True, WHITE)
text3 = FONT.render("ABOUT", True, WHITE)

#Buttons
rect1 = pygame.Rect(300,300,205,80)
rect2 = pygame.Rect(300,400,205,80)
rect3 = pygame.Rect(300,500,205,80)

buttons = [
    [text1, rect1, BLACK],
    [text2, rect2, BLACK],
    [text3, rect3, BLACK],
    ]

running = False

def game_intro():
    while not running: 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEMOTION:
                for button in buttons:
                    if button[1].collidepoint(event.pos):
                        button[2] = HOVER_COLOR
                    else:
                        button[2] = BLACK

                        screen.blit(bg, (0, 0))
        for text, rect, color in buttons:
            pygame.draw.rect(screen, color, rect)
            screen.blit(text, rect)
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if rect1.collidepoint(event.pos):
                        screen.fill((0, 0, 0))
                    elif rect2.collidepoint(event.pos):
                        print ('options')
                    elif rect3.collidepoint(event.pos):
                        print ('about')

            if event.type == KEYDOWN:
                if (event.key == K_UP):
                    print ("UP was pressed")
                elif (event.key == K_DOWN):
                    print ("DOWN was pressed")
                elif (event.key == K_w):
                    print ("W was pressed")
                elif (event.key == K_s):
                    print ("S was pressed")
                else:
                    print ("error")




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



game_intro()
pygame.quit()

事实上,我自己也不得不做类似的事情。这是您将要放在底部的内容:

while running:
event = pygame.event.wait()
if event.type == pygame.MOUSEBUTTONUP:
    if event.button == 1:
        x,y = pygame.mouse.get_pos()
        if 300 <= x <= 505 and 300 <= y <= 380:
            #change stuff here
            running = False

#insert code here \/ for the next screen
运行时:
event=pygame.event.wait()
如果event.type==pygame.MOUSEBUTTONUP:
如果event.button==1:
x、 y=pygame.mouse.get_pos()

如果你能看到我目前的代码上面,实现你的代码到我的,因为我非常困惑哈哈。我知道我是个白痴