Lenard Lindstrom avatar Lenard Lindstrom committed a6e94e9

examples can be run from outside the examples directory

Comments (0)

Files changed (15)

 # BUG    = fixed a bug that was (or could have been) crashing
 #
 #
+[SVN 1820] Jan 11, 2009
+    The Pygame documents and examples are now part of the Pygame package,
+    directories docs and examples respectively.
 
 [SVN 1812:1814] Jan 09, 2009
     Pygame unit test suite is now the pygame.tests package. For Pygame

examples/aliens.py

 SCREENRECT     = Rect(0, 0, 640, 480)
 SCORE          = 0
 
+main_dir = os.path.split(os.path.abspath(__file__))[0]
 
 def load_image(file):
     "loads an image, prepares it for play"
-    file = os.path.join('data', file)
+    file = os.path.join(main_dir, 'data', file)
     try:
         surface = pygame.image.load(file)
     except pygame.error:
 
 def load_sound(file):
     if not pygame.mixer: return dummysound()
-    file = os.path.join('data', file)
+    file = os.path.join(main_dir, 'data', file)
     try:
         sound = pygame.mixer.Sound(file)
         return sound
     boom_sound = load_sound('boom.wav')
     shoot_sound = load_sound('car_door.wav')
     if pygame.mixer:
-        music = os.path.join('data', 'house_lo.wav')
+        music = os.path.join(main_dir, 'data', 'house_lo.wav')
         pygame.mixer.music.load(music)
         pygame.mixer.music.play(-1)
 

examples/arraydemo.py

 import os
 try:
     import pygame
-    # Use numeric, not numpy here (otherwise, follow the NUMPY comments)
-    # NUMPY: import numpy as N
-    import Numeric as N
+    from pygame import surfarray
+    if 'numpy' in surfarray.get_arraytypes():
+        surfarray.use_arraytype('numpy')
+        import numpy as N
+    else:
+        import Numeric as N
+        surfarray.use_arraytype('numeric')
     from pygame.locals import *
-    surfarray = pygame.surfarray
-    if not surfarray: raise ImportError
 except ImportError:
     raise ImportError, 'Error Importing Pygame/surfarray or Numeric'
 
+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.'
 
-# Guarantee the usage of Numeric
-pygame.surfarray.use_arraytype ("numeric")
-# NUMPY: pygame.surfarray.use_arraytype ("numpy")
-
 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)
 
 
 #imgarray
-imagename = os.path.join('data', 'arraydemo.bmp')
+imagename = os.path.join(main_dir, 'data', 'arraydemo.bmp')
 imgsurface = pygame.image.load(imagename)
 imgarray = surfarray.array2d(imgsurface)
 surfdemo_show(imgarray, 'imgarray')
 dest = N.zeros(rgbarray.shape)
 dest[:] = 20, 50, 100
 diff = (dest - src) * 0.50
-# NUMPY: xfade = src + diff.astype(N.uint)
-xfade = src + diff.astype(N.Int)
+if surfarray.get_arraytype() == 'numpy':
+    xfade = src + diff.astype(N.uint)
+else:
+    xfade = src + diff.astype(N.Int)
 surfdemo_show(xfade, 'xfade')
 
 

examples/blend_fill.py

 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))
 
-image = pygame.image.load (os.path.join ("data", "liquid.bmp")).convert()
-blendimage = pygame.image.load (os.path.join ("data", "liquid.bmp")).convert()
+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))
 

examples/blit_blends.py

 
 import time
         
+main_dir = os.path.split(os.path.abspath(__file__))[0]
+data_dir = os.path.join(main_dir, 'data')
 
 def main():
     pygame.init()
 
     images = {}
     images[K_1] = im2
-    images[K_2] = pygame.image.load(os.path.join("data", "chimp.bmp"))
-    images[K_3] = pygame.image.load(os.path.join("data", "alien3.gif"))
-    images[K_4] = pygame.image.load(os.path.join("data", "liquid.bmp"))
+    images[K_2] = pygame.image.load(os.path.join(data_dir, "chimp.bmp"))
+    images[K_3] = pygame.image.load(os.path.join(data_dir, "alien3.gif"))
+    images[K_4] = pygame.image.load(os.path.join(data_dir, "liquid.bmp"))
     img_to_blit = im2.convert()
     iaa = img_to_blit.convert_alpha()
 

examples/chimp.py

 if not pygame.font: print 'Warning, fonts disabled'
 if not pygame.mixer: print 'Warning, sound disabled'
 
+main_dir = os.path.split(os.path.abspath(__file__))[0]
+data_dir = os.path.join(main_dir, 'data')
 
 #functions to create our resources
 def load_image(name, colorkey=None):
-    fullname = os.path.join('data', name)
+    fullname = os.path.join(data_dir, name)
     try:
         image = pygame.image.load(fullname)
     except pygame.error, message:
         def play(self): pass
     if not pygame.mixer or not pygame.mixer.get_init():
         return NoneSound()
-    fullname = os.path.join('data', name)
+    fullname = os.path.join(data_dir, name)
     try:
         sound = pygame.mixer.Sound(fullname)
     except pygame.error, message:

examples/liquid.py

 from pygame.locals import *
 from math import sin
 
+main_dir = os.path.split(os.path.abspath(__file__))[0]
+
 def main():
     #initialize and setup screen
     pygame.init()
     screen = pygame.display.set_mode((640, 480), HWSURFACE|DOUBLEBUF)
 
     #load image and quadruple
-    imagename = os.path.join('data', 'liquid.bmp')
+    imagename = os.path.join(main_dir, 'data', 'liquid.bmp')
     bitmap = pygame.image.load(imagename)
     bitmap = pygame.transform.scale2x(bitmap)
     bitmap = pygame.transform.scale2x(bitmap)

examples/moveit.py

 import os, pygame
 from pygame.locals import *
 
+main_dir = os.path.split(os.path.abspath(__file__))[0]
+
 #our game object class
 class GameObject:
     def __init__(self, image, height, speed):
 
 #quick function to load an image
 def load_image(name):
-    path = os.path.join('data', name)
+    path = os.path.join(main_dir, 'data', name)
     return pygame.image.load(path).convert()
 
 

examples/oldalien.py

 dirtyrects = [] # list of update_rects
 next_tick = 0   # used for timing
 class Img: pass # container for images
-
+main_dir = os.path.split(os.path.abspath(__file__))[0]  # Program's diretory
 
 
 #first, we define some utility functions
     
 def load_image(file, transparent):
     "loads an image, prepares it for play"
-    file = os.path.join('data', file)
+    file = os.path.join(main_dir, 'data', file)
     try:
         surface = pygame.image.load(file)
     except pygame.error:
 #if python says run, let's run!
 if __name__ == '__main__':
     main()
-    
+    

examples/pixelarray.py

 #!/usr/bin/env python
 import os, pygame
 
+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 ((255, 255))
 show (surface)
 
 # Manipulate the image. Flip it around the y axis.
-surface = pygame.image.load (os.path.join ('data', 'arraydemo.bmp'))
+surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
 ar = pygame.PixelArray (surface)
 ar[:] = ar[:,::-1]
 del ar
 show (surface)
 
 # Scale it by throwing each second pixel away.
-surface = pygame.image.load (os.path.join ('data', 'arraydemo.bmp'))
+surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
 ar = pygame.PixelArray (surface)
 sf2 = ar[::2,::2].make_surface ()
 del ar
 show (surface)
 
 # Extract anything which might be somewhat black.
-surface = pygame.image.load (os.path.join ('data', 'arraydemo.bmp'))
+surface = pygame.image.load (os.path.join (data_dir, 'arraydemo.bmp'))
 ar = pygame.PixelArray (surface)
 ar2 = ar.extract ((0, 0, 0), 0.07)
 sf2 = ar2.surface
 show (sf2)
 
 # Compare two images.
-surface = pygame.image.load (os.path.join ('data', 'alien1.gif'))
-surface2 = pygame.image.load (os.path.join ('data', 'alien2.gif'))
+surface = pygame.image.load (os.path.join (data_dir, 'alien1.gif'))
+surface2 = pygame.image.load (os.path.join (data_dir, 'alien2.gif'))
 ar1 = pygame.PixelArray (surface)
 ar2 = pygame.PixelArray (surface2)
 ar3 = ar1.compare (ar2, 0.07)

examples/scrap_clipboard.py

 """
 Demonstrates the clipboard capabilities of pygame.
 """
+import os
+
 import pygame
 from pygame.locals import *
 import pygame.scrap as scrap
     print "Press the 'a' key to get a list of the currently available types"
     print "Press the 'i' key to put an image into the clipboard"
 
+main_dir = os.path.split(os.path.abspath(__file__))[0]
 
 pygame.init ()
 screen = pygame.display.set_mode ((200, 200))
         elif e.type == KEYDOWN and e.key == K_i:
             print "Putting image into the clipboard."
             scrap.set_mode (SCRAP_CLIPBOARD)
-            fp = open ("data/liquid.bmp", "rb")
+            fp = open (os.path.join(main_dir, 'data', 'liquid.bmp'), 'rb')
             buf = fp.read ()
             scrap.put ("image/bmp", buf)
             fp.close ()

examples/sound.py

 mixer = pygame.mixer
 time = pygame.time
 
+main_dir = os.path.split(os.path.abspath(__file__))[0]
+
 #choose a desired audio format
 mixer.init(11025) #raises exception on fail
 
 if len(sys.argv) > 1 and "wav" in sys.argv[1]:
     file = sys.argv[1]
 else:
-    file = os.path.join('data', 'secosmic_lo.wav')
+    file = os.path.join(main_dir, 'data', 'secosmic_lo.wav')
 sound = mixer.Sound(file)
 
 

examples/testsprite.py

     except Exception:
         print "No psyco for you!  psyco failed to import and run."
 
+main_dir = os.path.split(os.path.abspath(__file__))[0]
+data_dir = os.path.join(main_dir, 'data')
 
 # use this to use update rects or not.
 #  If the screen is mostly full, then update rects are not useful.
 
     screen.fill([0,0,0])
     pygame.display.flip()
-    sprite_surface = pygame.image.load(os.path.join("data", "asprite.bmp"))
-    sprite_surface2 = pygame.image.load(os.path.join("data", "static.png"))
+    sprite_surface = pygame.image.load(os.path.join(data_dir, "asprite.bmp"))
+    sprite_surface2 = pygame.image.load(os.path.join(data_dir, "static.png"))
 
     if use_rle:
         sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY|RLEACCEL)

test/bufferproxy_test.py

 if __name__ == '__main__':
     import sys
     import os
-    parent_dir = os.path.split(os.path.split(os.path.abspath(__file__))[0])[0]
-    sys.path.insert(0, parent_dir)
+    pkg_dir = os.path.split(os.path.abspath(__file__))[0]
+    parent_dir, pkg_name = os.path.split(pkg_dir)
+    is_pygame_pkg = (pkg_name == 'tests' and
+                     os.path.split(parent_dir)[1] == 'pygame')
+    if not is_pygame_pkg:
+        sys.path.insert(0, parent_dir)
+else:
+    is_pygame_pkg = __name__.startswith('pygame.tests.')
 
-try:
+if is_pygame_pkg:
+    from pygame.tests.test_utils import test_not_implemented, unittest
+else:
     from test.test_utils import test_not_implemented, unittest
-except:
-    from pygame.tests.test_utils import test_not_implemented, unittest
 import pygame
 
 ################################################################################

test/test_utils/run_tests.py

     # false failures in subprocess mode. Same issue as python2.6. Needs some
     # env vars.
 
-    test_env = os.environ.copy()
-    if is_pygame_pkg:
-        package_dir = None
-    else:
-        package_dir = main_dir
-    python_path_dirs = [test_subdir, package_dir, test_env.get("PYTHONPATH")]
-    test_env["PYTHONPATH"] = os.pathsep.join (
-        [pth for pth in python_path_dirs if pth]
-    )
-
-    os.chdir(working_dir)  # Is this needed when absolute paths are used?
+    test_env = os.environ
 
     fmt1 = '%s.%%s' % test_mods_pkg_name
     fmt2 = '%s.%%s_test' % test_mods_pkg_name
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.