Commits

illume committed 0eb69a9

updated examples so that they all have a main() and use pygame.quit

Comments (0)

Files changed (9)

     if pygame.mixer:
         pygame.mixer.music.fadeout(1000)
     pygame.time.wait(1000)
+    pygame.quit()
 
 
 

examples/arraydemo.py

 
 main_dir = os.path.split(os.path.abspath(__file__))[0]
 
-pygame.init()
-print 'Using', surfarray.get_arraytype().capitalize()
-print 'Press the mouse button to advance image.'
-print 'Press the "s" key to save the current image.'
+def main():
 
-def surfdemo_show(array_img, name):
-    "displays a surface, waits for user to continue"
-    screen = pygame.display.set_mode(array_img.shape[:2], 0, 32)
-    surfarray.blit_array(screen, array_img)
-    pygame.display.flip()
-    pygame.display.set_caption(name)
-    while 1:
-        e = pygame.event.wait()
-        if e.type == MOUSEBUTTONDOWN: break
-        elif e.type == KEYDOWN and e.key == K_s:
-            #pygame.image.save(screen, name+'.bmp')
-            #s = pygame.Surface(screen.get_size(), 0, 32)
-            #s = s.convert_alpha()
-            #s.fill((0,0,0,255))
-            #s.blit(screen, (0,0))
-            #s.fill((222,0,0,50), (0,0,40,40))
-            #pygame.image.save_extended(s, name+'.png')
-            #pygame.image.save(s, name+'.png')
-            #pygame.image.save(screen, name+'_screen.png')
-            #pygame.image.save(s, name+'.tga')
-            pygame.image.save(screen, name+'.png')
-        elif e.type == QUIT:
-            raise SystemExit
+    pygame.init()
+    print 'Using', surfarray.get_arraytype().capitalize()
+    print 'Press the mouse button to advance image.'
+    print 'Press the "s" key to save the current image.'
 
-#allblack
-allblack = N.zeros((128, 128))
-surfdemo_show(allblack, 'allblack')
+    def surfdemo_show(array_img, name):
+        "displays a surface, waits for user to continue"
+        screen = pygame.display.set_mode(array_img.shape[:2], 0, 32)
+        surfarray.blit_array(screen, array_img)
+        pygame.display.flip()
+        pygame.display.set_caption(name)
+        while 1:
+            e = pygame.event.wait()
+            if e.type == MOUSEBUTTONDOWN: break
+            elif e.type == KEYDOWN and e.key == K_s:
+                #pygame.image.save(screen, name+'.bmp')
+                #s = pygame.Surface(screen.get_size(), 0, 32)
+                #s = s.convert_alpha()
+                #s.fill((0,0,0,255))
+                #s.blit(screen, (0,0))
+                #s.fill((222,0,0,50), (0,0,40,40))
+                #pygame.image.save_extended(s, name+'.png')
+                #pygame.image.save(s, name+'.png')
+                #pygame.image.save(screen, name+'_screen.png')
+                #pygame.image.save(s, name+'.tga')
+                pygame.image.save(screen, name+'.png')
+            elif e.type == QUIT:
+                raise SystemExit
 
+    #allblack
+    allblack = N.zeros((128, 128))
+    surfdemo_show(allblack, 'allblack')
 
-#striped
-striped = N.zeros((128, 128, 3))
-striped[:] = (255, 0, 0)
-striped[:,::3] = (0, 255, 255)
-surfdemo_show(striped, 'striped')
 
+    #striped
+    striped = N.zeros((128, 128, 3))
+    striped[:] = (255, 0, 0)
+    striped[:,::3] = (0, 255, 255)
+    surfdemo_show(striped, 'striped')
 
-#imgarray
-imagename = os.path.join(main_dir, 'data', 'arraydemo.bmp')
-imgsurface = pygame.image.load(imagename)
-imgarray = surfarray.array2d(imgsurface)
-surfdemo_show(imgarray, 'imgarray')
 
+    #imgarray
+    imagename = os.path.join(main_dir, 'data', 'arraydemo.bmp')
+    imgsurface = pygame.image.load(imagename)
+    imgarray = surfarray.array2d(imgsurface)
+    surfdemo_show(imgarray, 'imgarray')
 
-#flipped
-flipped = imgarray[:,::-1]
-surfdemo_show(flipped, 'flipped')
 
+    #flipped
+    flipped = imgarray[:,::-1]
+    surfdemo_show(flipped, 'flipped')
 
-#scaledown
-scaledown = imgarray[::2,::2]
-surfdemo_show(scaledown, 'scaledown')
 
+    #scaledown
+    scaledown = imgarray[::2,::2]
+    surfdemo_show(scaledown, 'scaledown')
 
-#scaleup
-size = N.array(imgarray.shape)*2
-scaleup = N.zeros(size)
-scaleup[::2,::2] = imgarray
-scaleup[1::2,::2] = imgarray
-scaleup[:,1::2] = scaleup[:,::2]
-surfdemo_show(scaleup, 'scaleup')
 
+    #scaleup
+    size = N.array(imgarray.shape)*2
+    scaleup = N.zeros(size)
+    scaleup[::2,::2] = imgarray
+    scaleup[1::2,::2] = imgarray
+    scaleup[:,1::2] = scaleup[:,::2]
+    surfdemo_show(scaleup, 'scaleup')
 
-#redimg
-rgbarray = surfarray.array3d(imgsurface)
-redimg = N.array(rgbarray)
-redimg[:,:,1:] = 0
-surfdemo_show(redimg, 'redimg')
 
+    #redimg
+    rgbarray = surfarray.array3d(imgsurface)
+    redimg = N.array(rgbarray)
+    redimg[:,:,1:] = 0
+    surfdemo_show(redimg, 'redimg')
 
-#soften
-soften = N.array(rgbarray)*1
-soften[1:,:]  += rgbarray[:-1,:]*8
-soften[:-1,:] += rgbarray[1:,:]*8
-soften[:,1:]  += rgbarray[:,:-1]*8
-soften[:,:-1] += rgbarray[:,1:]*8
-soften /= 33
-surfdemo_show(soften, 'soften')
 
+    #soften
+    soften = N.array(rgbarray)*1
+    soften[1:,:]  += rgbarray[:-1,:]*8
+    soften[:-1,:] += rgbarray[1:,:]*8
+    soften[:,1:]  += rgbarray[:,:-1]*8
+    soften[:,:-1] += rgbarray[:,1:]*8
+    soften /= 33
+    surfdemo_show(soften, 'soften')
 
-#crossfade (50%)
-src = N.array(rgbarray)
-dest = N.zeros(rgbarray.shape)
-dest[:] = 20, 50, 100
-diff = (dest - src) * 0.50
-if surfarray.get_arraytype() == 'numpy':
-    xfade = src + diff.astype(N.uint)
-else:
-    xfade = src + diff.astype(N.Int)
-surfdemo_show(xfade, 'xfade')
 
+    #crossfade (50%)
+    src = N.array(rgbarray)
+    dest = N.zeros(rgbarray.shape)
+    dest[:] = 20, 50, 100
+    diff = (dest - src) * 0.50
+    if surfarray.get_arraytype() == 'numpy':
+        xfade = src + diff.astype(N.uint)
+    else:
+        xfade = src + diff.astype(N.Int)
+    surfdemo_show(xfade, 'xfade')
 
 
-#alldone
+
+
+    #alldone
+    pygame.quit()
+
+
+if __name__ == '__main__': 
+    main()
+

examples/blend_fill.py

 #!/usr/bin/env python
-import sys, os
+import os
 import pygame
 from pygame.locals import *
 
     print "A - ADD, S- SUB, M- MULT, - MIN, + MAX"
     print "  to change the blend modes"
 
-color = [0, 0, 0]
-changed = False
-blendtype = 0
-step = 5
 
 main_dir = os.path.split(os.path.abspath(__file__))[0]
 data_dir = os.path.join(main_dir, 'data')
 
-pygame.init ()
-screen = pygame.display.set_mode ((640, 480), 0, 32)
-screen.fill ((100, 100, 100))
+def main():
+    color = [0, 0, 0]
+    changed = False
+    blendtype = 0
+    step = 5
 
-image = pygame.image.load (os.path.join (data_dir, "liquid.bmp")).convert()
-blendimage = pygame.image.load (os.path.join (data_dir, "liquid.bmp")).convert()
-screen.blit (image, (10, 10))
-screen.blit (blendimage, (200, 10))
+    pygame.init ()
+    screen = pygame.display.set_mode ((640, 480), 0, 32)
+    screen.fill ((100, 100, 100))
 
-pygame.display.flip ()
-pygame.key.set_repeat (500, 30)
-usage()
+    image = pygame.image.load (os.path.join (data_dir, "liquid.bmp")).convert()
+    blendimage = pygame.image.load (os.path.join (data_dir, "liquid.bmp")).convert()
+    screen.blit (image, (10, 10))
+    screen.blit (blendimage, (200, 10))
 
-while 1:
-    for event in pygame.event.get ():
-        if event.type == QUIT:
-            sys.exit()
+    pygame.display.flip ()
+    pygame.key.set_repeat (500, 30)
+    usage()
 
-        if event.type == KEYDOWN:
-            usage ()
+    going = True
+    while going:
+        for event in pygame.event.get ():
+            if event.type == QUIT:
+                going = False
 
-            if event.key == K_ESCAPE:
-                sys.exit ()
-            
-            if event.key == K_r:
-                color[0] += step
-                if color[0] > 255:
-                    color[0] = 0
-                changed = True
+            if event.type == KEYDOWN:
+                usage ()
 
-            elif event.key == K_g:
-                color[1] += step
-                if color[1] > 255:
-                    color[1] = 0
-                changed = True
+                if event.key == K_ESCAPE:
+                    going = False
 
-            elif event.key == K_b:
-                color[2] += step
-                if color[2] > 255:
-                    color[2] = 0
-                changed = True
+                if event.key == K_r:
+                    color[0] += step
+                    if color[0] > 255:
+                        color[0] = 0
+                    changed = True
 
-            elif event.key == K_a:
-                blendtype = BLEND_ADD
-                changed = True
-            elif event.key == K_s:
-                blendtype = BLEND_SUB
-                changed = True
-            elif event.key == K_m:
-                blendtype = BLEND_MULT
-                changed = True
-            elif event.key == K_PLUS:
-                blendtype = BLEND_MAX
-                changed = True
-            elif event.key == K_MINUS:
-                blendtype = BLEND_MIN
-                changed = True
+                elif event.key == K_g:
+                    color[1] += step
+                    if color[1] > 255:
+                        color[1] = 0
+                    changed = True
 
-            elif event.key in (K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_8, K_9):
-                step = int (event.unicode)
+                elif event.key == K_b:
+                    color[2] += step
+                    if color[2] > 255:
+                        color[2] = 0
+                    changed = True
 
-        if changed:
-            screen.fill ((100, 100, 100))
-            screen.blit (image, (10, 10))
-            blendimage.blit (image, (0, 0))
-            #blendimage.fill (color, (0, 0, 20, 20), blendtype)
-            blendimage.fill (color, None, blendtype)
-            screen.blit (blendimage, (200, 10))
-            print "Color: %s, Pixel (0,0): %s" % (tuple(color),
-                                                  [blendimage.get_at ((0, 0))])
-            changed = False
-            pygame.display.flip ()
+                elif event.key == K_a:
+                    blendtype = BLEND_ADD
+                    changed = True
+                elif event.key == K_s:
+                    blendtype = BLEND_SUB
+                    changed = True
+                elif event.key == K_m:
+                    blendtype = BLEND_MULT
+                    changed = True
+                elif event.key == K_PLUS:
+                    blendtype = BLEND_MAX
+                    changed = True
+                elif event.key == K_MINUS:
+                    blendtype = BLEND_MIN
+                    changed = True
+
+                elif event.key in (K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_8, K_9):
+                    step = int (event.unicode)
+
+            if changed:
+                screen.fill ((100, 100, 100))
+                screen.blit (image, (10, 10))
+                blendimage.blit (image, (0, 0))
+                #blendimage.fill (color, (0, 0, 20, 20), blendtype)
+                blendimage.fill (color, None, blendtype)
+                screen.blit (blendimage, (200, 10))
+                print "Color: %s, Pixel (0,0): %s" % (tuple(color),
+                                                      [blendimage.get_at ((0, 0))])
+                changed = False
+                pygame.display.flip ()
+
+
+    pygame.quit()
+
+
+if __name__ == '__main__': 
+    main()

examples/blit_blends.py

     pygame.display.flip()
     clock = pygame.time.Clock()
     print "one pixel is:%s:" % [im1.get_at((0,0))]
-    
-    while 1:
+
+    going = True
+    while going:
         clock.tick(60)
 
         for event in pygame.event.get():
             if event.type == QUIT:
-                return
+                going = False
             if event.type == KEYDOWN:
                 usage()
 
             if event.type == KEYDOWN and event.key == K_ESCAPE:
-                return
+                going = False
 
             elif event.type == KEYDOWN and event.key in images.keys():
                 img_to_blit = images[event.key]
         screen.blit(im1, (0, 0))
         pygame.display.flip()
 
+    pygame.quit()
+
 def usage():
     print "press keys 1-5 to change image to blit."
     print "A - ADD, S- SUB, M- MULT, - MIN, + MAX"
 import pygame.camera
 from pygame.locals import *
 
-pygame.init()
 
 class VideoCapturePlayer(object):
 
    def __init__(self, **argd):
        self.__dict__.update(**argd)
        super(VideoCapturePlayer, self).__init__(**argd)
-       
+
        # create a display surface. standard pygame stuff
        self.display = pygame.display.set_mode( self.size, 0 )
-       
+
        # gets a list of available cameras.
        self.clist = pygame.camera.list_cameras()
-       
+
        # creates the camera of the specified size and in RGB colorspace
        self.camera = pygame.camera.Camera(self.clist[0], self.size, "RGB")
-       
+
        # starts the camera
        self.camera.start()
-       
+
        self.clock = pygame.time.Clock()
-       
+
        # create a surface to capture to.  for performance purposes, you want the
        # bit depth to be the same as that of the display surface.
        self.snapshot = pygame.surface.Surface(self.size, 0, self.display)
        if self.camera.query_image():
            # capture an image
            self.snapshot = self.camera.get_image(self.snapshot)
-           
+
        # blit it to the display surface.  simple!
        self.display.blit(self.snapshot, (0,0))
        pygame.display.flip()
            self.clock.tick()
 #           print self.clock.get_fps()
 
-VideoCapturePlayer().main()
+def main():
+    pygame.init()
+    VideoCapturePlayer().main()
+    pygame.quit()
+
+if __name__ == '__main__':
+    main()
 
 
 #this calls the 'main' function when this script is executed
-if __name__ == '__main__': main()
+if __name__ == '__main__':
+    main()
     pygame.display.update()
     for cursor in [no, arrow]:
         TestCursor(cursor)
-        quit = 0
-        while not quit:
+        going = True
+        while going:
             pygame.event.pump()
             for e in pygame.event.get():
                 if e.type == pygame.MOUSEBUTTONDOWN:
-                    quit = 1
+                    going = False
     pygame.quit()
 
-main()
 
+if __name__ == '__main__':
+    main()
+

examples/eventlist.py

         img = Font.render('No Joysticks to Initialize', 1, (50, 200, 50), (0, 0, 0))
         history.append(img)
 
-    while 1:
+    going = True
+    while going:
         for e in event.get():
             if e.type == QUIT:
-                return
+                going = False
             if e.type == KEYDOWN:
                 if e.key == K_ESCAPE:
-                    return
+                    going = False
                 else:
                     global LastKey
                     LastKey = e.key
         display.flip()
         time.wait(10)
 
+    pygame.quit()
 
 
-if __name__ == '__main__': main()
+if __name__ == '__main__':
+    main()

examples/fastevents.py

 
 However... on my debian duron 850 machine fastevents is faster.
 """
+
+import pygame
 from pygame import *
 
 # the config to try different settings out with the event queues.
                 except:
                     pytime.sleep(0.001)
                     try_post = 1
-                
+
             if self.stop:
                 return
         self.done.append(1)
-        
-        
+
+
 
 import time as pytime
 
 def main():
     init()
-    
+
     if use_fast_events:
         fastevent.init()
 
     t1 = pytime.time()
     poster.start()
 
-
-    while 1:
+    going = True
+    while going:
 #        for e in event.get():
         #for x in range(200):
         #    ee = event.Event(USEREVENT)
         #    r = event_module.post(ee)
         #    print r
-        
+
         #for e in event_module.get():
         event_list = []
         event_list = event_module.get()
             if e.type == QUIT:
                 print c.get_fps()
                 poster.stop.append(1)
-                return
+                going = False
             if e.type == KEYDOWN:
                 if e.key == K_ESCAPE:
                     print c.get_fps()
                     poster.stop.append(1)
-                    return
+                    going = False
         if poster.done:
             print c.get_fps()
             print c
             t2 = pytime.time()
             print "total time:%s" % (t2 - t1)
             print "events/second:%s" % (NUM_EVENTS_TO_POST / (t2 - t1))
-            return
+            going = False
         if with_display:
             display.flip()
         if slow_tick:
             c.tick(40)
-        
-        
 
 
+    pygame.quit()
 
 
-if __name__ == '__main__': main()
+
+if __name__ == '__main__':
+    main()