从pygame的api,它有:
event type.MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION
但是没有办法区分右键,左键点击?
if event.type == pygame.MOUSEBUTTONDOWN: print event.button
event.button可以等于几个整数值:
1 - 左键单击
2 - 中间点击
3 - 右键单击
4 - 向上滚动
5 - 向下滚动
您也可以获取当前按钮状态,而不是事件:
pygame.mouse.get_pressed()
这会返回一个元组:
(leftclick,middleclick,rightclick)
每一个都是一个布尔整数,表示按钮向上/向下.
您可能需要仔细查看本教程,以及n.st 对此SO问题的回答.
因此,向您展示如何区分右键和左键单击的代码如下所示:
#!/usr/bin/env python
import pygame
LEFT = 1
RIGHT = 3
running = 1
screen = pygame.display.set_mode((320, 200))
while running:
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = 0
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
print "You pressed the left mouse button at (%d, %d)" % event.pos
elif event.type == pygame.MOUSEBUTTONUP and event.button == LEFT:
print "You released the left mouse button at (%d, %d)" % event.pos
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == RIGHT:
print "You pressed the right mouse button at (%d, %d)" % event.pos
elif event.type == pygame.MOUSEBUTTONUP and event.button == RIGHT:
print "You released the right mouse button at (%d, %d)" % event.pos
screen.fill((0, 0, 0))
pygame.display.flip()