【问题标题】:snake game button functionality蛇游戏按钮功能
【发布时间】:2017-12-31 12:53:21
【问题描述】:

我正在制作一个我已经完成的贪吃蛇游戏,但我正在尝试在其中制作一些关卡,比如简单的中等难度,为此我创建了这样的按钮

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)

我通过复制粘贴创建了一个简单的游戏循环和中等难度的游戏循环功能,并编辑了用于提高蛇速度的线条。但是当我玩游戏时,动作==“简单”似乎只有当我点击中等或困难时才会执行,它玩游戏循环很容易。这里有更多代码

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

我无法解决这个问题。为什么按钮不起作用为什么没有执行尊重的功能

【问题讨论】:

  • 您可以将函数名称分配给action - action = another_screen,然后您可以在没有所有这些if/elif的情况下执行函数action()
  • 你检查按钮是否被点击,但你不检查它是否在“xposition,yposition,width,height”中被点击——这是你的大错误。您可以在任何地方单击,不仅在按钮中,它将运行功能。

标签: python python-3.x button pygame


【解决方案1】:

你的大错误是你没有检查它是否在"xposition, yposition, width, height"区域点击。您可以单击屏幕上的任何位置,然后单击按钮。

你可以使用pygame.Rect()来保持按钮的位置和大小,然后你可以使用

rect.collidepoint(mouse)

检查碰撞。您也可以使用rect 来绘制矩形。

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

顺便说一句:您也不必在for event 中运行fill()、按钮和其他功能。您可以在for event 之后使用它们。因为你不使用event,所以你甚至不需要for event。但是你必须使用pygame.event.pump()(而不是 pygame.event.get()),因为没有这个get_pos()/get_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(mouse) 更改按钮颜色 ("hover"/"unhover")。

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

【讨论】:

猜你喜欢
  • 2015-07-04
  • 2023-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 2014-08-11
相关资源
最近更新 更多