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()

展示的效果:

游戏效果:

当鼠标经过窗口的时候,窗口背景颜色会随着鼠标的移动而发生改变,当鼠标点击窗口

会在控制台打印出是鼠标的那个键被点击了:左,右,滚轮

========================================

代码部分:

========================================

 1 #pygame mouse
 2
 3 import os, pygame
 4 from pygame.locals import *
 5 from sys import exit
 6 from random import *
 7
 8 __author__ = {'name' : 'Hongten',
 9               'mail' : 'hongtenzone@foxmail.com',
10               'blog' : 'http://www.cnblogs.com/hongten',
11               'Version' : '1.0'}
12
13 if not pygame.font:print('Warning, Can not found font!')
14
15 pygame.init()
16
17 screen = pygame.display.set_mode((255, 255), 0, 32)
18 screen.fill((255,255,255))
19
20 font = pygame.font.Font('data\\font\\TORK____.ttf', 20)
21 text = font.render('Cliked Me please!!!', True, (34, 252, 43))
22
23 mouse_x, mouse_y = 0, 0
24 while 1:
25     for event in pygame.event.get():
26         if event.type == QUIT:
27             exit()
28         elif event.type ==  MOUSEBUTTONDOWN:
29             pressed_array = pygame.mouse.get_pressed()
30             for index in range(len(pressed_array)):
31                 if pressed_array[index]:
32                     if index == 0:
33                         print('Pressed LEFT Button!')
34                     elif index == 1:
35                         print('The mouse wheel Pressed!')
36                     elif index == 2:
37                         print('Pressed RIGHT Button!')
38         elif event.type == MOUSEMOTION:
39             #return the X and Y position of the mouse cursor
40             pos = pygame.mouse.get_pos()
41             mouse_x = pos[0]
42             mouse_y = pos[1]
43
44     screen.fill((mouse_x, mouse_y, 0))
45     screen.blit(text, (40, 100))
46     pygame.display.update()

========================================================

More reading,and english is important.

I'm Hongten

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。Hongten博客排名在100名以内。粉丝过千。Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================

pygame系列_mouse鼠标事件相关推荐

  1. python pygame鼠标点击_pygame系列_mouse鼠标事件

    pygame.mouse提供了一些方法获取鼠标设备当前的状态 '''pygame.mouse.get_pressed - get the state of the mouse buttons get ...

  2. python pygame鼠标点击_Python中pygame的mouse鼠标事件用法实例

    本文实例讲述了Python中pygame的mouse鼠标事件用法.分享给大家供大家参考,具体如下: pygame.mouse提供了一些方法获取鼠标设备当前的状态 ''' pygame.mouse.ge ...

  3. python鼠标点击事件event_opencv-python教程学习系列5-处理鼠标事件

    前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍opencv-python处理鼠标事件,坚持学习,共同进步. 系统环境 系统:win_x64; p ...

  4. python鼠标事件_Python之pygame的鼠标事件

    鼠标 当显示模式设置后,事件队列就会开始收到鼠标事件.当按下和释放鼠标的按键时,会产生 pygame.MOUSEBUTTONDOWN和pygame.MOUSEBUTTONUP事件.这些事件包含一个bu ...

  5. 给键盘上的enter设置事件_Selenium3 + Python3自动化测试系列——鼠标事件和键盘事件...

    欢迎各位小哥哥小姐姐阅读本的文章,对大家学习有帮助,请点赞加关注哦!!!!!!!!!! 您的点赞和关注将是我持续更新的动力呢.^v^ 有不懂的问题可以私聊我哦! 一.鼠标事件 在 WebDriver ...

  6. Open3D-GUI系列教程(五)鼠标事件(拾取顶点)

    鼠标事件(拾取顶点) 这里实现一下鼠标拾取顶点的操作.open3d本身提供了交互选点的操作gui.SceneWidget.Controls.PICK_POINTS,但是出于某些超出我认知范围的因素,这 ...

  7. pygame系列_原创百度随心听音乐播放器_完整版

    程序名:PyMusic 解释:pygame+music 之前发布了自己写的小程序:百度随心听音乐播放器的一些效果图 你可以去到这里再次看看效果: pygame系列_百度随心听_完美的UI设计 这个程序 ...

  8. 常用事件方法及技巧(二) -- MouseEvent(鼠标事件)

    先说明一下,我并不会把所有的内容都写出来,只列我认为有必要讲解一下的内容.如果要了解全部内容,请参看Flash自带的帮助文件.该系列文章都是按照这个思路写的. 先说一个本人觉得很实用的通用方法:toS ...

  9. 第53天:鼠标事件、event事件对象

    -->鼠标事件 -->event事件对象 -->默认事件 -->键盘事件(keyCode) -->拖拽效果 一.鼠标事件 onclick ---------------鼠 ...

最新文章

  1. linux下使用tar命令
  2. 快速排序(Python实现)
  3. python那么慢为什么还有人用-为什么大家都说python编程的效率速度慢呢?
  4. 解决MySQL查询数据不一致诟病
  5. Git笔记(15) 远程分支
  6. linux 的FAQ
  7. Ansible Loops
  8. .30-浅析webpack源码之doResolve事件流(2)
  9. 如何写好一篇博客(文章)
  10. CentOS8下安装snort2.9.18.1
  11. 【工业机器人】机器人产业的前途取决于人工智能关键技术的发展
  12. # 第一次面试问题详解
  13. 磁盘如何除写保护(常规解决方案)
  14. php让浏览器全屏,js实现各浏览器全屏代码
  15. cesium 鼠标点击事件获取各种坐标
  16. Module Error (from ./node_modules/vue-loader/lib/loaders/templateLoader.js):(Emitted value instead o
  17. dnf神龙95端不显示服务器,《DNF》95版本任务BUG解决办法 95版本任务找不到怎么回事...
  18. scala中的breakable{}实现continu 与break
  19. 是对马的鬼魂日本RPG
  20. Java并发知识点快速复习手册(下)

热门文章

  1. IOS固定IP对动态IP用pre-share
  2. iOS红马甲项目Bug总结(3)
  3. 编译安装openresty+mariadb+php7
  4. 在XMPP的JAVA开源实现Openfire中,增加LBS 附近的人功能
  5. rails的一些问题
  6. [歌曲]心愿(by 四个女生)
  7. 河北省重大系统征集系统案例分析
  8. 大数据,人工智能网易百度这些公司都走在前列
  9. Android设备adb授权的原理【转】
  10. 微服务常见安全认证方案Session token cookie跨域