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 ---------------鼠标点击事 ...
随机推荐
- 《Java从入门到精通》src0-8
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello wo ...
- display:table 水平居中
<div style="width:auto; margin:auto;display:table"> <div style="width: 100px ...
- cpp check 分析
1 FileTabCharacterCheck 为什么检查: 因为对于一个TAB而言,所空的空格不定是固定的,如果在机器A上设置了是4个空格,显示正常,而在机器B上阅读,B机器是100个空格为一个TA ...
- Passenger/Nginx/Debian快速部署Rails
安装所需的linux包 sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl gi ...
- 如何在github中贡献自己的力量
如何参与github的开源项目? 1.找一个发出“pull requests”的项目.有以下几个方法:最简单的方式是,读项目的readme文件.它会告诉你,项目的拥有者是否急切的需要协助.如果read ...
- AS3开发必须掌握的内容
1.事件机制 2.显示列表 3.垃圾回收 4.常用方法 5.网络通信 6.位图动画 7.渲染机制 8.API结构 9.沙箱机制 10.资源管理 11.内存管理 12.性能优化 13.资源选择 14.安 ...
- 14.4.3.5 Configuring InnoDB Buffer Pool Flushing 配置InnoDB Buffer Pool 刷新:
14.4.3.5 Configuring InnoDB Buffer Pool Flushing 配置InnoDB Buffer Pool 刷新: InnoDB执行某些任务在后台, 包括flush 脏 ...
- 使用PageHeap.EXE或GFlags.EXE检查内存越界错误
必先利其器之一:使用PageHeap.EXE或GFlags.EXE检查内存越界错误 Article last modified on 2002-6-3 ------------------------ ...
- hdu 1070Milk
地址链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070 题意:多看几遍,学着静下来心去看英文题 代码: #include<bits/stdc++. ...
- [Android]Button按下后修改背景图
Button按下后修改背景图 错误做法:为Button添加OnTouch事件监听,根据ACTION_UP和ACTION_DOWN动作来修改Button的背景图 错误原因:从理论上讲,按钮按下修改背景色 ...