京公网安备 11010802034615号
经营许可证编号:京B2-20210330
Python中pygame的mouse鼠标事件用法实例
这篇文章主要介绍了Python中pygame的mouse鼠标事件用法,以完整实例形式详细分析了pygame响应鼠标事件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下具体如下:
pygame.mouse提供了一些方法获取鼠标设备当前的状态
'''
pygame.mouse.get_pressed - get the state of the mouse buttons get the state of the mouse buttons
pygame.mouse.get_pos - get the mouse cursor position get the mouse cursor position
pygame.mouse.get_rel - get the amount of mouse movement get the amount of mouse movement
pygame.mouse.set_pos - set the mouse cursor position set the mouse cursor position
pygame.mouse.set_visible - hide or show the mouse cursor hide or show the mouse cursor
pygame.mouse.get_focused - check if the display is receiving mouse input check if the display is receiving mouse input
pygame.mouse.set_cursor - set the image for the system mouse cursor set the image for the system mouse cursor
pygame.mouse.get_cursor - get the image for the system mouse cursor get the image for the system mouse cursor
'''
在下面的demo中,主要用到了:
pygame.mouse.get_pressed()
pygame.mouse.get_pos()
展示的效果:
游戏效果:
当鼠标经过窗口的时候,窗口背景颜色会随着鼠标的移动而发生改变,当鼠标点击窗口
会在控制台打印出是鼠标的那个键被点击了:左,右,滚轮
#pygame mouse
import os, pygame
from pygame.locals import *
from sys import exit
from random import *
__author__ = {'name' : 'Hongten',
'mail' : 'hongtenzone@foxmail.com',
'Version' : '1.0'}
if not pygame.font:print('Warning, Can not found font!')
pygame.init()
screen = pygame.display.set_mode((255, 255), 0, 32)
screen.fill((255,255,255))
font = pygame.font.Font('data\\font\\TORK____.ttf', 20)
text = font.render('Cliked Me please!!!', True, (34, 252, 43))
mouse_x, mouse_y = 0, 0
while 1:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
if index == 0:
print('Pressed LEFT Button!')
elif index == 1:
print('The mouse wheel Pressed!')
elif index == 2:
print('Pressed RIGHT Button!')
elif event.type == MOUSEMOTION:
#return the X and Y position of the mouse cursor
pos = pygame.mouse.get_pos()
mouse_x = pos[0]
mouse_y = pos[1]
screen.fill((mouse_x, mouse_y, 0))
screen.blit(text, (40, 100))
pygame.display.update()
希望本文所述对大家Python程序设计有所帮助。
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
CDA数据分析师 出品 作者:李诗怡 一、数据处理概述 定义: 在正式开展数据分析之前,对原始采集的数据进行整理、修正、加工和精 ...
2026-11-06一、为什么数据要先标准化 拿到一份数据,各列的量纲往往完全不同:年龄单位是"岁"(约 20~60),收入单位是"元"(几千到几万) ...
2026-11-06一、为什么 Python 成了统计分析的主流工具 十年前做统计分析,主流选择是 SPSS、SAS、Stata。现在越来越多的研究者、分析师转向 ...
2026-11-06核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-06核心资源推荐 CDA教材(2025):https://edu.cda.cn/goods/show/3151CDA模拟题(2025):https://edu.cda.cn/goods/show/2845CD ...
2026-11-06“大学生必备证书有哪些”最容易被答成一张越长越好的清单。但校招筛选的真实逻辑是:证书要对应你投的岗位任务,并且在校期间确 ...
2026-11-05大学期间能报的证书,先别急着看“含金量榜单”。更实用的第一步是问:我现在这个身份、这个学历,符合报考条件吗? 很多证书宣 ...
2026-11-05大学期间能考的证书很多,但真正需要先回答的是两个问题:当前有没有报考资格,以及这张证书对应什么岗位任务。 前者决定你能不 ...
2026-11-05先把结论说清楚:真正“只有在校大学生能考、毕业就彻底没资格”的证书数量不多,主要集中在语言类考试的部分专项、面向高校学生 ...
2026-11-05上班族问“考什么证书有用”,真正要解决的不是找一张“最值钱”的证,而是判断哪张证能嵌进你现有的岗位任务。同样一张证书,做 ...
2026-11-05职称证书不是统一的考试项目。同样“考职称”,经济、会计、统计、审计、软考走全国统一考试,工程、教师、卫生等系列多数走评审 ...
2026-11-0533岁选证书,最怕两件事:一是被“适合女生”的清单带着走,考完发现岗位用不上;二是投入大半年,报名时才发现学历、专业或工作 ...
2026-11-05“女生容易考的证书”这个问题,真正难的不是找一份名单,而是判断“容易”到底指什么。报名条件宽松、考试通过率高、拿证后能找 ...
2026-11-05“女生适合考什么证书”这类搜索,背后往往是同一个困惑:网上榜单太多,口径却说不清楚。有人把培训证书和职业资格混在一起排, ...
2026-11-05先把结论说清楚:没有一张证书对所有人都“又好考又实用”。同一个证,对在校生可能报名门槛都过不了,对有三年工作经验的人却只 ...
2026-11-05“考证书有哪些”这个问题,如果只想要一份名单,网上能搜到成百上千个。但真正有用的答案不是“有哪些”,而是“它们分别是什么 ...
2026-11-05“考什么证好一点”这句话,十个人问可能有十个意思。有人想问哪个证容易过,有人想知道哪个证找工作管用,还有人关心哪个证长期 ...
2026-11-05“考哪些证书最吃香”很难用一张榜单回答。同一张证书,在有的岗位是硬门槛,在另一些岗位只是加分项;对在校生和在职者的价值也 ...
2026-11-05家庭主妇能考的证书不少,但真正值得花时间的,不是“哪个证热门”,而是先确认你当前能不能报,再按每周可投入时间和未来出口缩 ...
2026-11-05“国家认证的职业技能证书”并不是某一本证书的正式名称,而是一句被大量使用的宣传口径。它可能指向人社部门的国家职业资格、备 ...
2026-11-05