当前位置: 首页 > 知识库问答 >
问题:

创建直立按钮与Pyplay[重复]

郝修为
2023-03-14

我试图在Pygame中使用rect制作按钮。我从红色的退出按钮开始,检查按钮的边界框内是否有单击。

    import pygame
"""import nemesis.py;"""

pygame.init();
screen = pygame.display.set_mode((400,300));
pygame.display.set_caption("menu");
menuAtivo = True;

start_button = pygame.draw.rect(screen,(0,0,240),(150,90,100,50));
continue_button = pygame.draw.rect(screen,(0,244,0),(150,160,100,50));
quit_button = pygame.draw.rect(screen,(244,0,0),(150,230,100,50));


pygame.display.flip();


while menuAtivo:
    for evento in pygame.event.get():
        print(evento);
        if evento.type == pygame.MOUSEBUTTONDOWN:
            if pygame.mouse.get_pos() >= (150,230):
                if pygame.mouse.get_pos() <= (250,280):
                        pygame.quit();
                    

共有3个答案

湛财
2023-03-14

另一个好的方法来创建pyplay上的按钮(在python)是通过安装包称为pygame_widgets(pip3安装pygame_widgets)在Mac或Linux和(pip安装pygame_widgets)在Windows上。并且还要确保你已经安装了pip,否则它将无法工作。

# Importing Modules
import pygame as pg
import pygame_widgets as pw
# creating screen
pg.init()
screen = pg.display.set_mode((800, 600))
running = True
button = pw.Button(
        screen, 100, 100, 300, 150, text='Hello',
        fontSize=50, margin=20,
        inactiveColour=(255, 0, 0),
        pressedColour=(0, 255, 0), radius=20,
        onClick=lambda: print('Click')
     )

while running:
      events = pg.event.get()
      for event in events:
           if event.type == pg.QUIT:
                running = False
      button.listen(events)
      button.draw()
      pg.display.update()

希望这对你有帮助。

祁宾白
2023-03-14

TKS伙计们,

以下是我的答案:

import pygame


pygame.init();
screen = pygame.display.set_mode((400,300));
pygame.display.set_caption("menu");
menuAtivo = True;

start_button = pygame.draw.rect(screen,(0,0,240),(150,90,100,50));
continue_button = pygame.draw.rect(screen,(0,244,0),(150,160,100,50));
quit_button = pygame.draw.rect(screen,(244,0,0),(150,230,100,50));


pygame.display.flip();

def startGame():
    screen.fill((0,0,0));
    pygame.display.flip();
    import nemesis.py;

while menuAtivo:
    for evento in pygame.event.get():
        print(evento);
        if evento.type == pygame.MOUSEBUTTONDOWN:
            if pygame.mouse.get_pos()[0] >= 150 and pygame.mouse.get_pos()[1] >= 230:
                if pygame.mouse.get_pos()[0] <= 250 and pygame.mouse.get_pos()[1] <= 280:
                        pygame.quit();
            if pygame.mouse.get_pos()[0] >= 150 and pygame.mouse.get_pos()[1] >= 90:
                if pygame.mouse.get_pos()[0] <= 250 and pygame.mouse.get_pos()[1] <= 140:
                        startGame();
贲宏硕
2023-03-14

在pygame中使用矩形时,最好使用内置的碰撞检测。下面是一个如何做的示例代码,希望它能帮助您解决问题。

在此之前,我想提一下,你应该使用while循环渲染/绘制你的对象,否则不会显示。

import pygame
import sys


def main():
    pygame.init()
    clock = pygame.time.Clock()
    fps = 60
    size = [200, 200]
    bg = [255, 255, 255]

    screen = pygame.display.set_mode(size)

    button = pygame.Rect(100, 100, 50, 50)  # creates a rect object
    # The rect method is similar to a list but with a few added perks
    # for example if you want the position of the button you can simpy type
    # button.x or button.y or if you want size you can type button.width or
    # height. you can also get the top, left, right and bottom of an object
    # with button.right, left, top, and bottom

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False

            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = event.pos  # gets mouse position

                # checks if mouse position is over the button

                if button.collidepoint(mouse_pos):
                    # prints current location of mouse
                    print('button was pressed at {0}'.format(mouse_pos))

        screen.fill(bg)

        pygame.draw.rect(screen, [255, 0, 0], button)  # draw button

        pygame.display.update()
        clock.tick(fps)

    pygame.quit()
    sys.exit


if __name__ == '__main__':
    main()
 类似资料:
  • pre { white-space: pre-wrap; } 分割按钮(Split Button)包含一个链接按钮(Link Button)和一个菜单(Menu)。当用户点击或者鼠标悬停在向下箭头区域,将会显示一个对应的菜单。本实例演示了如何创建和使用分割按钮(Split Button)。 我们创建一个分割按钮(Split Button)和一个链接按钮(Link Button):     <di

  • pre { white-space: pre-wrap; } 菜单按钮(Menu Button)包含一个按钮(button)和一个菜单(menu)组件,当点击或移动鼠标到按钮上,将显示一个对应的菜单。 为了定义一个菜单按钮(Menu Button),您应该定义一个链接按钮(Link Button)和一个菜单(menu),下面是一个实例:     <div>         <a href="#"

  • pre { white-space: pre-wrap; } 通常情况下,使用 <button> 元素来创建按钮,而链接按钮(Link Button)则是使用 <a> 元素来创建的。所以实际上一个链接按钮(Link Button)就是一个显示为按钮样式的 <a> 元素。 为了创建链接按钮(Link Button),所有您需要做的就是添加一个名为 'easyui-linkbutton' 的 clas

  • 如何将转换为带有圆边的按钮?我使用获得了圆角边框形状,但不知怎么需要给边框着色。

  • 我有一个2x2的按钮网格。现在我想把按钮缩小到200x200,把每个按钮放在一个容器中作为一个空格,然后把每个按钮放在各自容器的中心。 在我的图片中,我只缩小了顶部的两个按钮,以便您可以看到页面上的间距。我怎样才能从第一张图片转到第二张图片,这是Photoshop制作的? 文件:app.js 文件:index.html

  • 你玩过Loren Brichter的游戏Letterpress吗?我很喜欢的Loren构建的一个关于界面的东西可能不是每个人都明显喜欢的:我喜欢每个按钮在用户按下时立即切换到一个不同的状态的样子。绝对不会延迟。这不是一个简单实现的行为,因为即使你可以将一个图片设为UIButton的UIControlStateHighlighted状态图,它也只会在点击发生后一小会启动,而且它不允许更进一步的代码来