
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
Pandas 选取特定值所在行:6 类核心方法与实战指南 在使用 pandas 处理结构化数据时,“选取特定值所在的行” 是最高频的操作之 ...
2025-09-30球面卷积神经网络(SCNN) 为解决这一痛点,球面卷积神经网络(Spherical Convolutional Neural Network, SCNN) 应运而生。它通 ...
2025-09-30在企业日常运营中,“未来会怎样” 是决策者最关心的问题 —— 电商平台想知道 “下月销量能否达标”,金融机构想预判 “下周股 ...
2025-09-30Excel 能做聚类分析吗?基础方法、进阶技巧与场景边界 在数据分析领域,聚类分析是 “无监督学习” 的核心技术 —— 无需预设分 ...
2025-09-29XGBoost 决策树:原理、优化与工业级实战指南 在机器学习领域,决策树因 “可解释性强、处理非线性关系能力突出” 成为基础模型 ...
2025-09-29在标签体系的落地链路中,“设计标签逻辑” 只是第一步,真正让标签从 “纸上定义” 变为 “业务可用资产” 的关键,在于标签加 ...
2025-09-29在使用 Excel 数据透视表进行多维度数据汇总时,折叠功能是梳理数据层级的核心工具 —— 通过点击 “+/-” 符号可展开明细数据或 ...
2025-09-28在使用 Pandas 处理 CSV、TSV 等文本文件时,“引号” 是最容易引发格式混乱的 “隐形杀手”—— 比如字段中包含逗号(如 “北京 ...
2025-09-28在 CDA(Certified Data Analyst)数据分析师的技能工具箱中,数据查询语言(尤其是 SQL)是最基础、也最核心的 “武器”。无论 ...
2025-09-28Cox 模型时间依赖性检验:原理、方法与实战应用 在生存分析领域,Cox 比例风险模型(Cox Proportional Hazards Model)是分析 “ ...
2025-09-26检测因子类型的影响程度大小:评估标准、实战案例与管控策略 在检测分析领域(如环境监测、食品质量检测、工业产品合规性测试) ...
2025-09-26CDA 数据分析师:以数据库为基石,筑牢数据驱动的 “源头防线” 在数据驱动业务的链条中,“数据从哪里来” 是 CDA(Certified D ...
2025-09-26线性相关点分布的四种基本类型:特征、识别与实战应用 在数据分析与统计学中,“线性相关” 是描述两个数值变量间关联趋势的核心 ...
2025-09-25深度神经网络神经元个数确定指南:从原理到实战的科学路径 在深度神经网络(DNN)的设计中,“神经元个数” 是决定模型性能的关 ...
2025-09-25在企业数字化进程中,不少团队陷入 “指标困境”:仪表盘上堆砌着上百个指标,DAU、转化率、营收等数据实时跳动,却无法回答 “ ...
2025-09-25MySQL 服务器内存碎片:成因、检测与内存持续增长的解决策略 在 MySQL 运维中,“内存持续增长” 是常见且隐蔽的性能隐患 —— ...
2025-09-24人工智能重塑工程质量检测:核心应用、技术路径与实践案例 工程质量检测是保障建筑、市政、交通、水利等基础设施安全的 “最后一 ...
2025-09-24CDA 数据分析师:驾驭通用与场景指标,解锁数据驱动的精准路径 在数据驱动业务的实践中,指标是连接数据与决策的核心载体。但并 ...
2025-09-24在数据驱动的业务迭代中,AB 实验系统(负责验证业务优化效果)与业务系统(负责承载用户交互与核心流程)并非独立存在 —— 前 ...
2025-09-23CDA 业务数据分析:6 步闭环,让数据驱动业务落地 在企业数字化转型中,CDA(Certified Data Analyst)数据分析师的核心价值,并 ...
2025-09-23