Using classes

From TRCCompSci - AQA Computer Science
Revision as of 08:19, 13 March 2018 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Sprite Classes

We can create a new class for a player by creating a sub-class of Sprite. We will then inherit all of the functions of the sprite class. We have also created an init method which must be passed the inital_position of the sprite.

class HeroSprite(pygame.sprite.Sprite):
    image = None

    def __init__(self, initial_position):
        pygame.sprite.Sprite.__init__(self) # run the init for the base class
        if HeroSprite.image is None:
            HeroSprite.image = pygame.image.load("hero.png") # load image for sprite
        self.image = HeroSprite.image # set image for the sprite
        self.rect = self.image.get_rect() # set rectangle for sprite
        self.rect.topleft = initial_position # set position to value passed into method
        self.going_down = True # Start going downwards
        self.next_update_time = 0 # update() hasn’t been called yet.

We will also need to create an instance of the class within the setup section of your program (ie not game loop):

pos = [0,0]
h = HeroSprite
h.__init__(h, pos)

We can also create an update method which can be used to update position and so on:

class HeroSprite(pygame.sprite.Sprite):
    image = None

    def update(self, new_position):
        self.rect.topleft = new_position

    def __init__(self, initial_position):
        pygame.sprite.Sprite.__init__(self) # run the init for the base class
        if HeroSprite.image is None:
            HeroSprite.image = pygame.image.load("hero.png") # load image for sprite
        self.image = HeroSprite.image # set image for the sprite
        self.rect = self.image.get_rect() # set rectangle for sprite
        self.rect.topleft = initial_position # set position to value passed into method
        self.going_down = True # Start going downwards
        self.next_update_time = 0 # update() hasn’t been called yet.

We can also create a draw method within the class, you could actually just blit the object to the screen however it would be better to create a specific draw method (you could do other things at the same time):

class HeroSprite(pygame.sprite.Sprite):
    image = None

    def draw(self, screen):
        screen.blit(self.image, self.rect)

    def update(self, new_position):
        self.rect.topleft = new_position

    def __init__(self, initial_position):
        pygame.sprite.Sprite.__init__(self) # run the init for the base class
        if HeroSprite.image is None:
            HeroSprite.image = pygame.image.load("hero.png") # load image for sprite
        self.image = HeroSprite.image # set image for the sprite
        self.rect = self.image.get_rect() # set rectangle for sprite
        self.rect.topleft = initial_position # set position to value passed into method
        self.going_down = True # Start going downwards
        self.next_update_time = 0 # update() hasn’t been called yet.


We can now call the update method in the game loop and then the draw method:

h.update(h, pos)
h.draw(h, SCREEN)

so any changes in pos will move the hero around the screen.