Pygame實(shí)現(xiàn)監(jiān)聽鼠標(biāo)示例詳解
pygame如何捕捉鼠標(biāo)的活動(dòng)
初始化參數(shù)
import pygame, sys
from pygame.locals import *
def print_text(font, x, y, text, color=(0, 0, 0)):
"""打印字體函數(shù)"""
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
pygame.init()
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("監(jiān)聽鼠標(biāo)活動(dòng)")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
pygame.display.update()

鼠標(biāo)移動(dòng)
event.type 事件為MOUSEMOTION,則為鼠標(biāo)移動(dòng),event.pos可以獲取當(dāng)前位置,event.rel鼠標(biāo)的偏移。
event.type == MOUSEMOTION:
event.pos
event.rel
我們將位置輸出出來,定義鼠標(biāo)的位置和鼠標(biāo)的偏移量
mouse_x = mouse_y = 0
move_x = move_y = 0
print_text(font1, 0, 0, "鼠標(biāo)事件")
print_text(font1, 0, 20, "鼠標(biāo)的位置:" + str(mouse_x) + "," + str(mouse_y))
print_text(font1, 0, 40, "鼠標(biāo)的偏移:" + str(move_x) + "," + str(move_y))

鼠標(biāo)點(diǎn)擊位置
MOUSEBUTTONDOWN和MOUSEBUTTONUP記錄鼠標(biāo)的按下和放開動(dòng)作
mouse_down = mouse_up = 0 mouse_down_x = mouse_down_y = 0 mouse_up_x = mouse_up_y = 0

輸出鼠標(biāo)位置及其對(duì)用的按鈕
pygame.mouse.get_pressed() 可以監(jiān)聽鼠標(biāo)的三個(gè)按鍵。
x, y = pygame.mouse.get_pos()
print_text(font1, 0, 180, "鼠標(biāo)位置:" + str(x) + "," + str(y))
b1, b2, b3 = pygame.mouse.get_pressed()
print_text(font1, 0, 200, "按鈕:" + str(b1) + "," + str(b2) + "," + str(b3))

完整代碼?
import pygame, sys
from pygame.locals import *
def print_text(font, x, y, text, color=(0, 0, 0)):
"""打印字體函數(shù)"""
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
pygame.init()
# 字體
font1 = pygame.font.SysFont("方正粗黑宋簡體", 18)
# 鼠標(biāo)的移動(dòng)位置
mouse_x = mouse_y = 0
move_x = move_y = 0
mouse_down = mouse_up = 0
mouse_down_x = mouse_down_y = 0
mouse_up_x = mouse_up_y = 0
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("監(jiān)聽鼠標(biāo)活動(dòng)")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEMOTION:
mouse_x, mouse_y = event.pos
move_x, mouse_y = event.rel
elif event.type == MOUSEBUTTONDOWN:
mouse_down = event.button
mouse_down_x, mouse_down_y = event.pos
elif event.type == MOUSEBUTTONUP:
mouse_up = event.button
mouse_up_x, mouse_up_y = event.pos
screen.fill((255, 255, 255))
print_text(font1, 0, 0, "鼠標(biāo)事件")
print_text(font1, 0, 20, "鼠標(biāo)的位置:" + str(mouse_x) + "," + str(mouse_y))
print_text(font1, 0, 40, "鼠標(biāo)的偏移:" + str(move_x) + "," + str(move_y))
print_text(font1, 0, 60, "鼠標(biāo)按下:" + str(mouse_down)
+ "在" + str(mouse_down_x) + "," + str(mouse_down_y))
print_text(font1, 0, 80, "鼠標(biāo)松開:" + str(mouse_up)
+ "在" + str(mouse_up_x) + "," + str(mouse_up_y))
x, y = pygame.mouse.get_pos()
print_text(font1, 0, 180, "鼠標(biāo)位置:" + str(x) + "," + str(y))
b1, b2, b3 = pygame.mouse.get_pressed()
print_text(font1, 0, 200, "按鈕:" + str(b1) + "," + str(b2) + "," + str(b3))
pygame.display.update()
以上就是Pygame實(shí)現(xiàn)監(jiān)聽鼠標(biāo)示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Pygame監(jiān)聽鼠標(biāo)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python?pygame項(xiàng)目實(shí)戰(zhàn)英雄動(dòng)畫特效實(shí)現(xiàn)
- Python?pygame新手入門基礎(chǔ)教程
- Python+Pygame實(shí)現(xiàn)之見縫插針游戲的實(shí)現(xiàn)
- 教你使用Python的pygame模塊實(shí)現(xiàn)拼圖游戲
- Python+Pygame實(shí)現(xiàn)趣味足球游戲
- python?pygame實(shí)現(xiàn)打磚塊游戲
- Python+Pygame實(shí)現(xiàn)經(jīng)典魂斗羅游戲
- Python?pygame項(xiàng)目實(shí)戰(zhàn)監(jiān)聽退出事件
相關(guān)文章
python多進(jìn)程及通信實(shí)現(xiàn)異步任務(wù)的方法
這篇文章主要介紹了python多進(jìn)程及通信實(shí)現(xiàn)異步任務(wù)需求,本人也是很少接觸多進(jìn)程的場景,對(duì)于python多進(jìn)程的使用也是比較陌生的。在接觸了一些多進(jìn)程的業(yè)務(wù)場景下,對(duì)python多進(jìn)程的使用進(jìn)行了學(xué)習(xí),覺得很有必要進(jìn)行一個(gè)梳理總結(jié),感興趣的朋友一起看看吧2022-05-05
python中exec函數(shù)的實(shí)現(xiàn)
exec()是Python內(nèi)置的一個(gè)函數(shù),用于在運(yùn)行時(shí)執(zhí)行動(dòng)態(tài)生成的Python代碼,下面就來介紹一下python中exec函數(shù)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
python圖形開發(fā)GUI庫pyqt5的詳細(xì)使用方法及各控件的屬性與方法
這篇文章主要介紹了python圖形開發(fā)GUI庫pyqt5的詳細(xì)使用方法及各控件的屬性與方法,需要的朋友可以參考下2020-02-02
使用actor-critic方法來控制CartPole-V0 游戲詳解
這篇文章主要為大家介紹了使用actor-critic方法來控制CartPole-V0 游戲詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
python基于itchat模塊實(shí)現(xiàn)微信防撤回
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信防撤回,基于itchat模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Django啟動(dòng)時(shí)找不到mysqlclient問題解決方案
這篇文章主要介紹了Django啟動(dòng)時(shí)找不到mysqlclient問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

