2017-04-21 44 views
0

爲什麼我無法同時導入game.py,​​中的variable - 同時輸入player.pyenemy.py?我只能將它導入到一個文件中。爲什麼我不能多次導入變量?

當我將它導入到這兩個文件。
我得到以下錯誤:

from game import HEIGHT, WIDTH 
ImportError: cannot import name HEIGHT 

Game.py

#!/usr/bin/python 
VERSION = "0.1" 
import os, sys, player, enemy 
from os import path 

try: 
    import pygame 
    from pygame.locals import * 
except ImportError, err: 
    print 'Could not load module %s' % (err) 
    sys.exit(2) 

# main variables 
WIDTH, HEIGHT, FPS, BGIMG = 700, 400, 30, 'FlappyTrollbg.jpg' 

# initialize game 
pygame.init() 
screen = pygame.display.set_mode((WIDTH,HEIGHT)) 
pygame.display.set_caption("FlappyTroll - Python2.7") 

# background 
background = pygame.Surface(screen.get_size()) 
background = background.convert() 
background.fill((255,255,255)) 

img_dir = path.join(path.dirname(__file__), 'img') 
class Background(pygame.sprite.Sprite): 
    def __init__(self, image_file, location): 
     pygame.sprite.Sprite.__init__(self) 
     self.width = WIDTH 
     self.height = HEIGHT 
     self.image = pygame.image.load(path.join(img_dir,image_file)).convert_alpha() 
     self.image = pygame.transform.scale(self.image,(self.width,self.height)) 
     self.rect = self.image.get_rect() 
     #self.rect.left, self.rect.top = location 
     self.rect.x, self.rect.y = location 
     self.speedx = 5 
    def update(self): 
     self.rect.x -= self.speedx 
     if self.rect.x <= -WIDTH: 
      self.rect.x = WIDTH 

# blitting 
screen.blit(background,(0,0)) 
pygame.display.flip() 

# clock for FPS settings 
clock = pygame.time.Clock() 


def main(): 
    all_sprites = pygame.sprite.Group() 
    bgs = pygame.sprite.Group() 
    creature = pygame.sprite.Group() 
    bgs.add(Background(BGIMG, [0,0])) 
    bgs.add(Background(BGIMG, [WIDTH,0])) 
    creature.add(player.FlappyTroll()) 
    for i in range(0,4): 
     all_sprites.add(enemy.TrollEnemy()) 
    # variable for main loop 
    running = True 
    # init umbrella 
    # event loop 
    while running: 
     clock.tick(FPS) 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       running = False 

    # gets_hit = pygame.sprite.spritecollide(umb, all_sprites, True) 
    # if gets_hit: 
    #  newDrop() 
     screen.fill([255, 255, 255]) 
     # update 
     bgs.update() 
     all_sprites.update() 
     creature.update() 
     # draw 
     bgs.draw(screen) 
     all_sprites.draw(screen) 
     creature.draw(screen) 

     # flip the table 
     pygame.display.flip() 
    pygame.quit() 

if __name__ == '__main__': 
    main() 

player.py

import pygame 
from pygame.locals import * 
from os import path 
from random import randint 
from game import HEIGHT, WIDTH 

img_dir = path.join(path.dirname(__file__), 'img') 

class FlappyTroll(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.width = 64 
     self.height = 64 
     self.image = pygame.image.load(path.join(img_dir,"flappytroll.png")).convert_alpha() 
     self.image = pygame.transform.scale(self.image,(self.width,self.height)) 
     self.rect = self.image.get_rect() 
     self.rect.x = self.width*2 
     self.rect.y = HEIGHT/2-self.height 
     self.speedy = 5 

    def update(self): 
     pass 
    # keys = pygame.key.get_pressed() 
    # if(keys[pygame.K_SPACE]): 
    #  self.rect.y -= self.speedy*2 
    # else: 
    #  self.rect.y += self.speedy 

enemy.py

import pygame 
from pygame.locals import * 
from os import path 
from random import randint 
from game import HEIGHT, WIDTH 

img_dir = path.join(path.dirname(__file__), 'img') 

class TrollEnemy(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.width = 64 
     self.height = 64 
     self.image = pygame.image.load(path.join(img_dir,"TrollEnemy.png")).convert_alpha() 
     self.image = pygame.transform.scale(self.image,(self.width,self.height)) 
     self.rect = self.image.get_rect() 
     self.rect.x = WIDTH+self.width 
     self.rect.y = randint(self.height,HEIGHT-self.height) 
     self.speedy = 5 

    def update(self): 
     self.rect.x -= speedy 
+1

爲什麼你需要導入一個變量? –

+2

如果它在main.py中,那麼你應該從'main ...'執行'' –

+0

@MosesKoledoye好吧,它叫做game.py,但我在這裏寫了main。現在更新。 –

回答

1

正如user2357112提到的那樣,您有一個循環導入問題。我假設你想運行game.py.在第三行中,您導入播放器,所以Python解析該文件。當解析器到達player.py的第五行時,它應該導入game.py.有你的循環進口。

爲了解決這個問題,你可以定義在一個單獨的.py你的主要變量(容易)或的.ini(更復雜也更清潔)文件,並從那裏進口。

另一種選擇是隻是沒有進口的主要變量,並將它們傳遞給每一個需要他們,而不是功能。要做到這一點,你可以將它們分組成一本字典,這樣你只有一個對象可以通過。

2

好了,現在我明白了你會回來你有循環進口。您的game.py嘗試導入player.pyenemy.py。當您從game.py,蟒蛇進口game.pycreates references in the current namespace to the given objects所以game.py進口player.py,其中進口game.py導入變量。它被稱爲「循環進口」。

+0

我通過創建一個名爲'config.py'的文件解決了這個問題,並在那裏添加了變量。然後將'config.py'導入到所有文件中。現在正常工作xD –

+1

這是個不錯的選擇=) – Sklert

+0

@IndontReallywolf一個非常明智的方法。 –

相關問題