iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Pygame实现监听鼠标示例详解
  • 278
分享到

Pygame实现监听鼠标示例详解

2024-04-02 19:04:59 278人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录初始化参数鼠标移动鼠标点击位置输出鼠标位置及其对用的按钮完整代码 pygame如何捕捉鼠标的活动 初始化参数 import pygame, sys from pygame.l

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

以上就是Pygame实现监听鼠标示例详解的详细内容,更多关于Pygame监听鼠标的资料请关注编程网其它相关文章!

--结束END--

本文标题: Pygame实现监听鼠标示例详解

本文链接: https://www.lsjlt.com/news/159985.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作