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 相关文章推荐
详解Django中的ifequal和ifnotequal标签使用
Jul 16 Python
基于Python中单例模式的几种实现方式及优化详解
Jan 09 Python
基于Django与ajax之间的json传输方法
May 29 Python
在Django model中设置多个字段联合唯一约束的实例
Jul 17 Python
Python3实现二叉树的最大深度
Sep 30 Python
Python FTP文件定时自动下载实现过程解析
Nov 12 Python
利用Python绘制Jazz网络图的例子
Nov 21 Python
Python 实现try重新执行
Dec 21 Python
python实现密码验证合格程序的思路详解
Jun 01 Python
详解python命令提示符窗口下如何运行python脚本
Sep 11 Python
python try...finally...的实现方法
Nov 25 Python
Python实现制作销售数据可视化看板详解
Nov 27 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
Windows下的PHP安装文件线程安全和非线程安全的区别
2014/04/23 PHP
php建立Ftp连接的方法
2015/03/07 PHP
5款适合PHP使用的HTML编辑器推荐
2015/07/03 PHP
PHP 自动加载的简单实现(推荐)
2016/08/12 PHP
在云虚拟主机部署thinkphp5项目的步骤详解
2017/12/21 PHP
向fckeditor编辑器插入指定代码的方法
2007/05/25 Javascript
某人初学javascript的时候写的学习笔记
2010/12/30 Javascript
jQuery把表单元素变为json对象
2013/11/06 Javascript
各浏览器对document.getElementById等方法的实现差异解析
2013/12/05 Javascript
javascript中apply和call方法的作用及区别说明
2014/02/14 Javascript
浏览器窗口大小变化时使用resize事件对框架不起作用的解决方法
2014/05/11 Javascript
JavaScript中的包装对象介绍
2015/01/27 Javascript
AngularJS+Node.js实现在线聊天室
2015/08/28 Javascript
js简单判断移动端系统的方法
2016/02/25 Javascript
jQuery progressbar通过Ajax请求实现后台进度实时功能
2016/10/11 Javascript
使用开源工具制作网页验证码的方法
2016/10/17 Javascript
jQuery Checkbox 全选 反选的简单实例
2016/11/29 Javascript
js中setTimeout的妙用--防止循环超时
2017/03/06 Javascript
微信小程序动态生成二维码的实现代码
2018/07/25 Javascript
layui使用label标签的方法
2019/09/14 Javascript
python中的lambda表达式用法详解
2016/06/22 Python
VScode编写第一个Python程序HelloWorld步骤
2018/04/06 Python
pandas DataFrame数据转为list的方法
2018/04/11 Python
Python实现SQL注入检测插件实例代码
2019/02/02 Python
pandas dataframe的合并实现(append, merge, concat)
2019/06/24 Python
pandas读取CSV文件时查看修改各列的数据类型格式
2019/07/07 Python
在pytorch中对非叶节点的变量计算梯度实例
2020/01/10 Python
Python文件操作基础流程解析
2020/03/19 Python
解决Jupyter Notebook使用parser.parse_args出现错误问题
2020/04/20 Python
使用Keras实现Tensor的相乘和相加代码
2020/06/18 Python
pycharm如何使用anaconda中的各种包(操作步骤)
2020/07/31 Python
解决HTML5中滚动到底部的事件问题
2019/08/22 HTML / CSS
雅诗兰黛旗下走天然植物路线的彩妆品牌:Prescriptives
2016/08/14 全球购物
阿姆斯特丹杜莎夫人蜡像馆官方网站:Madame Tussauds Amsterdam
2019/03/12 全球购物
pandas中DataFrame数据合并连接(merge、join、concat)
2021/05/30 Python
详解apache编译安装httpd-2.4.54及三种风格的init程序特点和区别
2022/07/15 Servers