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中的choice()方法使用详解
May 15 Python
Django中使用celery完成异步任务的示例代码
Jan 23 Python
python八皇后问题的解决方法
Sep 27 Python
python 限制函数执行时间,自己实现timeout的实例
Jan 12 Python
python3在同一行内输入n个数并用列表保存的例子
Jul 20 Python
Python在OpenCV里实现极坐标变换功能
Sep 02 Python
python 字符串常用函数详解
Sep 11 Python
Python闭包与装饰器原理及实例解析
Apr 30 Python
Python多线程正确用法实例解析
May 30 Python
python和php哪个更适合写爬虫
Jun 22 Python
python中关于数据类型的学习笔记
Jul 19 Python
Python实现壁纸下载与轮换
Oct 19 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内置过滤器FILTER使用实例
2014/06/25 PHP
深入分析PHP优化及注意事项
2016/07/04 PHP
Gird组件 Part-3:范例RSSFeed Viewer
2007/03/10 Javascript
在textarea中显示html页面的javascript代码
2007/04/20 Javascript
javascript新手语法小结
2008/06/15 Javascript
一些常用且实用的原生JavaScript函数
2010/09/08 Javascript
各种常用的JS函数整理
2013/10/25 Javascript
关于javascript中dataset的问题小结
2015/11/16 Javascript
AngularJs bootstrap详解及示例代码
2016/09/01 Javascript
RGB和YUV 多媒体编程基础详细介绍
2016/11/04 Javascript
jQuery制作图片旋转效果
2017/02/02 Javascript
bootstrap输入框组使用方法
2017/02/07 Javascript
js省市区级联查询(插件版&无插件版)
2017/03/21 Javascript
react.js使用webpack搭配环境的入门教程
2017/08/14 Javascript
Vue.js组件间的循环引用方法示例
2017/12/27 Javascript
vue-router的HTML5 History 模式设置
2018/09/08 Javascript
微信小程序使用蓝牙小插件
2019/09/23 Javascript
javascript实现简易数码时钟
2020/03/30 Javascript
Vue发布订阅模式实现过程图解
2020/04/30 Javascript
浅谈JavaScript窗体Window.ShowModalDialog使用
2020/07/22 Javascript
python 内置函数filter
2017/06/01 Python
Python3几个常见问题的处理方法
2019/02/26 Python
python发送多人邮件没有展示收件人问题的解决方法
2019/06/21 Python
Python实现串口通信(pyserial)过程解析
2019/09/25 Python
Python接口开发实现步骤详解
2020/04/26 Python
使用Python内置模块与函数进行不同进制的数的转换
2020/04/26 Python
CSS3 @font-face属性使用指南
2014/12/12 HTML / CSS
css3编写浏览器背景渐变背景色的方法
2018/03/05 HTML / CSS
html5使用canvas画三角形
2014/12/15 HTML / CSS
英国的一家创新礼品和小工具零售商:Menkind
2019/08/24 全球购物
波兰快递服务:Globkurier.pl
2019/11/08 全球购物
大客户销售经理职责
2013/12/04 职场文书
会计专业应届生自荐信
2014/06/28 职场文书
领导班子四风问题对照检查材料
2014/09/27 职场文书
群众路线教育实践活动学习笔记内容
2014/11/06 职场文书
2015年行政人事部工作总结
2015/05/13 职场文书