Pygame實(shí)現(xiàn)監(jiān)聽鼠標(biāo)示例詳解
pygame如何捕捉鼠標(biāo)的活動
初始化參數(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)活動") 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)移動
event.type 事件為MOUSEMOTION,則為鼠標(biāo)移動,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)的按下和放開動作
mouse_down = mouse_up = 0 mouse_down_x = mouse_down_y = 0 mouse_up_x = mouse_up_y = 0
輸出鼠標(biāo)位置及其對用的按鈕
pygame.mouse.get_pressed() 可以監(jiān)聽鼠標(biāo)的三個按鍵。
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)的移動位置
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)活動")
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)的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python?pygame項(xiàng)目實(shí)戰(zhàn)英雄動畫特效實(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)文章
詳解Django+Uwsgi+Nginx的生產(chǎn)環(huán)境部署
這篇文章主要介紹了Django + Uwsgi + Nginx 的生產(chǎn)環(huán)境部署,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06在多種情況/開發(fā)環(huán)境中運(yùn)行python腳本和代碼的技巧分享
Python腳本或程序是包含可執(zhí)行Python代碼的文件,能夠運(yùn)行Python腳本和代碼可能是您作為Python開發(fā)人員所需的最重要的技能,在本教程中,您將學(xué)習(xí)一些運(yùn)行Python腳本和代碼的技術(shù),在每種情況下使用的技術(shù)將取決于您的環(huán)境、平臺、需求和技能2023-11-11Python實(shí)現(xiàn)自動化刷抖音的實(shí)例
今天我們來學(xué)習(xí)如何用Python實(shí)現(xiàn)自動刷抖音,并為顏值高的的小哥哥小姐姐點(diǎn)贊并評論。感興趣的可以了解一下2021-06-06Python NumPy創(chuàng)建數(shù)組方法
這篇文章主要介紹了Python NumPy創(chuàng)建數(shù)組方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09Python基礎(chǔ)之python循環(huán)控制語句break/continue詳解
Python中提供了兩個關(guān)鍵字用來控制循環(huán)語句,分別是break和continue,接下來通過兩個案例來區(qū)分這兩個控制語句的不同,感興趣的朋友一起看看吧2021-09-09pandas實(shí)戰(zhàn):分析三國志人物示例實(shí)現(xiàn)
這篇文章主要介紹了pandas實(shí)戰(zhàn):分析三國志人物示例實(shí)現(xiàn),本文章內(nèi)容詳細(xì),具有很好的參考價(jià)值,希望對大家有所幫助,需要的朋友可以參考下2023-01-01對Python 檢查文件名是否規(guī)范的實(shí)例詳解
今天小編就為大家分享一篇對Python 檢查文件名是否規(guī)范的實(shí)例詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06