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 ---------------鼠标点击事 ...
随机推荐
- Jsp中使用数据库连接池.
原文 Jsp中使用数据库连接池. 1. 在tomcat服务器目录下面的conf中找到一个叫Context.xml的配置文件,在其中加入以下代码 <Resource name="jdbc ...
- J2EE SSH学习(二)安装Eclipse插件和第一个Eclipse项目
(一)安装Eclipse插件 Eclipse有很多功能很强大的插件,我现在作为一个菜鸟只知道插件的功能通常都很牛叉实用或者很有趣,那么该怎么安装Eclipse插件呢? 我使用的是Eclipse 4.3 ...
- Codeforces Round #256 (Div. 2)总结
这次CF状态之悲剧,比赛就别提了.后来应该好好总结. A题:某个细节没考虑到,导致T了 代码: #include<cstdio> #include<cstring> #incl ...
- python与其它语言进行比較
近期python语言貌似比較火, 今天闲来无事,简单的看了下,算是个入门吧.一门语言之所以值得这么多人去学,必然有它的独到之处,以下我们就用python和其它语言做个比較. Pythond VS C# ...
- MongoDB学习笔记(五) MongoDB文件存取操作
由于MongoDB的文档结构为BJSON格式(BJSON全称:Binary JSON),而BJSON格式本身就支持保存二进制格式的数据,因此可以把文件的二进制格式的数据直接保存到MongoDB的文档结 ...
- 大约sources.list和apt-get [转载]
个人觉得,Debian这与最大的方便apt-get安装软件,apt-get这可能是工作:/etc/apt/sources.list文件中保存着一些server的设置,在这些server上有大量的能够用 ...
- JS - 点击 “+” 、“-” 改变数字
效果: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.a ...
- Delphi的VMT的结构图,很清楚
Every Delphi class is defined internally by its vmt—its virtual-method table. The vmt contains a li ...
- Mysql主键索引、唯一索引、普通索引、全文索引、组合索引的区别
原文:Mysql主键索引.唯一索引.普通索引.全文索引.组合索引的区别 Mysql索引概念: 说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不 ...
- Mybatis 3 返回布尔值,需要注意的地方
在Mybatis中,有时候需要返回布尔值 ,来确定某个记录行是否存在. 例如: <select id="isExistCode" parameterType="st ...