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用ConfigObj读写配置文件的实现代码
Mar 04 Python
跟老齐学Python之有容乃大的list(1)
Sep 14 Python
Python爬取网页中的图片(搜狗图片)详解
Mar 23 Python
Python使用遗传算法解决最大流问题
Jan 29 Python
如何使用 Pylint 来规范 Python 代码风格(来自IBM)
Apr 06 Python
python 列表,数组和矩阵sum的用法及区别介绍
Jun 28 Python
python脚本实现音频m4a格式转成MP3格式的实例代码
Oct 09 Python
Python要如何实现列表排序的几种方法
Feb 21 Python
python3中datetime库,time库以及pandas中的时间函数区别与详解
Apr 16 Python
python同时遍历两个list用法说明
May 02 Python
如何用Python提取10000份log中的产品信息
Jan 14 Python
Python3自带工具2to3.py 转换 Python2.x 代码到Python3的操作
Mar 03 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的优缺点
2015/07/14 PHP
让你的PHP7更快之Hugepage用法分析
2016/05/31 PHP
在laravel中实现ORM模型使用第二个数据库设置
2019/10/24 PHP
php设计模式之抽象工厂模式分析【星际争霸游戏案例】
2020/01/23 PHP
Laravel6.18.19如何优雅的切换发件账户
2020/06/14 PHP
JavaScript 滚轮事件使用说明
2010/03/07 Javascript
javascript代码加载优化方法
2011/01/30 Javascript
JavaScript作用域链使用介绍
2013/08/29 Javascript
利用javascript实现web页面中指定区域打印
2013/10/30 Javascript
javascript实现的DES加密示例
2013/10/30 Javascript
js键盘事件的keyCode
2014/07/29 Javascript
基于javascript的COOkie的操作实现只能点一次
2014/12/26 Javascript
jQuery插件实现控制网页元素动态居中显示
2015/03/24 Javascript
js滚动条平滑移动示例代码
2016/03/29 Javascript
jQuery UI仿淘宝搜索下拉列表功能
2017/01/10 Javascript
laravel5.3 vue 实现收藏夹功能实例详解
2018/01/21 Javascript
基于JS实现前端压缩上传图片的实例代码
2019/05/14 Javascript
kafka调试中遇到Connection to node -1 could not be established. Broker may not be available.
2019/09/17 Javascript
javascript设计模式 ? 命令模式原理与用法实例分析
2020/04/20 Javascript
[02:19]2018年度DOTA2最佳核心位选手-完美盛典
2018/12/17 DOTA
python实现redis三种cas事务操作
2017/12/19 Python
利用python求积分的实例
2019/07/03 Python
python程序如何进行保存
2020/07/03 Python
Python unittest生成测试报告过程解析
2020/09/08 Python
Django ModelForm组件原理及用法详解
2020/10/12 Python
使用CSS3实现一个3D相册效果实例
2016/12/03 HTML / CSS
博朗(Braun)俄罗斯官方商店:德国小家电品牌
2019/09/24 全球购物
巴西24小时在线药房:Droga Raia
2020/05/12 全球购物
少先队学雷锋活动总结范文
2014/03/09 职场文书
科学育儿宣传标语
2014/10/08 职场文书
公司人事任命通知
2015/04/20 职场文书
作弊检讨书范文
2015/05/06 职场文书
公文格式,规则明细(新手收藏)
2019/07/23 职场文书
HR必备:销售经理聘用合同范本
2019/08/21 职场文书
互联网创业商业模式以及赚钱法则有哪些?
2019/10/12 职场文书
关于python中模块和重载的问题
2021/11/02 Python