在pygame中移动一个矩形

时间:2014-10-23 10:03:13

标签: python algorithm pygame

例如,我有一排奶酪,如下图所示,有动物吃它。问题是,一旦我吃了奶酪,动物必须移动,奶酪必须被移除。我不知道该怎么做。需要一些指导。

enter image description here

我的Pet类看起来像这样:

class Pet(object):
    active = None

    def __init__(self):
        self.rect = pygame.Rect(32, 32, 16, 16)

    def move(self, dx, dy):

        if Pet.active and isinstance(self, Pet.active):
            if dx != 0:
                self.move_single_axis(dx, 0)
            if dy != 0:
                self.move_single_axis(0, dy)

    def move_single_axis(self, dx, dy):

        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        # If you collide with a wall, move out based on velocity
        for wall in walls:
            if self.rect.colliderect(wall.rect):
                if dx > 0: # Moving right; Hit the left side of the wall
                    self.rect.right = wall.rect.left
                if dx < 0: # Moving left; Hit the right side of the wall
                    self.rect.left = wall.rect.right
                if dy > 0: # Moving down; Hit the top side of the wall
                    self.rect.bottom = wall.rect.top
                if dy < 0: # Moving up; Hit the bottom side of the wall
                    self.rect.top = wall.rect.bottom
        for circle in circles:
            if mouse.rect.contains(circle.circle):
                #stuck here

我的奶酪是这样绘制的:

level = [
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWW",
]

# Parse the level string above. W = wall, E = exit
x = y = 0
for row in level:
    for col in row:
        if col == "W":
            Wall((x, y))
        x += 16
    y += 16
    x = 0

for wall in walls:
        pygame.draw.rect(screen, Yellow, circle.circle)

如果有人能帮忙做这一部分,我将不胜感激......

1 个答案:

答案 0 :(得分:1)

看看这个。我建议您通过Python教程更好地理解数据结构。

  from itertools import product

  level_definition = """
  WWWWWWW
  WCCCCCW
  WCCCCCW
  WCCPCCW
  WWWWWWW
  """

  class Level(object):

      def __init__(self, definition):
          self._data = [
              list(line.strip()) for line in
              definition.split("\n") if line.strip()
              ]

          self.height = len(self._data)
          self.width = len(self._data[0])
          for x, y in product(xrange(self.width), xrange(self.height)):
              if self[x][y] == "P":
                  self[x][y] = " "
                  self.initial_player_position = (x, y)
                  break


      def __getitem__(self, column):
          level = self
          class ColAccessor(object):

              def __getitem__(self, row):
                  return level._data[row][column]


              def __setitem__(self, row, value):
                  level._data[row][column] = value


          return ColAccessor()


      def __str__(self):
          return "\n".join("".join(row) for row in self._data)

  class Player(object):

      def __init__(self, level):
          self._pos = level.initial_player_position
          self._level = level

      def move(self, xoffset=0, yoffset=0):
          newx, newy = self._pos[0] + xoffset, self._pos[1] + yoffset
          try:
              next_thing = self._level[newx][newy]
          except IndexError:
              # you moved outside the bounds, impossible
              # so pretend we hit a wall
              return "W", self._pos
          if next_thing == "W":
              # you hit a wall
              return next_thing, self._pos
          self._level[newx][newy] = " "
          self._pos = newx, newy 
          return next_thing, self._pos


  if __name__ == "__main__":
      level = Level(level_definition)
      assert level[3][3] == " "
      player = Player(level)
      for _ in xrange(3):
          print player.move(-1)

      print str(level)