使用另一个脚本python 2.7和pygame中的函数和值

时间:2013-09-09 18:56:46

标签: python python-2.7 pygame

所以我正在尝试使用pygame模块学习使用python制作游戏,并且来自javascript背景我希望能够使用多个脚本构建程序。所以我尝试加载另一个脚本并使用它的一个函数以及在该脚本中声明的变量。我想要做的是从另一个脚本调用'update'函数,但使用在这个中声明的变量。有什么建议吗?

编辑: 好的,所以我显然没有很好地澄清我的问题。导入脚本不是我的问题,我可以让它导入正常。我遇到的麻烦是,在导入之后,我需要能够从使用此脚本中的变量的主脚本中调用此脚本中的函数。

现在发生的事情是我在这个脚本中调用了更新函数,它给了我一个错误,说明变量'animTimer'在声明之前被调用。这就是我需要解决的问题。

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)

2 个答案:

答案 0 :(得分:2)

编辑:我觉得你好像在尝试做这样的事情:

>>> a = 4
>>> def inca():
...     a += 1
... 
>>> inca()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in inca
UnboundLocalError: local variable 'a' referenced before assignment

当你应该传递参数时,像这样:

>>> def inc(n):
...     return n + 1
... 
>>> a = 4
>>> a = inc(a)
>>> a
5

Python不喜欢混淆全局命名空间。这是件好事,我保证!确保将变量分配给函数的结果,就像我在a = inc(a)行上所做的那样。否则,你将不得不捣乱全局变量,这不是很好的做法。

在您的情况下,Update()应该采用它希望修改的参数,并返回它们的新值。


您可以import将此模块放入其他脚本中。这将允许您使用语法

访问所有模块级函数,变量等

mymodulename.functionname()

mymodulename.varname

如果要访问模块中某个类的内容,相同的语法仍然有效!

mymodulename.classname.classmember

例如:

####file_a.py####
import file_b
print file_b.message
c = file_b.myclass()
c.helloworld()


####file_b.py####
message = "This is from file b"
class myclass:
    def helloworld(self):
        print "hello, world!"
#################

$ python file_a.py
This is from file b
hello, world!

如果您的模块不在同一目录中,则需要将要导入的目录添加到路径中。但是,我建议暂时将它们保留在同一目录中,因为您似乎在学习Python。

答案 1 :(得分:0)

您可以使用import,但需要在导入之前将脚本的路径添加到PYTHONPATH环境变量或一行代码,如下所示:

sys.path.insert(0, path_to_your_script_here)

然后您可以使用以下方法导入整个模块:

import module name

并使用module.function()引用函数,或者您可以导入特定的函数,只需直接调用函数名称:

from module import function
相关问题