Pygame实现监听鼠标示例详解

作者:我的天才女友 时间:2021-12-16 00:42:58 

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()

Pygame实现监听鼠标示例详解

鼠标移动

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))

Pygame实现监听鼠标示例详解

鼠标点击位置

MOUSEBUTTONDOWN和MOUSEBUTTONUP记录鼠标的按下和放开动作


mouse_down = mouse_up = 0
mouse_down_x = mouse_down_y = 0
mouse_up_x = mouse_up_y = 0

Pygame实现监听鼠标示例详解

输出鼠标位置及其对用的按钮

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))

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()
# 字体
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

标签:Pygame,监听,鼠标
0
投稿

猜你喜欢

  • python实例化对象的具体方法

    2023-11-05 21:52:11
  • 利用Python实现自动生成数据日报

    2022-08-26 11:12:48
  • 两个元祖T1=('a', 'b'),T2=('c', 'd')使用匿名函数将其转变成[{'a': 'c'},{'b': 'd'}]的几种方法

    2023-06-25 12:44:15
  • 一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息

    2023-01-09 19:50:16
  • 使用python从三个角度解决josephus问题的方法

    2022-02-22 02:19:55
  • 使用Alt提升可访问性

    2009-04-04 19:22:00
  • 基于Django框架利用Ajax实现点赞功能实例代码

    2022-01-29 02:35:38
  • Python爬取OPGG上英雄联盟英雄胜率及选取率信息的操作

    2023-12-01 07:06:05
  • Python进程通信之匿名管道实例讲解

    2021-08-26 16:23:46
  • 教你如何在SQL Server计算机列和平均值

    2009-01-20 15:10:00
  • Python叠加两幅栅格图像的实现方法

    2023-07-25 17:14:06
  • Python基于Google Bard实现交互式聊天机器人

    2022-12-14 22:05:20
  • Python分析特征数据类别与预处理方法速学

    2023-04-29 09:55:52
  • Python pandas RFM模型应用实例详解

    2023-10-15 23:27:34
  • 解决pandas展示数据输出时列名不能对齐的问题

    2021-02-12 03:00:23
  • 实例演示在SQL中启用全文检索

    2011-10-01 14:01:37
  • Javascript DOM 编程实例讲解--仿LightBox效果提示框

    2008-05-01 13:25:00
  • 通过Python来使用七牛云存储的方法详解

    2022-09-13 19:56:36
  • 微信跳一跳python自动代码解读1.0

    2022-11-30 05:31:49
  • python tkinter canvas 显示图片的示例

    2022-01-27 12:40:38
  • asp之家 网络编程 m.aspxhome.com