Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Projectile crashing
#1
After testing in an external loop, everything worked. However, in my level function, it does not work. Can someone please help me?
import os
import pygame
import sys
pygame.init()

#hitsound = pygame.mixer.Sound('hit.wav')
#shootsound = pygame.mixer.Sound('shoot.wav')
#spellcard1sound = pygame.mixer.Sound('bossattack1.wav')


win = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("pesto anchovy")
class player() :
    x = 500
    y = 560
    height = 30
    width = 20
    currentlevel = 1
    projectiles = []
    hitbox = [y-5, y+5, x-5, x+5]
    hp = 30
    align = 'good'
    actproj = False

    def draw(self, win):
        keys = pygame.key.get_pressed()

        if keys [pygame.K_x] and not self.actproj :
            #shootsound.play()
            self.projectiles.append(projectile(self.x-10, self.y, 2, 0, -20, (239, 229, 93)))
            self.projectiles.append(projectile(self.x, self.y, 2, 0, -20, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y, 2, 0, -20, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, 0, -20, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+30, self.y, 2, 0, -20, (239, 229, 93)))
            self.actproj = True
        if self.projectiles == []:
            self.actproj = False
#projectiles
        if keys [pygame.K_UP] :
            self.y -= 10
        if keys [pygame.K_DOWN] :
            self.y += 10
        if keys [pygame.K_RIGHT] :
            self.x += 10
        if keys [pygame.K_LEFT] :
            self.x -= 10
        self.hitbox = [self.y, self.y+self.height, self.x, self.x+self.width]

        pygame.draw.rect(win, (210, 0, 63), (10, 940, self.hp*10, 20))
        pygame.draw.rect(win, (69, 218, 24), (self.x, self.y, self.width, self.height))
#movement

class projectile():
    def __init__(self, x, y, damage, xmove, ymove, color):
        self.x = x
        self.y = y
        self.damage = damage
        self.xmove = xmove
        self.ymove = ymove
        self.color = color
    def checkcollision(self, owner, collider) :
        if collider.hitbox[0] < self.y and collider.hitbox[1] > self.y and collider.hitbox[2] < self.x and collider.hitbox[3] > self.x and owner.align != collider.align :
            #hitsound.play()
            collider.hp -= self.damage
            owner.projectiles.remove(self)
        if self.x > 1000 or self.x < 0 or self.y > 600 or self.y < 0 :
            owner.projectiles.remove(self)
    def draw(self, win) :
        self.x += self.xmove
        self.y += self.ymove
        pygame.draw.circle(win, self.color, (self.x, self.y), 5)

class bluecircle():
    actproj = False
    def __init__(self, going, x) :
        if going == 'r':
            self.goingright = True
            self.goingleft = False
        else:
            self.goingright = False
            self.goingleft = True
        self.x = x
    projectiles = []
    hp = 5
    y = 10
    align = 'bad'
    hitbox = []
    #hitbox format: up, down, left, right
    def draw(self, win) :
        if self.hp > 0 :
            if not self.actproj:
                self.projectiles.append(projectile(self.x, self.y+10, 1, 0, 10, (218, 69, 143)))
                if self.goingright:
                    self.projectiles.append(projectile(self.x, self.y+10, 1, 10, 10, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, 10, 0, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, 5, 10, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, 10, 5, (218, 69, 143)))
                else:
                    self.projectiles.append(projectile(self.x, self.y+10, 1, -10, 10, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, -10, 0, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, -5, 10, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, -10, 5, (218, 69, 143)))
                self.actproj = True
            elif self.goingright :
                self.x += 5
                if self.x >= 980 :
                    self.goingright = False
                    self.goingleft = True
            elif self.goingleft:
                self.x -= 5
                if self. x <= 20:
                    self.goingleft = False
                    self.goingright = True
            if self.projectiles == []:
                self.actproj = False
            self.hitbox = [self.y-10, self.y+10, self.x-10, self.x+10]
            pygame.draw.circle(win, (69, 188, 218), (self.x, self.y), 10)
#behaviors
class bluerect():
    def __init__(self, x):
        self.x = x-20
    y = 0
    projectiles = []
    actproj = False
    hp = 10
    align =  'bad'
    hitbox = []
    def draw(self, win):
        if not self.actproj:
            self.projectiles.append(projectile(self.x+20, self.y, 2, 5, 0, (218, 69, 143)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, 0, 5, (218, 69, 143)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, -5, 0, (218, 69, 143)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, 5, 5, (218, 69, 143)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, -5, 5, (218, 69, 143)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, 5, 2, (218, 69, 143)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, -5, 0, (218, 69, 143)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, 2, 5, (218, 69, 143)))
            self.projectiles.append(projectile(self.x+20, self.y, 2, -2, 5, (218, 69, 143)))
            self.actproj = True
        if self.projectiles == [] :
            self.actproj = False
        self.hitbox = [self.y, self.y+20, self.x, self.x+40]
        pygame.draw.rect(win, (69, 188, 218), (self.x, self.y, 40, 20))
    
def save (player) :
    if os.path.exists('save.txt') :
        os.remove('save.txt')
    o = open('save.txt', 'w+')
    o.write(str(player.currentlevel))

def load (player) :
    if os.path.exists('save.txt') :
        e = open('save.txt', 'r')
        if e.mode == 'r' :
            level = e.read()
            player.currentlevel = int(level)
    else :
        player.currentlevel = 1

def level(level, player, clock, run):
    for waves in level:
        intro = 50
        while intro > 0 and run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False

            clock.tick(27)
            win.fill((0, 0, 0))
            font = pygame.font.SysFont("timesnewroman", 50, False)
            text = font.render("Level {}: Wave {}".format(player.currentlevel, level.index(waves)+1), 1, (255, 255, 255))
            win.blit(text, (500, 300))
            player.draw(win)
            for projectiles in player.projectiles :
                projectiles.draw(win)
                projectiles.checkcollision(player, player)
            pygame.display.update()
            intro -= 1
        currentenemyhealth = 1
        while currentenemyhealth > 0 and run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False

            clock.tick(27)
            currentenemyhealth = 0
            win.fill((0, 0, 0))
            player.draw(win)
            for enemies in waves:
                currentenemyhealth += enemies.hp
                enemies.draw(win)
                currentenemyhealth -= 1
                for bullets in enemies.projectiles :
                    bullets.draw(win)
                    bullets.checkcollision(enemies, player)
            for projectiles in player.projectiles :
                projectiles.draw(win)
                for enemies in waves:
                    projectiles.checkcollision(player, enemies)
            pygame.display.update()
    player.currentlevel += 1
    save(player)

l1w1 = [bluecircle('r', 50), bluecircle('l', 950), bluerect(500)]
l1w2 = [bluerect(150), bluerect(850), bluecircle('r', 300), bluecircle('l', 700)]
l1w3 = [bluerect(150), bluerect(850), bluecircle('r', 50), bluecircle('r', 300), bluecircle('l', 700), bluecircle('l', 950)]

level1 = [l1w1, l1w2, l1w3]

clock = pygame.time.Clock()
p = player()
run = True

level(level1, p, clock, run)


pygame.quit()

In addition: the healthbar will not load.
Reply
#2
It seems to run fine. What specifically isn't working?
Reply
#3
Originally, it was crashing whenever a bullet touched the edge of the screen and the health bar was not working. I figured out the issues but new ones arose: the enemies shot too much and too fast. Get to level 3 of this new code and see how bad it is.
import os
import pygame
import sys
pygame.init()

hitsound = pygame.mixer.Sound('hit.wav')
shootsound = pygame.mixer.Sound('shoot.wav')
spellcard1sound = pygame.mixer.Sound('bossattack1.wav')


win = pygame.display.set_mode((1000, 600))
pygame.display.set_caption("pesto anchovy")
class player() :
    x = 500
    y = 560
    height = 30
    width = 20
    currentlevel = 1
    projectiles = []
    hitbox = [y-5, y+5, x-5, x+5]
    hp = 30
    align = 'good'
    actproj = False
    lives = 3

    def draw(self, win):
        keys = pygame.key.get_pressed()
        if self.hp > 0 :
            if keys [pygame.K_x] and not self.actproj :
                #shootsound.play()
                self.projectiles.append(projectile(self.x-10, self.y, 2, 0, -20, (239, 229, 93)))
                self.projectiles.append(projectile(self.x, self.y, 2, 0, -20, (239, 229, 93)))
                self.projectiles.append(projectile(self.x+10, self.y, 2, 0, -20, (239, 229, 93)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, 0, -20, (239, 229, 93)))
                self.projectiles.append(projectile(self.x+30, self.y, 2, 0, -20, (239, 229, 93)))
                self.actproj = True
            if self.projectiles == []:
                self.actproj = False
    #projectiles
            if keys [pygame.K_UP] :
                self.y -= 10
            if keys [pygame.K_DOWN] :
                self.y += 10
            if keys [pygame.K_RIGHT] :
                self.x += 10
            if keys [pygame.K_LEFT] :
                self.x -= 10
            self.hitbox = [self.y, self.y+self.height, self.x, self.x+self.width]

            pygame.draw.rect(win, (210, 0, 63), (10, 540, self.hp*10, 20))
            pygame.draw.rect(win, (69, 218, 24), (self.x, self.y, self.width, self.height))
    #existance
        elif self.lives > 0 :
            self.projectiles.append(projectile(self.x+10, self.y+15, 5, 0, 2, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y+15, 5, 2, 0, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y+15, 5, 0, -2, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y+15, 5, -2, 0, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y+15, 5, 1, 1, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y+15, 5, 1, -1, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y+15, 5, -1, 1, (239, 229, 93)))
            self.projectiles.append(projectile(self.x+10, self.y+15, 5, -1, -1, (239, 229, 93)))
            self.lives -= 1
            self.hp = 30

class projectile():
    def __init__(self, x, y, damage, xmove, ymove, color):
        self.x = x
        self.y = y
        self.damage = damage
        self.xmove = xmove
        self.ymove = ymove
        self.color = color
    def checkcollision(self, owner, collider) :
        if collider.hitbox[0] < self.y and collider.hitbox[1] > self.y and collider.hitbox[2] < self.x and collider.hitbox[3] > self.x and owner.align != collider.align :
            #hitsound.play()
            collider.hp -= self.damage
            if collider.hp < 0:
                collider.hp == 0
            owner.projectiles.remove(self)
        elif self.x > 1000 or self.x < 0 or self.y > 600 or self.y < 0 :
            owner.projectiles.remove(self)
    def draw(self, win) :
        self.x += self.xmove
        self.y += self.ymove
        pygame.draw.circle(win, self.color, (self.x, self.y), 5)

class bluecircle():
    actproj = False
    def __init__(self, going, x) :
        if going == 'r':
            self.goingright = True
            self.goingleft = False
        else:
            self.goingright = False
            self.goingleft = True
        self.x = x
    projectiles = []
    hp = 5
    y = 10
    align = 'bad'
    hitbox = []
    #hitbox format: up, down, left, right
    def draw(self, win, wave, player) :
        if self.hp > 0 :
            if not self.actproj:
                self.projectiles.append(projectile(self.x, self.y+10, 1, 0, 10, (218, 69, 143)))
                if self.goingright:
                    self.projectiles.append(projectile(self.x, self.y+10, 1, 10, 10, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, 10, 0, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, 5, 10, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, 10, 5, (218, 69, 143)))
                else:
                    self.projectiles.append(projectile(self.x, self.y+10, 1, -10, 10, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, -10, 0, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, -5, 10, (218, 69, 143)))
                    self.projectiles.append(projectile(self.x, self.y+10, 1, -10, 5, (218, 69, 143)))
                self.actproj = True
            elif self.goingright :
                self.x += 5
                if self.x >= 980 :
                    self.goingright = False
                    self.goingleft = True
            elif self.goingleft:
                self.x -= 5
                if self. x <= 20:
                    self.goingleft = False
                    self.goingright = True
            if self.projectiles == []:
                self.actproj = False
            self.hitbox = [self.y-10, self.y+10, self.x-10, self.x+10]
            pygame.draw.circle(win, (69, 188, 218), (self.x, self.y), 10)
            for bullet in self.projectiles :
                bullet.draw(win)
                bullet.checkcollision(self, player)
        else:
            wave.remove(self)
#behaviors
class bluerect():
    def __init__(self, x):
        self.x = x-20
    y = 0
    projectiles = []
    actproj = False
    hp = 10
    align =  'bad'
    hitbox = []
    def draw(self, win, wave, player):
        if self.hp > 0 :
            if not self.actproj:
                self.projectiles.append(projectile(self.x+20, self.y, 2, 5, 0, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, 0, 5, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, -5, 0, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, 5, 5, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, -5, 5, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, 5, 2, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, -5, 0, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, 2, 5, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, -2, 5, (218, 69, 143)))
                self.projectiles.append(projectile(self.x+20, self.y, 2, -5, 2, (218, 69, 143)))
                self.actproj = True
            if self.projectiles == [] :
                self.actproj = False
            self.hitbox = [self.y, self.y+30, self.x, self.x+40]
            pygame.draw.rect(win, (69, 188, 218), (self.x, self.y, 40, 20))
            for bullet in self.projectiles :
                bullet.draw(win)
                bullet.checkcollision(self, player)
        else :
            wave.remove(self)
    
def save (player) :
    if os.path.exists('save.txt') :
        os.remove('save.txt')
    o = open('save.txt', 'w+')
    o.write(str(player.currentlevel))

def load (player) :
    if os.path.exists('save.txt') :
        e = open('save.txt', 'r')
        if e.mode == 'r' :
            level = e.read()
            player.currentlevel = int(level)
    else :
        player.currentlevel = 1

def level(level, player, clock, run):
    for waves in level:
        intro = 50
        while intro > 0 and run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False

            clock.tick(27)
            win.fill((0, 0, 0))
            font = pygame.font.SysFont("timesnewroman", 50, False)
            text = font.render("Level {}: Wave {}".format(player.currentlevel, level.index(waves)+1), 1, (255, 255, 255))
            win.blit(text, (500, 300))
            player.draw(win)
            for projectiles in player.projectiles :
                projectiles.draw(win)
                projectiles.checkcollision(player, player)
            pygame.display.update()
            intro -= 1
        currentenemyhealth = 1
        while currentenemyhealth > 0 and run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False

            clock.tick(27)
            currentenemyhealth = 0
            win.fill((0, 0, 0))
            player.draw(win)
            for enemy in waves:
                currentenemyhealth += enemy.hp
                enemy.draw(win, waves, player)
            for bullet in player.projectiles :
                bullet.draw(win)
                for enemies in waves:
                    if bullet in player.projectiles:
                        bullet.checkcollision(player, enemies)
            pygame.display.update()
    player.currentlevel += 1
    save(player)

l1w1 = [bluecircle('r', 50), bluecircle('l', 950), bluerect(500)]
l1w2 = [bluerect(150), bluerect(850), bluecircle('r', 300), bluecircle('l', 700)]
l1w3 = [bluerect(150), bluerect(850), bluecircle('r', 50), bluecircle('r', 300), bluecircle('l', 700), bluecircle('l', 950)]

level1 = [l1w1, l1w2, l1w3]

clock = pygame.time.Clock()
p = player()
run = True

level(level1, p, clock, run)


pygame.quit()
Would it be violating forum rules to keep this thread alive to solve these issues, or would it need to be a new thread.
Am bean
[Image: am-bean.png]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020