我想让我的鼠标不可见并停留在屏幕上。当我单击“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()
我不知道如何在屏幕边框中设置鼠标阻挡。
您可以使用
pygame.event.set_grab(True)
函数将光标限制在 pygame 窗口内。要再次释放光标,只需使用 pygame.event.set_grab(False)
。