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实现系统状态监测和故障转移实例方法
Nov 18 Python
python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)
Jun 09 Python
Python中encode()方法的使用简介
May 18 Python
Python获取运行目录与当前脚本目录的方法
Jun 01 Python
python3设计模式之简单工厂模式
Oct 17 Python
python实现随机调用一个浏览器打开网页
Apr 21 Python
python使用opencv驱动摄像头的方法
Aug 03 Python
Python中文编码知识点
Feb 18 Python
Python3转换html到pdf的不同解决方案
Mar 11 Python
Flask框架实现的前端RSA加密与后端Python解密功能详解
Aug 13 Python
python自动化测试无法启动谷歌浏览器问题
Oct 10 Python
解决Jupyter Notebook开始菜单栏Anaconda下消失的问题
Apr 13 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
php匹配字符中链接地址的方法
2014/12/22 PHP
php将access数据库转换到mysql数据库的方法
2014/12/24 PHP
Laravel 5.0 发布 新版本特性详解
2015/02/10 PHP
smarty学习笔记之常见代码段用法总结
2016/03/19 PHP
浅谈mysql_query()函数的返回值问题
2016/09/05 PHP
Windows下php+mysql5.7配置教程
2017/05/16 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
小程序微信支付功能配置方法示例详解【基于thinkPHP】
2019/05/05 PHP
javascript 一些用法小结
2009/09/11 Javascript
什么是json和jsonp,jQuery json实例详详细说明
2012/12/11 Javascript
简单实现限制uploadify上传个数
2015/11/16 Javascript
jQuery事件绑定on()与弹窗实现代码
2016/04/28 Javascript
如何获取元素的最终background-color
2017/02/06 Javascript
基于jQuery实现一个marquee无缝滚动的插件
2017/03/09 Javascript
在Vue中使用echarts的方法
2018/02/05 Javascript
vue项目中实现的微信分享功能示例
2019/01/21 Javascript
TypeScript中的方法重载详解
2019/04/12 Javascript
Nodejs libuv运行原理详解
2019/08/21 NodeJs
浅谈vue异步数据影响页面渲染
2019/10/29 Javascript
Vue.js暴露方法给WebView的使用操作
2020/09/07 Javascript
python字典基本操作实例分析
2015/07/11 Python
Python 性能优化技巧总结
2016/11/01 Python
python3 中文乱码与默认编码格式设定方法
2018/10/31 Python
python 对多个csv文件分别进行处理的方法
2019/01/07 Python
django model object序列化实例
2020/03/13 Python
python批量修改交换机密码的示例
2020/09/22 Python
荷兰天然和有机产品网上商城:BigGreenSmile.nl
2020/07/26 全球购物
程序员岗位职责
2013/11/11 职场文书
孝敬父母的演讲稿
2014/05/14 职场文书
中层干部竞聘演讲稿
2014/05/15 职场文书
教师考核材料
2014/05/21 职场文书
通信工程专业求职信
2014/06/04 职场文书
钳工实训报告总结
2014/11/04 职场文书
先进个人总结范文
2015/02/15 职场文书
mongodb数据库迁移变更的解决方案
2021/09/04 MongoDB
Windows 11上手初体验:任务栏和开始菜单等迎来大改
2021/11/21 数码科技