{"id":67,"date":"2012-11-14T04:49:21","date_gmt":"2012-11-14T04:49:21","guid":{"rendered":"http:\/\/burnhamup.com\/blog\/?p=67"},"modified":"2012-11-14T04:49:21","modified_gmt":"2012-11-14T04:49:21","slug":"moving-camera-in-pygame","status":"publish","type":"post","link":"https:\/\/burnhamup.com\/blog\/2012\/11\/moving-camera-in-pygame\/","title":{"rendered":"Moving Camera in PyGame"},"content":{"rendered":"<p>I wanted to have a moving camera in Eccoes so that the player could explore levels that were way bigger than my default screen size.<\/p>\n<p>My first attempt was to see how this was done elsewhere. I followed a tutorial in <a href=\"http:\/\/inventwithpython.com\/pygame\/\">&#8220;Making Games With Python and Pygame&#8221;<\/a> book, <a href=\"http:\/\/inventwithpython.com\/pygame\/chapter8.html\">chapter 8<\/a>, has a simple game with a moving camera. I saw that they weren&#8217;t using <a href=\"http:\/\/www.pygame.org\/docs\/ref\/sprite.html#pygame.sprite.Group\">sprite groups<\/a> for drawing, and I realized I could benefit from that.<\/p>\n<p>The problem with using sprite groups is that the Sprite.rect object is used to determine where to draw each of your sprites. This makes things very difficult when having a moving camera, since you would need to offset the position of each one of your sprites whenever the camera moved. Instead, I decided to keep the Sprite.rect objects in my world coordinates, and my drawing method would translate them onto the screen.<\/p>\n<p>I created a Rect object to represent the camera. It would have dimensions that are the same size as my window, and would be locked to the center of my main player character. My update method on the level looks like:<\/p>\n<pre style=\"padding-left: 30px;\">def update(self):\r\n self.camera.center = self._player.rect.center\r\n self._clock.increment()\r\n self._gameObjects.update()<\/pre>\n<p>Then my drawing code was changed from<\/p>\n<pre>def draw(self, screen):\r\n self._visibleObjects.draw(screen)<\/pre>\n<p>To:<\/p>\n<pre>def draw(self, screen):\r\n  for sprite in self._visibleObjects:\r\n    if (self.camera.colliderect(sprite.rect)):\r\n       screen.blit(sprite.image, sprite.rect.move(-self.camera.left,-self.camera.top))<\/pre>\n<p><span style=\"font-size: small;\"><span style=\"line-height: 19px;\">I get a list of objects that are inside the camera rect, and then draw them to screen,\u00a0<\/span><\/span><span style=\"line-height: 19px;\">shifting<\/span><span style=\"font-size: small;\"><span style=\"line-height: 19px;\">\u00a0them according to the camera coordinates.<\/span><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"line-height: 19px;\">This works really well, and meant I didn&#8217;t have to go to any crazy schemes to get a moving camera. I may play around with getting a camera that doesn&#8217;t move\u00a0<\/span><\/span><span style=\"line-height: 19px;\">immediately<\/span><span style=\"font-size: small;\"><span style=\"line-height: 19px;\">\u00a0when the player moves, but that would only involve improvements to the update code and not to the actual drawing code.\u00a0<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to have a moving camera in Eccoes so that the player could explore levels that were way bigger than my default screen size. My first attempt was to see how this was done elsewhere. I followed a tutorial &hellip; <a href=\"https:\/\/burnhamup.com\/blog\/2012\/11\/moving-camera-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\/67"}],"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=67"}],"version-history":[{"count":3,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/posts\/67\/revisions\/70"}],"wp:attachment":[{"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/categories?post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/burnhamup.com\/blog\/wp-json\/wp\/v2\/tags?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}