我在GIMP上为游戏制作了一个自定义光标,我希望将鼠标集中在光标上。因此,普通箭头指针的尖端位于十字路口的中心。
有什么想法?
我已经隐藏了另一个光标并显示了新的光标,我只想将其集中。
为光标创建一个pygame.Rect
,当发生pygame.MOUSEMOTION
事件时,将其center
坐标设置为鼠标位置,并在RECT处爆炸光标图像。
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()
您应该首先在表面周围绘制一个矩形,并将矩形中心放在鼠标的位置,最后闪烁矩形上的表面
elif event.type == pg.MOUSEMOTION:
cursor_rect = CURSOR_IMG.get_rect(center = event.pos)
这应该有效