網路資源很多,很快就搜尋到別人寫的pygame.mouse的code.
https://sivasantosh.wordpress.com/2012/07/18/mouse-handling-in-pygame/
但看來得需要視窗化的環境才能運作,且運作範圍在開啟的視窗內.難怪我上一篇blog是失敗的…
下圖是執行畫面.右邊的視窗是screen= pygame.display.set_mode(size) 開啟的視窗
滑鼠的按鍵動作 以及 指標位置 需在視窗內才會有反應.
原始碼如下
import pygame, sys
white= (255, 255, 255)
black= (0, 0, 0)
pygame.init()
pygame.display.set_caption('Mouse Example')
size= [640, 480]
screen= pygame.display.set_mode(size)
clock= pygame.time.Clock()
# make os mouse pointer icon invisible
pygame.mouse.set_visible(False)
while True:
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
sys.exit()
# print if mouse is pressed.
# get_pressed() tells you which mouse button is pressed
if event.type== pygame.MOUSEBUTTONDOWN:
print ('mouse pressed',pygame.mouse.get_pressed())
# print if mouse is released.
elif event.type== pygame.MOUSEBUTTONUP:
print ('mouse released', pygame.mouse.get_pressed())
# print if mouse is in motion.
# get_rel() - Returns the amount of movement in X and Y since the previous call to this function.
if event.type== pygame.MOUSEMOTION:
print ('mouse is moving', pygame.mouse.get_rel())
screen.fill(white)
# draw a circle around the mouse pointer
pos= (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
pygame.draw.circle(screen, black, pos, 10, 0)
pygame.display.update()
clock.tick(20)
沒有留言:
張貼留言