2015年7月8日 星期三

pygame.mouse動作測試

網路資源很多,很快就搜尋到別人寫的pygame.mouse的code.
https://sivasantosh.wordpress.com/2012/07/18/mouse-handling-in-pygame/
但看來得需要視窗化的環境才能運作,且運作範圍在開啟的視窗內.難怪我上一篇blog是失敗的…

下圖是執行畫面.右邊的視窗是screen= pygame.display.set_mode(size) 開啟的視窗
滑鼠的按鍵動作 以及 指標位置 需在視窗內才會有反應.

image
 
原始碼如下

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)

沒有留言:

張貼留言

2024東京迪士尼,愛與夢想的樂園行

疫情的關係, 很多年沒有出國旅遊了.這回計畫出遊日本.歷經磨難的挑選.最後勝出的行程是五福旅行社的"銀色雪東京五日-戲雪,和服體驗,迪士尼"這標題簡單的標註出此行的目的. 上回去東京是20年前了.可以參考一下過去 2004年的Blog紀錄 心中的願景是能看到前...