Is It Possible To Shorten This Code? Pygame-

Viewed 55

I have a bunch of different bullets with different list and I wondering if I could shorten the way of doing this without copying and pasting and making thousands of lines of code.

the code is checking if all those bullets list are collided with the enemy sprite

is there way I could put a list in a list or something so I dont have to write all these code for example I have

bullet1 = []
bullet2 = []
bullet3 = []
# and store all those bullet in another list?
all_bullets = [bullet1,bullet2,bullet3] is that possibale?
def upgrade_tower_1():
    if upgrade1.x == 9000:
        collid_1 = False
        for monster in monsters:
            for shootss in shootsright:
                if monster.rect.colliderect(shootss.rect):
                    collid_1 = False
                    monster.direction = "hit"
                    if monster.health > 2:
                        monster.health -= 0.2
                    else:
                        monster.x = void.x + 20 
                        monster.y = void.y + 150
                        money1.cash += 5


    if upgrade5.x == 9000:
        collid_1 = False
        for monster in monsters:
            for shootss in shootsright:
                if monster.rect.colliderect(shootss.rect):
                    collid_1 = False
                    monster.direction = "hit"
                    if monster.health > 2:
                        monster.health -= 0.3
                    else:
                        monster.x = void.x + 20 
                        monster.y = void.y + 150
                        money1.cash += 5




    if upgrade5.x == 9000:
        collid_1 = False
        for monster in monsters:
            for shootss2 in shootsright2:
                if monster.rect.colliderect(shootss2.rect):
                    collid_1 = False
                    monster.direction = "hit"
                    if monster.health > 2:
                        monster.health -= 0.3
                    else:
                        monster.x = void.x + 20 
                        monster.y = void.y + 150
                        money1.cash += 5


1 Answers

I recommend to write a function (or a method) that tests for collisions between shootsright and monsters:

def detect_collision(monsterlist, shootlist, money):
    collide = False
    for monster in monsterlist:
        for shootss in shootlist:
            if monster.rect.colliderect(shootss.rect):
                collide = False # True ?
                monster.direction = "hit"
                if monster.health > 2:
                    monster.health -= 0.2
                else:
                    monster.x = void.x + 20 
                    monster.y = void.y + 150
                    money.cash += 5
    return collide

Instead of duplicating the code, call the function:

if upgrade1.x == 9000:
    collid_1 = detect_collision(monsters, shootsright, money1)

# [...]

if upgrade5.x == 9000:
    collid_1 = detect_collision(monsters, shootsright, money1)

# [...]

if upgrade5.x == 9000:
    collid_1 = detect_collision(monsters, shootsright2, money1)

# [...]
Related