本文实例讲述了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程序设计有所帮助。

本文标题: Python中pygame的mouse鼠标事件用法实例

本文地址: http://www.cppcns.com/jiaoben/python/134762.html

python pygame鼠标点击_Python中pygame的mouse鼠标事件用法实例相关推荐

  1. python字典会自动排序_python中字典不自动排序的用法

    1.python 字典的用法 from collections import OrderedDict dict =OrderedDict() dict['foo']=3 dcit['aol']=1 2 ...

  2. python切片是什么意思_python中的切片是什么?(实例解析)

    在以下的文章之中我们来了解一下什么是python中的切片.了解一下python中的切片是什么意思,以及切片在python之中能起到什么样的作用. 取一个list或tuple的部分元素是在python编 ...

  3. python判断对象是否实例化_python中如何判断class当前有哪些实例?

    展开全部 这个需求可以通过定义类属性来实现.类属性不属62616964757a686964616fe4b893e5b19e31333332633031于对象,可以被这个类的所有对象所共享.可以通过类属 ...

  4. python之pygame安装教程_Python中pygame安装方法图文详解

    搜索热词 本文实例讲述了Python中pygame安装方法.分享给大家供大家参考,具体如下: 这里主要描述一下我们怎样来安装pygame 可能很多人像我一样,发现了pygame是个好东东,但是就是不知 ...

  5. python获取游戏窗口_python中pygame针对游戏窗口的显示方法实例分析(附源码)

    本文实例讲述了python中pygame针对游戏窗口的显示方法.分享给大家供大家参考,具体如下: 在这篇教程中,我将给出一个demo演示: 当我们按下键盘的'f'键的时候,演示的窗口会切换到全屏显示和 ...

  6. python pygame模块_python中pygame模块用法实例

    本文实例讲述了python中pygame模块用法,分享给大家供大家参考.具体方法如下: import pygame, sys from pygame.locals import * #set up p ...

  7. python pygame模块按键延迟_Python之pygame学习键盘事件操作(9)

    pygame键盘事件操作 ✕ 什么是事件呢?按下键盘某个按键,鼠标移动,包括点击关闭按钮都可以算是事件操作. pygame中事件模块是pygame.event,官方说法是用于与事件和队列进行交互. 不 ...

  8. python3.7 的pygame安装方法_Python中pygame安装方法图文详解

    本文实例讲述了Python中pygame安装方法.分享给大家供大家参考,具体如下: 这里主要描述一下我们怎样来安装pygame 可能很多人像我一样,发现了pygame是个好东东,但是就是不知道怎样使用 ...

  9. python selenium鼠标点击_Python+Selenium学习--鼠标事件

    场景 前景讲解了鼠标的click()事件,而我们在实际的web产品测试中,有关鼠标的操作,不仅仅只有单击,有时候还包括右击,双击,拖动等操作,这些操作包含在ActionChains类中. Action ...

最新文章

  1. 一文打尽目标检测NMS | 精度提升篇
  2. 机器学习(Part I)机器学习的种类
  3. hive窗口函数分组排序并取第一个值_Hive(七)Hive分析窗口函数
  4. spring之继承配置
  5. java二进制转十进制代码_二进制转十进制java代码
  6. python 输入正方形_Python打印正方形
  7. JavaMonitor 监视器
  8. 领域应用 | ​英文抗生素药物医学知识图谱 IASO1.0 版发布 线上试用正式启动
  9. 研究科学的科学院,一开始也没那么科学!
  10. MVVMLight绑定数据
  11. 锤子濒危、金立倒闭,华米 OV 们如何艰难求生?
  12. 在 Mac 上的 Pages 文稿中如何设定影片和图像格式?
  13. 计算机实战项目之 [含论文+开题报告+源码等]SSM图书馆预约占座系统[包运行成功]
  14. Python线程安全的单例模式
  15. 云桌面选不对,再好的产品也白用
  16. 都可以看懂的JS轮播图(全代码注释)
  17. Soui Button学习一
  18. iOS10更新_本地通知新框架UserNotifications
  19. 地形建模(一)——TIN地形的生成
  20. 推荐一款绝对不能错过的 ORM 框架 dbVisitor,目前版本 4.3.0

热门文章

  1. C#序列化与反序列化以及深拷贝浅拷贝方法
  2. C语言中内存分布及程序运行中(BSS段、数据段、代码段、堆栈)
  3. iOS15仿微信详情二维码支持保存本地相册
  4. 15-description-Objective-C笔记
  5. vba 将html转换excel,利用VBA将不同格式excel模板之间进行数据转换实例
  6. python实现傅里叶变换求幅值和相位_Python 实现图像快速傅里叶变换和离散余弦变换...
  7. Connect to dl.google.com:443 [dl.google.com/142.250.66.142] failed: Connection timed out:
  8. android阿里滑块验证码,在Android App中接入HTML5滑块验证
  9. 3706 teradata 语句报错_Teradata SQL
  10. iOS 使用 MailCore2