Any nice collision framework ideas, Simple Maze

Any nice collision framework ideas, Simple Maze

Postby DrakeMagi » Fri Jun 07, 2013 2:37 pm

Here something I threw together using some images from this sight.
It a working example can be downloaded.

My first image created in blender scale down use in game.
I'll still need some practice.

probably still need to work on map framework too.

I just feel i threw in collision. It all under the player class.
Just wonder if anyone has any collision framework idea.
Code: Select all
class Player(object):
   def __init__(self, x, y):
      self.position = Point(x,y)
      self.image =
      self.frame = 0
      self.direction = 0
      self.tick = 0
      self.interval = 90
      self.walk = 4
      self.explosion = None
   def Blit(self, surface):
      if self.explosion is None:
         surface.blit(self.image[self.direction][self.frame], self.position.Int())
   def _Move(self, pyint):
      if self.direction != pyint:
         self.direction = pyint
         self.frame = 0
         self.frame += 1
         if self.frame > 3:
            self.frame = 0
   def SetPosition(self, pytuple):
      self.position = Point( *pytuple )
   def Update(self, tick, maprect, deathrect, exitrect):
      if self.explosion is not None:
         if self.explosion.expire:
            self.explosion = None
      if tick > self.tick:
         self.tick = tick + self.interval
         moved = -1
         if screen.GetKeyPress( pygame.K_DOWN ):
            moved = 0
            self.position.y += self.walk
         elif screen.GetKeyPress( pygame.K_LEFT ):
            moved = 1
            self.position.x -= self.walk
         elif screen.GetKeyPress( pygame.K_RIGHT ):
            moved = 2
            self.position.x += self.walk
         elif screen.GetKeyPress( pygame.K_UP ):
            moved = 3
            self.position.y -= self.walk
         # moving the collosion rect to the feet
         pos = self.position + Point(3, 24)
         mrect = pygame.Rect(pos.Int(), (26, 24))
         if moved != -1:   
            for rect in maprect:
               if rect.colliderect(mrect):
                  if moved == 0:
                     self.position.y -= self.walk
                  elif moved == 3:
                     self.position.y += self.walk
                  elif moved == 1:
                     self.position.x += self.walk
                  elif moved == 2:
                     self.position.x -= self.walk
            if self.position.x < 0:
               self.position.x += self.walk
            elif self.position.x > 800 - 32:
               self.position.x -= self.walk
            if self.position.y < -24:
               self.position.y += self.walk
            elif self.position.y > 600 - 48:
               self.position.y -= self.walk
            if exitrect.colliderect(mrect):
               return 2
         for rect in deathrect:
               if rect.colliderect(mrect):
                  return mrect
      return 0

Simple Maze
(212.04 KiB) Downloaded 144 times

you need image below for game. was to big to be added.
Floor Image from
crackles_800by600.jpg (172.17 KiB) Viewed 1058 times
Linux: won't find windows here.
Linux: the choice of a GNU generation.
Posts: 112
Joined: Sun May 12, 2013 8:36 pm

Re: Any nice collision framework ideas, Simple Maze

Postby Mekire » Fri Jun 07, 2013 3:33 pm

Hmm... usually I like to have each of my sprites that can collide have a detect_collision and collide_with method which details if and how they react on collision. Then I check collisions for all objects in the class which is keeping the lists of screen objects, every frame. I check collisions for everything and take the appropriate action using polymorphism. Make a really generic class with the basic collision functions and have every sprite you intend to behave that way inherit from it. Then overIoad for more specific behavior as needed. I would have to do some experimentation to try to come up with the most generalized framework for instituting it though.

New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests