pygame模块无法正常工作

时间:2019-11-03 02:21:43

标签: python pygame

我不能使用主模块中另一个模块中的功能

#MAIN FILE - mainfile.py

#Imports / display / pygame.init

from file2 import *

font=pygame.font.SysFont("Arial",35)

example(1) #THERE I DEFINE DE CLASS

Text=font.render(list1[0],0,(255,255,255))
win.blit(Text,(5,5))


#while loop

#file 2 - file2.py


class example:
    def __init__(self, whichlist):
        global list1
        if whichlist==1:
            list1=["bird", "bird2", "bird3"]
        elif whichlist==2:
            list1=["bird", "bird2", "bird3"]

        #more code


我知道我可以在file2中定义示例类,但是我想在主文件中定义它。

1 个答案:

答案 0 :(得分:0)

在您的示例中,functionclass更好。并且最好不要使用global。{p>

return

file2.py

def get_list(whichlist) if whichlist == 1: return ["bird", "bird2", "bird3"] elif whichlist == 2: return ["bird", "bird2", "bird3"] #else: # return []

file1.py

如果您确实需要使用类,则应在使用from file2 import get_list list1 = get_list(1)

的类中创建方法

对于类使用return也是一个很好的规则-这意味着CamelCaseNames而不是`example

Example

file2.py

class Example: def get_list(self, whichlist): if whichlist == 1: return ["bird", "bird2", "bird3"] elif whichlist == 2: return ["bird", "bird2", "bird3"] #else: # return []

file1.py

最终

from file2 import Example

ex = Example()
list1 = ex.get_list(1)