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实现登陆知乎获得个人收藏并保存为word文件
Mar 16 Python
Python中的自省(反射)详解
Jun 02 Python
一篇文章快速了解Python的GIL
Jan 12 Python
Python实现的NN神经网络算法完整示例
Jun 19 Python
python使用numpy读取、保存txt数据的实例
Oct 14 Python
Django缓存系统实现过程解析
Aug 02 Python
python实现微信打飞机游戏
Mar 24 Python
如何基于python实现不邻接植花
May 01 Python
python怎么判断素数
Jul 01 Python
python录音并调用百度语音识别接口的示例
Dec 01 Python
利用Python过滤相似文本的简单方法示例
Feb 03 Python
python中的None与NULL用法说明
May 25 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 远程关机实现代码
2009/11/10 PHP
php Smarty date_format [格式化时间日期]
2010/03/15 PHP
PHP文件上传、客户端和服务器端加限制、抓取错误信息、完整步骤解析
2017/01/12 PHP
php异常处理捕获错误整理
2019/09/23 PHP
深入分析PHP设计模式
2020/06/15 PHP
eclipse如何忽略js文件报错(附图)
2013/10/30 Javascript
jquery学习总结(超级详细)
2014/09/04 Javascript
javascript 回调函数详解
2014/11/11 Javascript
node.js中的fs.readdir方法使用说明
2014/12/17 Javascript
jQuery经过一段时间自动隐藏指定元素的方法
2015/03/17 Javascript
简述Jquery与DOM对象
2015/07/10 Javascript
javascript实现的上下无缝滚动效果
2016/09/19 Javascript
echarts3 使用总结(绘制各种图表,地图)
2017/01/05 Javascript
jQuery Validate表单验证插件实现代码
2017/06/08 jQuery
vue中七牛插件使用的实例代码
2017/07/28 Javascript
SVG动画vivus.js库使用小结(实例代码)
2017/09/14 Javascript
如何基于viewport vm适配移动端页面
2020/11/13 Javascript
[02:23]2016国际邀请赛中国区预选赛wings晋级之路
2016/06/29 DOTA
Python语法快速入门指南
2015/10/12 Python
python中redis查看剩余过期时间及用正则通配符批量删除key的方法
2018/07/30 Python
django echarts饼图数据动态加载的实例
2019/08/12 Python
详解python3 GUI刷屏器(附源码)
2021/02/18 Python
详解HTML5 window.postMessage与跨域
2017/05/11 HTML / CSS
W Concept美国:精选全球独立设计师
2017/02/22 全球购物
Lime Crime官网:美国一家主打梦幻精灵系的彩妆品牌
2019/03/22 全球购物
科颜氏香港官方网店:Kiehl’s香港
2021/03/07 全球购物
什么是接口(Interface)?
2013/02/01 面试题
掌上明珠Java程序员面试总结
2016/02/23 面试题
《唯一的听众》教学反思
2014/02/20 职场文书
松材线虫病防治方案
2014/06/15 职场文书
创先争优活动承诺书
2014/08/30 职场文书
庆七一晚会主持词
2015/06/30 职场文书
活动简报范文
2015/07/22 职场文书
教你解决往mysql数据库中存入汉字报错的方法
2021/05/06 MySQL
python unittest单元测试的步骤分析
2021/08/02 Python
Java死锁的排查
2022/05/11 Java/Android