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 Django模板的使用方法(图文)
Nov 04 Python
Python threading多线程编程实例
Sep 18 Python
在Python中操作文件之truncate()方法的使用教程
May 25 Python
Python字典实现简单的三级菜单(实例讲解)
Jul 31 Python
python虚拟环境virtualenv的安装与使用
Sep 21 Python
MAC中PyCharm设置python3解释器
Dec 15 Python
详解python读取image
Apr 03 Python
python查找重复图片并删除(图片去重)
Jul 16 Python
使用Python脚本zabbix自定义key监控oracle连接状态
Aug 28 Python
python实现简单学生信息管理系统
Apr 09 Python
python中delattr删除对象方法的代码分析
Dec 15 Python
Python中使用Selenium环境安装的方法步骤
Feb 22 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写的基于Memcache的Queue实现代码
2011/11/27 PHP
使用php实现截取指定长度
2013/08/06 PHP
PHP反射机制用法实例
2014/08/28 PHP
php插入mysql数据返回id的方法
2018/05/31 PHP
laravel Task Scheduling(任务调度)在windows下的使用详解
2019/10/22 PHP
php把文件设置为插件的技巧方法
2020/02/03 PHP
在网页中屏蔽快捷键
2006/09/06 Javascript
JS+ACTIVEX实现网页选择本地目录路径对话框
2013/03/18 Javascript
js简单实现根据身份证号码识别性别年龄生日
2013/11/29 Javascript
jQuery简易图片放大特效示例代码
2014/06/09 Javascript
jQuery .tmpl() 用法示例介绍
2014/08/21 Javascript
javascript解析json实例详解
2014/11/05 Javascript
a标签的href与onclick事件的区别详解
2014/11/12 Javascript
vue.js,ajax渲染页面的实例
2018/02/11 Javascript
vue之将echart封装为组件
2018/06/02 Javascript
Vue组件创建和传值的方法
2018/08/17 Javascript
Vue2.4+新增属性.sync、$attrs、$listeners的具体使用
2020/03/08 Javascript
Vue toFixed保留两位小数的3种方式
2020/10/23 Javascript
深入理解Python中的元类(metaclass)
2015/02/14 Python
Python代码调试的几种方法总结
2015/04/15 Python
Python实现的数据结构与算法之链表详解
2015/04/22 Python
Python进阶之递归函数的用法及其示例
2018/01/31 Python
python实现log日志的示例代码
2018/04/28 Python
python筛选出两个文件中重复行的方法
2018/05/31 Python
python实现批量文件重命名
2019/10/31 Python
python反转列表的三种方式解析
2019/11/08 Python
Python学习之time模块的基本使用
2021/01/17 Python
关于期中考试的反思
2014/02/02 职场文书
出纳担保书范文
2014/04/02 职场文书
公务员政审单位鉴定材料
2014/05/16 职场文书
植物生产学专业求职信
2014/08/08 职场文书
2014办公室副主任四风对照检查材料思想汇报
2014/09/20 职场文书
学生会个人总结范文
2015/02/15 职场文书
十大必看国产动漫排名,魁拔上线,第二曾在日本播出
2022/03/18 国漫
利用nginx搭建RTMP视频点播、直播、HLS服务器
2022/05/25 Servers
类和原型的设计模式之复制与委托差异
2022/07/07 Javascript