简体   繁体   中英

How do I set the position of my mouse cursor on the center of an object in pygame?

I used the drawing commands to create a little stick figure at basically 100,100 on a 700,500 screen and I want to move it around with the mouse. The mouse shows up way left and right of the stick figure.

To do this is simple.

For example,

cat_image = "cat.png"
mouse_c = pygame.image.load(mouse_image).convert_alpha()

while True:
 for event in pygame.event.get():
     if event.type == QUIT:
         pygame.quit()
         sys.exit()

 x,y = pygame.mouse.get_pos()
 x -= mouse_c.get_width()/2
 y -= mouse_c.get_height()/2

 screen.blit(mouse_c,(x,y))

 pygame.display.update()

You need to subtract half the height and width to get to the center.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM