Unable to make enemy move towards player
Question: I'm struggling to get this working. Basically, I want my enemy sprite to chase
my player sprite. At the moment, it moves away diagonally to the bottom right
edge of the screen.

I'm learning python my making a game and am still new so I apologise if this
is a very simple thing I'm having issues with.

Relevant code:

    
    
    class Enemy(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
    
            self.bitmap = pygame.image.load("ball.bmp")
            self.enemy_rect = self.bitmap.get_rect()
            self.enemy_rect.topleft = [100, 200]
            self.speed = 2
    
        def move_to_player(self, Player):
            dx, dy = self.enemy_rect.x - player.player_rect.x, self.enemy_rect.y - player.player_rect.y
            dist = math.hypot(dx, dy)
            if dist == 0:
                dist = 1
            else:
                dx, dy = dx / dist, dy / dist
    
            self.enemy_rect.x += dx * self.speed
            self.enemy_rect.y += dx * self.speed
    
    while 1:  # main game loop
    enemy.move_to_player(player) 
    

And all code, for context:

    
    
    import pygame
    import sys
    import pygame.locals
    import math
    
    pygame.init() 
    
    size = width, height = 800, 500
    colour = 100, 20, 30  # colour for background
    
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('Test')
    
    class Player(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
    
            self.bitmap = pygame.image.load("ball.bmp")
            self.player_rect = self.bitmap.get_rect(
            self.player_rect.topleft = [100, 200]
    
        def move(self, x, y):
            self.player_rect.centerx += x
            self.player_rect.centery += y
    
        def render(self):
            screen.blit(self.bitmap, (self.shipRect))
    
    class Enemy(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)
    
            self.bitmap = pygame.image.load("ball.bmp")
            self.enemy_rect = self.bitmap.get_rect()
            self.enemy_rect.topleft = [100, 200]
            self.speed = 2
    
        def move_to_player(self, Player):
            dx, dy = self.enemy_rect.x - player.player_rect.x, self.enemy_rect.y - player.player_rect.y
            dist = math.hypot(dx, dy)
            if dist == 0:
                dist = 1
            else:
                dx, dy = dx / dist, dy / dist
    
            self.enemy_rect.x += dx * self.speed
            self.enemy_rect.y += dx * self.speed
    
    player = Player()
    enemy = Enemy()
    
    player.player_rect.x = 50
    player.player_rect.y = 50
    
    enemy.enemy_rect.x = 500
    enemy.enemy_rect.y = 300
    
    while 1:  # main game loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_DOWN:
                    player.move(0, 5)
                if event.key == pygame.K_UP:
                    player.move(0, -5)
                if event.key == pygame.K_LEFT:
                    player.move(-5, 0)
                if event.key == pygame.K_RIGHT:
                    player.move(5, 0)
    
            if player.player_rect.colliderect(enemy.enemy_rect):
                pygame.display.set_caption('Hit!')
    
        screen.fill(colour)
        screen.blit(player.bitmap, player.player_rect)
        screen.blit(enemy.bitmap, enemy.enemy_rect)
        enemy.move_to_player(player)
        pygame.display.flip()
    


Answer: First off, `self.enemy_rect.y += dx * self.speed` probably should be
`self.enemy_rect.y += dy * self.speed`

Secondly, I think you have your math backwards such that your enemy will flee.
Let's try a simple example:

    
    
    Player at 0,0; enemy at 2,0.  
    Expected result if speed = 2: both at 0,0
    dx = 2, dy = 0 (before normalizing by hypot)
    dx = 1, dy = 0 (after)
    New result: player still at 0,0; enemy at 4,0
    

