Provide background drawing for scrolling left or right
authorTJ <git@iam.tj>
Sat, 9 Nov 2013 23:56:38 +0000 (23:56 +0000)
committerTJ <git@iam.tj>
Sat, 9 Nov 2013 23:56:38 +0000 (23:56 +0000)
pygame-scroller.py

index 6daca9c..effb1cd 100755 (executable)
@@ -83,15 +83,23 @@ class SeamlessBackground:
   if self.image == None:
    print("Failed to load image", filename)
 
- def draw(self, screen,  x_pos, step):
-  if DEBUG: print("SeamlessBackground(screen, %d, %d)" % (x_pos, step))
+ def draw(self, screen, x_pos, step):
+  """ x_pos == -1 requests the full screen be painted, not just the damaged left/right margins
+  """
+  if DEBUG: print("SeamlessBackground.draw(screen, %d, %d)" % (x_pos, step))
   if self.image != None:
    resolution = screen.get_size()
+   # adjust x_pos to be x coord of damaged rectangle (left or right side of screen)
    if step > 0:
-    x_pos = x_pos + resolution[0] - step
-   
-   bk_x_start = x_pos % self.width
-   bk_rect = pygame.Rect(bk_x_start, 0, abs(step) if step != 0 else resolution[0], resolution[1])
+    x_pos += resolution[0] - step
+   elif step < 0:
+    x_pos -= step
+   elif step == 0 and x_pos >= 0:
+    # nothing to do if not drawing the initial background
+    return
+
+   bk_x_start = x_pos % self.width if x_pos != -1 else 0
+   bk_rect = pygame.Rect(bk_x_start, 0, resolution[0] if x_pos == -1 else abs(step), resolution[1])
    patch = pygame.Surface((bk_rect.width, bk_rect.height))
    patch.blit(self.image, (0, 0), bk_rect)
    remainder = self.width - bk_x_start
@@ -124,7 +132,7 @@ screen.fill(background_colour)
 
 # background image
 background_image = SeamlessBackground(os.path.join("resources", "binary.jpg"))
-background_image.draw(screen, 0, 0)
+background_image.draw(screen, -1, 0) # -1 flags initial background drawing
 pygame.display.flip()
 
 # scrolling object
@@ -170,7 +178,7 @@ while (myGame.play):
 
  # redraw the display
  screen.scroll(-movement, 0)
- background_image.draw(screen, x_pos, stepping)
+ background_image.draw(screen, x_pos, movement)
  screen.blit(blip, (resolution[0] - abs(stepping), y))
  pygame.display.flip()