0

Relatively new to Pygame. I'm trying to make a rectangle move from a character (xeon) like a bullet, horizontally when pressing the downarrow key. I keep getting this error. "UnboundLocalError: local variable 'bullets' referenced before assignment"

Any feedback is much appreciated. Excuse the mess...........................

import pygame
import time
import random
import sys
import os
pygame.init()
display_width=800
display_height=600
black=(0,0,0)
white=(255,255,255)
red=(200,0,0)
green=(0,200,0)
blue=(0,0,200)
bright_green=(0,255,0)
bright_red=(255,0,0)
gameDisplay=pygame.display.set_mode((800,600))  
pygame.display.set_caption("Xeongame")
clock=pygame.time.Clock()
zheImg=pygame.image.load("xeon.png").convert()
bgImg=pygame.image.load("Spacebackground.jpg").convert()
xeon_width=300







def xeon(x,y):

    gameDisplay.blit(zheImg,(x,y))




def bullets(x,y,bx,by,bw,bh,color):
    while True:
        pygame.draw.rect(gameDisplay,color,[bx,by,bw,bh])

def quitgame():
    pygame.quit()
    quit()

def text_objects(text,font):
    textSurface=font.render(text,True,black)
    return textSurface,textSurface.get_rect()

def obstacles(obstaclex,obstacley,obstaclew,obstacleh,color):
    pygame.draw.rect(gameDisplay,color,[obstaclex,obstacley,obstaclew,obstacleh])




def message_display(text):
    largeText=pygame.font.Font("freesansbold.ttf",115)
    TextSurf,TextRect=text_objects(text,largeText)
    TextRect.center=((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf,TextRect) 
    pygame.display.update()
    time.sleep(1)
    game_loop()

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()

    if x+w>mouse[0]>x and y+h>mouse[1]>y:
        pygame.draw.rect(gameDisplay,ac,(x,y,w,h))
        if click[0]==1 and action !=None:
            action()
            if action=="Play":
                game_loop()
            elif action=="Quit":
                pygame.quit()
                quit()

    else:
        pygame.draw.rect(gameDisplay,ic,(x,y,w,h))  
    smallText=pygame.font.Font("freesansbold.ttf",20)
    textSurf,textRect=text_objects(msg,smallText)
    textRect.center=(   (x+(w/2)),(y+(h/2)) )
    gameDisplay.blit(textSurf,textRect)
    largeText=pygame.font.Font("freesansbold.ttf",115)


def game_intro():
    intro=True

    while intro:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.fill(white)
        largeText=pygame.font.Font("freesansbold.ttf",115)
        TextSurf,TextRect=text_objects("Xeon",largeText)
        TextRect.center=((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf,TextRect)
        xeon(10,100)
        button("Play",150,450,100,50,green,bright_green,game_loop)
        button("Quit",550,450,100,50,red,bright_red,quitgame)




        pygame.display.update()
        clock.tick(15)

def game_loop():



    x=(display_width*.10)
    y=(display_height*0.50)
    x_change=0
    y_change=0
    x+=x_change
    y+=y_change


    ##ALTERNATIVE JUMPING
    #xeon_gravity=0.8
    #xeon_acc=0.5


    obstacle_speed=7
    obstacle_height=100
    obstacle_width=50
    obstacle_startx=random.randrange(300,display_width)
    obstacle_starty=-100

    ##bullets
    bullets_height=10
    bullets_width=50
    bullets_startx=xeon_width
    bullets_starty=xeon_width
    bullets_speed=0.7

    bullets_x_change=0





    ####KEYS



    gameExit=False
    while not gameExit:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_RIGHT:
                    x_change=-5
                if event.key==pygame.K_LEFT:
                    x_change=+5
                if event.key==pygame.K_UP:
                    y_change=-5




                if event.key==pygame.K_DOWN:
                    bullets=True
                    pygame.draw.rect(gameDisplay,[bx,by,bw,bh,color])
                    bullets_x_change=-5                 


            if event.type==pygame.KEYUP:
                if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
                    x_change=0  
        x+=x_change
        y+=y_change

        rel_x=x%bgImg.get_rect().width
        gameDisplay.blit(bgImg,(rel_x-bgImg.get_rect().width,0))

        gameDisplay.blit(bgImg,(rel_x,0))
        xeon(x_change,y)



        bullets(bullets_startx,bullets_starty,bullets_width,bullets_height,blue)
        bullets_starty+=bullets_speed   



        obstacles(obstacle_startx,obstacle_starty,obstacle_width,obstacle_height,red)
        obstacle_starty+=obstacle_speed


        if obstacle_starty>display_height:
            obstacle_starty=0-obstacle_height
            obstacle_startx=random.randrange(300,display_width)





        pygame.display.update()
        clock.tick(60)







game_intro()
game_loop()
pygame.quit()
quit()
Hauk
  • 15
  • 1
  • 6
  • 1
    your `bullets` definition requires 7 parameters, you only pass 5 parameters when you call the function later on – mast3rd3mon Apr 23 '18 at 13:37
  • You also have a variable and a fuction with the name `bullets`. Not sure if that is oké – Gert Kommer Apr 23 '18 at 13:38
  • 3
    Please always post the full traceback and try to reduce your code to a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve). – skrx Apr 23 '18 at 13:43

1 Answers1

1

The problem is that you have a local variable named bullets (in the game_loop function) and a global function with the same name. When you call the function bullets() in game_loop, Python thinks you mean the local variable to which you haven't assigned anything yet, so the UnboundLocalError is raised.

Rename the function or the variable to avoid the UnboundLocalError.

Here's an answer with more information.


You're also not passing enough arguments to the bullets function.

skrx
  • 19,980
  • 5
  • 34
  • 48