game.py
、- player.py
とenemy.py
に同時にvariable
をインポートできないのはなぜですか?私は1つのファイルにしかインポートできません。変数を何度もインポートできないのはなぜですか?
私は両方のファイルにインポートします。
私は次のエラーを取得:[OK]を、今私が見
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
なぜ変数をインポートする必要がありますか? –
それがmain.pyにあれば 'from main ... 'を実行する必要があります。 –
@MosesKoledoyeこれはgame.pyと呼ばれていますが、ここにmainを書きました。今更新しています。 –