质疑这样的 Python 类是否正确

问题描述

我是初学者。我有一个问题,如果在 Python 中创建一个类,这个代码片段是否可以接受。这是python中乒乓球游戏的一类球。 它有效,但它是我在做什么的良好编程实践。如您所见,我正在使用 pygame 并直接在 draw 方法中使用它。特别是这部分我想知道这是否可以。

class Ball(pygame.rect.Rect):
   def __init__(self,left,top,width,height):
      super() .__init__(left,height)
      self.color = (255,0)


   def draw(self,screen):

       pygame.draw.rect(screen,self.color,self)

解决方法

不要从 pygame.rect.Rect 派生。 “PyGame”方式是使用 pygame.sprite.Sprite 对象