Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取名称错误:未定义名称“IMGS”,虽然我已在类中定义了它_Python_Python 3.x_Nameerror - Fatal编程技术网

Python 获取名称错误:未定义名称“IMGS”,虽然我已在类中定义了它

Python 获取名称错误:未定义名称“IMGS”,虽然我已在类中定义了它,python,python-3.x,nameerror,Python,Python 3.x,Nameerror,在bird类中,定义了变量IMGS,但是如果将其声明为全局变量,则会出现名称错误 **BIRD_IMGS** = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird2.png"))), pygame.transform.scale2x(pygame.

在bird类中,定义了变量IMGS,但是如果将其声明为全局变量,则会出现名称错误

**BIRD_IMGS** = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird2.png"))), pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird3.png")))]

class Bird:
    **IMGS** = BIRD_IMGS
    MAX_ROTATION = 25
    ROT_VEL = 20
    ANIMATION_TIME = 5

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.tilt = 0
        self.tick_count = 0
        self.vel = 0
        self.height = self.y
        self.img_count = 0
        self.img = self.IMGS[0]

    def draw (self, win):
        self.img_count += 1
NameError:未定义名称“IMGS”

    # to make the wing flapping animation
        if self.img_count < self.ANIMATION_TIME:
            self.img = IMGS[0]
        elif self.img_count < self.ANIMATION_TIME*2:
            self.img = IMGS[1]
        elif self.img_count < self.ANIMATION_TIME*3:
            self.img = IMGS[2]
        elif self.img_count < self.ANIMATION_TIME*4:
            self.img = IMGS[1]
        elif self.img_count < self.ANIMATION_TIME*4 + 1:
            self.img = IMGS[0]

在类块顶层定义的主

名称将成为类对象的类属性,由类的所有实例共享-它们不会在全局模块命名空间中公开。注意,您必须通过类或其实例来访问它们,即:

print(Bird.IMGS)
b = Bird()
print(b.IMGS)
print(b.IMGS is Bird.IMGS)
由于Python没有隐式this指针,因此在方法中,必须使用当前实例self来访问该属性:

if self.img_count < self.ANIMATION_TIME:
    self.img = self.IMGS[0]