[Sugar-devel] [PATCH] adapt pippy examples to screen dimensions, dev.laptop.org #9260

James Cameron quozl at laptop.org
Fri May 21 05:42:28 EDT 2010


Adjust all the examples to work for different screen dimensions, and
briefly fix formatting or style in some of the other code examples.

Yet to be done:

1.  slideshow, it finds no objects in journal with MIME type
image/jpeg, and as a result does not show any slides,

2.  xolympics, it tests an object location against physics world
coordinates to determine winning condition.
---
 data/graphics/bounce    |   32 +++++++++++----------
 data/graphics/camera    |   29 ++++++++++++-------
 data/graphics/jump      |    6 ++--
 data/graphics/lines     |   11 ++++---
 data/graphics/physics   |   17 +++++------
 data/graphics/pong      |   29 ++++++++++---------
 data/graphics/slideshow |   17 ++++++-----
 data/graphics/snow      |   11 ++++---
 data/graphics/tree      |   13 +++++----
 data/graphics/xolympics |   70 +++++++++++++++++++++++-----------------------
 data/sound/playSine     |    2 +-
 data/sound/playWave     |    2 +-
 data/string/thanks      |   12 +++++---
 13 files changed, 134 insertions(+), 117 deletions(-)

diff --git a/data/graphics/bounce b/data/graphics/bounce
index d0bcd3c..42889f0 100644
--- a/data/graphics/bounce
+++ b/data/graphics/bounce
@@ -2,7 +2,15 @@
 
 import pippy, pygame, sys
 from pygame.locals import *
-from random import *
+
+# the text to bounce around the screen
+msg = "Hello!"
+
+# the size of the text, in pixels
+fsize = 36
+
+# vector for motion, will control speed and angle
+mvect = [3, 2]
 
 # always need to init first thing
 pygame.init()
@@ -10,20 +18,12 @@ pygame.init()
 # turn off cursor
 pygame.mouse.set_visible(False)
 
-# XO screen is 1200x900
-size = width, height = 1200, 900
-
-# we'll use 36 pixel high text
-fsize = 36
-
-# vector for motion, will control speed and angle
-mvect = [3,2]
-
 # create the window and keep track of the surface
 # for drawing into
-screen = pygame.display.set_mode(size)
+screen = pygame.display.set_mode()
 
-msg = "Hello!"
+# ask for screen's width and height
+size = width, height = screen.get_size()
 
 # create a Font object from a file, or use the default
 # font if the file name is None. size param is height
@@ -35,7 +35,7 @@ font = pygame.font.Font(None, fsize)
 # Font.render draws text onto a new surface.
 #
 # usage: Font.render(text, antialias, color, bg=None)
-text = font.render(msg, True, (10,10,10))
+text = font.render(msg, True, (10, 10, 10))
 
 # the Rect object is used for positioning
 textRect = text.get_rect()
@@ -46,6 +46,7 @@ textRect.top = 0;
 
 while pippy.pygame.next_frame():
 
+  # every time we move the text, check for quit or keydown events and exit
   for event in pygame.event.get():
     if event.type == QUIT:
       sys.exit()
@@ -53,8 +54,9 @@ while pippy.pygame.next_frame():
     elif event.type == KEYDOWN:
       sys.exit()
 
-  screen.fill((250,250,250))
-  
+  # fill the screen with white
+  screen.fill((250, 250, 250))
+
   # draw the text
   screen.blit(text, textRect)
 
diff --git a/data/graphics/camera b/data/graphics/camera
index 6eea88f..023e04f 100644
--- a/data/graphics/camera
+++ b/data/graphics/camera
@@ -1,50 +1,57 @@
-# image: take a picture
+# camera: take a picture, animate it on screen
 
 import gst, pippy, pygame, sys, time
-from random import *
-
-# XO screen is 1200 by 900
-size = width, height = 1200, 900
 
 # grey background
-bgcolor = (128,128,128)
+bgcolor = (128, 128, 128)
 
 # grab a frame from camera to file
 pipeline = gst.parse_launch('v4l2src ! ffmpegcolorspace ! jpegenc ! filesink location=/tmp/pippypic.jpg')
 pipeline.set_state(gst.STATE_PLAYING)
 
-# pygame always needs to be initialized as the first call
+# start using pygame
 pygame.init()
 
 # turn off cursor
 pygame.mouse.set_visible(False)
 
-# create the pygame window at the desired size and return a Surface object for
+# create the pygame window and return a Surface object for
 # drawing in that window.
-screen = pygame.display.set_mode(size)
+screen = pygame.display.set_mode()
 
+# pause for a second to allow the camera frame to be grabbed
 time.sleep(1)
+
+# stop the camera frame grabbing
 pipeline.set_state(gst.STATE_NULL)
 
-# load in previously grabbed frame
+# load in the grabbed camera frame
 image = pygame.image.load("/tmp/pippypic.jpg")
 
 angle = 0.0
 scale = 2.0
 
 while pippy.pygame.next_frame():
+  # every time we animate, check for quit or keydown events and exit
   for event in pygame.event.get():
     if event.type == pygame.QUIT: sys.exit()
     elif event.type == pygame.KEYDOWN: sys.exit()
 
-  newImage = pygame.transform.rotozoom(image, angle,scale)
+  # angle and scale the image
+  newImage = pygame.transform.rotozoom(image, angle, scale)
   newImageRect = newImage.get_rect()
   newImageRect.centerx = screen.get_rect().centerx
   newImageRect.centery = screen.get_rect().centery
 
+  # display the angled and scaled image
   screen.fill(bgcolor)
   screen.blit(newImage, newImageRect)
   pygame.display.flip()
 
+  # choose a new angle and scale
   angle = angle + 5.0
   scale = scale * 0.95
+
+  # finish once the scale becomes very very small
+  if scale < 0.001:
+    break
diff --git a/data/graphics/jump b/data/graphics/jump
index f74ea71..7786187 100644
--- a/data/graphics/jump
+++ b/data/graphics/jump
@@ -12,19 +12,19 @@ for i in xrange(0,50):
     print "_|_"
     print "   "
     pippy.wait()
-    
+
     pippy.console.clear()
     print "_o_"
     print " | "
     print "/ \\"
     pippy.wait()
-    
+
     pippy.console.clear()
     print " o "
     print "/|\\"
     print "| |"
     pippy.wait()
-    
+
     pippy.console.clear()
     print "_o_"
     print " | "
diff --git a/data/graphics/lines b/data/graphics/lines
index c0faf93..bb67a34 100644
--- a/data/graphics/lines
+++ b/data/graphics/lines
@@ -4,9 +4,6 @@ import pippy, pygame, sys
 from pygame.locals import *
 from random import *
 
-# XO screen is 1200x900
-size = width, height = 1200, 900
-
 # always need to init first thing
 pygame.init()
 
@@ -15,7 +12,11 @@ pygame.mouse.set_visible(False)
 
 # create the window and keep track of the surface
 # for drawing into
-screen = pygame.display.set_mode(size)
+screen = pygame.display.set_mode()
+
+# ask for screen's width and height
+size = width, height = screen.get_size()
+
 # start the screen all black
 screen.fill((0,0,0))
 
@@ -48,7 +49,7 @@ while pippy.pygame.next_frame():
   pygame.display.flip()
 
   # update the end points and the color
-  for i in range(2): 
+  for i in range(2):
     start[i] = start[i] + mvect_start[i]
     end[i]   = end[i]   + mvect_end[i]
 
diff --git a/data/graphics/physics b/data/graphics/physics
index 556442a..0611351 100644
--- a/data/graphics/physics
+++ b/data/graphics/physics
@@ -6,12 +6,12 @@ from pippy import physics
 
 # initialize pygame first thing
 pygame.init()
-screen = pygame.display.set_mode((1200,900))
- 
+screen = pygame.display.set_mode()
+
 # set up the physics world (instance of Elements)
 world = physics.Elements(screen.get_size())
 world.renderer.set_surface(screen)
-       
+
 # set up initial physics objects
 world.add.ground()
 world.add.ball((600,0), 50)
@@ -19,9 +19,9 @@ world.add.rect((500,0), 25, 300, dynamic=True, density=1.0, restitution=0.16, fr
 
 # add 20 more balls
 balls = 0
-while(balls<20):
-    world.add.ball((balls*5+200,balls*5), 50)
-    balls+=1
+while(balls < 20):
+    world.add.ball((balls*5+200, balls*5), 50)
+    balls += 1
 
 # begin physics simulation
 world.run_physics = True
@@ -46,8 +46,8 @@ while pippy.pygame.next_frame() and world.run_physics:
             world.mouse_move(event.pos)
 
     # clear display with a color
-    # (r,g,b), where 0<=value<256
-    screen.fill((80,160,240))
+    # (red, green, blue), where 0 <= value < 256
+    screen.fill((80, 160, 240))
 
     # update & draw physics world
     world.update()
@@ -55,4 +55,3 @@ while pippy.pygame.next_frame() and world.run_physics:
 
     # update the display
     pygame.display.flip()
-
diff --git a/data/graphics/pong b/data/graphics/pong
index 5916c0b..b94aeb9 100644
--- a/data/graphics/pong
+++ b/data/graphics/pong
@@ -1,7 +1,7 @@
 # pong: hit the ball with the paddle
 #
 # use the escape key to exit
-# 
+#
 # on the XO, the escape key is the top lefthand key,
 # circle with an x in it.
 
@@ -12,40 +12,40 @@ from random import *
 # always need to init first thing
 pygame.init()
 
-# XO screen is 1200x900
-size = width, height = 1200, 900
-
 # create the window and keep track of the surface
 # for drawing into
-screen = pygame.display.set_mode(size)
+screen = pygame.display.set_mode()
+
+# ask for screen's width and height
+size = width, height = screen.get_size()
 
 # turn off the cursor
 pygame.mouse.set_visible(False)
 
 # turn on key repeating (repeat 40 times per second)
-pygame.key.set_repeat(25,25)
+pygame.key.set_repeat(25, 25)
 
 # start the screen all black
-bgcolor = (0,0,0)
+bgcolor = (0, 0, 0)
 screen.fill(bgcolor)
 
 # paddle constants
 paddle_width = 20
 paddle_length = 100
 paddle_radius = paddle_length / 2
-paddle_color = (250,250,250)
+paddle_color = (250, 250, 250)
 step = 6 # paddle moves 3 pixels at a go
 
 # ball constants
-ball_color = (250,250,250)
+ball_color = (250, 250, 250)
 ball_radius = 25
 
 # game constants
 fsize = 48
 msg = "Press 'g' to start game"
 
-font=pygame.font.Font(None, fsize)
-text = font.render(msg, True, (250,250,250))
+font = pygame.font.Font(None, fsize)
+text = font.render(msg, True, (250, 250, 250))
 textRect = text.get_rect()
 textRect.centerx = screen.get_rect().centerx
 textRect.centery = screen.get_rect().centery
@@ -100,7 +100,7 @@ while pippy.pygame.next_frame():
                 or event.key == 259 \
                 or event.key == 258: # down
                   paddle_location = paddle_location + step
-  
+
             # make sure the paddle is in-bounds
             if paddle_location - paddle_radius < 0:
               paddle_location = paddle_radius
@@ -113,7 +113,7 @@ while pippy.pygame.next_frame():
             # draw the paddle on the right side of the screen
             pygame.draw.line(screen,
   	                     paddle_color,
-                             (width - paddle_width, paddle_location - 
+                             (width - paddle_width, paddle_location -
                               paddle_radius),
                              (width - paddle_width,
                               paddle_location+paddle_radius),
@@ -124,7 +124,8 @@ while pippy.pygame.next_frame():
 
             # draw the unused balls
             for i in range(balls):
-              pygame.draw.circle(screen, ball_color, (int(round(30+i*ball_radius*2.4)), 30), 
+              pygame.draw.circle(screen, ball_color,
+                                 (int(round(30+i*ball_radius*2.4)), 30),
                                  ball_radius)
 
             # update the display
diff --git a/data/graphics/slideshow b/data/graphics/slideshow
index 0d38ab8..7ddd58b 100644
--- a/data/graphics/slideshow
+++ b/data/graphics/slideshow
@@ -1,4 +1,8 @@
-# slideshow: show datastore photos 
+# slideshow: show datastore photos
+
+# FIXME: does not currently work, because no objects are returned by
+# the journal query.
+
 def pippy_activity_class(): return 'activity.PyGameActivity'
 if __name__ == '__main__':
   import gst, pippy, pygame, sys, time
@@ -6,12 +10,9 @@ if __name__ == '__main__':
 
   from random import *
 
-  # XO screen is 1200 by 900
-  size = width, height = 1200, 900
-
   # grey background
-  bgcolor = (128,128,128)
-  
+  bgcolor = (128, 128, 128)
+
   # Create a search dict
   search = {}
   search["mime_type"] = "image/jpeg"
@@ -38,9 +39,9 @@ if __name__ == '__main__':
   # turn off cursor
   pygame.mouse.set_visible(False)
 
-  # create the pygame window at the desired size and return a Surface object for
+  # create the pygame window and return a Surface object for
   # drawing in that window.
-  screen = pygame.display.set_mode(size)
+  screen = pygame.display.set_mode()
 
   # load in previously grabbed frame
   image = pygame.image.load(next_image.next())
diff --git a/data/graphics/snow b/data/graphics/snow
index 4fc30f2..efcb28f 100644
--- a/data/graphics/snow
+++ b/data/graphics/snow
@@ -1,3 +1,4 @@
+# snow
 
 import pippy, pygame, sys
 from pygame.locals import *
@@ -6,17 +7,17 @@ from random import *
 # always need to init first thing
 pygame.init()
 
-# XO screen is 1200x900
-size = width, height = 1200, 900
-
 # create the window and keep track of the surface
 # for drawing into
-screen = pygame.display.set_mode(size)
+screen = pygame.display.set_mode()
+
+# ask for screen's width and height
+width, height = screen.get_size()
 
 # turn off the cursor
 pygame.mouse.set_visible(False)
 
-bg_color = (0,0,0)
+bg_color = (0, 0, 0)
 
 xs = []
 ys = []
diff --git a/data/graphics/tree b/data/graphics/tree
index f4cdae7..e01cf8e 100644
--- a/data/graphics/tree
+++ b/data/graphics/tree
@@ -1,3 +1,4 @@
+# tree
 
 import pippy, pygame, sys
 from pygame.locals import *
@@ -7,17 +8,17 @@ import math
 # always need to init first thing
 pygame.init()
 
-# XO screen is 1200x900
-size = width, height = 1200, 900
-
 # create the window and keep track of the surface
 # for drawing into
-screen = pygame.display.set_mode(size)
+screen = pygame.display.set_mode()
+
+# ask for screen's width and height
+width, height = screen.get_size()
 
 # turn off the cursor
 pygame.mouse.set_visible(False)
 
-color = (250,250,250)
+color = (250, 250, 250)
 min_factor = 0.8
 max_factor = 0.9
 start_length = 130
@@ -61,7 +62,7 @@ draw_tree((width / 2), height - 20, start_length, 0)
 pygame.display.flip()
 
 while pippy.pygame.next_frame():
-  # chill until a key is pressed
+  # chill until escape key is pressed
   for event in pygame.event.get():
     if event.type == QUIT:
         sys.exit()
diff --git a/data/graphics/xolympics b/data/graphics/xolympics
index 5fe273c..525e480 100644
--- a/data/graphics/xolympics
+++ b/data/graphics/xolympics
@@ -13,7 +13,7 @@ from pygame.color import *
 from pippy.physics import box2d
 
 class XOlympicsGame:
-    def __init__(self,screen):
+    def __init__(self):
         self.rightscore = self.leftscore = 0
         self.forcespeed = 75
         self.jumpforce = 20
@@ -27,31 +27,34 @@ class XOlympicsGame:
         self.rightJump = False
         self.updateList = []
 
-        self.screen = pygame.display.set_mode((1200,900)) 
+        self.screen = pygame.display.set_mode()
+        self.width, self.height = self.screen.get_size()
 
         self.clock = pygame.time.Clock()
-        self.font = pygame.font.Font(None, 24) # font object
 
         # set up the world (instance of Elements)
         self.world = physics.Elements(self.screen.get_size())
         self.world.renderer.set_surface(self.screen)
         # set up static environment
-        self.world.set_color((0, 255, 0))  
+        self.world.set_color((0, 255, 0))
         self.world.add.ground()
-        self.ball = self.world.add.ball((600, 0), 50)
+        self.ball = self.world.add.ball((self.width / 2, 0), 50)
         # add the left border and player (red)
-        self.world.set_color((255, 0, 0))  
-        self.world.add.rect((0,-20), 25, 900, dynamic=False, density=1.0, restitution=0.16, friction=0.5)   
-        self.leftplayer = self.world.add.poly(( 264.0, 81.0 ),  ((-109.9405166666667, -64.244016666666653), (110.60718333333335, -63.089316666666605), (-0.66666666666668561, 127.33333333333337)) , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False)
-        # add the right border and player (blue) 
-        self.world.set_color((0, 0, 255))  
-        self.world.add.rect((1180,-20), 25, 900, dynamic=False, density=1.0, restitution=0.16, friction=0.5)   
-        self.rightplayer = self.world.add.poly(( 885.0, 78.0 ),  [(108.94051666666667, -65.976066666666611), (2.6666666666666288, 127.33333333333337), (-111.60718333333341, -61.357266666666646)] , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False)
-        # we're getting 2 grounds - grey and green. wtf.  
-        self.leftplayer.linearDamping = 0.07
-        self.test = self.leftplayer.GetWorldCenter() 
+        self.world.set_color((255, 0, 0))
+        self.world.add.rect((0, -20), 25, self.height,
+                                      dynamic=False, density=1.0,
+                                      restitution=0.16, friction=0.5)
+        self.leftplayer = self.world.add.poly(( self.width * 0.25, 81.0 ),  [(-109.9405166666667, -64.244016666666653), (110.60718333333335, -63.089316666666605), (-0.66666666666668561, 127.33333333333337)] , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False)
+        # add the right border and player (blue)
+        self.world.set_color((0, 0, 255))
+        self.world.add.rect((self.width, -20), 25, self.height,
+                                      dynamic=False, density=1.0,
+                                      restitution=0.16, friction=0.5)
+        self.rightplayer = self.world.add.poly(( self.width * 0.75, 81.0 ),  [(108.94051666666667, -65.976066666666611), (2.6666666666666288, 127.33333333333337), (-111.60718333333341, -61.357266666666646)] , dynamic=True, density=1.0, restitution=0.16, friction=0.5, screenCoord=False)
+        # we're getting 2 grounds - grey and green. why?
+
     def run(self):
-        self.running = True    
+        self.running = True
         while self.running:
 
             for event in pygame.event.get():
@@ -84,11 +87,11 @@ class XOlympicsGame:
                 if (event.type == KEYDOWN and (event.key == K_UP or event.key == K_KP9)):
                     self.rightJump = True
                 if (event.type == KEYUP and (event.key == K_UP or event.key == K_KP9)):
-                    self.rightJump = False            
+                    self.rightJump = False
                 if (event.type == KEYDOWN and (event.key == K_DOWN or event.key == K_KP3)):
                     self.rightDPress = True
                 if (event.type == KEYUP and (event.key == K_DOWN or event.key == K_KP3)):
-                    self.rightDPress = False             
+                    self.rightDPress = False
 
             if self.leftLPress:
                 self.leftplayer.ApplyForce(box2d.b2Vec2(-self.forcespeed,0), self.leftplayer.GetWorldCenter())
@@ -102,49 +105,46 @@ class XOlympicsGame:
             if self.rightRPress:
                 self.rightplayer.ApplyForce(box2d.b2Vec2(self.forcespeed,0), self.rightplayer.GetWorldCenter())
             if self.rightDPress:
-	            self.rightplayer.ApplyImpulse(box2d.b2Vec2(0,-self.jumpforce), self.rightplayer.GetWorldCenter())
+                self.rightplayer.ApplyImpulse(box2d.b2Vec2(0,-self.jumpforce), self.rightplayer.GetWorldCenter())
             if self.rightJump:
                 if self.rightplayer.GetWorldCenter().y < 0.75:
-	                self.rightplayer.ApplyImpulse(box2d.b2Vec2(0,self.jumpforce), self.rightplayer.GetWorldCenter())
+                    self.rightplayer.ApplyImpulse(box2d.b2Vec2(0,self.jumpforce), self.rightplayer.GetWorldCenter())
             if self.leftDPress:
 	            self.leftplayer.ApplyImpulse(box2d.b2Vec2(0,-self.jumpforce), self.leftplayer.GetWorldCenter())
 
-           # Clear Display
+            # Clear Display
             if self.ball.GetWorldCenter().x < 1:
                 self.leftscore += 1
                 print "Goal Blue!", self.leftscore
                 self.world.set_color((0, 0, 255))
-                self.ball = self.world.add.ball((600, 0), 50)
+                self.ball = self.world.add.ball((self.width/2, 0), 50)
             elif self.ball.GetWorldCenter().x > 11:
+                # FIXME: the 11 above works only when display width is
+                # 1200 pixels
                 self.rightscore += 1
                 print "Goal Red!", self.rightscore
                 self.world.set_color((255, 0, 0))
-                self.ball = self.world.add.ball((600, 0), 50)
+                self.ball = self.world.add.ball((self.width/2, 0), 50)
 
-            self.screen.fill((255,255,255)) 
+            self.screen.fill((255,255,255))
             # Update & Draw World
             self.world.update()
             self.world.draw()
-           
+
             # Flip Display
-            pygame.display.flip()  
-            
+            pygame.display.flip()
+
             # Try to stay at 30 FPS
-            self.clock.tick(30) # originally 50    
+            self.clock.tick(30) # originally 50
 
 def main():
-    toolbarheight = 0
-    tabheight = 0
     pygame.init()
     pygame.display.init()
-    x,y  = pygame.display.list_modes()[0]
-    screen = pygame.display.set_mode((x,y-toolbarheight))#-tabheight))
     # create an instance of the game
-    game = XOlympicsGame(screen) 
+    game = XOlympicsGame()
     # start the main loop
     game.run()
 
-# make sure that main get's called
+# make sure that main is called
 if __name__ == '__main__':
     main()
-
diff --git a/data/sound/playSine b/data/sound/playSine
index 159b84b..216b4aa 100644
--- a/data/sound/playSine
+++ b/data/sound/playSine
@@ -2,4 +2,4 @@ import pippy
 
 pippy.sound.playSine(pitch=1000, amplitude=5000)
 pippy.sound.audioOut()
-        
+
diff --git a/data/sound/playWave b/data/sound/playWave
index 503b475..819ad4f 100644
--- a/data/sound/playWave
+++ b/data/sound/playWave
@@ -2,4 +2,4 @@ import pippy
 
 pippy.sound.playWave(sound='didjeridu', loop=True, duration=5)
 pippy.sound.audioOut()
-        
+
diff --git a/data/string/thanks b/data/string/thanks
index 52e47a3..ddd470a 100644
--- a/data/string/thanks
+++ b/data/string/thanks
@@ -17,14 +17,14 @@ table = {
           'Directors & Advisors':   'Howard Anderson, Rebecca Allen, Ayo Kusamotu, Jose Maria Aznar, V. Michael Bove, Jr., Rodrigo Mesquita, Seymour Papert, Ted Selker, Ethan Beard (Google); John Roese (Nortel); Dandy Hsu (Quanta); Marcelo Claure (Brightstar); Gary Dillabough (eBay); Gustavo Arenas (AMD); Mike Evans (Red Hat); Ed Horowitz (SES Astra); Jeremy Philips (NewsCorp); Scott Soong (Chi Lin); Sehat Sutardja (Marvell); Joe Jacobson (MIT Media Lab); Steve Kaufman (Riverside); and Tom Meredith (MFI)',
           'Pippy':                  'Chris Ball, C. Scott Ananian'
        }
- 
+
 import random, time
 from pippy.console import *
 from textwrap import fill
 
-# Determine the number of columns in our window in 
-# order to wrap text to that width -- this changes 
-# when we run as a standalone activity instead of 
+# Determine the number of columns in our window in
+# order to wrap text to that width -- this changes
+# when we run as a standalone activity instead of
 # inside the output pane.
 cols, lines = size()
 
@@ -36,5 +36,9 @@ while True:
     random.choice([red, green, yellow, blue, magenta, cyan])()
     #random.choice([normal, bold, underlined, inverse])()
     print '\n', fill("%s:    %s" % (subsystem, table[subsystem]), int(cols))
+    table.pop(subsystem)
+    if len(table) == 0:
+        break
 
     time.sleep(3)
+reset()
-- 
1.7.1



More information about the Sugar-devel mailing list