Package pygame
[hide private]
[frames] | no frames]

Source Code for Package pygame

  1  ##    pygame - Python Game Library 
  2  ##    Copyright (C) 2000-2001  Pete Shinners 
  3  ## 
  4  ##    This library is free software; you can redistribute it and/or 
  5  ##    modify it under the terms of the GNU Library General Public 
  6  ##    License as published by the Free Software Foundation; either 
  7  ##    version 2 of the License, or (at your option) any later version. 
  8  ## 
  9  ##    This library is distributed in the hope that it will be useful, 
 10  ##    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  ##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 12  ##    Library General Public License for more details. 
 13  ## 
 14  ##    You should have received a copy of the GNU Library General Public 
 15  ##    License along with this library; if not, write to the Free 
 16  ##    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 17  ## 
 18  ##    Pete Shinners 
 19  ##    pete@shinners.org 
 20  """Pygame is a set of Python modules designed for writing games. 
 21  It is written on top of the excellent SDL library. This allows you 
 22  to create fully featured games and multimedia programs in the python 
 23  language. The package is highly portable, with games running on 
 24  Windows, MacOS, OS X, BeOS, FreeBSD, IRIX, and Linux. 
 25  """ 
 26   
 27  import sys, os, string 
 28   
 29  # check if is old windows... if so use directx video driver by default. 
 30  # if someone sets this respect their setting... 
 31  if not 'SDL_VIDEODRIVER' in os.environ: 
 32      # http://docs.python.org/lib/module-sys.html 
 33      # 0 (VER_PLATFORM_WIN32s)   Win32s on Windows 3.1 
 34      # 1 (VER_PLATFORM_WIN32_WINDOWS)    Windows 95/98/ME 
 35      # 2 (VER_PLATFORM_WIN32_NT)         Windows NT/2000/XP 
 36      # 3 (VER_PLATFORM_WIN32_CE)         Windows CE 
 37      if hasattr(sys, "getwindowsversion"): 
 38          try: 
 39              if (sys.getwindowsversion()[3] in [1,2] and 
 40                  sys.getwindowsversion()[0] in [0,1,2,3,4,5]): 
 41                  os.environ['SDL_VIDEODRIVER'] = 'directx' 
 42          except: 
 43              pass 
 44   
 45   
46 -class MissingModule:
47 _NOT_IMPLEMENTED_ = True
48 - def __init__(self, name, info='', urgent=0):
49 self.name = name 50 self.info = str(info) 51 try: 52 exc = sys.exc_info() 53 if exc[0] != None: 54 self.reason = "%s: %s" % (exc[0].__name__, str(exc[1])) 55 else: 56 self.reason = "" 57 finally: 58 del exc 59 self.urgent = urgent 60 if urgent: 61 self.warn()
62
63 - def __getattr__(self, var):
64 if not self.urgent: 65 self.warn() 66 self.urgent = 1 67 MissingPygameModule = "%s module not available" % self.name 68 if self.reason: 69 MissingPygameModule += "\n(%s)" % self.reason 70 raise NotImplementedError(MissingPygameModule)
71
72 - def __nonzero__(self):
73 return 0
74
75 - def warn(self):
76 if self.urgent: type = 'import' 77 else: type = 'use' 78 message = '%s %s: %s' % (type, self.name, self.info) 79 if self.reason: 80 message += "\n(%s)" % self.reason 81 try: 82 import warnings 83 if self.urgent: level = 4 84 else: level = 3 85 warnings.warn(message, RuntimeWarning, level) 86 except ImportError: 87 print (message)
88 89 90 91 #we need to import like this, each at a time. the cleanest way to import 92 #our modules is with the import command (not the __import__ function) 93 94 #first, the "required" modules 95 from pygame.base import * 96 from pygame.constants import * 97 from pygame.version import * 98 from pygame.rect import Rect 99 from pygame.compat import geterror 100 import pygame.rwobject 101 import pygame.surflock 102 import pygame.color 103 Color = color.Color 104 __version__ = ver 105 106 #next, the "standard" modules 107 #we still allow them to be missing for stripped down pygame distributions 108 try: import pygame.cdrom 109 except (ImportError,IOError):cdrom=MissingModule("cdrom", geterror(), 1) 110 111 try: import pygame.cursors 112 except (ImportError,IOError):cursors=MissingModule("cursors", geterror(), 1) 113 114 try: import pygame.display 115 except (ImportError,IOError):display=MissingModule("display", geterror(), 1) 116 117 try: import pygame.draw 118 except (ImportError,IOError):draw=MissingModule("draw", geterror(), 1) 119 120 try: import pygame.event 121 except (ImportError,IOError):event=MissingModule("event", geterror(), 1) 122 123 try: import pygame.image 124 except (ImportError,IOError):image=MissingModule("image", geterror(), 1) 125 126 try: import pygame.joystick 127 except (ImportError,IOError):joystick=MissingModule("joystick", geterror(), 1) 128 129 try: import pygame.key 130 except (ImportError,IOError):key=MissingModule("key", geterror(), 1) 131 132 try: import pygame.mouse 133 except (ImportError,IOError):mouse=MissingModule("mouse", geterror(), 1) 134 135 try: import pygame.sprite 136 except (ImportError,IOError):sprite=MissingModule("sprite", geterror(), 1) 137 138 139 try: import pygame.threads 140 except (ImportError,IOError):threads=MissingModule("threads", geterror(), 1) 141 142
143 -def warn_unwanted_files():
144 """ Used to warn about unneeded old files. 145 """ 146 147 # a temporary hack to warn about camera.so and camera.pyd. 148 install_path= os.path.split(pygame.base.__file__)[0] 149 extension_ext = os.path.splitext(pygame.base.__file__)[1] 150 151 # here are the .so/.pyd files we need to ask to remove. 152 ext_to_remove = ["camera"] 153 154 # here are the .py/.pyo/.pyc files we need to ask to remove. 155 py_to_remove = ["color"] 156 157 if os.name == "e32": # Don't warn on Symbian. The color.py is used as a wrapper. 158 py_to_remove = [] 159 160 # See if any of the files are there. 161 extension_files = ["%s%s" % (x, extension_ext) for x in ext_to_remove] 162 163 py_files = ["%s%s" % (x, py_ext) 164 for py_ext in [".py", ".pyc", ".pyo"] 165 for x in py_to_remove] 166 167 files = py_files + extension_files 168 169 unwanted_files = [] 170 for f in files: 171 unwanted_files.append( os.path.join( install_path, f ) ) 172 173 174 175 ask_remove = [] 176 for f in unwanted_files: 177 if os.path.exists(f): 178 ask_remove.append(f) 179 180 181 if ask_remove: 182 message = "Detected old file(s). Please remove the old files:\n" 183 184 for f in ask_remove: 185 message += "%s " % f 186 message += "\nLeaving them there might break pygame. Cheers!\n\n" 187 188 try: 189 import warnings 190 level = 4 191 warnings.warn(message, RuntimeWarning, level) 192 except ImportError: 193 print (message)
194 195 196 # disable, because we hopefully don't need it. 197 #warn_unwanted_files() 198 199 200 201 try: from pygame.surface import * 202 except (ImportError,IOError):Surface = lambda:Missing_Function 203 204 205 try: 206 import pygame.mask 207 from pygame.mask import Mask 208 except (ImportError,IOError):Mask = lambda:Missing_Function 209 210 try: from pygame.pixelarray import * 211 except (ImportError,IOError): PixelArray = lambda:Missing_Function 212 213 try: from pygame.overlay import * 214 except (ImportError,IOError):Overlay = lambda:Missing_Function 215 216 try: import pygame.time 217 except (ImportError,IOError):time=MissingModule("time", geterror(), 1) 218 219 try: import pygame.transform 220 except (ImportError,IOError):transform=MissingModule("transform", geterror(), 1) 221 222 #lastly, the "optional" pygame modules 223 try: 224 import pygame.font 225 import pygame.sysfont 226 pygame.font.SysFont = pygame.sysfont.SysFont 227 pygame.font.get_fonts = pygame.sysfont.get_fonts 228 pygame.font.match_font = pygame.sysfont.match_font 229 except (ImportError,IOError):font=MissingModule("font", geterror(), 0) 230 231 # try and load pygame.mixer_music before mixer, for py2app... 232 try: 233 import pygame.mixer_music 234 #del pygame.mixer_music 235 #print ("NOTE2: failed importing pygame.mixer_music in lib/__init__.py") 236 except (ImportError,IOError): 237 pass 238 239 try: import pygame.mixer 240 except (ImportError,IOError):mixer=MissingModule("mixer", geterror(), 0) 241 242 try: import pygame.movie 243 except (ImportError,IOError):movie=MissingModule("movie", geterror(), 0) 244 245 #try: import pygame.movieext 246 #except (ImportError,IOError):movieext=MissingModule("movieext", geterror(), 0) 247 248 try: import pygame.scrap 249 except (ImportError,IOError):scrap=MissingModule("scrap", geterror(), 0) 250 251 try: import pygame.surfarray 252 except (ImportError,IOError):surfarray=MissingModule("surfarray", geterror(), 0) 253 254 try: import pygame.sndarray 255 except (ImportError,IOError):sndarray=MissingModule("sndarray", geterror(), 0) 256 257 try: import pygame.fastevent 258 except (ImportError,IOError):fastevent=MissingModule("fastevent", geterror(), 0) 259 260 #there's also a couple "internal" modules not needed 261 #by users, but putting them here helps "dependency finder" 262 #programs get everything they need (like py2exe) 263 try: import pygame.imageext; del pygame.imageext 264 except (ImportError,IOError):pass 265
266 -def packager_imports():
267 """ 268 Some additional things that py2app/py2exe will want to see 269 """ 270 import atexit 271 import Numeric 272 import numpy 273 import OpenGL.GL 274 import pygame.macosx 275 import pygame.mac_scrap 276 import pygame.bufferproxy 277 import pygame.colordict
278 279 #make Rects pickleable 280 try: 281 import copy_reg 282 except ImportError: 283 import copyreg as copy_reg
284 -def __rect_constructor(x,y,w,h):
285 return Rect(x,y,w,h)
286 -def __rect_reduce(r):
287 assert type(r) == Rect 288 return __rect_constructor, (r.x, r.y, r.w, r.h)
289 copy_reg.pickle(Rect, __rect_reduce, __rect_constructor) 290 291 292 #make Colors pickleable
293 -def __color_constructor(r,g,b,a):
294 return Color(r,g,b,a)
295 -def __color_reduce(c):
296 assert type(c) == Color 297 return __color_constructor, (c.r, c.g, c.b, c.a)
298 copy_reg.pickle(Color, __color_reduce, __color_constructor) 299 300 301 302 303 #cleanup namespace 304 del pygame, os, sys, rwobject, surflock, MissingModule, copy_reg, geterror 305