Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

My friend and I are making a game in Pygame for fun. I was trying to clean up the code by putting more things into separate scripts as functions. This is the main script, and the only function is for loading some images.

#set up
import pygame, sys, random, time, math, imageL
from pygame.locals import *
pygame.init()

#variables start----------------------------------
imageL.load_image

x, y = 0, 0 #character position
movex, movey = 0, 0 #how far the character will move
#x is left and right, y is up and down

z, w = random.randint(10, 480), random.randint(10, 500)
movez, movew = 0, 0


#variables end------------------------------------

screen = pygame.display.set_mode((850, 640),0,32) #set screen                       
background = pygame.image.load(niass).convert() #load image to screen


char = pygame.image.load(mil).convert_alpha() #covert player image
ali = pygame.image.load(ali_shit).convert_alpha() #covert alien image


while True:

    for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
    if event.type==KEYDOWN:    
            if event.key==K_a:
                    movex=-1
            elif event.key==K_d:
                    movex=+1
            elif event.key==K_w:
                    movey=-1
            elif event.key==K_s:
                    movey=+1
    if event.type==KEYUP:
            if event.key==K_a:
                    movex=0
            elif event.key==K_d:
                    movex=0
            elif event.key==K_w:
                    movey=0
            elif event.key==K_s:
                    movey=0

    if z < x:
        movez =+ 0.20
    elif z > x:
        movez =- 0.20
    if w < y:
        movew =+ 0.20
    elif w > y:
        movew =- 0.20


    x += movex
    y += movey
    w += movew
    z += movez


    screen.blit(background,(0,0))
    screen.blit(char,(x,y))
    screen.blit(ali,(z,w))

    pygame.display.update()

This is the script with the load image function:

def load_image():
    niass = "grass.png" #grass image
    milf = "head.png" #player image
    ali_shit = "ahead_2.png" #alien image

then this is the error i get when i run this

>>> 

Traceback (most recent call last):
  File "/home/claude/Dropbox/BigKahunaBurger/BigKahunaBurger LOOP.py", line 24, in <module>
    background = pygame.image.load(niass).convert() #load image to screen
NameError: name 'niass' is not defined
>>> 

I've heard of global variables, but I don't know how to use them.

Any help would be greatly appreciated.

share|improve this question
 
I suspect something has to do with the scoping. I think you define niass in the function. But you are calling it outside that function. –  dragons Oct 16 at 9:43
 
is there a way of defining it in the funtion and then calling it outside the function? –  Vincent Vega Oct 16 at 9:52
 
You can put the 3 variables you define in the functions, in a tuple, and then return them. Or return 1 variable only. Other way would be to call the image file directly inside pygame.image.load(niass).convert() –  dragons Oct 16 at 10:01

put on hold as off-topic by Vedran Šego, Brian Reichle, Jamal, Jeff Vanzella, palacsint Oct 16 at 19:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After your code is working you can edit this question for reviewing your working code." – Vedran Šego, Brian Reichle, Jamal, Jeff Vanzella, palacsint
If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.