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 相关文章推荐
Unicode和Python的中文处理
Mar 19 Python
用python结合jieba和wordcloud实现词云效果
Sep 05 Python
Python实现简单的文本相似度分析操作详解
Jun 16 Python
解决在pycharm中显示额外的 figure 窗口问题
Jan 15 Python
python中对数据进行各种排序的方法
Jul 02 Python
Python搭建Spark分布式集群环境
Jul 05 Python
扩展Django admin的list_filter()可使用范围方法
Aug 21 Python
Python3 虚拟开发环境搭建过程(图文详解)
Jan 06 Python
Python Selenium参数配置方法解析
Jan 19 Python
python numpy--数组的组合和分割实例
Feb 24 Python
python中for in的用法详解
Apr 17 Python
pyecharts在数据可视化中的应用详解
Jun 08 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
历史证明,懒惰才是推动科学发展技术进步的动力
2021/03/02 无线电
PHP迭代器实现斐波纳契数列的函数
2013/11/12 PHP
php计算2个日期的差值函数分享
2015/02/02 PHP
PHP实现股票趋势图和柱形图
2015/02/07 PHP
php删除二维数组中的重复值方法
2018/03/12 PHP
csdn 博客的css样式 v3
2009/02/24 Javascript
利用javascript解决图片缩放及其优化的代码
2012/05/23 Javascript
js函数返回多个返回值的示例代码
2013/11/05 Javascript
js出生日期 年月日级联菜单示例代码
2014/01/10 Javascript
将查询条件的input、select清空
2014/01/14 Javascript
JavaScript中如何通过arguments对象实现对象的重载
2014/05/12 Javascript
基于NodeJS的前后端分离的思考与实践(四)安全问题解决方案
2014/09/26 NodeJs
分享使用AngularJS创建应用的5个框架
2015/12/05 Javascript
javascript显示倒计时控制按钮的简单实现
2016/06/07 Javascript
HTML5 JS压缩图片并获取图片BASE64编码上传
2020/11/16 Javascript
jquery Form轻松实现文件上传
2017/05/24 jQuery
微信小程序学习之数据处理详解
2017/07/05 Javascript
js分页之前端代码实现和请求处理
2017/08/04 Javascript
前端常见跨域解决方案(全)
2017/09/19 Javascript
详解Vue如何支持JSX语法
2017/11/10 Javascript
JS实现百度搜索接口及链接功能实例代码
2018/02/02 Javascript
layui加载表格,绑定新增,编辑删除,查看按钮事件的例子
2019/09/06 Javascript
JavaScript 斐波那契数列 倒序输出 输出100以内的质数代码实例
2019/09/11 Javascript
JavaScript 面向对象程序设计详解【类的创建、实例对象、构造函数、原型等】
2020/05/12 Javascript
Openlayers学习之地图比例尺控件
2020/09/28 Javascript
原生JavaScript实现幻灯片效果
2021/02/19 Javascript
[01:08:30]DOTA2-DPC中国联赛 正赛 Ehome vs Elephant BO3 第一场 2月28日
2021/03/11 DOTA
python基础教程之实现石头剪刀布游戏示例
2014/02/11 Python
在Python3中使用asyncio库进行快速数据抓取的教程
2015/04/02 Python
python文件操作相关知识点总结整理
2016/02/22 Python
美国一家主打母婴用品的团购网站:zulily
2017/09/19 全球购物
Skyscanner香港:机票比价, 平机票和廉价航空机票预订
2020/02/07 全球购物
澳大利亚家具商店:Freedom
2020/12/17 全球购物
仓库规划计划书
2014/04/28 职场文书
离婚答辩状怎么写
2015/05/22 职场文书
竞聘书的秘诀
2019/04/02 职场文书