Python 2.7 在pyGame中切换游戏级别

Python 2.7 在pyGame中切换游戏级别,python-2.7,pygame,Python 2.7,Pygame,TL;DR-它告诉我全局变量“main”没有定义,有没有办法从另一个文件的另一个类中调用main- 我正在为我的CS课程创建一个小型平台游戏,在作业的下半部分,我们定制了游戏,我想添加一个第二级。当你“点击”门型瓷砖时,我在想我可以像这样重新运行main 这里是主要的,下面是碰撞测试和返回 def main(): if level == 1: p = Platformer('Adventure Time!', 'map1.tmx', 600, 600, 30)

TL;DR-它告诉我全局变量“main”没有定义,有没有办法从另一个文件的另一个类中调用main-

我正在为我的CS课程创建一个小型平台游戏,在作业的下半部分,我们定制了游戏,我想添加一个第二级。当你“点击”门型瓷砖时,我在想我可以像这样重新运行main

这里是主要的,下面是碰撞测试和返回

def main():

    if level == 1:
        p = Platformer('Adventure Time!', 'map1.tmx', 600, 600, 30)
    else:
        p = Platformer('Adventure Time!', 'map2.tmx', 600, 600, 30)
    p.main_loop()

main()
接下来,在检测到与不同对象碰撞的player类中,当我点击“door”类型时,我运行此代码

它告诉我全局变量“main”没有定义,有没有办法从另一个文件中的另一个类中调用main?

试试以下方法:

def main(level): #note i added the level parameter that you have to pass in
    if level == "level1":
        p = Platformer('Adventure Time!', 'map1.tmx', 600, 600, 30)
    else:
        p = Platformer('Adventure Time!', 'map2.tmx', 600, 600, 30)
    p.main_loop()


def handleCollisionWith(self, name, other):
    if other.kind == 'door':
        level = 'level2'
        Class_name.main(level) #you have to call main() with reference to the class it is in
请尝试以下方法:

def main(level): #note i added the level parameter that you have to pass in
    if level == "level1":
        p = Platformer('Adventure Time!', 'map1.tmx', 600, 600, 30)
    else:
        p = Platformer('Adventure Time!', 'map2.tmx', 600, 600, 30)
    p.main_loop()


def handleCollisionWith(self, name, other):
    if other.kind == 'door':
        level = 'level2'
        Class_name.main(level) #you have to call main() with reference to the class it is in
你将得到:
main()
call
main()
哪个调用
main()
等等。因此你将得到
mainloop()
内部
mainloop()
内部
mainloop()
等等。你的游戏结构不正确
handleCollisionWith
应该离开
Platformer
回到第一个
main()
你会得到:
main()
call
main()
哪个调用
main()
等等。所以你会得到
mainloop()
里面的
mainloop()
等。您的游戏结构不正确
handleCollisionWith
应该离开
Platformer
回到第一个
main()