Collisions

From TRCCompSci - AQA Computer Science
Revision as of 10:57, 14 March 2018 by Admin (talk | contribs) (Created page with "==Rectangular Collision== The sprite class has built in methods to handle collision detection, one of the methods is collide_rect. For this example we are going to create 2 sp...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Rectangular Collision

The sprite class has built in methods to handle collision detection, one of the methods is collide_rect. For this example we are going to create 2 sprite based classes first, one for the enemy and one for the player:

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.

The code above will load the image for the sprite, set its position and its bounds (rect). We can create another for the enemy:

class EnemySprite(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 EnemySprite.image is None:
            HEnemySprite.image = pygame.image.load("Enemy.png") # load image for sprite
        self.image = EnemySprite.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.

Before the game loop you can now create an instance of each class, and run the init method for each:

e = EnemySprite
e.__init__(e, [100,100])

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

Now in the game loop we can make the player move (you can't have collisions if you can't move):

for events in pygame.event.get(): #get all pygame events
        if events.type == pygame.KEYDOWN:
            if events.key == pygame.K_LEFT:
                pos[0]=pos[0]-10 # pos[0] should be first item in the tuple 
            elif events.key == pygame.K_RIGHT:
               pos[0]=pos[0]+10 # pos[0] should be first item in the tuple
            elif events.key == pygame.K_UP:
                pos[1]=pos[1]-10  # pos[1] should be second item in the tuple
            elif events.key == pygame.K_DOWN:
                pos[1]=pos[1]+10 # pos[1] should be second item in the tuple

         h.update(h, pos)