如何将鼠标锁定在游戏窗口的边框中并在单击 ESKAPE 时将其解锁

问题描述 投票:0回答:1

我想让我的鼠标不可见并停留在屏幕上。当我单击“ESCAPE”时,我想解锁鼠标,使其可见并暂停游戏。然后,当我在窗口中单击鼠标时,我想让鼠标不可见并再次留在屏幕中。

这是我的程序:

import pgzrun
import pygame

WIDTH = 800
HEIGHT = 600

def start():
    global exit, playing
    exit = False
    playing = True
    pygame.mouse.set_visible(False) #set my mouse invisible
    #here I want to write a code witch will block my mouse in the borders

def update():
    global exit, playing
    if keyboard.Escape:
        exit = True
    if exit:
        pygame.mouse.set_visible(True) #set my mouse visible
        #here I want to unblock it
        playing = False
        if mouse.LEFT:
            playing = True
    if playing:
        start()

def draw():
    if exit:
        screen.draw.text("Paused", (200, 200), color=(255,255,255), fontsize=60)
        screen.draw.text('Press in the screen to continue', (200, 250), color=(255,255,255), fontsize =60)

pgzrun.go()

我不知道如何在屏幕边框中设置鼠标阻挡。

python pygame pgzero
1个回答
0
投票

您可以使用

pygame.event.set_grab(True)
函数将光标限制在 pygame 窗口内。要再次释放光标,只需使用
pygame.event.set_grab(False)

© www.soinside.com 2019 - 2024. All rights reserved.