Pygame 鼠标点击和检测

5.Pygame 鼠标点击和检测

快速准确地从用户那里获取输入是任何游戏的重要组成部分。在本 Pygame 教程中,我们将解释和演示如何检测鼠标点击输入以及许多其他与鼠标相关的功能。

模板代码

这是我们将在本教程中使用的基本模板代码。所以我们每次演示新功能的使用时都不再显示这些基本的代码。

import pygame
from pygame.locals import *
import sys
 
pygame.init()
display = pygame.display.set_mode((300, 300))
FPS_CLOCK = pygame.time.Clock()
 
 
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    //...
    //...
    pygame.display.update()
    FPS_CLOCK.tick(30)

在上面的代码中,我们创建了 Pygame 窗口,设置了游戏和事件循环,并为 Quit 事件创建了一个事件检测并设置了一个时钟。

鼠标按下(输入)

这段代码直接进入游戏循环。get_pressed()函数返回三个值,一个对应鼠标上的每个按钮(滚轮在中间)。您可以将其存储到列表中,或使用以下格式将每个值存储在单独的变量中。

left, middle, right = pygame.mouse.get_pressed()
 
if left:
    print("Left Mouse Key is being pressed")

然后,您可以使用这些值来确定按下的是哪个键。

通过列表索引获取各个键值

mouse_presses = pygame.mouse.get_pressed()
 
if mouse_presses[0]:
    print("Left Mouse Key is being pressed")

以上两段代码,只要按住鼠标,就会一直打印。

下面的代码是演示当鼠标按键按下,再对按按键进行处理。这确保它只打印一次消息,无论您按住它多长时间。

if event.type == pygame.MOUSEBUTTONDOWN:
    mouse_presses = pygame.mouse.get_pressed()
    if mouse_presses[0]:
        print("Left Mouse key was clicked")

这是使用的两种技术,一种用于鼠标单击,一种用于鼠标按下。使用那种方法,取决于你的需求。

鼠标位置

Pygame 中一些与鼠标位置相关的函数。

get_pos()函数返回鼠标光标在屏幕上的坐标。请记住,左上角是 (0 , 0)。

print(pygame.mouse.get_pos())

get_rel() 该函数返回 自上次调用此函数以来的 移动 量 。换句话说,它给出了相对运动。xy

print(pygame.mouse.get_rel())

如果您的鼠标光标静止一段时间,它将返回 (0, 0)。如果将它向右移动 5 个像素,在两次get_rel()调用之间,它将返回 (5, 0)。尝试运行此代码并对其进行调整以查看其效果。

另请记住,帧速率将影响返回值。(较低的帧速率会导致get_rel()通话之间的持续时间增加)。这也是我在本教程的代码中实现帧率限制的原因。

其他鼠标功能

这里有一些其他方便的鼠标相关功能。

首先是set_visible()函数,用于更改鼠标的可见性。简而言之,您可以通过将 False 传递给它来使用它来隐藏鼠标。

flag = 1
 
while 1:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                if flag == 1:
                    pygame.mouse.set_visible(False)
                    flag = 0
                elif flag == 0:
                    pygame.mouse.set_visible(True)
                    flag = 1

上面的代码使用了一个切换系统,可以在您按下“A”键时打开和关闭可见性。尝试自己运行它。

下面的代码使用了 Pygame 2.0 中引入的一个非常方便的功能。对于那些在 2021 年之前安装 Pygame 的人,应该更新 pygame。

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_1:
        pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW)
    if event.key == pygame.K_2:
        pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_HAND)

上面的代码改变了出现在屏幕上的光标类型。

与其他精灵/矩形碰撞

这是一种非常方便的检测鼠标是否接触精灵的方法。再次尝试运行代码以获得最佳结果。

class Player:
    def __init__(self):
        self.rect = pygame.draw.rect(display, (255, 0, 0), (100, 100, 100, 100))
 
player = Player()
 
while 1:
    for event in pygame.event.get():

        if event.type == pygame.MOUSEBUTTONDOWN:           
            if player.rect.collidepoint(pygame.mouse.get_pos()):
                print("Mouse clicked on the Player")
  
        if event.type == pygame.MOUSEBUTTONUP:
            if player.rect.collidepoint(pygame.mouse.get_pos()):
                print("Mouse released on the Player")

collidepoint()函数,它采用一组坐标,并确定它们是否与rect碰撞

猜你喜欢

转载自blog.csdn.net/qq_67984864/article/details/128931259