Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
2

How to make sprite follow mouse in pygame

Dunce
Dunce

I would like to make a sprite rotate to face the cursor, in pygame, and I would like it to move in the direction of the cursor.
How would I do that?

2 years ago
You are viewing a single comment. View All
1
Coder100
Coder100

That requires trig.

Step 1.

Calculate angle

import math ang = math.atan2(MOUSEY - PLAYERY, MOUSEX - PLAYERX)

replace those variables with you know what

Step 2.

Rotate the sprite with that angle

Step 3.

Move:

PLAYERX = math.cos(ang) PLAYERY = math.sin(ang)

**Note: ** Don't forget to replace the variables!

Also, if the player is in a fixed position on the screen, replace PLAYERY and PLAYERX with the y and x position of where the player is on the screen actually.

2 years ago
1
Dunce
Dunce

@Coder100
Thanks!

2 years ago