我试图在我的pygame项目中使用多态,但我一直在收到错误。
我有一个Moveable类和一个继承自Moveable的Objects类,我得到一个AttributeError:' NoneType'对象没有属性' ypos'在我的游戏循环中。
我计划在我的项目中拥有更多可移动的物体,比如人,房屋,道路标志......如果我可以使用它。
class Moveable:
__name = ""
__xpos = 0
__ypos = 0
__hight = 0
__width = 0
__speed = 0
__image = 0
def __init__(self, xpos, ypos, hight, width, speed, image):
self.__xpos = xpos
self.__ypos = ypos
self.__hight = hight
self.__width = width
self.__image = image
def set_xpos(self, xpos):
self.__xpos = xpos
def get_xpos(self):
return self.__xpos
# ... and all my other getters and setters...
class Objects(Moveable):
def car(xpos, ypos, width, hight, speed, image):
pygame.Surface.blit(gameDisplay, image, [xpos, ypos, hight, width])
def game_loop():
game_exit = False #sets the game loop to false
while not game_exit: # runs the game loop until the game exit = true
car = Objects.car(random.randrange(250, 550), -400, 60, 70, 4, pygame.image.load('car.png'))
gameDisplay.blit(backGroundImg, (0, 0)) #displays the background
car.ypos += car.speed # the error is here **********
[R
答案 0 :(得分:1)
如果这是你的意图,你需要返回pygame。
def car(xpos, ypos, width, hight, speed, image):
return pygame.Surface.blit(gameDisplay, image, [xpos, ypos, hight, width])
目前,car
没有return语句,因此为方法分配变量将返回None
,因此您尝试访问NoneType