【发布时间】:2016-01-22 20:30:43
【问题描述】:
我目前正在开发一个 pygame 游戏。有两个班。一个是质量,另一个是球。 Ball 是 Mass 的子类。
我创建的每个对象都将继承自 Mass。它是一个基类。在我创建的对象中,我设计了move 方法来实现重力。但我想要完成的事情是在Mass 类中提供这种重力(基本上是方法中定义的负加速度值)。因此,通过在所有 move 方法中进行编码来避免重复。
所以我应该在Mass 中定义另一个move 方法。子类的方法会覆盖它吗?到目前为止,我试图找到解决方案的尝试都失败了。所以我不是要你为我写代码,我恳请你能否指出正确的方向,也许告诉我应该探索什么,我将不胜感激。
class Mass(object):
# The base class
class Ball(Mass):
def __init__(self, surface, radius, color, starting_pos):
self.radius = radius
self.color = color
self.starting_pos = starting_pos
self.surface = surface
def move(self):
# Keyboard handlers
def draw(self):
pygame.draw.circle(self.surface, self.color, (self.starting_pos), self.radius )
【问题讨论】:
标签: python python-2.7 inheritance pygame