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