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)

沒有留言:

張貼留言

名古屋行前準備

旅行先預訂了機+酒;不能讓這件旅遊的任務失敗.行前準備要做好 名古屋5天4夜家庭旅行計畫 ( 根據Grok的建議) 2026年1月30日 - 2月3日(2大2小,一個家庭) 行程採用 QFN思考模式 ,確保父母(45-50歲)執行力與小孩(6-12歲)樂趣兼顧: Quality(...