1

I'm designing a side scrolling computer game for my class, that needs to be handed in soon, however, there is a section of the code that I am struggling with.

I have created an almost Artificial level, where there is a "villain" throwing object at the user, however, I want these objects to be able to follow an angle so that when the sprite moves, a new angle is created to follow, so that the object being thrown essentially "follows" the sprites movement.

This is the code I have so far but I am not sure how to announce the direction the object moves in. I have created the calculation to find the required angle, but do not know how create the line of code needed in order to make the object follow the direction of travel. Not sure if this is relevant, but the object and screen both need to move from right to left.

if player_y < 268: #if the sprite is positioned above the x coordinate where the obstacle is initialised
    opposite = player_y - 268
    angle = (math.atan(opposite/adjacent))#Inverse tan using the opposite and adjacent angles
    o_rect = screen.blit(obstacle,(angle)) #blit obstacle on screen
    obstacle_x -= obstacle_speed #allows the obstacle to constantly move to the left 
    if obstacle_x<-50: #allows net regeneration
        obstacle_x = 400 #obstacle regenerated at a x position 400
                                
if player_y > 268: #if the sprite is positioned below the x coordinate where the obstacle is initialised
    opposite = 268 - player_y
    angle = (math.atan(opposite/adjacent))#Inverse tan using the opposite and adjacent angles
    o_rect = screen.blit(obstacle,(angle)) #blit obstacle on screen
    obstacle_x -= obstacle_speed #allows the obstacle to constantly move to the left 
    if obstacle_x<-50: #allows net regeneration
         obstacle_x = 400 #obstacle regenerated at a x position 400

if player_y == 268: #if the sprite is positioned directly on the x coordinate 
    angle = 0
    o_rect = screen.blit(obstacle,(angle)) #blit obstacle on screen
    obstacle_x -= obstacle_speed #allows the obstacle to constantly move to the left
    if obstacle_x<-50: #allows net regeneration
          obstacle_x = 400 #obstacle regenerated at a x position 400
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Lauren-R
  • 13
  • 5

2 Answers2

0

Usually to make an object move at an angle, you directly manipulate how much it moves on the x and y axes.

To have the obstacle follow the player, the pseudocode should look something like this:

while True:
    # get current relative positions
    displacement_x = player_x - obstacle_x
    displacement_y = player_y - obstable_y

    # update obstacle position to follow player
    obstacle_x += displacement_x
    obstacle_y += displacement_y
    blit()

This may have an unintended behavior that the obstacle will move faster the further away it is from the player. If you want the obstacle to remain at a constant speed and only change the angle:

import math

# modify the value to suit your need
CONSTANT_SPEED = 12

while True:
    # assign displacement_x, displacement_y same as above

    speed = math.sqrt(displacement_x**2 + displacement_y**2)
    displacement_x *= CONSTANT_SPEED/speed
    displacement_y *= CONSTANT_SPEED/speed

    # the rest the same

I hope it helps.

Felix Fourcolor
  • 320
  • 2
  • 8
0

Looks like Felix already gave a reasonable answer, but I have some other suggestions for you to consider

I'm not certain, but it seems like those are 3 redundant if statements and can be consolidated into 1 fairly easily, since:

  • if player_y < 268, player_y - 268 is negative
  • if player_y > 268, 268 - player_y is negative
  • if player_y == 0, player_y - 268 is 0

and since arctan(0) == 0, the following code should be equivalent to what you have. Try to keep the DRY (dont repeat yourself) priciple in mind :)

Additionally, I'd try to assign 268 to a variable with a meaningful name since to anyone else it's just an arbitrary magic number, as is -50

opposite = -abs(player_y - 268)
angle = (math.atan(opposite/adjacent))#Inverse tan using the opposite and adjacent angles
o_rect = screen.blit(obstacle,(angle)) #blit obstacle on screen
obstacle_x -= obstacle_speed #allows the obstacle to constantly move to the left 
if obstacle_x<-50: #allows net regeneration
        obstacle_x = 400 #obstacle regenerated at a x position 400

I might be a little off here, but it's defitely plausible to consolidate these to make things a little more readable and maintanable

Jam
  • 476
  • 3
  • 9