2 回答
![?](http://img1.sycdn.imooc.com/54584cde0001d19202200220-100-100.jpg)
TA贡献1795条经验 获得超7个赞
pygame.mouse.get_pressed()
当处理事件时,将评估返回的坐标。pygame.event.pump()
您需要通过或 来处理事件pygame.event.get()
。
参见pygame.event.get()
:
对于游戏的每一帧,您都需要对事件队列进行某种调用。这确保您的程序可以在内部与操作系统的其余部分进行交互。
pygame.mouse.get_pressed()
返回代表所有鼠标按钮状态的布尔值序列。因此,您必须评估any
按钮是否被按下(any(buttons)
)或者是否通过订阅按下了特殊按钮(例如buttons[0]
)。
例如:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 800))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
buttons = pygame.mouse.get_pressed()
# if buttons[0]: # for the left mouse button
if any(buttons): # for any mouse button
print("You are clicking")
else:
print("You released")
pygame.display.update()
如果您只想检测鼠标按钮何时按下或释放,那么您必须实现MOUSEBUTTONDOWN
and MOUSEBUTTONUP
(参见pygame.event
模块):
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 800))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
print("You are clicking", event.button)
if event.type == pygame.MOUSEBUTTONUP:
print("You released", event.button)
pygame.display.update()
Whilepygame.mouse.get_pressed()返回按钮的当前状态,而 MOUSEBUTTONDOWN和MOUSEBUTTONUP仅在按下按钮后发生。
![?](http://img1.sycdn.imooc.com/5458502c00012d4a02200220-100-100.jpg)
TA贡献1848条经验 获得超10个赞
函数 pygame.mouse.get_pressed 返回一个包含 true 或 false 的列表,因此对于单击,您应该使用-
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 800))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
mouse = pygame.mouse.get_pressed()
if mouse[0]:
print("You are clicking")
else:
print("You released")
添加回答
举报