Python:无法使用pygame正确导入另一个文件

时间:2018-01-17 15:33:36

标签: python error-handling directory pygame

我在尝试使用pygame正确导入另一个python脚本时遇到问题。

这是我的文件结构:

folder 1
    a1.py
    folder 2
        a2.py
        img1.py
        folder 3
            img1.bmp

a1.py

import os, sys
import pygame

sys.path.insert(0, os.path.join('E:\Raspberry 
Engine\projects\TestGame\Scripts'))

from tiger import *
from ASTRO2 import *

(ScrnWidth, ScrnHeight) = (1080, 720)
screen = pygame.display.set_mode((ScrnWidth, ScrnHeight))

pygame.display.set_caption("Raspberry Engine")

backgroundcolour = (255,255,255)
screen.fill(backgroundcolour)

Running = True
while Running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Running = False
            pygame.quit()

    screen.blit(tiger,(tigerX,TigerY))

    pygame.display.flip()

运行a1.py时,它正在打开img1.py(与a2.py相同),但无论出于何种原因,img1.py仅打开a2.py的图像,不适用于a1.pyimg1.py,稍后会显示错误。)

a2.py

import pygame

from tiger import *
from ASTRO2 import *

(width, height) = (1080, 720)
screen = pygame.display.set_mode((width, height))

pygame.display.set_caption("Test Game")

backgroundcolour = (255,255,255)
screen.fill(backgroundcolour)

Running = True
while Running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Running = False
            pygame.quit()

    screen.blit(tiger,(tigerX,tigerY))
    screen.blit(ASTRO2,(ASTRO2X,ASTRO2Y))

    pygame.display.flip()

img1.py

import pygame
import os, sys

tiger = pygame.image.load(os.path.join('Images','tiger.bmp'))
tigerX = 10
tigerY = 10

img1.py应该打开tiger.bmpimg1.bmp),它可以在a2.py中正常运行,但出于某种原因尝试使用a1.py没有工作并得到以下错误:

Traceback (most recent call last):
  File "F:\Raspberry Engine\Engine.py", line 6, in <module>
    from tiger import *
ModuleNotFoundError: No module named 'tiger'

有人可以帮忙吗?我需要a1.py与来自不同目录的a2.py做同样的事情。

0 个答案:

没有答案