如何将鼠标指针居中使其成为十字线?

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

我在 GIMP 上为游戏制作了一个自定义光标,我希望鼠标位于光标的中心。所以普通箭头指针的尖端位于十字准线的中心。

有什么想法吗?

我已经隐藏了另一个光标并显示了新的光标,我只是想让它居中。

python python-3.x pygame
3个回答
1
投票

为光标创建一个

pygame.Rect
,当
center
事件发生时将其
pygame.MOUSEMOTION
坐标设置为鼠标位置,并将光标图像位块传输到矩形。

import pygame as pg

pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
# pg.mouse.set_visible(False)
BG_COLOR = pg.Color('gray12')

CURSOR_IMG = pg.Surface((40, 40), pg.SRCALPHA)
pg.draw.circle(CURSOR_IMG, pg.Color('white'), (20, 20), 20, 2)
pg.draw.circle(CURSOR_IMG, pg.Color('white'), (20, 20), 2)
# Create a rect which we'll use as the blit position of the cursor.
cursor_rect = CURSOR_IMG.get_rect()

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEMOTION:
            # If the mouse is moved, set the center of the rect
            # to the mouse pos. You can also use pygame.mouse.get_pos()
            # if you're not in the event loop.
            cursor_rect.center = event.pos

    screen.fill(BG_COLOR)
    # Blit the image at the rect's topleft coords.
    screen.blit(CURSOR_IMG, cursor_rect)
    pg.display.flip()
    clock.tick(30)

pg.quit()

0
投票

首先在表面周围画一个矩形,并将矩形的中心放在鼠标的位置上,最后将表面复制到矩形上

elif event.type == pg.MOUSEMOTION:
    cursor_rect = CURSOR_IMG.get_rect(center = event.pos)

这应该有效


0
投票

只需使用

pygame.event.grab(True)
之类的东西,它应该可以工作并将鼠标指针保持在屏幕中间以获得十字准线功能。

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