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

时间:2018-02-19 18:05:37

标签: python python-3.x pygame

我在游戏的GIMP上制作了一个自定义光标,我希望鼠标在光标上居中。因此,正常箭头指针的尖端位于十字准线的中心。

有什么想法吗?

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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

你应该先在表面周围画一个矩形,把矩形的中心放在鼠标的位置,最后在矩形上blit表面

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

这应该有效