pygame监听鼠标

16 篇文章 2 订阅

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()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要利用鼠标开始结束游戏,你可以使用 Pygame 中的 event 循环和鼠标事件来实现。 首先,你需要在 Pygame 中创建一个窗口并显示游戏界面。然后,你可以使用 Pygame 的 event 循环来监听鼠标事件,例如鼠标点击事件。当鼠标点击事件被触发时,你可以根据鼠标点击的位置来判断是开始游戏还是结束游戏。 下面是一个简单的示例代码,可以帮助你开始实现这个功能: ```python import pygame pygame.init() # 创建窗口并显示游戏界面 window = pygame.display.set_mode((640, 480)) pygame.display.set_caption('My Game') # 设置游戏是否结束的标志 game_over = False # 开始游戏循环 while not game_over: for event in pygame.event.get(): # 监听鼠标事件 if event.type == pygame.MOUSEBUTTONDOWN: # 判断鼠标点击的位置 if event.pos[0] < 320 and event.pos[1] < 240: # 进行开始游戏的操作 print('开始游戏') else: # 进行结束游戏的操作 print('结束游戏') # 监听退出事件 elif event.type == pygame.QUIT: # 结束游戏循环 game_over = True # 更新窗口显示 pygame.display.update() # 退出 Pygame pygame.quit() ``` 在这个示例代码中,我们创建了一个大小为 640x480 的窗口,并设置了游戏标题。然后,我们使用一个布尔型变量 `game_over` 来表示游戏是否结束,初始值为 False。在游戏循环中,我们使用 Pygame 的 event 循环来监听鼠标事件和退出事件。当鼠标点击事件被触发时,我们根据鼠标点击的位置来判断是开始游戏还是结束游戏,并进行相应的操作。当退出事件被触发时,我们将 `game_over` 设置为 True,以结束游戏循环。最后,我们使用 `pygame.quit()` 函数退出 Pygame。 请注意,这只是一个简单的示例代码,你需要根据自己的需求来修改和完善代码。同时,你还需要了解 Pygame 中其他的鼠标事件和函数,以便更好地利用鼠标来控制游戏。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的天才女友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值