Pygame鼠标点击检测

问题描述 投票:0回答:4

我想知道如何编写代码来检测鼠标点击精灵。例如:

if #Function that checks for mouse clicked on Sprite:
    print ("You have opened a chest!")
python mouseevent pygame
4个回答
64
投票

我假设你的游戏有一个主循环,你所有的精灵都在一个名为

sprites
的列表中。

在主循环中,获取所有事件,并检查

MOUSEBUTTONDOWN
MOUSEBUTTONUP
事件。

while ... # your main loop
  # get all events
  ev = pygame.event.get()

  # proceed events
  for event in ev:

    # handle MOUSEBUTTONUP
    if event.type == pygame.MOUSEBUTTONUP:
      pos = pygame.mouse.get_pos()

      # get a list of all sprites that are under the mouse cursor
      clicked_sprites = [s for s in sprites if s.rect.collidepoint(pos)]
      # do something with the clicked sprites...

所以基本上你必须在主循环的每次迭代中自己检查精灵的点击。你会想要使用 mouse.get_pos()rect.collidepoint().

Pygame 不提供事件驱动编程,例如cocos2d 可以。

另一种方法是检查鼠标光标的位置和按下按钮的状态,但这种方法存在一些问题。

if pygame.mouse.get_pressed()[0] and mysprite.rect.collidepoint(pygame.mouse.get_pos()):
  print ("You have opened a chest!")

如果你处理了这种情况,你必须引入某种标志,否则这段代码将打印“你打开了一个箱子!”主循环的每次迭代。

handled = False

while ... // your loop

  if pygame.mouse.get_pressed()[0] and mysprite.rect.collidepoint(pygame.mouse.get_pos()) and not handled:
    print ("You have opened a chest!")
    handled = pygame.mouse.get_pressed()[0]

当然你可以继承

Sprite
并添加一个名为
is_clicked
的方法,如下所示:

class MySprite(Sprite):
  ...

  def is_clicked(self):
    return pygame.mouse.get_pressed()[0] and self.rect.collidepoint(pygame.mouse.get_pos())

所以,恕我直言,最好使用第一种方法。


17
投票

MOUSEBUTTONDOWN
事件在您单击鼠标按钮时发生一次,
MOUSEBUTTONUP
事件在您释放鼠标按钮时发生一次。
pygame.event.Event()
对象有两个属性,提供有关鼠标事件的信息。
pos
是一个存储被点击位置的元组。
button
存储被点击的按钮。每个鼠标按钮都关联一个值。例如鼠标左键、鼠标中键、鼠标右键、鼠标滚轮向上分别为鼠标滚轮向下的属性值为 1、2、3、4、5。当按下多个键时,会发生多个鼠标按钮事件。进一步的解释可以在模块的文档中找到
pygame.event
.

使用

rect
对象的
pygame.sprite.Sprite
属性和
collidepoint
方法查看Sprite是否被点击。 将事件列表传递给
update
pygame.sprite.Group
方法,这样就可以处理Sprite类中的事件了:

class SpriteObject(pygame.sprite.Sprite):
    # [...]

    def update(self, event_list):

        for event in event_list:
            if event.type == pygame.MOUSEBUTTONDOWN:
                if self.rect.collidepoint(event.pos):
                    # [...]

my_sprite = SpriteObject()
group = pygame.sprite.Group(my_sprite)

# [...]

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

    group.update(event_list)

    # [...]

最小的例子: repl.it/@Rabbid76/PyGame-MouseClick

import pygame

class SpriteObject(pygame.sprite.Sprite):
    def __init__(self, x, y, color):
        super().__init__() 
        self.original_image = pygame.Surface((50, 50), pygame.SRCALPHA)
        pygame.draw.circle(self.original_image, color, (25, 25), 25)
        self.click_image = pygame.Surface((50, 50), pygame.SRCALPHA)
        pygame.draw.circle(self.click_image, color, (25, 25), 25)
        pygame.draw.circle(self.click_image, (255, 255, 255), (25, 25), 25, 4)
        self.image = self.original_image 
        self.rect = self.image.get_rect(center = (x, y))
        self.clicked = False

    def update(self, event_list):
        for event in event_list:
            if event.type == pygame.MOUSEBUTTONDOWN:
                if self.rect.collidepoint(event.pos):
                    self.clicked = not self.clicked

        self.image = self.click_image if self.clicked else self.original_image

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

sprite_object = SpriteObject(*window.get_rect().center, (128, 128, 0))
group = pygame.sprite.Group([
    SpriteObject(window.get_width() // 3, window.get_height() // 3, (128, 0, 0)),
    SpriteObject(window.get_width() * 2 // 3, window.get_height() // 3, (0, 128, 0)),
    SpriteObject(window.get_width() // 3, window.get_height() * 2 // 3, (0, 0, 128)),
    SpriteObject(window.get_width() * 2// 3, window.get_height() * 2 // 3, (128, 128, 0)),
])

run = True
while run:
    clock.tick(60)
    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            run = False 

    group.update(event_list)

    window.fill(0)
    group.draw(window)
    pygame.display.flip()

pygame.quit()
exit()

进一步了解从 Pygame 中的同一精灵类创建具有不同更新()的多个精灵


鼠标的当前位置可以通过

pygame.mouse.get_pos()
确定。返回值是一个元组,表示鼠标光标的 x 和 y 坐标。
pygame.mouse.get_pressed()
返回代表所有鼠标按钮状态(
True
False
)的布尔值列表。只要按下按钮,按钮的状态就是
True
。当按下多个按钮时,列表中的多个项目为
True
。列表中的第 1、2、3 个元素代表鼠标左键、中键和右键。

检测评估

Update
方法中
pygame.sprite.Sprite
对象的鼠标状态:

class SpriteObject(pygame.sprite.Sprite):
    # [...]

    def update(self, event_list):

        mouse_pos = pygame.mouse.get_pos()
        mouse_buttons = pygame.mouse.get_pressed()

        if  self.rect.collidepoint(mouse_pos) and any(mouse_buttons):
            # [...]

my_sprite = SpriteObject()
group = pygame.sprite.Group(my_sprite)

# [...]

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

    group.update(event_list)

    # [...]

最小的例子: repl.it/@Rabbid76/PyGame-MouseHover

import pygame

class SpriteObject(pygame.sprite.Sprite):
    def __init__(self, x, y, color):
        super().__init__() 
        self.original_image = pygame.Surface((50, 50), pygame.SRCALPHA)
        pygame.draw.circle(self.original_image, color, (25, 25), 25)
        self.hover_image = pygame.Surface((50, 50), pygame.SRCALPHA)
        pygame.draw.circle(self.hover_image, color, (25, 25), 25)
        pygame.draw.circle(self.hover_image, (255, 255, 255), (25, 25), 25, 4)
        self.image = self.original_image 
        self.rect = self.image.get_rect(center = (x, y))
        self.hover = False

    def update(self):
        mouse_pos = pygame.mouse.get_pos()
        mouse_buttons = pygame.mouse.get_pressed()

        #self.hover = self.rect.collidepoint(mouse_pos)
        self.hover = self.rect.collidepoint(mouse_pos) and any(mouse_buttons)

        self.image = self.hover_image if self.hover else self.original_image

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

sprite_object = SpriteObject(*window.get_rect().center, (128, 128, 0))
group = pygame.sprite.Group([
    SpriteObject(window.get_width() // 3, window.get_height() // 3, (128, 0, 0)),
    SpriteObject(window.get_width() * 2 // 3, window.get_height() // 3, (0, 128, 0)),
    SpriteObject(window.get_width() // 3, window.get_height() * 2 // 3, (0, 0, 128)),
    SpriteObject(window.get_width() * 2// 3, window.get_height() * 2 // 3, (128, 128, 0)),
])

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    group.update()

    window.fill(0)
    group.draw(window)
    pygame.display.flip()

pygame.quit()
exit()

10
投票

鼠标事件的 pygame 文档在这里.

您可以与

pygame.mouse.get_pressed
(如果需要)合作使用
pygame.mouse.get_pos
方法。

记得通过主事件循环使用鼠标点击事件。事件循环之所以更好,是因为“短点击”。在普通机器上您可能不会注意到这些,但是在触控板上使用点击的计算机的点击周期非常小。使用鼠标事件可以防止这种情况发生。

编辑: 要执行像素完美碰撞,请使用

pygame.sprite.collide_rect()
their docs for sprites上找到。


6
投票

我一直在寻找这个问题的相同答案,经过一番摸索之后,我得出了这个答案:

# Python 3.4.3 with Pygame
from sys import exit
import pygame
pygame.init()

WIDTH = HEIGHT = 300
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Crash!')

# Draw Once
rectangle = pygame.draw.rect(window, (255, 0, 0), (100, 100, 100, 100))
pygame.display.update()

# Main Loop
while True:
    # Mouse position and button clicking
    pos = pygame.mouse.get_pos()
    pressed1 = pygame.mouse.get_pressed()[0]

    # Check if rectangle collided with pos and if the left mouse button was pressed
    if rectangle.collidepoint(pos) and pressed1:
        print("You have opened a chest!")

    # Quit pygame
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
© www.soinside.com 2019 - 2024. All rights reserved.