Python中pygame的mouse鼠标事件用法实例


Posted in Python onNovember 11, 2015

本文实例讲述了Python中pygame的mouse鼠标事件用法。分享给大家供大家参考,具体如下:

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()

展示的效果:

Python中pygame的mouse鼠标事件用法实例

游戏效果:

当鼠标经过窗口的时候,窗口背景颜色会随着鼠标的移动而发生改变,当鼠标点击窗口

会在控制台打印出是鼠标的那个键被点击了:左,右,滚轮

#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程序设计有所帮助。

Python 相关文章推荐
Python实现的拉格朗日插值法示例
Jan 08 Python
解决Numpy中sum函数求和结果维度的问题
Dec 06 Python
使用Python爬虫库requests发送请求、传递URL参数、定制headers
Jan 25 Python
基于python实现查询ip地址来源
Jun 02 Python
一文弄懂Pytorch的DataLoader, DataSet, Sampler之间的关系
Jul 03 Python
python中关于数据类型的学习笔记
Jul 19 Python
python绘制分布折线图的示例
Sep 24 Python
互斥锁解决 Python 中多线程共享全局变量的问题(推荐)
Sep 28 Python
Python 下载Bing壁纸的示例
Sep 29 Python
Python3读写ini配置文件的示例
Nov 06 Python
python subprocess pipe 实时输出日志的操作
Dec 05 Python
用Python制作音乐海报
Jan 26 Python
Python基于pygame实现的font游戏字体(附源码)
Nov 11 #Python
python中pygame针对游戏窗口的显示方法实例分析(附源码)
Nov 11 #Python
python基于pygame实现响应游戏中事件的方法(附源码)
Nov 11 #Python
Python基于pygame实现的弹力球效果(附源码)
Nov 11 #Python
Python中pygame安装方法图文详解
Nov 11 #Python
Python基于pygame实现图片代替鼠标移动效果
Nov 11 #Python
python开发之thread线程基础实例入门
Nov 11 #Python
You might like
ThinkPHP CURD方法之data方法详解
2014/06/18 PHP
php实现猴子选大王问题算法实例
2015/04/20 PHP
PHP与Web页面交互操作实例分析
2020/06/02 PHP
dojo 之基础篇(二)之从服务器读取数据
2007/03/24 Javascript
对google个性主页的拖拽效果的js的完整注释[转]
2007/04/10 Javascript
JavaScript和ActionScript的交互实现代码
2010/08/01 Javascript
Three.js源码阅读笔记(光照部分)
2012/12/27 Javascript
一个简单的JS时间控件示例代码(JS时分秒时间控件)
2013/11/22 Javascript
JavaScript自定义日期格式化函数详细解析
2014/01/14 Javascript
jQuery focus和blur事件的应用详解
2014/01/26 Javascript
JavaScript实现下拉列表框数据增加、删除、上下排序的方法
2015/08/11 Javascript
JavaScript基础重点(必看)
2016/07/09 Javascript
jQuery 选择器(61种)整理总结
2016/09/26 Javascript
jQuery中$.ajax()方法参数解析
2016/10/22 Javascript
AngularJS入门教程之过滤器用法示例
2016/11/02 Javascript
BootStrap实现带关闭按钮功能
2017/02/15 Javascript
Vue写一个简单的倒计时按钮功能
2018/04/20 Javascript
JavaScript获取当前url路径过程解析
2019/12/27 Javascript
Python中threading模块join函数用法实例分析
2015/06/04 Python
Python抓取淘宝下拉框关键词的方法
2015/07/08 Python
python日志记录模块实例及改进
2017/02/12 Python
利用Django内置的认证视图实现用户密码重置功能详解
2017/11/24 Python
python-pyinstaller、打包后获取路径的实例
2019/06/10 Python
python构建指数平滑预测模型示例
2019/11/21 Python
PyTorch中torch.tensor与torch.Tensor的区别详解
2020/05/18 Python
Python timeit模块原理及使用方法
2020/10/10 Python
CSS3 中filter(滤镜)属性使用详解
2020/04/07 HTML / CSS
StubHub美国:购买或出售您的门票
2019/07/09 全球购物
武汉世纪畅想数字传播有限公司.NET笔试题
2014/07/22 面试题
2014年道德讲堂实施方案
2014/03/05 职场文书
电子信息专业应届生自荐信
2014/06/04 职场文书
个人查摆问题自查报告
2014/10/16 职场文书
QT与javascript交互数据的实现
2021/05/26 Javascript
Tensorflow与RNN、双向LSTM等的踩坑记录及解决
2021/05/31 Python
win11无线投屏在哪设置? win11无线投屏功能的使用方法
2022/04/08 数码科技
Android开发手册自定义Switch开关按钮控件
2022/06/10 Java/Android