2
\$\begingroup\$

while trying to make my game, I noticed that you can't change the anchor point of sprites in pyglet.
I use some convoluted way (see bottom of the post) to draw my minimap (WIP) so that the rooms are centered and lined-up correctly, however, I thought of the following occurrence.

I'll be using the mouse pointer to direct a 2D person from the top. However, if the anchor point of a sprite is the bottom-left corner of the original image, how can you make it feel as though the anchor point is in the middle of the sprite?

This may not be so important for the player, but it would be hell to algorithmically control the NPC's that fight.

Any help and input appreciated.

Code:

pic_img = image.load(imgPath)
picX, picY = window.width/2,window.height/2
pic = pyglet.sprite.Sprite(pic_img, x=picX, y=picY)
pic.rotation = roomType.ImageRotation

if pic.rotation == 0:
    picX = picX - pic.width / 2
    picY = picY - pic.height / 2
elif pic.rotation == 90:
    picX = picX - pic.width / 2
    picY = picY + pic.height / 2
elif pic.rotation == 180:
    picX = picX + pic.width / 2
    picY = picY + pic.height / 2
    
elif pic.rotation == 270 or pic.rotation == -90:
    picX = picX + pic.width / 2
    picY = picY - pic.height / 2
pic.x,pic.y = picX, picY
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Literally a few seconds after posting this I found the solution(Thought of it while typing actually)

after the loading of the image, but before assigning to the sprite, you change the anchor point for the image, which fixes the issue(if the width of your image is even, it works perfectly)

updated code:

        pic_img = image.load(imgPath)
        pic_img.anchor_x = pic_img.width // 2 ##this line is new
        pic_img.anchor_y = pic_img.height // 2 ## and this line also
        picX, picY = window.width/2,window.height/2
        pic = pyglet.sprite.Sprite(pic_img, x=picX, y=picY)
        pic.rotation = roomType.ImageRotation
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .