1

Ok, I've created a small game with pygame.draw.rect and so far I have created a player who can move around on the screen and an enemy who doesn't do anything as of right now. I would like the enemy to follow the player around on the screen if I come within a certain distance (say 100 pixels). How would I define this? My player starts at x = 300 and y = 300, the enemy starts at x = 500 and y = 400.

import pygame
import time
import random 
import math


pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
blue = (35,65,155)

gameDisplay = pygame.display.set_mode((1280,800))
pygame.display.set_caption('Practice Game')

#FPS
clock = pygame.time.Clock()
#player block size
block_size = 20

#enemy block size
block_sz = 30

enemy_x = 500
enemy_y = 400

#player health
playerHealth = 100

#Health image
healthImg = pygame.image.load('healthbarimg.png')
#enemy image
enemyImg = pygame.image.load('enemyimg.png')

font = pygame.font.SysFont(None, 25)

def player(block_size, playerList):
    for XnY in playerList:
        pygame.draw.rect(gameDisplay, black, [XnY[0],XnY[1],block_size,block_size])        

def enemy(block_sz):
    #gameDisplay.blit(enemyImg,(900,700))
    pygame.draw.rect(gameDisplay, blue,(enemy_x,enemy_y,block_sz,block_sz))

def playerHp(block_sz):
    gameDisplay.blit(healthImg,(10,10))
    gameDisplay.blit(healthImg,(45,10))
    gameDisplay.blit(healthImg,(80,10))

def message_to_screen(msg,color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [450,350])

def gameLoop():
    gameExit = False
    gameOver = False

    lead_x = 300
    lead_y = 300

    enemy_x = 500
    enemy_y = 400

    enemy_x_change = 0
    enemy_y_change = 0

    lead_x_change = 0
    lead_y_change = 0

    healthList = []
    healthLength = 1

    playerList = []
    playerLength = 1

    randAppleX = round(random.randrange(0, 800-10))#/10.0)*10.0
    randAppleY = round(random.randrange(0, 800-10))#/10.0)*10.0


    while not gameExit:

        while gameOver == True:
            gameDisplay.fill(white)
            message_to_screen("Game over, press C to play again or Q to quit", red)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    gameOver = False
                    gameExit = True

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True  
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    lead_x_change = -10
                if event.key == pygame.K_RIGHT:
                    lead_x_change = 10
                if event.key == pygame.K_UP:
                    lead_y_change = -10
                if event.key == pygame.K_DOWN:
                    lead_y_change = 10   
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    lead_x_change = 0
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    lead_y_change = 0
            if lead_x >= 1280 or lead_x < 0 or lead_y >= 800 or lead_y < 0:
                gameOver = True       

            '''if enemy_x >= 900:
                enemy_x = -10 * enemySpeed                            
            if enemy_x <= 250:
                enemy_x = 10 * enemySpeed
            if enemy_y >= 700:
                enemy_y = -10 * enemySpeed
            if enemy_y <= 50:
                enemy_y = -10 * enemySpeed'''




        pygame.display.update()

        lead_x += lead_x_change
        lead_y += lead_y_change


        gameDisplay.fill(white)

        AppleThickness = 30
        pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, AppleThickness,AppleThickness])


        playerHead = []
        playerHead.append(lead_x)
        playerHead.append(lead_y)
        playerList.append(playerHead)

        if len(playerList) > playerLength:
            del playerList[0]
        player(block_size, playerList)
        enemy(block_sz)
        playerHp(block_sz)         

        pygame.display.update()

##        if lead_x >= randAppleX and lead_x <= randAppleX + AppleThickness:
##            if lead_y >= randAppleY and lead_y <= randAppleY + AppleThickness:
##                randAppleX = round(random.randrange(0, 800-10))#/10.0)*10.0
##                randAppleY = round(random.randrange(0, 800-10))#/10.0)*10.0
##                snakeLength += 1 

        if lead_x > randAppleX and lead_x < randAppleX + AppleThickness or lead_x + block_size > randAppleX and lead_x + block_size < randAppleX + AppleThickness:
            #print("x crossover")
            if lead_y > randAppleY and lead_y < randAppleY + AppleThickness:

                randAppleX = round(random.randrange(0, 800-10))#/10.0)*10.0
                randAppleY = round(random.randrange(0, 800-10))#/10.0)*10.0
                playerLength += 1 

            elif lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:

                randAppleX = round(random.randrange(0, 800-10))#/10.0)*10.0
                randAppleY = round(random.randrange(0, 800-10))#/10.0)*10.0
                playerLength += 1






        clock.tick(10)

    pygame.quit()
    quit()

gameLoop()
  • 2
    Any question for which you need to post your entire program's code isn't a good fit for our format. StackOverflow focuses on small, narrow questions having canonical answers, presented with *the shortest possible code* that can represent them. Here, there are numerous algorithms available; no such thing as a canonical answer exists. – Charles Duffy Jan 23 '19 at 04:07

2 Answers2

2

First you have to solve an issue. The variables enemy_x and enemy_y are declared twice. Onec global and once inside the gameLoop. Keep the global variables, but delete the local variables in the function:

enemy_x = 500
enemy_y = 400

# [...]

def gameLoop():

     global enemy_x, enemy_y

     # [...]

     # enemy_x = 500 <---- delete this
     # enemy_y = 400 <---- delete this

Define a threshold distance between the enemy and the player, which activates and deactivates the following up and define the speed of the enemy:

e.g.

enemySpeed = 5
min_dist = 200

To make the enemy follow player you have to calculate the distance between the enemy and the player:

delta_x = lead_x - enemy_x
delta_y = lead_y - enemy_y

Check if the player is close enough to the enemy. Test whether the threshold is undershot in both directions (x and y). Decide i the enemy steps along the x or y axis:

if abs(delta_x) <= min_dist and abs(delta_y) <= min_dist:
    enemy_move_x = abs(delta_x) > abs(delta_y)

If the enemy is far away enough on both axis (more then 1 step), the the axis for the step can be chosen randomly. Note this is optional:

if abs(delta_x) > enemySpeed and abs(delta_x) > enemySpeed:
    enemy_move_x = random.random() < 0.5

Do a step with the enemy, but limit the step by the distance to the player (the enemy should not step "over" the player):

if enemy_move_x:
    enemy_x += min(delta_x, enemySpeed) if delta_x > 0 else max(delta_x, -enemySpeed)
else:
    enemy_y += min(delta_y, enemySpeed) if delta_y > 0 else max(delta_y, -enemySpeed)

Add the code after lead_x and lead_y have been updated:

enemySpeed = 5
min_dist = 200

while not gameExit:

    while gameOver == True:
        gameDisplay.fill(white)
        # [...]


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True  
        # [...]


    pygame.display.update()

    lead_x += lead_x_change
    lead_y += lead_y_change

    delta_x = lead_x - enemy_x
    delta_y = lead_y - enemy_y

    if abs(delta_x) <= min_dist and abs(delta_y) <= min_dist:
        enemy_move_x = abs(delta_x) > abs(delta_y)
        if abs(delta_x) > enemySpeed and abs(delta_x) > enemySpeed:
           enemy_move_x = random.random() < 0.5
        if enemy_move_x:
           enemy_x += min(delta_x, enemySpeed) if delta_x > 0 else max(delta_x, -enemySpeed)
        else:
           enemy_y += min(delta_y, enemySpeed) if delta_y > 0 else max(delta_y, -enemySpeed)

    gameDisplay.fill(white)

    # [...]

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you Rabbid76 for giving the best response. You have been a tremendous help. – BrandOcOder Jan 25 '19 at 05:41
  • I'm sorry if I'm necroing this answer, but is there a way to achieve smoother diagonal movement with this? Trying the system right now, whenever the enemies move diagonal its more of a zigzag pattern than something smooth, and I cant seem to find a solution anywhere else. – Mercury Platinum May 20 '19 at 14:08
  • @Pygasm Yes it is possible, linke at: [Enemy doesn't follow player (pygame)](https://stackoverflow.com/questions/54734679/enemy-doesnt-follow-player-pygame/54834341#54834341), but as far as i can remember the task of this question was to move step by step. – Rabbid76 May 20 '19 at 14:45
0

You need a pathfinding. The most frequently used pathfinding in games is A* pathfinding. There has been tons of implementations in Python.

However, A* algorithm will find a shortest path to the target (thus, the player) and it may not be a necessity. Dumb moves sometimes give player time to react and knock back, so simply move towards the player with regardless of obstacles would probably be sufficient. The situation depends.

knh190
  • 2,744
  • 1
  • 16
  • 30