Add support for saving the raw output to a file
[experiments/pygame.git] / pygame-vertical-scrolling-map.py
index 132b44a..90e1af0 100755 (executable)
@@ -21,6 +21,8 @@
 # https://www.cs.ucsb.edu/~pconrad/cs5nm/topics/pygame/drawing/
 
 import pygame
+import sys
+import os
 
 IMAGE_PATH = 'road.png'
 SCREEN_WIDTH = 800
@@ -50,15 +52,15 @@ class VerticalScrollingMap(pygame.Surface):
         # Center along X
         self.x = (self.viewport_width - self.texture_widht) / 2.0
 
-    def draw(self, screen):
-        self.y += self.offset
-
         # NOTE, we assume that the texture has at least twice the height of the
         # viewport
-        lastframe_limit = -(self.texture_height - self.viewport_height)
+        self.lastframe_limit = -(self.texture_height - self.viewport_height)
+
+    def draw(self, screen):
+        self.y += self.offset
 
         # Wrap around to cycle seamlessly
-        self.y %= lastframe_limit
+        self.y %= self.lastframe_limit
 
         screen.blit(self.texture, (self.x, self.y))
 
@@ -99,6 +101,14 @@ def main():
     except:
         joystick = None
 
+    if len(sys.argv) > 1:
+        if sys.argv[1] == '-':
+            outputfile = sys.stdout.fileno()
+        else:
+            outputfile = os.open(sys.argv[1], os.O_WRONLY)
+    else:
+        outputfile = None
+
     clock = pygame.time.Clock()
 
     fps = 30
@@ -129,6 +139,10 @@ def main():
         screen.blit(background.convert(), (0, 0))
         scrolling_map.draw(screen)
 
+        if outputfile:
+            buf = pygame.image.tostring(pygame.display.get_surface(), "RGB", False)
+            os.write(outputfile, buf)
+
         pygame.display.update()
 
     pygame.quit()