0
\$\begingroup\$

I have been using pygame and have been struggling to understand how to implement vectors and the pygame rect class to make an image move diagonally on the screen at the correct speed.I know it must be normalised else it moves at 2x the correct speed.I don't know how to implement vectors with pygame.rect.x and pygame.rect.y

This is the code I have so far

class Player(pygame.sprite.Sprite):

def __init__(self, location, *groups):
    super(Player, self).__init__(*groups)
    self.image = pygame.image.load('player.png')
    self.rect = pygame.rect.Rect(location, self.image.get_size())

    # movement in the X direction; positive is right, negative is left
    self.direction = 1
def update(self, dt, game):

    # take a copy of the current position of the player before movement for
    # use in movement collision response
    last = self.rect.copy()
    clock = pygame.time.Clock()

    # handle the player movement keys
    key = pygame.key.get_pressed()
    if key[pygame.K_LEFT]:
        self.rect.x -= 300 * dt
        self.direction = -1
    if key[pygame.K_RIGHT]:
        self.rect.x += 300 * dt
        self.direction = 1
    if key[pygame.K_UP]:
        self.rect.y -= 300 * dt
    if key[pygame.K_DOWN]:
        self.rect.y += 300 * dt

Thanks

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

A simple solution for your case without implementing vectors by yourself and it's common operations (although I'd recommend to learn about such subject) would be to normalize X and Y when going diagonal:

def update(self, dt, game):

    # take a copy of the current position of the player before movement for
    # use in movement collision response
    last = self.rect.copy()
    clock = pygame.time.Clock()

    # handle the player movement keys
    last = self.rect.copy()
    key = pygame.key.get_pressed()
    x, y = 0, 0
    if key[pygame.K_LEFT]:
        x = -300
        self.direction = -1
    if key[pygame.K_RIGHT]:
        x = +300
        self.direction = 1
    if key[pygame.K_UP]:
        y = -300
    if key[pygame.K_DOWN]:
        y = +300
    if x != 0 and y != 0:
        x = x*(math.sqrt(2)/2)
        y = y*(math.sqrt(2)/2)
    self.rect.x += x * dt
    self.rect.y += y * dt

For a 2d vector implementation this is an excellent class to have as reference: Vector2 libgdx

Hope I could help.

\$\endgroup\$
2
  • \$\begingroup\$ Thank You LordCoppetti It works just had some trouble with dt and then moving it and it disappears.Once I removed dt out out the equation the diagonal movement works properly so now must just figure out the problem with dt \$\endgroup\$ Commented Jul 25, 2015 at 6:45
  • \$\begingroup\$ This might be a late response and maybe you already solved your problem. I'm not sure if your issue with dt is how it gets calculated but just in case it is let me share the answer I followed to calculate it in my loop: current time millis and delta time. As it is mentioned you should use a Clock object to tick and the pygame.time.Clock.get_time function. I pass the dt value as an arg to all my pygame.sprite.Group objects via their update method in every loop iteration. \$\endgroup\$ Commented Jul 26, 2015 at 6:22

You must log in to answer this question.

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