iolim5678
iolim5678

Reputation: 117

UnboundLocalError when checking lists of sprite collisions in Pygame

I am trying to write a function in pygame that handles collisions in a tile based rpg.

def player_collisions(sprite, group, dir):
    hits_walls = pg.sprite.spritecollide(sprite, group[0], False, collide_hit_rect)
    hits_obj = pg.sprite.spritecollide(sprite, group[1], False, collide_hit_rect)
    inrect = pg.sprite.spritecollide(sprite, group[2], False, collide_hit_rect)
    try:
        if inrect:
            hits_ang = pg.sprite.spritecollide(sprite, group[2], False, pg.sprite.collide_mask)
    except: pass

    if dir == "x":
        if hits_walls:
            sprite.vel.x = 0
            if hits_walls[0].rect.centerx > sprite.hit_rect.centerx:
                sprite.pos.x = hits_walls[0].rect.left - sprite.hit_rect.width / 2
            if hits_walls[0].rect.centerx < sprite.hit_rect.centerx:
                sprite.pos.x = hits_walls[0].rect.right + sprite.hit_rect.width / 2
            sprite.hit_rect.centerx = sprite.pos.x

        if hits_obj:
            sprite.vel.x = 0
            if hits_obj[0].rect.centerx > sprite.hit_rect.centerx:
                sprite.pos.x = hits_obj[0].rect.left - sprite.hit_rect.width / 2
            if hits_obj[0].rect.centerx < sprite.hit_rect.centerx:
                sprite.pos.x = hits_obj[0].rect.right + sprite.hit_rect.width / 2
            sprite.hit_rect.centerx = sprite.pos.x

                
    if dir == "y":
        if hits_walls:
            sprite.vel.y = 0
            if hits_walls[0].rect.centery > sprite.hit_rect.centery:
                sprite.pos.y = hits_walls[0].rect.top - sprite.hit_rect.height / 2
            if hits_walls[0].rect.centery < sprite.hit_rect.centery:
                sprite.pos.y = hits_walls[0].rect.bottom + sprite.hit_rect.height / 2
            sprite.hit_rect.centery = sprite.pos.y

        if hits_obj:
            sprite.vel.y = 0
            if hits_obj[0].rect.centery > sprite.hit_rect.centery:
                sprite.pos.y = hits_obj[0].rect.top - sprite.hit_rect.height / 2
            if hits_obj[0].rect.centery < sprite.hit_rect.centery:
                sprite.pos.y = hits_obj[0].rect.bottom + sprite.hit_rect.height / 2
            sprite.hit_rect.centery = sprite.pos.y

    if hits_ang:
        sprite.slide_allow = True
        if hits_walls or hits_obj:
            sprite.slide_allow = False

hits_walls and hits_obj store walls and objects the player collides with. hits_ang stores pixel perfect collisions with angled walls and only loads when the player is inside the rectangle that surrounds the wall from point to point.

However, I keep having this error flag up everytime my program runs and reaches the if hits_ang: line.

UnboundLocalError: local variable 'hits_ang' referenced before assignment

All I need is for the program to check if the list hits_ang exists at all, so I don't understand why this line is being rejected when the program is fine with checking for the other 3 collision groups.

Upvotes: 1

Views: 36

Answers (1)

Kingsley
Kingsley

Reputation: 14916

The variable hits_ang is not always defined as the execution progresses. It is only defined at the top:

inrect = pg.sprite.spritecollide(sprite, group[2], False, collide_hit_rect)
try:
    if inrect:
        hits_ang = pg.sprite.spritecollide(sprite, group[2], False, pg.sprite.collide_mask)
except: 
    pass

So if inrect is False, None, or zero, hits_ang never exists.

An easy way to fix this is to simply default hits_ang to False:

try:
    hits_ang = False
    if inrect:
        hits_ang = pg.sprite.spritecollide(sprite, group[2], False, pg.sprite.collide_mask)
except: 
    pass

Upvotes: 1

Related Questions