在不同文件中使用函数

时间:2017-01-23 05:30:35

标签: python python-3.x pygame

当我运行我的代码时,我得到错误:“NameError:name'arminanceCity'未定义”并且我不确定为什么它没有被定义为我认为我在Loading.py的加载函数中定义。< / p>

mainGame.py:

import pygame
from settings import *
from Loading import *
from characters import *

pygame.init()

class game():

    def __init__(self):

        self.gameRunning = True
        self.gameWindow = pygame.display.set_mode((gameWindowWidth, gameWindowHeight))
        pygame.display.set_caption(title)
        self.clock = pygame.time.Clock()
        self.playerSpeed = speed
        load()

    def update(self):

        self.gameWindow.blit(distantCity, (0,0))
        for layer in gameMap.visible_layers:
            for x, y, gid, in layer:
                tile = gameMap.get_tile_image_by_gid(gid)
                self.gameWindow.blit(tile, (x * gameMap.tilewidth, y * gameMap.tileheight))
        self.gameWindow.blit(ninjaIdle01, (player1.playerX, player1.playerY))
        pygame.display.update()

    def gameLoop(self):

        self.clock.tick(FPS)
        self.event()
        self.update()

    def event(self):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.gameRunning = False

            if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
                self.playerSpeed = 5
            if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
                self.playerSpeed = -5
            if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
                self.playerSpeed = 0
            if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
                self.playerSpeed = 0

        player1.playerX = player1.playerX + self.playerSpeed

playGame = game()
player1 = player()
while playGame.gameRunning == True:
    playGame.gameLoop()


pygame.quit()
quit()

Loading.py

import pygame
import pytmx

pygame.init()

def load():

    ninjaIdle01 = pygame.image.load("gameImages\s_ninjaIdle_01.png")
    ninjaIdle02 = pygame.image.load("gameImages\s_ninjaIdle_02.png")
    ninjaIdle03 = pygame.image.load("gameImages\s_ninjaIdle_03.png")
    ninjaIdle04 = pygame.image.load("gameImages\s_ninjaIdle_04.png")
    ninjaIdle05 = pygame.image.load("gameImages\s_ninjaIdle_05.png")
    ninjaIdle06 = pygame.image.load("gameImages\s_ninjaIdle_06.png")
    ninjaIdle07 = pygame.image.load("gameImages\s_ninjaIdle_07.png")
    ninjaIdle08 = pygame.image.load("gameImages\s_ninjaIdle_08.png")
    ninjaIdle09 = pygame.image.load("gameImages\s_ninjaIdle_09.png")
    ninjaIdle10 = pygame.image.load("gameImages\s_ninjaIdle_10.png")
    ninjaJump01 = pygame.image.load("gameImages\s_ninjaJump_01.png")
    ninjaJump02 = pygame.image.load("gameImages\s_ninjaJump_02.png")
    ninjaJump03 = pygame.image.load("gameImages\s_ninjaJump_03.png")
    ninjaJump04 = pygame.image.load("gameImages\s_ninjaJump_04.png")
    ninjaJump05 = pygame.image.load("gameImages\s_ninjaJump_05.png")
    ninjaJump06 = pygame.image.load("gameImages\s_ninjaJump_06.png")
    ninjaJump07 = pygame.image.load("gameImages\s_ninjaJump_07.png")
    ninjaJump08 = pygame.image.load("gameImages\s_ninjaJump_08.png")
    ninjaJump09 = pygame.image.load("gameImages\s_ninjaJump_09.png")
    ninjaJump10 = pygame.image.load("gameImages\s_ninjaJump_10.png")
    ninjaRun01 = pygame.image.load("gameImages\s_ninjaRun_01.png")
    ninjaRun02 = pygame.image.load("gameImages\s_ninjaRun_02.png")
    ninjaRun03 = pygame.image.load("gameImages\s_ninjaRun_03.png")
    ninjaRun04 = pygame.image.load("gameImages\s_ninjaRun_04.png")
    ninjaRun05 = pygame.image.load("gameImages\s_ninjaRun_05.png")
    ninjaRun06 = pygame.image.load("gameImages\s_ninjaRun_06.png")
    ninjaRun07 = pygame.image.load("gameImages\s_ninjaRun_07.png")
    ninjaRun08 = pygame.image.load("gameImages\s_ninjaRun_08.png")
    ninjaRun09 = pygame.image.load("gameImages\s_ninjaRun_09.png")
    ninjaRun10 = pygame.image.load("gameImages\s_ninjaRun_10.png")
    distantCity = pygame.image.load("gameImages\s_distantCity.png")
    gameMap = pytmx.load_pygame("gameMap.tmx", pixelalpha = True)

1 个答案:

答案 0 :(得分:1)

distantCity中定义Loading.load时,它仍然是一个局部变量。因此,您无法从该函数外部直接访问它。

对此的一个解决方案是将所有数据加载到字典中并从load返回:

def load():
    loaded = {}
    loaded['ninjaIdle01'] = pygame.image.load("gameImages\s_ninjaIdle_01.png")
    loaded['ninjaIdle02'] = pygame.image.load("gameImages\s_ninjaIdle_02.png")
    # ...
    loaded['distantCity'] = pygame.image.load("gameImages\s_distantCity.png")
    loaded['gameMap'] = pytmx.load_pygame("gameMap.tmx", pixelalpha = True)
    return loaded

然后,在调用load

的代码中
def __init__(self):

    self.gameRunning = True
    self.gameWindow = pygame.display.set_mode((gameWindowWidth, gameWindowHeight))
    pygame.display.set_caption(title)
    self.clock = pygame.time.Clock()
    self.playerSpeed = speed
    self.preloaded_data = load()

def update(self):
    self.gameWindow.blit(self.preloaded_data['distantCity'], (0,0))
    # ...

通过这样做,您可以保留对可以通过字典访问的所有预加载数据的引用。