{"id":40,"date":"2012-09-26T01:59:32","date_gmt":"2012-09-26T01:59:32","guid":{"rendered":"http:\/\/burnhamup.com\/?p=40"},"modified":"2012-09-26T02:16:27","modified_gmt":"2012-09-26T02:16:27","slug":"collision-detection-in-pygame","status":"publish","type":"post","link":"https:\/\/burnhamup.com\/blog\/2012\/09\/collision-detection-in-pygame\/","title":{"rendered":"Collision Detection in PyGame"},"content":{"rendered":"<p>I recently added collision detection to Eccoes using <a href=\"http:\/\/www.pygame.org\">PyGame<\/a>. It wasn&#8217;t as straightforward as I hoped that it would be.<\/p>\n<p>Collision detection in PyGame is based around sprite groups. The methods sprite.spritecollide() and sprite.spritecollideany() checks to see if a specific sprite is colliding with sprites inside the group. The examples that I looked at used sprite groups to represent the other objects in the game and not the player object. This made collision detection easy and straightforward.<\/p>\n<p>In my game I currently maintain two sprite groups. One is called visibleObjects and the other is called gameObjects. I didn&#8217;t think it would be a good idea to have another sprite group with all of the objects that aren&#8217;t the main character because I also want to track collisions between enemy characters and the terrain. Instead I looked for a more general solution. VisibleObjects contain all the objects that I want to do collision detection between.<\/p>\n<p>My collision detection code is that following:<\/p>\n<pre>def getCollision(self, obj):\r\n  self._visibleObjects.remove(obj)\r\n  result = pygame.sprite.spritecollideany(\r\n       obj, self._visibleObjects)\r\n  self._visibleObjects.add(obj)\r\n  return result<\/pre>\n<p>In the code I removed the current object from the visibleObject group, use spritecollide, and then replace the object in the visibleObject group. I do this because the object that I&#8217;m testing is a part of the visibleObject list. This should be reasonably fast and it will keep me from having to keep track of lots of sprite groups.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently added collision detection to Eccoes using PyGame. It wasn&#8217;t as straightforward as I hoped that it would be. Collision detection in PyGame is based around sprite groups. The methods sprite.spritecollide() and sprite.spritecollideany() checks to see if a specific &hellip; <a href=\"https:\/\/burnhamup.com\/blog\/2012\/09\/collision-detection-in-pygame\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,10],"tags":[],"_links":{"self":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":9,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}