Hi guys,
I'm trying to make an enemy object follow a player object but I'm having a problem with picking up the player co-ordinates, it gives me an error "Player object has no attribute x"
Here's whats iv'e got:

import pygame
pygame.init()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        
        self.image = pygame.image.load("evin.png")
        self.image = self.image.convert()
        tranColor = self.image.get_at((1, 1))
        self.image.set_colorkey(tranColor)
        self.rect = self.image.get_rect()
        
        self.x = 300
        self.y = 300
        self.rect.center = (self.x, self.y)
        
    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP]:
            self.y -= 5
        if keys[pygame.K_DOWN]:
            self.y += 5
        if keys[pygame.K_LEFT]:
            self.x -= 5
        if keys[pygame.K_RIGHT]:
            self.x += 5
                
        self.rect.center = (self.x, self.y)

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        
        self.image = pygame.image.load("enemy.png")
        self.image = self.image.convert()
        tranColor = self.image.get_at((1, 1))
        self.image.set_colorkey(tranColor)
        self.rect = self.image.get_rect()
        
        self.x = 300
        self.y = 200
        self.rect.center = (self.x, self.y)
        
    def update(self):
        if self.x < Player.x:
            self.x += 1
            self.rect.center = (self.x, self.y)
            
        elif self.x > Player.x:
            self.x -= 1
            self.rect.center = (self.x, self.y)
            
        if self.y < Player.y:
            self.y += 1
            self.rect.center = (self.x, self.y)
            
        elif self.y > Player.y:
            self.y -= 1
            self.rect.center = (self.x, self.y)
                
def main():
    'Create the screen'
    screen = pygame.display.set_mode((640, 480))
    
    'Set the background'
    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 0))
    
    'call the object'
    player = Player()
    enemy = Enemy()
    'add object to group'
    allSprites = pygame.sprite.Group(player)
    enemySprites = pygame.sprite.Group(enemy)
    
    clock = pygame.time.Clock()
    
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        
        ' handle events'
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
        
        allSprites.clear(screen, background)
        enemySprites.clear(screen, background)
        allSprites.update()
        enemySprites.update()
        allSprites.draw(screen)
        enemySprites.draw(screen)
        pygame.display.flip()
    
if __name__ == "__main__":
    main()

I know there is something missing like paramaters or something but I just don't know how to go about it :/

You have to use the player instance and not the class name, also make player global since you create it in main():

import pygame
pygame.init()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        
        self.image = pygame.image.load("evin.png")
        self.image = self.image.convert()
        tranColor = self.image.get_at((1, 1))
        self.image.set_colorkey(tranColor)
        self.rect = self.image.get_rect()
        
        self.x = 300
        self.y = 300
        self.rect.center = (self.x, self.y)
        
    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP]:
            self.y -= 5
        if keys[pygame.K_DOWN]:
            self.y += 5
        if keys[pygame.K_LEFT]:
            self.x -= 5
        if keys[pygame.K_RIGHT]:
            self.x += 5
                
        self.rect.center = (self.x, self.y)

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        
        self.image = pygame.image.load("enemy.png")
        self.image = self.image.convert()
        tranColor = self.image.get_at((1, 1))
        self.image.set_colorkey(tranColor)
        self.rect = self.image.get_rect()
        
        self.x = 300
        self.y = 200
        self.rect.center = (self.x, self.y)
        
    def update(self):
        global player
        if self.x < player.x:
            self.x += 1
            self.rect.center = (self.x, self.y)
            
        elif self.x > player.x:
            self.x -= 1
            self.rect.center = (self.x, self.y)
            
        if self.y < player.y:
            self.y += 1
            self.rect.center = (self.x, self.y)
            
        elif self.y > player.y:
            self.y -= 1
            self.rect.center = (self.x, self.y)
                
def main():
    global player
    'Create the screen'
    screen = pygame.display.set_mode((640, 480))
    
    'Set the background'
    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 0))
    
    'call the object'
    player = Player()
    enemy = Enemy()
    'add object to group'
    allSprites = pygame.sprite.Group(player)
    enemySprites = pygame.sprite.Group(enemy)
    
    clock = pygame.time.Clock()
    
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        
        ' handle events'
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
        
        allSprites.clear(screen, background)
        enemySprites.clear(screen, background)
        allSprites.update()
        enemySprites.update()
        allSprites.draw(screen)
        enemySprites.draw(screen)
        pygame.display.flip()
    
if __name__ == "__main__":
    main()

Nice it works!
Thanks a lot! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.