Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame鼠标点击检测_Python_Mouseevent_Pygame - Fatal编程技术网

Python Pygame鼠标点击检测

Python Pygame鼠标点击检测,python,mouseevent,pygame,Python,Mouseevent,Pygame,我想知道如何编写代码来检测鼠标点击精灵。例如: if #Function that checks for mouse clicked on Sprite: print ("You have opened a chest!") 。您可以使用pygame.mouse.get_pressed方法与pygame.mouse.get_pos配合使用(如果需要)。但请通过主事件循环使用鼠标单击事件。事件循环之所以更好,是因为“短点击”。您可能不会在普通机器上注意到这些,但在轨迹板上使用点击的计算机

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

if #Function that checks for mouse clicked on Sprite:
    print ("You have opened a chest!")
。您可以使用
pygame.mouse.get_pressed
方法与
pygame.mouse.get_pos
配合使用(如果需要)。但请通过主事件循环使用鼠标单击事件。事件循环之所以更好,是因为“短点击”。您可能不会在普通机器上注意到这些,但在轨迹板上使用点击的计算机的点击周期过小。使用鼠标事件将防止出现这种情况

编辑:
要执行像素完美碰撞,请使用上的
pygame.sprite.collide\rect()

我假设您的游戏有一个主循环,并且所有精灵都在一个名为
sprite
的列表中

在主循环中,获取所有事件,并检查
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...
所以基本上你必须在主循环的每一次迭代中自己检查是否点击了精灵。您将需要使用和

Pygame不提供事件驱动编程,例如

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

if pygame.mouse.get_pressed()[0] and mysprite.rect.collidepoint(pygame.mouse.get_pos()):
  print ("You have opened a chest!")
如果您处理这个案例,您将不得不引入某种标志,因为否则在主循环的每次迭代中,代码都会打印“youhaveopenabecast!”

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
,并添加一个名为
的方法,单击后如下所示:

class MySprite(Sprite):
  ...

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

因此,最好使用第一种方法IMHO。

我一直在寻找这个问题的相同答案,经过反复思考,我得出了以下答案:

#Python 3.4.3 with Pygame
import pygame

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


# Draw Once
Rectplace = 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, pressed2, pressed3 = pygame.mouse.get_pressed()
    # Check if the rect collided with the mouse pos
    # and if the left mouse button was pressed.
    if Rectplace.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()
            quit(1)

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

使用对象的
rect
属性和方法查看是否单击了精灵。 将事件列表传递给的方法,以便可以处理Sprite类中的事件:

类SpriteObject(pygame.sprite.sprite):
# [...]
def更新(自身、事件列表):
对于事件列表中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN:
如果self.rect.collidepoint(事件位置):
# [...]
我的精灵=精灵对象()
group=pygame.sprite.group(我的精灵)
# [...]
运行=真
运行时:
event_list=pygame.event.get()
对于事件列表中的事件:
如果event.type==pygame.QUIT:
运行=错误
组更新(事件列表)
# [...]
最简单的例子:

导入pygame
类SpriteObject(pygame.sprite.sprite):
定义初始值(自、x、y、颜色):
super()。\uuuu init\uuuuu()
self.original_image=pygame.Surface((50,50),pygame.SRCALPHA)
pygame.draw.circle(self.original_图像,颜色,(25,25,25)
self.click_image=pygame.Surface((50,50),pygame.SRCALPHA)
pygame.draw.circle(self.click_图像,颜色,(25,25,25)
pygame.draw.circle(self.click_image,(255,255,255),(25,25),25,4)
self.image=self.original\u图像
self.rect=self.image.get_rect(中心=(x,y))
self.clicked=False
def更新(自身、事件列表):
对于事件列表中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN:
如果self.rect.collidepoint(事件位置):
self.clicked=未self.clicked
self.image=self.click\u image如果self.clicked else self.original\u image
pygame.init()
window=pygame.display.set_模式((300300))
clock=pygame.time.clock()
sprite_object=SpriteObject(*window.get_rect().center,(128,128,0))
group=pygame.sprite.group([
SpriteObject(window.get\u width()//3,window.get\u height()//3,(128,0,0)),
SpriteObject(window.get\u width()*2//3,window.get\u height()//3,(0,128,0)),
SpriteObject(window.get\u width()//3,window.get\u height()*2//3,(0,0,128)),
SpriteObject(window.get_width()*2//3,window.get_height()*2//3,(128,128,0)),
])
运行=真
运行时:
时钟滴答(60)
event_list=pygame.event.get()
对于事件列表中的事件:
如果event.type==pygame.QUIT:
运行=错误
组更新(事件列表)
窗口填充(0)
组图(窗口)
pygame.display.flip()
pygame.quit()
退出()
进一步看


鼠标的当前位置可通过确定。返回值是一个元组,表示鼠标光标的x和y坐标。返回布尔值的列表​​表示所有鼠标按钮的状态(
True
False
)。只要按下按钮,按钮的状态就是
True
。当按下多个按钮时,列表中的多个项目为
True
。列表中的第一、第二和第三个元素表示鼠标左键、中键和右键

在对象的
更新
方法中检测并评估鼠标状态:

类SpriteObject(pygame.sprite.sprite):
# [...]
def更新(自身、事件列表):
mouse\u pos=pygame.mouse.get\u pos()
mouse\u buttons=pygame.mouse.get\u pressed()
如果self.rect.collidepoint(鼠标位置)和任意(鼠标按钮):
# [...]
我的精灵=精灵对象()
group=pygame.sprite.group(我的