Python中pygame的mouse鼠标事件用法实例


Posted in Python onNovember 11, 2015

本文实例讲述了Python中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()

展示的效果:

Python中pygame的mouse鼠标事件用法实例

游戏效果:

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

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

#pygame mouse
import os, pygame
from pygame.locals import *
from sys import exit
from random import *
__author__ = {'name' : 'Hongten',
       'mail' : 'hongtenzone@foxmail.com',
       'Version' : '1.0'}
if not pygame.font:print('Warning, Can not found font!')
pygame.init()
screen = pygame.display.set_mode((255, 255), 0, 32)
screen.fill((255,255,255))
font = pygame.font.Font('data\\font\\TORK____.ttf', 20)
text = font.render('Cliked Me please!!!', True, (34, 252, 43))
mouse_x, mouse_y = 0, 0
while 1:
  for event in pygame.event.get():
    if event.type == QUIT:
      exit()
    elif event.type == MOUSEBUTTONDOWN:
      pressed_array = pygame.mouse.get_pressed()
      for index in range(len(pressed_array)):
        if pressed_array[index]:
          if index == 0:
            print('Pressed LEFT Button!')
          elif index == 1:
            print('The mouse wheel Pressed!')
          elif index == 2:
            print('Pressed RIGHT Button!')
    elif event.type == MOUSEMOTION:
      #return the X and Y position of the mouse cursor
      pos = pygame.mouse.get_pos()
      mouse_x = pos[0]
      mouse_y = pos[1]
  screen.fill((mouse_x, mouse_y, 0))    
  screen.blit(text, (40, 100))
  pygame.display.update()

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python2.5/2.6实用教程 入门基础篇
Nov 29 Python
Django admin美化插件suit使用示例
Dec 12 Python
python实现飞机大战微信小游戏
Mar 21 Python
Python3爬虫使用Fidder实现APP爬取示例
Nov 27 Python
用python做游戏的细节详解
Jun 25 Python
python实现字符串完美拆分split()的方法
Jul 16 Python
django中上传图片分页三级联动效果的实现代码
Aug 30 Python
python二进制读写及特殊码同步实现详解
Oct 11 Python
python实现遍历文件夹图片并重命名
Mar 23 Python
浅谈python 调用open()打开文件时路径出错的原因
Jun 05 Python
sklearn中的交叉验证的实现(Cross-Validation)
Feb 22 Python
Python下opencv库的安装过程及问题汇总
Jun 11 Python
Python基于pygame实现的font游戏字体(附源码)
Nov 11 #Python
python中pygame针对游戏窗口的显示方法实例分析(附源码)
Nov 11 #Python
python基于pygame实现响应游戏中事件的方法(附源码)
Nov 11 #Python
Python基于pygame实现的弹力球效果(附源码)
Nov 11 #Python
Python中pygame安装方法图文详解
Nov 11 #Python
Python基于pygame实现图片代替鼠标移动效果
Nov 11 #Python
python开发之thread线程基础实例入门
Nov 11 #Python
You might like
一个分页的论坛
2006/10/09 PHP
PHP用星号隐藏部份用户名、身份证、IP、手机号等实例
2014/04/08 PHP
Yii实现复选框批量操作实例代码
2017/03/15 PHP
用javascript实现在小方框中浏览大图的代码
2007/08/14 Javascript
JavaScript 撑出页面文字换行
2009/06/15 Javascript
javascript代码加载优化方法
2011/01/30 Javascript
jquery css 设置table的奇偶行背景色示例
2014/06/03 Javascript
JavaScript获取页面上被选中文字的方法技巧
2015/03/13 Javascript
jquery处理页面弹出层查询数据等待操作实例
2015/03/25 Javascript
JavaScript让网页出现渐隐渐显背景颜色的方法
2015/04/21 Javascript
JavaScript中reduce()方法的使用详解
2015/06/09 Javascript
jquery+CSS实现的水平布局多级网页菜单效果
2015/08/24 Javascript
jQuery.extend 函数及用法详细
2015/09/06 Javascript
jQuery实现的文字hover颜色渐变效果实例
2016/02/20 Javascript
JS实现的几个常用算法
2016/11/12 Javascript
Vue.js 2.0学习教程之从基础到组件详解
2017/04/24 Javascript
5分钟学会Vue动画效果(小结)
2018/07/21 Javascript
配置node服务器并且链接微信公众号接口配置步骤详解
2019/06/21 Javascript
[50:11]2018DOTA2亚洲邀请赛 4.7总决赛 LGD vs Mineski 第三场
2018/04/09 DOTA
使用Python的Scrapy框架十分钟爬取美女图
2016/12/26 Python
[原创]教女朋友学Python(一)运行环境搭建
2017/11/29 Python
python批量查询、汉字去重处理CSV文件
2018/05/31 Python
Python基于sklearn库的分类算法简单应用示例
2018/07/09 Python
python获取本机所有IP地址的方法
2018/12/26 Python
详解django+django-celery+celery的整合实战
2019/03/19 Python
用Python获取摄像头并实时控制人脸的实现示例
2019/07/11 Python
Python 函数绘图及函数图像微分与积分
2019/11/20 Python
Python多进程编程multiprocessing代码实例
2020/03/12 Python
python中什么是面向对象
2020/06/11 Python
scrapy-redis分布式爬虫的搭建过程(理论篇)
2020/09/29 Python
潘多拉意大利官方网上商城:网上选购PANDORA珠宝
2018/10/07 全球购物
《学会待客》教学反思
2014/02/22 职场文书
感谢信模板大全
2015/01/23 职场文书
2015年科室工作总结
2015/04/10 职场文书
2015年农村党员公开承诺事项
2015/04/28 职场文书
工程竣工验收申请报告
2015/05/15 职场文书