Error changing global variable inside draw() function

I’m trying to make a space invaders game using Processing Python but for some reason when a barrier is destroyed and I try to repopulate it after the player tries to play again, the entire code just freezes I think. The barriers are stored in a global list called barrierlist and I use global barrierlist to try to tell that I’m trying to modify the global variable.

Below is the actual code written, please excuse the numerous print statements as they were mostly for debugging purposes. I believe that all the global variables are correctly defined but for some reason, I still cannot repopulate the barrierlist in the “elif (gameOver == True and victory == False):” and “elif (gameOver == True and victory == True):” sections.

Main file:

from bullet import bullet
from destroy import enemy
from barrier import barrier

def setup():
    global b, movement, player, barrierImage, photo, enemylist, barrierlist, line1movements,line2movements, gameOver,start, tic,victory
    #movements
    line1movements = ["moving side to side", 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, "moving down", 10, 10, 10, 10, 10, 
                          "moving side to side", -10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10, -10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,
                          -10,-10,-10,-10,-10, "moving down", 10, 10, 10, 10, 10, "moving side to side", 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
    line2movements = ["moving side to side", -10, -10, -10, -10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10, "moving down", 10, 10,10,10,10,"moving side to side",
                     12,12,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14, "moving down", 10, 10, 10, 10, 10,"moving side to side",
                     -20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20]
    #Statuses
    gameOver = False
    start = True
    victory=False
    #Position
    movement = 575
    #bullet
    b = None
    #images
    tic = loadImage("TIC.png")
    player=loadImage("player.png")
    barrierImage = loadImage("barrier.png")
    photo = loadImage("enemy.png")
    #enemy List
    enemylist = [enemy(450, 25, photo, line1movements), enemy(350, 25, photo,line1movements), enemy(250,25,photo,line1movements),enemy(550,25,photo,line1movements),
                 enemy(650,25,photo,line1movements),enemy(750, 25, photo,line1movements), enemy(850, 25, photo,line1movements),enemy(950, 25, photo,line1movements), 
                 enemy(1050,25,photo,line1movements),enemy(150, 25, photo,line1movements), enemy(200, 80, photo, line2movements), enemy(300, 80, photo,line2movements),enemy(400, 80, photo,line2movements),
                 enemy(500, 80, photo,line2movements), enemy(600, 80, photo,line2movements), enemy(700, 80, photo,line2movements), enemy(800, 80, photo,line2movements),
                  enemy(900, 80, photo,line2movements), enemy(1000, 80, photo,line2movements)]
    barrierlist = [barrier(600, 390, barrierImage),barrier(1000,390,barrierImage),barrier(200,390,barrierImage)]
    size(1400,800)

def draw():
    global b, movement, player, barrierImage, photo, enemylist, barrierlist, line1movements,line2movements, gameOver,start, tic,victory

    background(140, 60, 210)

    if start == True:
        textSize(100)
        text("SPACE",200,300)
        text('INVADERS',200,400)
        textSize(50)
        text("press s to start",200,600)
        text('keys are w,a,d',200,500)
        image(tic,700,250,300,300)
        if (keyPressed):
            if key == 's':
                start=False
                
    elif (not gameOver):
        if (keyPressed):
            if key=='a':
                movement -=7
            if key=='d':
                movement+=7
            if key =='w':
                b = bullet(movement,500,13)
        for e in enemylist:
            e.display()
            index = 9999
            for x in range(len(barrierlist)):
                if  enemyintersection(e,barrierlist[x]):
                    index=x
            if index != 9999:
                barrierlist.pop(index)

        for barrier in barrierlist:
            barrier.display()
        
        for e in enemylist:
            if e.y >=450:
                gameOver = True
        
        image(player, movement, 500, 150, 150)
        
        if b is not None:
            b.display()
            for x in range(len(barrierlist)):
                if barrierintersection(b,barrierlist[x]):
                    b = None
                    break
                
        if b is not None:
            index=9999
            for x in range(len(enemylist)):
                if intersection(b,enemylist[x]):
                    index=x
            if index != 9999:
                enemylist.pop(index)
                b = None

        if len(enemylist) == 0:
            victory=True
            gameOver = True
            
    elif (gameOver == True and victory == False):
        global b, movement, player, barrierImage, photo, enemylist, barrierlist, line1movements,line2movements, gameOver,start, tic,victory
        textSize(100)
        fill(255,255,255)
        text('GAME OVER', 200,400)
        text('press s to play again',200,600)
        if (keyPressed):
            if key == 's':
                global b, movement, player, barrierImage, photo, enemylist, barrierlist, line1movements,line2movements, gameOver,start, tic,victory
                print(line1movements)
                print(barrierImage)
                start = False
                gameOver = False
                victory = False
                enemylist = [enemy(450, 25, photo, line1movements), enemy(350, 25, photo,line1movements), enemy(250,25,photo,line1movements),enemy(550,25,photo,line1movements),
                 enemy(650,25,photo,line1movements),enemy(750, 25, photo,line1movements), enemy(850, 25, photo,line1movements),enemy(950, 25, photo,line1movements), 
                 enemy(1050,25,photo,line1movements),enemy(150, 25, photo,line1movements), enemy(200, 80, photo, line2movements), enemy(300, 80, photo,line2movements),enemy(400, 80, photo,line2movements),
                 enemy(500, 80, photo,line2movements), enemy(600, 80, photo,line2movements), enemy(700, 80, photo,line2movements), enemy(800, 80, photo,line2movements),
                  enemy(900, 80, photo,line2movements), enemy(1000, 80, photo,line2movements)]
                movement = 575
                print(len(barrierlist))
                print(barrierlist)
                barrierlist = ["banana"]
                print(barrierlist)
                g = barrier(600, 390, barrierImage)
                print("banana")
                barrierlist = [barrier(600, 390, barrierImage), barrier(1000,390,barrierImage), barrier(200,390,barrierImage)]
    elif (victory == True and gameOver == True):
        textSize(100)
        text("YOU",500,250)
        text("WIN",500,400)
        text('press s to play again', 500, 600)
        if (keyPressed):
            if key == 's':
                global enemylist, barrierlist, movement, start, gameOver, victory, line1movements, line2movements, photo, barrierImage
                start = False
                gameOver = False
                victory = False
                enemylist = [enemy(450, 25, photo, line1movements), enemy(350, 25, photo,line1movements), enemy(250,25,photo,line1movements),enemy(550,25,photo,line1movements),
                 enemy(650,25,photo,line1movements),enemy(750, 25, photo,line1movements), enemy(850, 25, photo,line1movements),enemy(950, 25, photo,line1movements), 
                 enemy(1050,25,photo,line1movements),enemy(150, 25, photo,line1movements), enemy(200, 80, photo, line2movements), enemy(300, 80, photo,line2movements),enemy(400, 80, photo,line2movements),
                 enemy(500, 80, photo,line2movements), enemy(600, 80, photo,line2movements), enemy(700, 80, photo,line2movements), enemy(800, 80, photo,line2movements),
                  enemy(900, 80, photo,line2movements), enemy(1000, 80, photo,line2movements)]
                #barrierlist = [barrier(600, 390, barrierImage),barrier(1000,390,barrierImage),barrier(200,390,barrierImage)]
                movement = 575
        
def intersection(bullet,enemy):
    for enemyx in range(enemy.x-65,enemy.x+50):
        if enemyx == bullet.x:
            for enemyy in range(enemy.y-65,enemy.y+50):
                if enemyy == bullet.y:
                    return True
    return False

def barrierintersection(bullet,br):
    for barrierx in range(br.x-60,br.x+45):
        if barrierx == bullet.x:
            for barriery in range(br.y-60,br.y+60):
                if barriery == bullet.y:
                    return True
    return False

def enemyintersection(enemy, br):
    for barrierx in range(br.x-60,br.x+45):
        if barrierx == enemy.x:
            for barriery in range(br.y-60,br.y+60):
                if barriery == (enemy.y):
                    return True
    return False

def playerintersection(enemy, player):
    for barrierx in range(br.x-60,br.x+45):
        if barrierx == enemy.x:
            for barriery in range(br.y-60,br.y+60):
                if barriery == (enemy.y):
                    return True
    return False

barrier.py:

class barrier:
    def __init__(self,x,y, photo):
        print("fire")
        self.x=x
        self.y=y
        self.photo=photo
        print("barnicle")
    def display(self):
        image(self.photo,self.x,self.y,125,125)

bullet.py:

class bullet:
    def __init__(self, x,y, speed):
        self.x = x
        self.speed = speed
        self.y = y
    def display(self):
       self.y-=self.speed
       fill(255, 0, 0)
       rect(self.x+69,self.y,10,20)

destroy.py:

class enemy:
    def __init__(self,x,y, photo, movement):
        self.x=x
        self.y=y
        self.photo=photo    
        #self.movements = ["moving side to side", 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, "moving down", 10, 10, 10, 10, 10, 
         #                 "moving side to side", -10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10, -10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,-10,
         #                 -10,-10,-10,-10,-10, "moving down", 10, 10, 10, 10, 10, "moving side to side", 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
        self.movements=movement
        self.index = 0
        self.currentframe = 1
        self.changeX = True
    def display(self):
        if self.currentframe % 4 == 0:    
            if self.movements[self.index%len(self.movements)] == "moving down":
                self.changeX = False
                self.index += 1
            if self.movements[self.index%len(self.movements)] == "moving side to side":
                self.changeX= True
                self.index+=1
            if self.changeX == True:
                self.x +=self.movements[self.index%len(self.movements)]
                self.index+=1
            else:
                self.y +=self.movements[self.index%len(self.movements)]
                self.index+=1
        self.currentframe +=1
        image(self.photo,self.x,self.y,100,100)

Hello @bruhnana,

It helps to know which version of Processing Python mode you are using.

With Processing 3.5.4 Python mode on W10 I get this error:

I did not get an error message with Processing 4.2 Python mode on W10.
A lack of an error message does not mean there is not an error.

There are some topics suggesting Processing 4.2 is not ready for prime time and you should use Processing 3.5.4

I did not explore further.

References:

Latest Python modes used in each case above.

:)

1 Like