nameError within Inheritance

2020-05-10 08:26发布

When the program is run I get this error:

Traceback (most recent call last):
  File "G:/assets/gametest1.py", line 141, in <module>
    cam = camera(318,0,220)
  File "G:/gametest1.py", line 85, in __init__
    super(camera,self).__init__(playerposX,characterPosX,y)
NameError: name 'characterPosX' is not defined



The player class

class player:
    def __init__(self, playerposX,characterPosX,y):
        self.playerposX = playerposX
        self.y = y
        self.width = 88
        self.height = 135
        self.standing = True
        self.left = False
        self.right = True
        self.vel = 15
        self.jumping = False
        self.jumpCount = 10
        self.attacking = False
        self.characterPosX = characterPosX

Above is the The constructor code for the player class The aim of the code is to get the screen to scroll as the player moves eventually stopping when the map limit is reached


    def move(self, playerposX, y):
        self.k = pygame.key.get_pressed()
        if self.k[pygame.K_LEFT] and self.playerposX > 0 - 45:
            self.left = True
            self.right = False
            self.playerposX -= self.vel
            self.standing = False
        elif self.k[pygame.K_RIGHT] and self.playerposX < 1500 - 90:
            self.right = True
            self.left = False
            self.standing = False
            self.playerposX += self.vel
        else:
            self.standing = True

    def jump(self, y):
        if not (self.jumping):  # checks if user's jumping intiating jump
            if self.k[pygame.K_SPACE]:
                self.jumping = True
        else:
            if self.jumpCount >= -10:
                neg = 1
                if self.jumpCount < 0:
                    neg = -1
                self.y -= (self.jumpCount ** 2) * 0.5 * neg
                self.jumpCount -= 1
            else:
                self.jumping = False
                self.jumpCount = 10

    def draw(self, win):
        wLeft = pygame.image.load('runningleft.png')
        wRight = pygame.image.load('running.png')
        char = [pygame.image.load('idleright.png'), pygame.image.load('idleleft.png')]
        attack = [pygame.image.load('attackleft.png'), pygame.image.load('attackright.png')]
        if not (self.standing):
            if self.left:
                win.blit(wLeft, (self.playerposX, self.y))
            elif self.right:
                win.blit(wRight, (self.playerposX, self.y))
        else:
            if self.right:
                win.blit(char[0], (self.playerposX, self.y))
            if self.left:
                win.blit(char[1], (self.playerposX, self.y))
        if self.attacking == True:
            if self.left:
                win.blit(attack[0], (self.playerposX, self.y))
            if self.right:
                win.blit(attack[1], (self.playerposX, self.y))

The child class camera which I aimed to inherit attributes from the parent class(player). This is the code which I will be using to move the background as the player move forward or backward

class camera(player):
    def __init__(self, stagePosX,playerposX,y):
        super(camera,self).__init__(playerposX,characterPosX,y)
        self.playerposX = playerposX
        self.startscrolling = bgWidth / 2
        self.stagePosX = stagePosX
        self.rel_x = 0


    def scroll(self,stagePosX):
        if self.k[pygame.K_LEFT]:
            self.vel = (self.vel*-1)
        elif self.k[pygame.K_RIGHT]:
            self.vel = (self.vel*1)
        else:
            self.vel = 0



        self.rel_x = self.stagePosX % bgWidth
        if self.playerPosX > stageWidth - self.width:
            self.playerPosX = self.stageWidth - self.width  # If the player position exceeds the stage
        if self.playerposX < 0 - self.width:
            self.playerposX = self.width  # If the player position is far left
        if self.playerposX < self.startscrolling:
            self.characterPosX = self.playerposX
        elif self.playerposXplayerposX > stageWidth - self.startscrolling:
            self.characterPosX = self.playerPosX - stageWidth + self.width
        else:
            self.characterPosX = self.startscrolling
            self.stagePosX +=  -self.vel






    def draw(self, win):
        win.blit(bg, (self.rel_x - bgWidth, 0))
        if self.rel_x < bgWidth:
            win.blit(bg, (self.rel_x, 0))

1条回答
一纸荒年 Trace。
2楼-- · 2020-05-10 08:49

I'm hazarding a guess here, but the second call to __init__() is being passed parameters that do not exist yet.

def __init__(self, stagePosX,playerposX,y):
    super(camera,self).__init__(playerposX,characterPosX,y)

Should that be something like:

def __init__( self, stagePosX, playerposX, y):
    super( camera, self ).__init__( playerposX, stagePosX, y ) # NOT characterPosX

Maybe you need to send a different or extra parameters for this? It's hard to know from the question's code.

查看更多
登录 后发表回答