From: TJ Date: Sat, 9 Nov 2013 23:56:38 +0000 (+0000) Subject: Provide background drawing for scrolling left or right X-Git-Url: https://iam.tj/gitweb/gitweb.cgi?p=base2-runner.git;a=commitdiff_plain;h=cc87ab0616913bb530793497f767464ab7197c08 Provide background drawing for scrolling left or right --- diff --git a/pygame-scroller.py b/pygame-scroller.py index 6daca9c..effb1cd 100755 --- a/pygame-scroller.py +++ b/pygame-scroller.py @@ -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()