这是我如何重写代码以使其适合一个类?

时间:2014-04-09 03:05:18

标签: python class methods

如果我有这样的课程:

class WorldView:
   def __init__(self, view_cols, view_rows, screen, world, tile_width,
      tile_height, mouse_img=None):
      self.viewport = pygame.Rect(0, 0, view_cols, view_rows)
      self.screen = screen
      self.mouse_pt = point.Point(0, 0)
      self.world = world
      self.tile_width = tile_width
      self.tile_height = tile_height
      self.num_rows = world.num_rows
      self.num_cols = world.num_cols
      self.mouse_img = mouse_img

还有一些像这样的功能(在课堂外留下):

def clamp(v, low, high):
   return min(high, max(v, low))


def create_shifted_viewport(viewport, delta, num_rows, num_cols):
   new_x = clamp(viewport.left + delta[0], 0, num_cols - viewport.width)
   new_y = clamp(viewport.top + delta[1], 0, num_rows - viewport.height)

   return pygame.Rect(new_x, new_y, viewport.width, viewport.height)


def draw_background(view):
   for y in range(0, view.viewport.height):
      for x in range(0, view.viewport.width):
         w_pt = viewport_to_world(view.viewport, point.Point(x, y))
         img = worldmodel.get_background_image(view.world, w_pt)
         view.screen.blit(img, (x * view.tile_width, y * view.tile_height))

我想重写这些函数,以便它们是WorldView类的方法(尽管如果函数看起来更适合离开类,它们不一定是这样),它会看起来像此?

class WorldView:
   def __init__(self, view_cols, view_rows, screen, world, tile_width,
      tile_height, mouse_img=None):
      self.viewport = pygame.Rect(0, 0, view_cols, view_rows)
      self.screen = screen
      self.mouse_pt = point.Point(0, 0)
      self.world = world
      self.tile_width = tile_width
      self.tile_height = tile_height
      self.num_rows = world.num_rows
      self.num_cols = world.num_cols
      self.mouse_img = mouse_img

   def create_shifted_viewport(self, viewport, delta):
      new_x = clamp(viewport.left + delta[0], 0, self.num_cols - viewport.width)
      new_y = clamp(viewport.top + delta[1], 0, self.num_rows - viewport.height)

      return pygame.Rect(new_x, new_y, viewport.width, viewport.height)

   def draw_background(self, view):
      for y in range(0, view.viewport.height):
         for x in range(0, view.viewport.width):
            w_pt = viewport_to_world(view.viewport, point.Point(x, y))
            img = worldmodel.get_background_image(view.world, w_pt)
            view.screen.blit(img, (x * view.tile_width, y * view.tile_height))

我是否仍然不确定是否应该包含" clamp"作为一种方法;我可以看到" create_shifted_viewport"使用clamp来获取" new_x和new_y",但是我应该将clamp作为一种方法吗?或者我应该把它排除在课堂之外?一般来说,当一个类中的一个方法正在使用另一个当前不属于该类的函数时,将该函数作为一个方法包含在逻辑中是否合乎逻辑?

1 个答案:

答案 0 :(得分:0)

有一些错误。我希望修复这些,看看评论。

class WorldView:
   def __init__(self, view_cols, view_rows, screen, world, tile_width,
      tile_height, mouse_img=None):
      self.viewport = pygame.Rect(0, 0, view_cols, view_rows)
      self.screen = screen
      self.mouse_pt = point.Point(0, 0)
      self.world = world
      self.tile_width = tile_width
      self.tile_height = tile_height
      # these seems not required since you can get these from self.world
      #self.num_rows = world.num_rows
      #self.num_cols = world.num_cols

      self.mouse_img = mouse_img

   def clamp(self, v, low, high):
       return min(high, max(v, low))

   def create_shifted_viewport(self, delta):
      #need self.num_cols, no need viewport parameter
      new_x = self.clamp(self.viewport.left + delta[0], 0, self.world.num_cols - viewport.width)
      new_y = self.clamp(self.viewport.top + delta[1], 0, self.world.num_rows - viewport.height)
      #need self.viewport
      return pygame.Rect(new_x, new_y, self.viewport.width, self.viewport.height)


   def draw_background(self):
      # i hope view.viewport is the self.viewport
      for y in range(0, self.viewport.height):
         for x in range(0, self.viewport.width):
            w_pt = viewport_to_world(self.viewport, point.Point(x, y))
            img = worldmodel.get_background_image(self.world, w_pt)
            self.screen.blit(img, (x * self.tile_width, y * self.tile_height))