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 ---------------鼠标点击事 ...
随机推荐
- lastIndexOf方法——获取字符最后的索引
1.2.20 lastIndexOf方法——获取字符最后的索引 2013-08-29 14:55:18 我来说两句 收藏 我要投稿 本文所属图书 > Java程序开发参考手册 ...
- ASIO攻破!!!----转
from:http://www.cppblog.com/shanoa/archive/2009/06/26/88606.aspx 花了足足3天时间,外加1天心情休整,终于在第5天编写出了一个能运行的基 ...
- Perl入门(四)Perl的正則表達式
正則表達式是Perl语言的特色.主要的语法不是非常难,可是编写一个符合需求.高效的正則表達式.还是有一些挑战的. Perl的三种匹配模式 1.查找 语法:m/正則表達式内容/; 作用:查找匹配内容中是 ...
- Qt调用Delphi编写的COM组件
这个问题捣鼓了两天,现在终于解决了,做个笔记分享给大家,以免走弯路 起初,我的想法是在DLL中写一个interface并从函数中导出这个interface,像这样的代码 ICom1 = interfa ...
- Xtrabackup使用指南 | 简单.生活
Xtrabackup使用指南 | 简单.生活 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbackup的一个很好 ...
- jqueryUI中datepicker的使用,解决与asp.net中的UpdatePanel联合使用时的失效问题
1.jqueryUI的datepicker的使用 -->首先在jqueryUI官网上根据你的需要下载适合你系统主题的样式,jqueryUI主题下载地址: -->下载后的文件 jquery- ...
- opencv做的美女找茬程序~
// CMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <cv.h> #include <high ...
- FOJ 2170 花生的序列 dp
题目链接:http://acm.fzu.edu.cn/problem.php? pid=2170 贴个baka爷的代码留念.. 数据出的有问题.输入的字符串长度不超过1000 #include< ...
- DTD学习笔记
1. DTD基本介绍 xml文件分为两种类型,一个是在好形式,这是well-formed,还有一个合法有效,这是valid. XML文件遵循-called"好形式"各种语法规则要 ...
- UVa 474 - Heads / Tails Probability
题目:计算1/(2^n)的值的前4为有效数字以及位数. 分析:数论,大整数.直接用数组模拟就可以. 说明:打表计算.查询输出. #include <iostream> #include & ...