Pygame实现监听鼠标示例详解
作者:我的天才女友 发布时间:2021-12-16 00:42:58
标签:Pygame,监听,鼠标
pygame如何捕捉鼠标的活动
初始化参数
import pygame, sys
from pygame.locals import *
def print_text(font, x, y, text, color=(0, 0, 0)):
"""打印字体函数"""
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
pygame.init()
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("监听鼠标活动")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
pygame.display.update()
鼠标移动
event.type 事件为MOUSEMOTION,则为鼠标移动,event.pos可以获取当前位置,event.rel鼠标的偏移。
event.type == MOUSEMOTION:
event.pos
event.rel
我们将位置输出出来,定义鼠标的位置和鼠标的偏移量
mouse_x = mouse_y = 0
move_x = move_y = 0
print_text(font1, 0, 0, "鼠标事件")
print_text(font1, 0, 20, "鼠标的位置:" + str(mouse_x) + "," + str(mouse_y))
print_text(font1, 0, 40, "鼠标的偏移:" + str(move_x) + "," + str(move_y))
鼠标点击位置
MOUSEBUTTONDOWN和MOUSEBUTTONUP记录鼠标的按下和放开动作
mouse_down = mouse_up = 0
mouse_down_x = mouse_down_y = 0
mouse_up_x = mouse_up_y = 0
输出鼠标位置及其对用的按钮
pygame.mouse.get_pressed() 可以监听鼠标的三个按键。
x, y = pygame.mouse.get_pos()
print_text(font1, 0, 180, "鼠标位置:" + str(x) + "," + str(y))
b1, b2, b3 = pygame.mouse.get_pressed()
print_text(font1, 0, 200, "按钮:" + str(b1) + "," + str(b2) + "," + str(b3))
完整代码
import pygame, sys
from pygame.locals import *
def print_text(font, x, y, text, color=(0, 0, 0)):
"""打印字体函数"""
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
pygame.init()
# 字体
font1 = pygame.font.SysFont("方正粗黑宋简体", 18)
# 鼠标的移动位置
mouse_x = mouse_y = 0
move_x = move_y = 0
mouse_down = mouse_up = 0
mouse_down_x = mouse_down_y = 0
mouse_up_x = mouse_up_y = 0
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("监听鼠标活动")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEMOTION:
mouse_x, mouse_y = event.pos
move_x, mouse_y = event.rel
elif event.type == MOUSEBUTTONDOWN:
mouse_down = event.button
mouse_down_x, mouse_down_y = event.pos
elif event.type == MOUSEBUTTONUP:
mouse_up = event.button
mouse_up_x, mouse_up_y = event.pos
screen.fill((255, 255, 255))
print_text(font1, 0, 0, "鼠标事件")
print_text(font1, 0, 20, "鼠标的位置:" + str(mouse_x) + "," + str(mouse_y))
print_text(font1, 0, 40, "鼠标的偏移:" + str(move_x) + "," + str(move_y))
print_text(font1, 0, 60, "鼠标按下:" + str(mouse_down)
+ "在" + str(mouse_down_x) + "," + str(mouse_down_y))
print_text(font1, 0, 80, "鼠标松开:" + str(mouse_up)
+ "在" + str(mouse_up_x) + "," + str(mouse_up_y))
x, y = pygame.mouse.get_pos()
print_text(font1, 0, 180, "鼠标位置:" + str(x) + "," + str(y))
b1, b2, b3 = pygame.mouse.get_pressed()
print_text(font1, 0, 200, "按钮:" + str(b1) + "," + str(b2) + "," + str(b3))
pygame.display.update()
来源:https://blog.csdn.net/qq_40801987/article/details/121948923


猜你喜欢
- #coding=utf-8#对话框import sysfrom PyQt4 import QtGui, QtCoreclass Window
- 介绍:pyenv-virtualenv是pyenv的一个插件,作用如同virtualenv一样,是用来管理虚拟环境的,配合pyenv主体使用
- 本文实例讲述了Python中itertools模块用法,分享给大家供大家参考。具体分析如下:一般来说,itertools模块包含创建有效迭代
- 前言PyInstaller可以用来打包python应用程序,打包完的程序就可以在没有安装Python解释器的机器上运行了。PyInstall
- 一段基于OpenCV2的代码。作用是从摄像头获取帧并将帧写入指定的视频文件中。需要注意的是,视频文件所在的路径需要存在,例如D:/image
- 私有变量表示方法在变量前加上两个下划线的是私有变量。class Teacher(): def __init__(self,nam
- 0. 简介上篇博客介绍了goroutine的创建、执行和退出的过程,并且提及了在协程切换时涉及的调度循环,本篇博客我们就来探究一下其他情形引
- 我们先来看一个例子:#encoding=utf-8 # #by panda #桥接模式 def printInfo(info): &nbs
- 那我们什么时候可以用到ibatis呢,我们用hibernate的时候会发现,有的时候hibernate不支持一些特别精确的查询,这个时候我们
- 在并发编程中,我们需要处理多个线程同时对共享资源的访问问题。如果不加控制地同时访问共享资源,就会导致竞争条件(Race Condition)
- 这篇文章主要介绍了Python使用微信接入图灵机器人过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 自动生成api文档(不管是函数视图还是类视图都能显示)1.安装rest_framework_swagger库pip install djan
- 并行查询其优势就是可以通过多个线程来处理查询作业,从而提高查询的效率。SQL Server数据库为具有多个CPU的数据库服务器提供并行查询的
- 1、类变量、实例变量概念类变量:类变量就是定义在类中,但是在函数体之外的变量。通常不使用self.变量名赋值的变量。类变量通常不作为类的实例
- 一、配置文件大小及数量日志文件代码需要引入RotatingFileHandler方法,如下:from logging.handlers im
- 表结构: mysql> desc demo; +-------+------------------+------+-----+---
- '/***' 作 者 :冻结回忆'修改时间:2007-10-17'功能说明
- 本文实例讲述了Python基于二分查找实现求整数平方根的方法。分享给大家供大家参考,具体如下:x=int(raw_input('pl
- 0x00 字符的编码计算机毕竟是西方国家的发明,最开始并没有想到会普及到全世界,只用一个字节中的7位(ASCII)来表示字符对于现在庞大的文
- 1、#coding:utf-8chose = [ ('foo',1,2), ('bar