Python AttributeError:“模块”没有属性“ currentanimation”

Aearnus

我在其中有一些代码main.py

import pygame, char
def move(char):
    #movement code
    #friction
    if char.xaccel == 0:
        if abs(char.xvel) < char.xvelfalloff: char.xvel = 0
        elif abs(char.xvel) == char.xvel: char.xvel -= char.xvelfalloff
        elif abs(char.xvel) != char.xvel: char.xvel += char.xvelfalloff
    #acceleration
    char.xvel += char.xaccel
    #max speed
    if abs(char.xvel) > char.xmaxvel: char.xvel = math.copysign(char.xmaxvel,char.xvel)
    #position/collision detection
    char.x += char.xvel
    char.xaccel = 0
    char.yaccel = char.ygravity
    char.yvel += char.yaccel
    char.y += char.yvel
    if char.y < 0: #TODO: more collision detection
        char.yvel = 0
        char.y = 0
        char.onground = True
    if char.jumping:
        char.yvel = char.jumpstrength
        char.jumping = False
        char.onground = False
    #no more movement code
charObj = char.Char()
charObj = move(charObj)
screen.blit(char.currentanimation, pygame.Rect(char.x, (screen_height-char.y)-char.height, char.width, char.height))
pygame.display.flip()

和一些代码char.py

import pygame
class Char(): 
    spritesheet = pygame.image.load("images/spritesheet.png")
        walkanim = []
        for i in range(7):
            spritesheet.set_clip(pygame.Rect(((sprite_width-7)*i)+3,12,sprite_width,sprite_height))
            spritesheetsubsurface = spritesheet.subsurface(spritesheet.get_clip())
            walkanim.append(pygame.transform.scale(spritesheetsubsurface, (width, height)))

        spritesheet.set_clip(pygame.Rect(0,577,sprite_width,sprite_height))
        spritesheetsubsurface = spritesheet.subsurface(spritesheet.get_clip())
        idleanim = pygame.transform.scale(spritesheetsubsurface, (width, height))
        lastanim = "right"

        currentanimation = idleanim
        animationframeindex = 0
        animationframepause = 6 #how many frames go by between animation frames
        animationframetime = 0 #how many frames we have been on the same animation frame

请注意,我在这里已经删除了所有无关的内容。因此,当我运行以下代码时出现错误:

Traceback (most recent call last):
  File "C:\Users\spng453\scripts\super smash feminist\main.py", line 90, in <module>
    screen.blit(char.currentanimation, pygame.Rect(char_x, (screen_height-char_y)-char_height, char_width, char_height))
AttributeError: 'module' object has no attribute 'currentanimation'

我真的不知道在哪里我会出错。任何了解此问题根源的帮助将不胜感激。

Scarza62

如果您定义一个具有属性的类(即self.currentanimation),则可以这样访问它:

charObj = char.Char()
charObj.currentanimation

您在代码中所做的工作是在模块内部而char不是在Char(charObj实例内部char模块没有属性/变量/等命名currentanimation但是Chardo的实例–或者更确切地说,一旦您__init__()在类定义中定义了一个方法并开始使用self:-) ,它们便会实例化

有关设置和访问属性的更多信息,请参阅docs的本部分

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Tornado AttributeError:模块“ test”没有属性“ __path__”

Python AttributeError:“模块”对象没有属性“获取”

AttributeError:“模块”对象没有属性“调用”:Python

Python3.6 AttributeError:模块“ asyncio”没有属性“运行”

Python Pandas错误:AttributeError:“模块”对象没有属性“格式”

Python导入:AttributeError:'模块'对象没有属性'test'

Python:AttributeError模块x没有属性y

python-chess-AttributeError:模块“ chess”没有属性“ pgn”

Python AttributeError:“模块”对象没有属性“序列”

Python:AttributeError:“模块”对象没有属性“ randrange”

Python AttributeError:“模块”对象没有属性“连接”

python-AttributeError:“模块”对象没有属性“锁”

python-opencv AttributeError:“模块”对象没有属性“ createBackgroundSubtractorGMG”

Python 3.6-AttributeError:模块'tkinter'没有属性'filedialog'

获取AttributeError:模块'mysql'在python中没有属性'connector'

Python 3 AttributeError:模块'sys'没有属性'argv'

Python AttributeError:“模块”对象没有属性“ atoi”

Python AttributeError:“模块”对象没有属性“套房”

AttributeError:“模块”对象没有属性“ urlopen”(Python 2.7)

Python AttributeError:模块“请求”没有属性“get_price”

Python 3.7 错误:AttributeError:模块“subprocess”没有属性“run”

AttributeError :模块“tensorflow”没有属性“contrib”-python 3.8

python错误AttributeError:模块'os'没有属性'WNOHANG'

AttributeError:“模块”对象没有带有robobrowser的属性“ open” Python 3.4

Python命中选项卡失败并显示Python AttributeError:模块“ readline”没有属性“ redisplay”

Python / Json AttributeError:部分初始化的模块'json'没有属性

AttributeError:“模块”对象没有属性“ fsdecode”;PYTHON 2.7; Mac OS X

python错误“ AttributeError:'模块'对象没有属性'sha1'”

google-api-python-client 1.8.1的重大更改-AttributeError:模块'googleapiclient'没有属性'__version__'