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()
展示的效果:
游戏效果:
当鼠标经过窗口的时候,窗口背景颜色会随着鼠标的移动而发生改变,当鼠标点击窗口
会在控制台打印出是鼠标的那个键被点击了:左,右,滚轮
========================================
代码部分:
========================================

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

pygame系列_mouse鼠标事件的更多相关文章
- JavaScript进阶系列07,鼠标事件
鼠标事件有Keydown, Keyup, Keypress,但Keypress与Keydown和Keyup不同,如果按ctrl, shift, caps lock......等修饰键,不会触发Keyp ...
- Selenium3 + Python3自动化测试系列四——鼠标事件和键盘事件
一.鼠标事件 在 WebDriver 中, 将这些关于鼠标操作的方法封装在 ActionChains 类提供. ActionChains 类提供了鼠标操作的常用方法. ActionChains 类的成 ...
- opencv-python教程学习系列5-处理鼠标事件
前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍opencv-python处理鼠标事件,坚持学习,共同进步. 系列教程参照OpenCV-Pytho ...
- 吴裕雄--天生自然python学习笔记:python 用pygame模块检测键盘事件和鼠标事件
用户可通过键盘输入来操控游戏中角色的运动,取得键盘事件的方法有以下两种 : 常用的按键与键盘常数对应表 : 按下右箭头键,蓝色小球会 向 右移动:按住右箭头键不放 , 球体会快速 向 右移 动, 若到 ...
- 深入理解DOM事件类型系列第一篇——鼠标事件
× 目录 [1]类型 [2]顺序 [3]坐标位置[4]修改键[5]相关元素[6]鼠标按键[7]滚轮事件[8]移动设备 前面的话 鼠标事件是web开发中最常用的一类事件,毕竟鼠标是最主要的定位设备.本文 ...
- opencv入门系列教学(四)处理鼠标事件
一.鼠标事件的简单演示 opencv中的鼠标事件,值得是任何与鼠标相关的任何事物,例如左键按下,左键按下,左键双击等.我们先来看看鼠标事件有哪些,在python中执行下面代码: import cv2 ...
- pygame系列_原创百度随心听音乐播放器_完整版
程序名:PyMusic 解释:pygame+music 之前发布了自己写的小程序:百度随心听音乐播放器的一些效果图 你可以去到这里再次看看效果: pygame系列_百度随心听_完美的UI设计 这个程序 ...
- Java知多少(93)鼠标事件
鼠标事件的事件源往往与容器相关,当鼠标进入容器.离开容器,或者在容器中单击鼠标.拖动鼠标时都会发生鼠标事件.java语言为处理鼠标事件提供两个接口:MouseListener,MouseMotionL ...
- 第53天:鼠标事件、event事件对象
-->鼠标事件-->event事件对象-->默认事件-->键盘事件(keyCode)-->拖拽效果 一.鼠标事件 onclick ---------------鼠标点击事 ...
随机推荐
- vs2012 不显示最近项目
visual studio起始页不显示最近使用项目的解决办法方法一 1.开始 → 运行 → 输入 regedit 回车,打开注册表编辑器. 2.定位到 HKEY_CURRENT_USER/Softwa ...
- 虚拟机安装中文Fedora14和C/C++IDE开发环境
虚拟机安装中文Fedora14和C/C++IDE开发环境 2010-12-05 00:15:58 标签:中文Fedora14 IDE 开发环境 C/C++ 原创作品,允许转载,转载时请务必以超链接形式 ...
- mysql基础: mysql列类型--字符串
mysql列类型:整型 http://blog.csdn.net/jk110333/article/details/9342283 mysql列类型--时间和日期 http://blog.csd ...
- Android学习笔记(十三)——碎片(一)
碎片 碎片可看作第二种形式的活动,能够创建碎片来包括视图. 碎片总是嵌入在活动中,一般有两种常见形式: 1.碎片A和碎片B分别处于不同的活动中,当选择碎片A中的某一项时,触发碎片B启动: 2.碎片A和 ...
- javascript (十二)对象二
JavaScript 中的所有事物都是对象:字符串.数字.数组.日期,等等. 在 JavaScript 中,对象是拥有属性和方法的数据. 属性和方法 属性是与对象相关的值. 方法是能够在对象上执行的动 ...
- Swift 可展开可收缩的表视图
主要学习与运行效果 在本节的内容中,我们将通过一个具体实例的实现过程,详细讲解在表视图当中,如何创建一个可展开可收缩的表视图.为了让读者有着更为直观的印象,我们将通过模仿QQ好友列表来实现这个效果. ...
- Swift - 协议(protocol)
1,Swift中协议类似于别的语言里的接口,协议里只做方法的声明,包括方法名.返回值.参数等信息,而没有具体的方法实现. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- 五、Linux/UNIX操作命令积累【cp、mv、cat、grep、ps】
在使用Linux/UNIX下,常常会使用文本界面去设置系统或操作系统,作者本人在工作的过程也在不断接触这方面的命令,所以为此特酝酿.准备.開始了本文的编写.本文主要记录自己平时遇到的一些Linux/U ...
- 中国大概可用NTPserver地址
133.100.11.8 prefer210.72.145.44203.117.180.36131.107.1.10time.asia.apple.com64.236.96.53130.149.17. ...
- MVC之国际化
MVC之国际化 前言 在项目中遇到国际化语言的问题是常有的事情,之前在做关于MVC国际化语言时,刚开始打算全部利用AngularJS来实现,但是渐渐发现对于页面Title难以去控制其语言转换,于是对于 ...