So I'm trying to learn making games with python using the pygame module and coming from a javascript background I want to be able to build a program using multiple scripts. So I try to load another script and use one of its functions as well as variables declared in that script. What I want to be able to do is call the 'update' funciton from the other script but use the variables declared in this one. Any suggestions?
Edit: Ok, so I clearly haven't clarified my question very well. Importing the script is not my problem, I can get it to import fine. The trouble I have is that after I import it I need to be able to call a function from this script from the main script that uses variables from this script.
What happens right now is that I call the update function in this script an it gives me an error that says that the variable 'animTimer' is being called before it is declared. That's what I need to fix.
import pygame, sys, time, random
from pygame.locals import *
# Create animation class
class animation2D:
"a class for creating animations"
frames = []
speed = 3
cFrame = 0
player = pygame.Rect(300, 100, 40, 40)
playerImg1 = pygame.image.load('player1.png')
playerImgS = pygame.transform.scale(playerImg1, (40,40))
playerImg2 = pygame.image.load('player2.png')
playerImg3 = pygame.image.load('player3.png')
playerImg4 = pygame.image.load('player4.png')
playerImg5 = pygame.image.load('player5.png')
playerImg6 = pygame.image.load('player6.png')
playerAnim = animation2D
playerAnim.frames = [playerImg1, playerImg2, playerImg3, playerImg4, playerImg5, playerImg6]
animTimer = 0
print(animTimer)
def Update():
# Draw Player
if animTimer < playerAnim.speed:
animTimer += 1
else:
animTimer = 0
playerImgS = pygame.transform.scale((playerAnim.frames[playerAnim.cFrame]), (40,40))
if playerAnim.cFrame < len(playerAnim.frames)-1:
playerAnim.cFrame += 1
else:
playerAnim.cFrame = 0
windowSurface.blit(playerImgS, player)
import pygame, sys, time, random
from pygame.locals import *
import animationScript
# Set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# Set up window
screenW = 400
screenH = 400
windowSurface = pygame.display.set_mode((screenW, screenH), 0, 32)
pygame.display.set_caption('Sprites and sound')
# Set up the colors
black = (0,0,200)
# Set up music
pygame.mixer.music.load('testmidi.mid')
#pygame.mixer.music.play(-1,0.0)
# Run the game loop
while True:
# Check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
# Draw the background onto the surface
windowSurface.fill(black)
# Draw Player
animationScript.Update()
# Draw the window onto the screen
pygame.display.update()
mainClock.tick(40)
import myotherscriptandmyotherscript.function(something)etc.