Python 蛇游戏按钮功能

Python 蛇游戏按钮功能,python,python-3.x,button,pygame,Python,Python 3.x,Button,Pygame,我正在做一个蛇的游戏,我已经完成了,但我试图在它的一些水平,像容易,中等和困难的目的,我创造了我的按钮像这样 def button(msg, xposition, yposition, width, height, color, action = None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if click[0] == 1 and action!= None:

我正在做一个蛇的游戏,我已经完成了,但我试图在它的一些水平,像容易,中等和困难的目的,我创造了我的按钮像这样

def button(msg, xposition, yposition, width, height, color, action = None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if click[0] == 1 and action!= None:
        if action == "play":
            another_screen()
        elif action == "easy":
            gameLoop_easy()
        elif action == "medium":
            gameLoop_medium()
        elif action == "hard":
            gameLoop_hard()
        elif action == "quit":
            pygame.quit()
            quit()
    pygame.draw.rect(screen, color, (xposition, yposition, width, height))
    message(msg, black, xposition + 10, yposition +10, 50)
我通过复制粘贴创建了gameloop easy和gameloop中硬函数,并编辑了用于提高蛇速度的行。但当我玩游戏的时候,似乎只有当我点击中键或硬键时,动作==“轻松”才是正确的,它玩游戏循环轻松。这里有更多的代码

def another_screen():
    another_screen = True
    while another_screen:
        for event in pygame.event.get():
            screen.fill(white)
            border_design()
            message('Select Level', red, 20, 100, 100)
            button("Easy", 330, 290, 200, 65, green, "easy")
            button("Medium", 330, 390, 200, 65, pink, "medium")
            button("hard", 330, 490, 200, 65, blue, "hard")
            clock.tick(15)
            pygame.display.update()

我无法解决这个问题。为什么按钮不起作用为什么没有执行受尊重的功能你最大的错误是没有检查它是否在区域“X位置、Y位置、宽度、高度”中单击。你们可以点击屏幕上的任何地方,它就会点击按钮

您可以使用来保持按钮的位置和大小,然后您可以使用

rect.collidepoint(mouse)
检查碰撞。您还可以使用
rect
绘制矩形

我还将函数分配给
action=
而不是文本,然后您可以使用
action()
而不是那些
if/elif

顺便说一句:您也不必为事件运行
fill()
、按钮和
中的其他函数。您可以在事件的
之后使用它们。因为您不使用
事件
,所以事件
甚至不需要
。但是您必须使用(而不是),因为没有这个
get\u pos()/get\u pressed()
可能无法工作

def button(msg, x, y, width, height, color, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    rect = pygame.Rect(x, y, width, height)

    if click[0] and action and rect.collidepoint(mouse):
        action()

    pygame.draw.rect(screen, color, rect)
    message(msg, black, x+10, y+10, 50)

def another_screen():
    another_screen = True
    while another_screen:
        pygame.event.pump()            

        screen.fill(white)
        border_design()
        message('Select Level', red, 20, 100, 100)

        button("Easy", 330, 290, 200, 65, green, gameLoop_easy)
        button("Medium", 330, 390, 200, 65, pink, gameLoop_medium)
        button("hard", 330, 490, 200, 65, blue, gameLoop_hard)

        clock.tick(15)
        pygame.display.update()
顺便说一句:当鼠标悬停在按钮上方时(
“悬停”/“取消悬停”
),您可以使用
rect.collidepoint(鼠标)
更改按钮颜色


顺便说一句:使用
get_pressed()
当您在新屏幕上的同一位置有其他按钮时,您将遇到问题。在您释放鼠标按钮之前,它将更改屏幕并检查新屏幕上的按钮-因此它将在“新建”按钮中自动单击。

您可以将函数名称分配给
操作
-
操作=另一个屏幕
,然后您可以执行函数
操作()
如果没有所有这些
if/elif
您会检查按钮是否被点击,但不会检查按钮是否在“X位置、Y位置、宽度、高度”中被点击,这是您的大错误。您可以在任何地方单击,而不仅仅是在按钮中,它将使用类运行function.btw,您不必一次又一次地创建pygame.Rect()。按钮的示例:,