How to make an explosion in pgzrun python?

28 Views Asked by At

I'm trying to make an explosion in Python by pgzrun. Note that my files are in the right folders

This is my code:

from pgzrun import *
from pgzhelper import *
from random import *

TITLE = "title"
WIDTH = 600
HEIGHT = 800

level = 1
lives = 3
score = 0

player = Actor("player2", (300, 580))
enemies = []
bls = []
ebls = []
i = 0
exl = []

def draw():
    screen.clear()
    screen.fill("skyblue")
    player.draw()
    for enemy in enemies:
        enemy.draw()
    for bl in bls:
        bl.draw()
    for ebl in ebls:
        ebl.draw()
    for explode in exl:
        explode.draw()
    draw_text()

def update():
    move_bl()
    move_enemy()
    create_ebl()
    move_ebls()
    check()

def remove(list: list, item):
    try:
        list.remove(item)
    except Exception:
        pass

def move_enemy():
    global score
    for enemy in enemies:
        enemy.x += enemy.vx
        if enemy.x > WIDTH or enemy.x < 0:
            enemy.vx = -enemy.vx
            animate(enemy, duration = 0.1, y = enemy.y + 60)
        for bl in bls:
            if bl.colliderect(enemy):
                remove(enemies, enemy)
                remove(bls, bl)
                score += 1
                sounds.explode.play()
        if enemy.colliderect(player):
            die()

def move_bl():
    for bl in bls:
        bl.y -= 6
        if bl.y < 0:
            remove(bl, bls)

def create_ebl():
    if randint(0, 100 - level * 6) == 0:
        enemy = choice(enemies)
        ebl = Actor("enemy_fire", enemy.pos)
        ebls.append(ebl)

def move_ebls():
    global lives
    for ebl in ebls:
        ebl.y += 10
        if ebl.colliderect(player):
            remove(ebls, ebl)
            lives -= 1
            if lives == 0:
                die()
            else:
                sounds.mat_mang.play()

def check():
    global level
    if len(enemies) == 0:
        level += 1
        create_enemies()

def draw_text():
    screen.draw.text("Level: " + str(level), (0, 0), color="red")
    screen.draw.text("Score: " + str(score), (100, 0), color="red")
    screen.draw.text("Lives: " + str(lives), (200, 0), color="red")

def create_enemies():
    for x in range(0, 600, 60):
        for y in range(0, 200, 60):
            enemy = Actor('enemy_plane', (x, y))
            enemy.vx = level * 2
            enemies.append(enemy)
create_enemies()

def die():
    info = {
        "Level": level,
        "Score": score,
    }
    with open("score.txt", "a") as f:
        f.write(str(info) + ",\n")
    sounds.thua.play()
    exit()

def move_player(key):
    if key == keys.LEFT:
        player.x -= 30
    elif key == keys.RIGHT:
        player.x += 30
    elif key == keys.UP:
        player.y -= 30
    elif key == keys.DOWN:
        player.y += 30

def on_key_down(key):
    if key == keys.SPACE:
        bl = Actor("bullet", pos=(player.x, player.y))
        bls.append(bl)
        sounds.ban.play()
    move_player(key)

go()

I have tried to add an Actor to the code but it does not show. I did a lot of research but I found nothing. If the plane is shot, it will explode for around 0.1s and hide. Can you help me?

0

There are 0 best solutions below