testsprite.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/usr/bin/env python
  2. # like the testsprite.c that comes with sdl, this pygame version shows
  3. # lots of sprites moving around.
  4. import pygame, sys, os
  5. from pygame.locals import *
  6. from random import randint
  7. from time import time
  8. import pygame.joystick
  9. from pygame.compat import xrange_
  10. ##import FastRenderGroup as FRG
  11. import pygame.sprite as FRG
  12. if "-psyco" in sys.argv:
  13. try:
  14. import psyco
  15. psyco.full()
  16. except Exception:
  17. print ("No psyco for you! psyco failed to import and run.")
  18. main_dir = os.path.split(os.path.abspath(__file__))[0]
  19. data_dir = os.path.join(main_dir, 'data')
  20. # use this to use update rects or not.
  21. # If the screen is mostly full, then update rects are not useful.
  22. update_rects = True
  23. if "-update_rects" in sys.argv:
  24. update_rects = True
  25. if "-noupdate_rects" in sys.argv:
  26. update_rects = False
  27. use_static = False
  28. if "-static" in sys.argv:
  29. use_static = True
  30. use_FastRenderGroup = False
  31. if "-FastRenderGroup" in sys.argv:
  32. update_rects = True
  33. use_FastRenderGroup = True
  34. flags = 0
  35. if "-flip" in sys.argv:
  36. flags ^= DOUBLEBUF
  37. if "-fullscreen" in sys.argv:
  38. flags ^= FULLSCREEN
  39. if "-sw" in sys.argv:
  40. flags ^= SWSURFACE
  41. use_rle = True
  42. if "-hw" in sys.argv:
  43. flags ^= HWSURFACE
  44. use_rle = False
  45. screen_dims = [640, 480]
  46. if "-height" in sys.argv:
  47. i = sys.argv.index("-height")
  48. screen_dims[1] = int(sys.argv[i+1])
  49. if "-width" in sys.argv:
  50. i = sys.argv.index("-width")
  51. screen_dims[0] = int(sys.argv[i+1])
  52. if "-alpha" in sys.argv:
  53. use_alpha = True
  54. else:
  55. use_alpha = False
  56. print (screen_dims)
  57. ##class Thingy(pygame.sprite.Sprite):
  58. ## images = None
  59. ## def __init__(self):
  60. ## pygame.sprite.Sprite.__init__(self)
  61. ## self.image = Thingy.images[0]
  62. ## self.rect = self.image.get_rect()
  63. ## self.rect.x = randint(0, screen_dims[0])
  64. ## self.rect.y = randint(0, screen_dims[1])
  65. ## #self.vel = [randint(-10, 10), randint(-10, 10)]
  66. ## self.vel = [randint(-1, 1), randint(-1, 1)]
  67. ##
  68. ## def move(self):
  69. ## for i in [0, 1]:
  70. ## nv = self.rect[i] + self.vel[i]
  71. ## if nv >= screen_dims[i] or nv < 0:
  72. ## self.vel[i] = -self.vel[i]
  73. ## nv = self.rect[i] + self.vel[i]
  74. ## self.rect[i] = nv
  75. class Thingy(FRG.DirtySprite):
  76. images = None
  77. def __init__(self):
  78. ## pygame.sprite.Sprite.__init__(self)
  79. FRG.DirtySprite.__init__(self)
  80. self.image = Thingy.images[0]
  81. self.rect = self.image.get_rect()
  82. self.rect.x = randint(0, screen_dims[0])
  83. self.rect.y = randint(0, screen_dims[1])
  84. #self.vel = [randint(-10, 10), randint(-10, 10)]
  85. self.vel = [randint(-1, 1), randint(-1, 1)]
  86. self.dirty = 2
  87. def update(self):
  88. for i in [0, 1]:
  89. nv = self.rect[i] + self.vel[i]
  90. if nv >= screen_dims[i] or nv < 0:
  91. self.vel[i] = -self.vel[i]
  92. nv = self.rect[i] + self.vel[i]
  93. self.rect[i] = nv
  94. class Static(FRG.DirtySprite):
  95. images = None
  96. def __init__(self):
  97. FRG.DirtySprite.__init__(self)
  98. self.image = Static.images[0]
  99. self.rect = self.image.get_rect()
  100. self.rect.x = randint(0, 3*screen_dims[0]/4)
  101. self.rect.y = randint(0, 3*screen_dims[1]/4)
  102. def main(update_rects = True,
  103. use_static = False,
  104. use_FastRenderGroup = False,
  105. screen_dims = [640, 480],
  106. use_alpha = False,
  107. flags = 0,
  108. ):
  109. """Show lots of sprites moving around
  110. Optional keyword arguments:
  111. update_rects - use the RenderUpdate sprite group class (default True)
  112. use_static - include non-moving images (default False)
  113. use_FastRenderGroup - Use the FastRenderGroup sprite group (default False)
  114. screen_dims - Pygame window dimensions (default [640, 480])
  115. use_alpha - use alpha blending (default False)
  116. flags - additional display mode flags (default no addiontal flags)
  117. """
  118. if use_FastRenderGroup:
  119. update_rects = True
  120. #pygame.init()
  121. pygame.display.init()
  122. #if "-fast" in sys.argv:
  123. screen = pygame.display.set_mode(screen_dims, flags)
  124. # this is mainly for GP2X, so it can quit.
  125. pygame.joystick.init()
  126. num_joysticks = pygame.joystick.get_count()
  127. if num_joysticks > 0:
  128. stick = pygame.joystick.Joystick(0)
  129. stick.init() # now we will receive events for the joystick
  130. screen.fill([0,0,0])
  131. pygame.display.flip()
  132. sprite_surface = pygame.image.load(os.path.join(data_dir, "asprite.bmp"))
  133. sprite_surface2 = pygame.image.load(os.path.join(data_dir, "static.png"))
  134. if use_rle:
  135. sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY|RLEACCEL)
  136. sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY|RLEACCEL)
  137. else:
  138. sprite_surface.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY)
  139. sprite_surface2.set_colorkey([0xFF, 0xFF, 0xFF], SRCCOLORKEY)
  140. if use_alpha:
  141. sprite_surface = sprite_surface.convert_alpha()
  142. sprite_surface2 = sprite_surface2.convert_alpha()
  143. else:
  144. sprite_surface = sprite_surface.convert()
  145. sprite_surface2 = sprite_surface2.convert()
  146. Thingy.images = [sprite_surface]
  147. if use_static:
  148. Static.images = [sprite_surface2]
  149. if len(sys.argv) > 1:
  150. try:
  151. numsprites = int(sys.argv[-1])
  152. except Exception:
  153. numsprites = 100
  154. else:
  155. numsprites = 100
  156. sprites = None
  157. if use_FastRenderGroup:
  158. ## sprites = FRG.FastRenderGroup()
  159. sprites = FRG.LayeredDirty()
  160. else:
  161. if update_rects:
  162. sprites = pygame.sprite.RenderUpdates()
  163. else:
  164. sprites = pygame.sprite.Group()
  165. for i in xrange_(0, numsprites):
  166. if use_static and i%2==0:
  167. sprites.add(Static())
  168. sprites.add(Thingy())
  169. done = False
  170. frames = 0
  171. start = time()
  172. background = pygame.Surface(screen.get_size())
  173. background = background.convert()
  174. background.fill([0,0,0])
  175. while not done:
  176. if not update_rects:
  177. screen.fill([0,0,0])
  178. ## for sprite in sprites:
  179. ## sprite.move()
  180. if update_rects:
  181. sprites.clear(screen, background)
  182. sprites.update()
  183. rects = sprites.draw(screen)
  184. if update_rects:
  185. pygame.display.update(rects)
  186. else:
  187. pygame.display.flip()
  188. for event in pygame.event.get():
  189. if event.type in [KEYDOWN, QUIT, JOYBUTTONDOWN]:
  190. done = True
  191. frames += 1
  192. end = time()
  193. print ("FPS: %f" % (frames / ((end - start))))
  194. pygame.quit()
  195. if __name__ == "__main__":
  196. main( update_rects,
  197. use_static,
  198. use_FastRenderGroup,
  199. screen_dims,
  200. use_alpha,
  201. flags )