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

 更新时间:2015年11月11日 14:34:20   作者:Hongten  
这篇文章主要介绍了Python中pygame的mouse鼠标事件用法,以完整实例形式详细分析了pygame响应鼠标事件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

展示的效果:

游戏效果:

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

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

#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程序设计有所帮助。

相关文章

  • Pycharm远程调试原理及具体配置详解

    Pycharm远程调试原理及具体配置详解

    这篇文章主要介绍了Pycharm远程调试原理及具体配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • python学习之SpaCy库的高级特性详解

    python学习之SpaCy库的高级特性详解

    在之前的文章中,我们介绍了SpaCy库的一些基本概念和功能,在这篇文章中,我们将深入学习一些更高级的特性,包括词向量、依赖性解析、和自定义组件
    2023-07-07
  • Python2与python3中 for 循环语句基础与实例分析

    Python2与python3中 for 循环语句基础与实例分析

    Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串,也是python中比较常用的一个函数,这里通过基础与实例给大家分享一下
    2017-11-11
  • python使用reportlab实现图片转换成pdf的方法

    python使用reportlab实现图片转换成pdf的方法

    这篇文章主要介绍了python使用reportlab实现图片转换成pdf的方法,涉及Python使用reportlab模块操作图片转换的相关技巧,需要的朋友可以参考下
    2015-05-05
  • python开发中两个list之间传值示例

    python开发中两个list之间传值示例

    这篇文章主要介绍了python开发中两个list列表之间传值的方法,有需要的朋友可以借鉴参考下,希望可以有所帮助,希望大家共同学习,共同进步
    2021-09-09
  • Python实现统计代码行的方法分析

    Python实现统计代码行的方法分析

    这篇文章主要介绍了Python实现统计代码行的方法,结合实例形式分析了Python针对代码行数的计算实现步骤与操作技巧,需要的朋友可以参考下
    2017-07-07
  • Python 逐行分割大txt文件的方法

    Python 逐行分割大txt文件的方法

    本文通过代码给大家介绍了Python 逐行分割大txt文件的方法,在文中给大家提到了Python从txt文件中逐行读取数据的方法,需要的朋友参考下吧
    2017-10-10
  • python用match()函数爬数据方法详解

    python用match()函数爬数据方法详解

    在本篇文章里小编给大家整理了关于python用match()函数爬数据方法以及相关知识点,需要的朋友们学习下。
    2019-07-07
  • python pytest进阶之conftest.py详解

    python pytest进阶之conftest.py详解

    这篇文章主要介绍了python pytest进阶之conftest.py详解,如果我们在编写测试用的时候,每一个测试文件里面的用例都需要先登录后才能完成后面的操作,那么们该如何实现呢?这就需要我们掌握conftest.py文件的使用了,需要的朋友可以参考下
    2019-06-06
  • opencv python简易文档之图像处理算法

    opencv python简易文档之图像处理算法

    OpenCV是一个开源库,包含了许多计算机视觉算法,它在计算机视觉和图像处理中起着重要作用,用于实时操作,其效率足以满足工业上的要求,这篇文章主要给大家介绍了关于opencv python简易文档之图像处理算法的相关资料,需要的朋友可以参考下
    2021-08-08

最新评论