我正在制作某种游戏,但我只设法完成了开始屏幕。现在我正在尝试对第一级进行介绍(所以屏幕上有一些解释和一个继续按钮)。这段程序是写在另一个文件中的,所以我没有得到一个包含 1000 行的文件。
但现在我有一个问题。我通过以下方式开始我的关卡:
import level1 # here is my code of the level
level1.level(screen) # "level" is the class and screen is used so pygame
# uses the same screen and doesn't start a new one
这是level1.py:
class level():
def __init__(self,originalscreen):
screen = originalscreen
self.levelintro()
def levelintro(self):
# Setting some variables
levelintro = True
# Background
background = pygame.Surface(screen.get_size()) # Change this
background = background.convert()
background.fill((255,0,0))
# "Got it" button
gotitbutton = pygbutton.PygButton((350, 450, 300, 40))
while levelintro:
screen.fill(0)
# Blitting
screen.blit(background, (0,0))
gotitbutton.draw(screen)
# Refreshing screen
pygame.display.flip()
所以现在我的问题是:在我原来的 .py 中,我导入了 pygame。但是现在我尝试在 level1.py 中导入 pygame 但它不起作用。我该如何导入它?
谢谢你的帮助!卢卡斯
ps:对不起我的英语不好,我是荷兰人......
编辑:如果我把“import pygame”放在我的班级前面:
Traceback (most recent call last):
File "C:\Users\Lukas\Google Drive\Project\0.1\Project.py", line 17, in <module>
class startscreen():
File "C:\Users\Lukas\Google Drive\Project\0.1\Project.py", line 51, in startscreen
level1.level(screen)
File "C:\Users\Lukas\Google Drive\Project\0.1\level1.py", line 8, in __init__
self.levelintro()
File "C:\Users\Lukas\Google Drive\Project\0.1\level1.py", line 15, in levelintro
background = pygame.Surface(screen.get_size()) # Change this
NameError: global name 'screen' is not defined
我不明白这一点,因为我用 level1.level(screen) 调用了方法“level”,所以它通过了屏幕,然后设置 screen = originalscreen(女巫等于我原始文件中的屏幕)
如果我将“import pygame”放在课堂上,我会得到:
Warning (from warnings module):
File "C:\Users\Lukas\Google Drive\Project\0.1\level1.py", line 3
class level():
SyntaxWarning: import * only allowed at module level
Traceback (most recent call last):
File "C:\Users\Lukas\Google Drive\Project\0.1\Project.py", line 17, in <module>
class startscreen():
File "C:\Users\Lukas\Google Drive\Project\0.1\Project.py", line 51, in startscreen
level1.level(screen)
File "C:\Users\Lukas\Google Drive\Project\0.1\level1.py", line 9, in __init__
self.levelintro()
File "C:\Users\Lukas\Google Drive\Project\0.1\level1.py", line 16, in levelintro
background = pygame.Surface(screen.get_size()) # Change this
NameError: global name 'pygame' is not defined
>>>