【发布时间】:2017-04-21 20:36:26
【问题描述】:
为什么我不能同时将位于game.py、WIDTH, HEIGHT 中的variable 导入player.py 和enemy.py?我只能将它导入到一个文件中。
当我在两个文件上导入它时。
我收到以下错误:
from game import HEIGHT, WIDTH
ImportError: cannot import name HEIGHT
游戏.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
【问题讨论】:
-
为什么需要导入变量?
-
如果它在 main.py 中,那么你应该这样做
from main ... -
@MosesKoledoye 好吧,它叫做game.py,但我在这里写了main。现在更新。
-
敌方是否导入玩家,反之亦然?
-
您遇到了循环导入问题。这不是因为两个文件正在导入同一个文件;这是因为来自某个文件的一系列导入正在返回该文件。