Commits

Anonymous committed 5696b91

new importing

Comments (0)

Files changed (14)

 # BREAK = change breaks existing code
 # BUG	= fixed a bug that was (or could have been) crashing
 
+January 5, 2001
+        new and upgraded importing [BREAK?]
+        fixed numberhandling in set_alpha and set_colorkey
+
 January 2, 2001
 	Group add/remove methods work
 

examples/aliens.py

 import random, os.path, sys
 
 #import basic pygame modules
-import pygame, pygame.image, pygame.transform, pygame.sprite
+import pygame
 from pygame.locals import *
 
 #see if we can load more than standard BMP
     raise SystemExit, "Sorry, extended image module required"
 
 
-#try importing pygame optional modules
-try:
-    import pygame.mixer
-except ImportError:
-    print 'Warning, no sound'
-    pygame.mixer = None
-
-
 #game constants
 MAX_SHOTS      = 2      #most player bullets onscreen
 ALIEN_ODDS     = 29     #chances a new alien appears
     Player.images = [img, pygame.transform.flip(img, 1, 0)]
     img = load_image('explosion1.gif')
     Explosion.images = [img, pygame.transform.flip(img, 1, 1)]
-    Alien.images = [load_images('alien1.gif', 'alien2.gif', 'alien3.gif')]
+    Alien.images = load_images('alien1.gif', 'alien2.gif', 'alien3.gif')
     Bomb.images = [load_image('bomb.gif')]
     Shot.images = [load_image('shot.gif')]
 

examples/arraydemo.py

 import os
 try:
     import pygame
-    import pygame.image
     import Numeric as N
-    import pygame.surfarray as surfarray
     from pygame.locals import *
+    surfarray = pygame.surfarray
+    if not surfarray: raise ImportError
 except ImportError:
-    raise ImportError, 'Error Importing Pygame/surfarray/image or Numeric'
+    raise ImportError, 'Error Importing Pygame/surfarray or Numeric'
 
 
 pygame.init()
 print 'Press the "s" key to save the current image.'
 
 
-
 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)

examples/chimp.py

 
 #Import Modules
 import os, sys
-import pygame, pygame.sprite, pygame.transform, pygame.image
+import pygame
 from pygame.locals import *
-try:
-    import pygame.font
-except ImportError:
-    print 'Warning, fonts disabled'
-    pygame.font = None
-try:
-    import pygame.mixer
-except ImportError:
-    print 'Warning, sound disabled'
-    pygame.mixer = None
+
+if not pygame.font: print 'Warning, fonts disabled'
+if not pygame.mixer: print 'Warning, sound disabled'
 
 
 #functions to create our resources

examples/fonty.py

 font management."""
 
 
-import pygame, pygame.font, os.path
-import pygame.cursors
+import pygame
 from pygame.locals import *
 
 

examples/glcube.py

 
         drawcube()
         pygame.display.flip()
-        pygame.time.delay(10)
+        pygame.time.wait(10)
 
 
 if __name__ == '__main__': main()

examples/liquid.py

 pygame to compare the results. I didn't bother porting the text and
 sound stuff, that's an easy enough challenge for the reader :]"""
 
-import os
-import pygame, pygame.image
+import pygame, os
 from pygame.locals import *
 from math import sin
 

examples/moveit.py

 
 
 #import everything
-import os, pygame, pygame.image
+import os, pygame
 from pygame.locals import *
 
 #our game object class

examples/oldalien.py

 
 
 #import
-import whrandom, os.path, sys
-import pygame, pygame.image
+import random, os.path, sys
+import pygame
 from pygame.locals import *
 
 if not pygame.image.get_extended():
 
 
 #constants
-FRAMES_PER_SEC = 50
-PLAYER_SPEED   = 5
+FRAMES_PER_SEC = 40
+PLAYER_SPEED   = 12
 MAX_SHOTS      = 2
-SHOT_SPEED     = 6
-ALIEN_SPEED    = 6
+SHOT_SPEED     = 10
+ALIEN_SPEED    = 12
 ALIEN_ODDS     = 45
 EXPLODE_TIME   = 6
 SCREENRECT     = Rect(0, 0, 640, 480)
 
 
 
-def wait_frame():
-    "wait for the correct fps time to expire"
-    global next_tick
-    this_tick = pygame.time.get_ticks()
-    if this_tick < next_tick:
-        pygame.time.delay(next_tick - this_tick)
-    next_tick = this_tick + (1000/FRAMES_PER_SEC)
-
-
-
 # The logic for all the different sprite types
 
 class Actor:
     "Destroy him or suffer"
     def __init__(self):
         Actor.__init__(self, Img.alien)
-        self.facing = whrandom.choice((-1,1)) * ALIEN_SPEED
+        self.facing = random.choice((-1,1)) * ALIEN_SPEED
         if self.facing < 0:
             self.rect.right = SCREENRECT.right
             
     # Initialize SDL components
     pygame.init()
     screen = pygame.display.set_mode(SCREENRECT.size, 0)
+    clock = pygame.time.Clock()
 
     # Load the Resources
     Img.background = load_image('background.gif', 0)
 
     # Main loop
     while player.alive or explosions:
-        wait_frame()
+        clock.tick(FRAMES_PER_SEC)
 
         # Gather Events
         pygame.event.pump()
         player.reloading = keystate[K_SPACE]
 
         # Create new alien
-        if not int(whrandom.random() * ALIEN_ODDS):
+        if not int(random.random() * ALIEN_ODDS):
             aliens.append(Alien())
 
         # Detect collisions
         pygame.display.update(dirtyrects)
         dirtyrects = []
 
-    pygame.time.delay(100)
+    pygame.time.wait(50)
     
 
 #if python says run, let's run!

examples/sound.py

 time = pygame.time
 
 #choose a desired audio format
-mixer.init(11025)
-if not mixer.get_init():
-    raise SystemExit, 'Cannot Initialize Mixer'
+mixer.init(11025) #raises exception on fail
 
 
 #load the sound    
 #poll until finished
 while channel.get_busy(): #still playing
     print '  ...still going...'
-    time.delay(1000)
+    time.wait(1000)
 print '...Finished'
 
 

examples/vgrade.py

 try:
     from Numeric import *
     from RandomArray import *
-    import pygame.surfarray
 except ImportError:
     raise SystemExit, 'This example requires Numeric and the pygame surfarray module'
 
 # main pygame source
 # lets get things coordinated
 
-from pygame.version import ver
+def import_all_pygame():
+    def makemodules(**mods): return mods
+    modules = makemodules(
+        base=2, cdrom=0, constants=2, cursors=0, display=0,
+        draw=0, event=0, font=0, image=0, joystick=0,
+        key=0, mixer=0, mouse=0, movie=0, rect=1, sprite=0,
+        surface=0, time=0, transform=0, surfarray=0
+    )
+    for mod, required in modules.items():
+        try:
+            module = __import__('pygame.'+mod, None, None, ['pygame.'+mod])
+            globals()[mod] = module
+            if required == 2:
+                for item in dir(module):
+                    if item[0] != '_':
+                        globals()[item] = getattr(module, item)
+        except ImportError:
+            if required:
+                CannotImportPygame = ImportError
+                MissingModule =  "Cannot Import 'pygame.%s'"%mod
+                raise CannotImportPygame, MissingModule
+            globals()[mod] = None
 
-from pygame.base import *
-from pygame.surface import *
-from pygame.rect import *
-from pygame.locals import *
-import pygame.cdrom
-import pygame.display
-import pygame.event
-import pygame.key
-import pygame.mouse
-import pygame.time
-import pygame.joystick
+import_all_pygame()
+del import_all_pygame
 
+Surface = getattr(surface, 'Surface', lambda:Missing_Pygame_Function)
+Rect = getattr(rect, 'Rect', lambda:Missing_Pygame_Function)
+
+
 
 
 /* older python compatability */
-
 #if PYTHON_API_VERSION < 1009
 #define PyObject_DEL(op)		free(op)
 #define PyMem_New(type, n)  	((type*)PyMem_Malloc((n) * sizeof(type)))
 {
 	SDL_Surface* surf = PySurface_AsSurface(self);
 	Uint32 flags = 0, color = 0;
-	PyObject* rgba_obj = NULL;
+	PyObject* rgba_obj = NULL, *intobj = NULL;
 	Uint8 rgba[4];
 	int result, hascolor=0;
 
 
 	if(rgba_obj && rgba_obj!=Py_None)
 	{
-		if(PyInt_Check(rgba_obj))
-			color = (Uint32)PyInt_AsLong(rgba_obj);
+		if(PyNumber_Check(rgba_obj) && (intobj=PyNumber_Int(rgba_obj)))
+                {
+			color = (Uint32)PyInt_AsLong(intobj);
+                        Py_DECREF(intobj);
+                }
 		else if(RGBAFromObj(rgba_obj, rgba))
 			color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
 		else
 {
 	SDL_Surface* surf = PySurface_AsSurface(self);
 	Uint32 flags = 0;
-	PyObject* alpha_obj = NULL;
+	PyObject* alpha_obj = NULL, *intobj=NULL;
 	Uint8 alpha;
 	int result, alphaval=0, hasalpha=0;
 
 
 	if(alpha_obj && alpha_obj!=Py_None)
 	{
-		if(PyInt_Check(alpha_obj))
-			alphaval = (int)PyInt_AsLong(alpha_obj);
-		else
-			return RAISE(PyExc_TypeError, "invalid alpha argument");
-		hasalpha = 1;
+		if(PyNumber_Check(alpha_obj) && (intobj=PyNumber_Int(alpha_obj)))
+                {
+                        alphaval = (int)PyInt_AsLong(intobj);
+                        Py_DECREF(intobj);
+                }
+                else
+                        return RAISE(PyExc_TypeError, "invalid alpha argument");
+                hasalpha = 1;
 	}
 	if(hasalpha)
 		flags |= SDL_SRCALPHA;