1

I dunno if the title was very specific, but what I meant was that I want a class object to go directly to another place. By directly, I mean you can see it going towards it. I can change the x & y variables of that class object, yet I don't know how much to change it at what time. There should something that should help with that right?

This is what I've gotten with the class so far:

class gems(pygame.sprite.Sprite):
    def __init__(self, which_gem):
        self.gem_x = random.randint(500, 600)
        self.gem_y = random.randint(500, 600)

    def gem_spawn(self):
        which_gem = random.randint(1, 4)
        if which_gem == 1:
            self.gem = pygame.image.load('gem1.png')
        elif which_gem == 2:
            self.gem = pygame.image.load('gem2.png')
        elif which_gem == 3:
            self.gem = pygame.image.load('gem3.png')
        elif which_gem == 4:
            self.gem = pygame.image.load('gem5.png')
        screen.blit(self.gem, (self.gem_x, self.gem_y))
        pygame.display.flip()

Also, just in case you're wondering, I want the class object to move to the mouse position.

Thanks

Duck Duck
  • 159
  • 6
  • 1
    If I understand you correctly (you want to go through intermediate steps between your position and a target, instead of "teleporting" there in one step), you'll need trigonometry. If you form a right triangle that makes up the differences between the two positions, the hypotenuse of the triangle is the direct path between the two points. From there, you figure out how much distance you want to move per "step" (which will depend on the FPS of your game), then do the math to figure out what that translates to in each dimension. – Carcigenicate Dec 07 '20 at 03:50

0 Answers0