Adam Ashwal
Adam Ashwal

Reputation: 1472

Pygame subclassing issues?

So basically I just wanted to create an enemy class with pygames.sprite.Sprite as the parent. But then I wanted to move this sprite object I created as well as add it to the screen. But it says blit is not a attribute of class "Enemy". Sorry for the newb question but how do I go about doing this?

Class enemy:

class Enemy(pygame.sprite.Sprite):
#Class for falling enemys
def __init__(self):

# initialize the pygame sprite 
    pygame.sprite.Sprite.__init__(self)

# set image and rect
    self.image = pygame.image.load("enemysprite.png").convert()
    self.rect = self.image.get_rect()

What I'm calling to try to ad the sprite to screen

enemy = Enemy()
enemy.blit(100,100)
pygame.display.update()

Upvotes: 0

Views: 476

Answers (2)

ninMonkey
ninMonkey

Reputation: 7501

Sprites can be placed in SpriteGroups, which have nice features, but also blit all in one batch.

To move the enemy, move the Sprite's rect.

Enemy.rect.topleft = (100,100)
#draw
Enemy.rect.center = (400,400)
#draw

Upvotes: 0

Cédric Julien
Cédric Julien

Reputation: 80761

Try this way :

pygame.init()
screen = pygame.display.set_mode([320, 240])
enemy = Enemy()
screen.blit(enemy.image, enemy.rect)
pygame.display.update()

you have to blit the sprite image in the pygame screen to display it.

Upvotes: 2

Related Questions