我正在编写一个简单的 Pong 游戏。我希望我的 Paddle 和我的 Ball 对象都从 DynamicObj 继承,DynamicObj 是一个父类,它有一个update
使用基本的简单欧拉积分来标记位置的方法。问题是 Paddle 对象的位置存储在 rect 中(因此使用 rect.x 访问 x pos),而 Ball 对象的位置存储在 ax 和 y 值中。有没有办法让它们都继承自同一个能够标记它们位置的类?
这是我到目前为止所拥有的:
class DynamicObj:
def __init__(self, vel):
self.vel = vel
def update(self, delta):
self.x += self.vel.x * delta
self.y += self.vel.y * delta
class Ball(DynamicObj):
def __init__(self, x, y, radius, vel):
self.x = x
self.y = y
self.radius = radius
self.vel = vel
class Paddle(DynamicObj):
def __init__(self, rect, vel):
self.rect = rect
self.vel = vel
#what I want is to create self.x and self.y that respectively point to the same place as self.rect.x and self.rect.y
我在考虑可能用property
.