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 相关文章推荐
Python的string模块中的Template类字符串模板用法
Jun 27 Python
Python学习小技巧之列表项的排序
May 20 Python
CentOS下使用yum安装python-pip失败的完美解决方法
Aug 16 Python
对pandas中时间窗函数rolling的使用详解
Nov 28 Python
Python设计模式之适配器模式原理与用法详解
Jan 15 Python
Django学习笔记之为Model添加Action
Apr 30 Python
Python自定义函数计算给定日期是该年第几天的方法示例
May 30 Python
Python3内置模块pprint让打印比print更美观详解
Jun 02 Python
Python学习笔记之集合的概念和简单使用示例
Aug 22 Python
python绘制动态曲线教程
Feb 24 Python
python实现横向拼接图片
Mar 23 Python
Python logging模块进行封装实现原理解析
Aug 07 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
php ctype函数中文翻译和示例
2014/03/21 PHP
php实现zip压缩文件解压缩代码分享(简单易懂)
2014/05/10 PHP
php判断当前用户已在别处登录的方法
2015/01/06 PHP
Windows7下的php环境配置教程
2015/02/28 PHP
PHP日期函数date格式化UNIX时间的方法
2015/03/19 PHP
PHP分页初探 一个最简单的PHP分页代码的简单实现
2016/06/21 PHP
php技巧小结【推荐】
2017/01/19 PHP
JavaScript For Beginners(转载)
2007/01/05 Javascript
JQuery 学习笔记 选择器之四
2009/07/23 Javascript
jquery实现心算练习代码
2010/12/06 Javascript
window.location 对象所包含的属性
2014/10/10 Javascript
JS简单实现DIV相对于浏览器固定位置不变的方法
2016/06/17 Javascript
基于Turn.js 实现翻书效果实例解析
2016/06/20 Javascript
js获取指定时间的前几秒
2017/04/05 Javascript
vue表单绑定实现多选框和下拉列表的实例
2017/08/12 Javascript
带你了解session和cookie作用原理区别和用法
2017/08/14 Javascript
JS获取当前地理位置的方法
2017/10/25 Javascript
JavaScript深拷贝和浅拷贝概念与用法实例分析
2018/06/07 Javascript
Angular resolve基础用法详解
2018/10/03 Javascript
jQuery实现数字自动增加或者减少的动画效果示例
2018/12/11 jQuery
Nodejs让异步变成同步的方法
2019/03/02 NodeJs
Python获取当前页面内所有链接的四种方法对比分析
2017/08/19 Python
python中字符串比较使用is、==和cmp()总结
2018/03/18 Python
Windows下Anaconda2安装NLTK教程
2018/09/19 Python
Python实现最常见加密方式详解
2019/07/13 Python
python对Excel的读取的示例代码
2020/02/14 Python
在jupyter notebook 添加 conda 环境的操作详解
2020/04/10 Python
Python实现计算图像RGB均值方式
2020/06/04 Python
TCP协议通讯的过程和步骤是什么
2015/10/18 面试题
中学生自我评价范文
2014/02/08 职场文书
幼儿园小班评语
2014/04/18 职场文书
大学班级学风建设方案
2014/05/01 职场文书
师范生自荐信模板
2014/05/28 职场文书
公司合作协议范文
2014/10/01 职场文书
电工生产实习心得体会
2016/01/22 职场文书
JS实现数组去重的11种方法总结
2022/04/04 Javascript