lakshmen
lakshmen

Reputation: 29064

Moving over a rectangle in pygame

For example, I have a rows of cheese like the image below and there is an animal to eat it. Problem is once I eat the cheese, the animal has to move over and the cheese has to be removed. I am not sure how to do this. Need some guidance on this.

enter image description here

My Pet class looks like this:

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

My cheese is drawn this way:

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)

I would appreciate if anyone could help out in doing this part...

Upvotes: 0

Views: 215

Answers (1)

deets
deets

Reputation: 6395

Take a look at this. I suggest though you work through a Python tutorial to understand data-structures better.

  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)

Upvotes: 1

Related Questions