Python Forum
How to make enemy chase me in pygame?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make enemy chase me in pygame?
#1
I've been working on a mini project for a while and I want to make an enemy object follow my player object around the map. I've tried various things like using functions in the while loop, making function that uses both enemy and player classes to deal with if statements etc, but nothing that I come up with is good enough. I'm also a little surprised at how few examples there are of this type of scenario, as I've tried to find some good showcasings for this type of task.

I've removed all code that isn't directly relevant to what I want to do to make it easier to read.

Any help is appreciated. I'm trying to learn pygame but it isn't easy for me...

import pygame,sys


#Color definitions
BLUE = (10,10,128)
RED = (255,0,0)
BLACK = (0,0,0)
#Screen width

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600


pygame.init()




class Player(pygame.sprite.Sprite):
    def __init__(self,x,y):
        super().__init__()
        self.image = pygame.Surface([50,45])
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x
        self.score = 0

        self.change_x = 0
        self.change_y = 0
        self.walls = None

    def changespeed(self,x,y):
        self.change_x += x
        self.change_y += y

    def update(self):
        self.rect.x += self.change_x


        block_hit_list = pygame.sprite.spritecollide(self,self.walls, False)
        for block in block_hit_list:
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                self.rect.left = block.rect.right

        self.rect.y += self.change_y

        block_hit_list = pygame.sprite.spritecollide(self,self.walls, False)
        for block in block_hit_list:

            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom


class Player2(pygame.sprite.Sprite):
    def __init__(self,x,y):
        super().__init__()
        self.image = pygame.Surface([50,45])
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x
        self.score = 0

        self.rect.x = 400
        self.rect.y = 40

        self.change_x = 0
        self.change_y = 0
        self.walls = None

    def changespeed(self,x,y):
        self.change_x += x
        self.change_y += y

    def update(self):
        self.rect.x += self.change_x





class Wall(pygame.sprite.Sprite):
    def __init__(self,x,y,width,height):
        super().__init__()

        self.image = pygame.Surface([width, height])
        self.image.fill(BLUE)

        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x



#Wall init

wall_list = pygame.sprite.Group()

#Pygame definitions

screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption('test!')
all_sprite_list = pygame.sprite.Group()





#Left wall
wall = Wall(0,0,10,600)
wall_list.add(wall)
all_sprite_list.add(wall)
#top wall
wall = Wall(10,0,790,10)
wall_list.add(wall)
all_sprite_list.add(wall)

#Right wall
wall = Wall (790,0,10,600)
wall_list.add(wall)
all_sprite_list.add(wall)

#Bottom wall
wall = Wall (0,590,1000,300)
wall_list.add(wall)
all_sprite_list.add(wall)


#Create the player
player = Player(50,50)
all_sprite_list.add(player)
player.walls = wall_list

player2 = Player2(50,50)
all_sprite_list.add(player2)
player2.walls = wall_list


clock = pygame.time.Clock()

#Loop

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True


        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_d:
                player.changespeed(3,0)
            elif event.key == pygame.K_w:
                player.changespeed(0,-3)
            elif event.key == pygame.K_s:
                player.changespeed(0,3)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                player.changespeed(3,0)
            elif event.key == pygame.K_d:
                player.changespeed(-3,0)
            elif event.key == pygame.K_w:
                player.changespeed(0,3)
            elif event.key == pygame.K_s:
                player.changespeed(0,-3)


    all_sprite_list.update()
    screen.fill(BLACK)
    all_sprite_list.draw(screen)
    pygame.display.flip()
    clock.tick(60)
    pygame.display.flip()





pygame.quit()
Reply


Messages In This Thread
How to make enemy chase me in pygame? - by Jan_97 - Nov-22-2021, 07:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 715 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  [PyGame] platformer enemy animation Yegor123 3 2,064 May-03-2023, 08:42 AM
Last Post: Windspar
  How to make an image move in relation to another in PYGAME CompleteNewb 1 2,335 Nov-10-2021, 03:38 PM
Last Post: metulburr
  cant make a door automatically close a few seconds after i open it in pygame cooImanreebro 2 2,276 Dec-15-2020, 08:40 PM
Last Post: michael1789
  Trying to make boundries for the pygame bluewing101 2 2,067 Mar-23-2020, 01:35 AM
Last Post: Windspar
  [PyGame] pygame, help with making a function to detect collision between player and enemy. Kris1996 3 3,384 Mar-07-2020, 12:32 PM
Last Post: Kris1996
  How can I make music with python/pygame? xBlackHeartx 12 7,283 Oct-15-2019, 08:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020