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实现信用卡系统(支持购物、转账、存取钱)
Jun 24 Python
matplotlib设置legend图例代码示例
Dec 19 Python
python列表的增删改查实例代码
Jan 30 Python
Python使用pyh生成HTML文档的方法示例
Mar 10 Python
Django实现登录随机验证码的示例代码
Jun 20 Python
详解Python 数据库的Connection、Cursor两大对象
Jun 25 Python
Python中的元组介绍
Jan 28 Python
用OpenCV将视频分解成单帧图片,图片合成视频示例
Dec 10 Python
pytorch构建多模型实例
Jan 15 Python
解决pycharm debug时界面下方不出现step等按钮及变量值的问题
Jun 09 Python
浅析Python实现DFA算法
Jun 26 Python
pytorch实现加载保存查看checkpoint文件
Jul 15 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
NOT NULL 和NULL
2007/01/15 PHP
PHP 批量删除数据的方法分析
2009/10/30 PHP
探讨fckeditor在Php中的配置详解
2013/06/08 PHP
php实现向javascript传递数组的方法
2015/07/27 PHP
PHP递归创建多级目录
2015/11/05 PHP
使用WordPress发送电子邮件的相关PHP函数用法解析
2015/12/15 PHP
Zend Framework过滤器Zend_Filter用法详解
2016/12/09 PHP
30个最好的jQuery 灯箱插件分享
2011/04/25 Javascript
解析ScrollPic在ie8下只滚动一遍,然后变为空白 ie6,ie7,chrome,firefox正常
2013/06/26 Javascript
js控制淡入淡出示例代码
2013/11/12 Javascript
javascript中的if语句使用介绍
2013/11/20 Javascript
自己动手实现jQuery Callbacks完整功能代码详解
2013/11/25 Javascript
IE中的File域无法清空使用jQuery重设File域
2014/04/24 Javascript
JavaScript中的值类型转换介绍
2014/12/31 Javascript
AngularJS 实现按需异步加载实例代码
2015/10/18 Javascript
JQuery在循环中绑定事件的问题详解
2016/06/02 Javascript
React简单介绍
2017/05/24 Javascript
详解Angular 自定义结构指令
2017/06/21 Javascript
js生成word中图片处理方法
2018/01/06 Javascript
Vue+elementui 实现复杂表头和动态增加列的二维表格功能
2019/09/23 Javascript
在react-antd中弹出层form内容传递给父组件的操作
2020/10/24 Javascript
[03:02]安得倚天剑,跨海斩长鲸——中国军团出征DOTA2国际邀请赛
2018/08/14 DOTA
python通过shutil实现快速文件复制的方法
2015/03/14 Python
Python 稀疏矩阵-sparse 存储和转换
2017/05/27 Python
基于python的图片修复程序(实现水印去除)
2018/06/04 Python
Python WEB应用部署的实现方法
2019/01/02 Python
python单线程文件传输的实例(C/S)
2019/02/13 Python
对pyqt5多线程正确的开启姿势详解
2019/06/14 Python
opencv 图像滤波(均值,方框,高斯,中值)
2020/07/08 Python
.NET面试题:什么是值类型和引用类型
2016/01/12 面试题
幼儿园教师辞职信
2014/01/18 职场文书
2014年售后服务工作总结
2014/11/18 职场文书
离婚案件上诉状
2015/05/23 职场文书
javascript的setTimeout()使用方法总结
2021/11/20 Javascript
Python使用OpenCV实现虚拟缩放效果
2022/02/28 Python
Python first-order-model实现让照片动起来
2022/06/25 Python