继承中的nameError [英] nameError within Inheritance
问题描述
程序运行时出现此错误:
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
播放器类
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))
推荐答案
我在这里有个猜测,但第二次调用__init__()
的原因是传递尚不存在的参数.
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)
应该是这样的:
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.
这篇关于继承中的nameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!