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.