I have a fixed player in center of the screen and for now I'm moving the background and the elements in the opposite direction to give the illusion of moving forward with the directional keys.
But now, I want to detect the position of the mouse and deplace the player in the direction of the mouse (as in the game agar.io for example) and that the acceleration depends on the distance with the object (the further the mouse is, the faster the player moves forward and if the mouse is on the player,he does not advance anymore)
My program work like this :
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
if y_max > -1000:
y_max -= int(player_1.speed * dt)
bg_y += int(player_1.speed * dt)
if bg_y > 0:
bg_y = -400
if keys[pygame.K_DOWN]:
if y_max < 1000:
y_max += int(player_1.speed * dt)
bg_y -= int(player_1.speed * dt)
if bg_y < -HEIGHT:
bg_y = -400
if keys[pygame.K_LEFT]:
if x_max > -1000:
x_max -= int(player_1.speed * dt)
bg_x += int(player_1.speed * dt)
if bg_x > 0:
bg_x = -400
if keys[pygame.K_RIGHT]:
if x_max < 1000:
x_max += int(player_1.speed * dt)
bg_x -= int(player_1.speed * dt)
if bg_x < -WIDTH:
bg_x = -400
I found that to help but I do not understand how it works :
dX, dY = pygame.mouse.get_pos()
rotation = math.atan2(dY - (float(HEIGHT) / 2), dX - (float(WIDTH) / 2)) * 180 / math.pi
and maybe I have to use sine and cosine but I do not know how ?
Thanks for your help !
Making an object follow the mouse is pretty easy if you know how to use vectors. You just need to create a vector that points to the target (in this case the mouse) by subtracting the target position from the position of the object:
Then move the object by adding this vector to its position vector:
I also scale the
heading
vector here, otherwise it would move to the target immediately. By multiplying it by 0.1 the speed will be a tenth of the distance to the target (the length of the heading vector).Here's a minimal, complete example:
Here's a version without vectors and sprites if you're not familiar with them. It does pretty much the same as the code above.