用Pygame创建rect按钮

时间:2016-10-31 05:18:27

标签: python button pygame

我正在尝试在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();

6 个答案:

答案 0 :(得分:3)

在pygame中使用rects时,最好使用内置的碰撞检测。 这是一个关于如何做的示例代码希望它可以帮助您解决问题。

在此之前,虽然我想提一下你应该用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()

答案 1 :(得分:1)

在pygame上创建按钮的另一种好方法(在python中)是在Mac或Linux上安装名为pygame_widgets的软件包(pip3 install pygame_widgets),在Windows上安装(pip install pygame_widgets)。还要确保您已安装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()

希望这对您有所帮助。

答案 2 :(得分:0)

我认为代码中的主要问题是你将pygame.mouse.get_pos()直接与一个不明确的元组进行比较。你应该尝试分别测试x和y:

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

答案 3 :(得分:0)

TKS GUYS,

以下是我的回答:

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

答案 4 :(得分:0)

我已经提供了上述答案。这是我没有模块pygame_widgets的另一个答案:

function exportObject(obj: Record<string, Array<string>>, key: string){
  return {key: obj[key]}
}

希望这有助于在您的python版本中无法安装pygame_widgets(pip安装pygame_widgets)模块。

答案 5 :(得分:0)

这是我的解决方案

$