sprite.py 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. ## pygame - Python Game Library
  2. ## Copyright (C) 2000-2003, 2007 Pete Shinners
  3. ## (C) 2004 Joe Wreschnig
  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 module with basic game object classes
  21. This module contains several simple classes to be used within games. There
  22. are the main Sprite class and several Group classes that contain Sprites.
  23. The use of these classes is entirely optional when using Pygame. The classes
  24. are fairly lightweight and only provide a starting place for the code
  25. that is common to most games.
  26. The Sprite class is intended to be used as a base class for the different
  27. types of objects in the game. There is also a base Group class that simply
  28. stores sprites. A game could create new types of Group classes that operate
  29. on specially customized Sprite instances they contain.
  30. The basic Sprite class can draw the Sprites it contains to a Surface. The
  31. Group.draw() method requires that each Sprite have a Surface.image attribute
  32. and a Surface.rect. The Group.clear() method requires these same attributes
  33. and can be used to erase all the Sprites with background. There are also
  34. more advanced Groups: pygame.sprite.RenderUpdates() and
  35. pygame.sprite.OrderedUpdates().
  36. Lastly, this module contains several collision functions. These help find
  37. sprites inside multiple groups that have intersecting bounding rectangles.
  38. To find the collisions, the Sprites are required to have a Surface.rect
  39. attribute assigned.
  40. The groups are designed for high efficiency in removing and adding Sprites
  41. to them. They also allow cheap testing to see if a Sprite already exists in
  42. a Group. A given Sprite can exist in any number of groups. A game could use
  43. some groups to control object rendering, and a completely separate set of
  44. groups to control interaction or player movement. Instead of adding type
  45. attributes or bools to a derived Sprite class, consider keeping the
  46. Sprites inside organized Groups. This will allow for easier lookup later
  47. in the game.
  48. Sprites and Groups manage their relationships with the add() and remove()
  49. methods. These methods can accept a single or multiple group arguments for
  50. membership. The default initializers for these classes also take a
  51. single group or list of groups as argments for initial membership. It is safe
  52. to repeatedly add and remove the same Sprite from a Group.
  53. While it is possible to design sprite and group classes that don't derive
  54. from the Sprite and AbstractGroup classes below, it is strongly recommended
  55. that you extend those when you create a new Sprite or Group class.
  56. Sprites are not thread safe, so lock them yourself if using threads.
  57. """
  58. ##todo
  59. ## a group that holds only the 'n' most recent elements.
  60. ## sort of like the GroupSingle class, but holding more
  61. ## than one sprite
  62. ##
  63. ## drawing groups that can 'automatically' store the area
  64. ## underneath so they can "clear" without needing a background
  65. ## function. obviously a little slower than normal, but nice
  66. ## to use in many situations. (also remember it must "clear"
  67. ## in the reverse order that it draws :])
  68. ##
  69. ## the drawing groups should also be able to take a background
  70. ## function, instead of just a background surface. the function
  71. ## would take a surface and a rectangle on that surface to erase.
  72. ##
  73. ## perhaps more types of collision functions? the current two
  74. ## should handle just about every need, but perhaps more optimized
  75. ## specific ones that aren't quite so general but fit into common
  76. ## specialized cases.
  77. import pygame
  78. from pygame import Rect
  79. from pygame.time import get_ticks
  80. from operator import truth
  81. # Python 3 does not have the callable function, but an equivalent can be made
  82. # with the hasattr function.
  83. if 'callable' not in dir(__builtins__):
  84. callable = lambda obj: hasattr(obj, '__call__')
  85. # Don't depend on pygame.mask if it's not there...
  86. try:
  87. from pygame.mask import from_surface
  88. except:
  89. pass
  90. class Sprite(object):
  91. """simple base class for visible game objects
  92. pygame.sprite.Sprite(*groups): return Sprite
  93. The base class for visible game objects. Derived classes will want to
  94. override the Sprite.update() method and assign Sprite.image and Sprite.rect
  95. attributes. The initializer can accept any number of Group instances that
  96. the Sprite will become a member of.
  97. When subclassing the Sprite class, be sure to call the base initializer
  98. before adding the Sprite to Groups.
  99. """
  100. def __init__(self, *groups):
  101. self.__g = {} # The groups the sprite is in
  102. if groups:
  103. self.add(*groups)
  104. def add(self, *groups):
  105. """add the sprite to groups
  106. Sprite.add(*groups): return None
  107. Any number of Group instances can be passed as arguments. The
  108. Sprite will be added to the Groups it is not already a member of.
  109. """
  110. has = self.__g.__contains__
  111. for group in groups:
  112. if hasattr(group, '_spritegroup'):
  113. if not has(group):
  114. group.add_internal(self)
  115. self.add_internal(group)
  116. else:
  117. self.add(*group)
  118. def remove(self, *groups):
  119. """remove the sprite from groups
  120. Sprite.remove(*groups): return None
  121. Any number of Group instances can be passed as arguments. The Sprite
  122. will be removed from the Groups it is currently a member of.
  123. """
  124. has = self.__g.__contains__
  125. for group in groups:
  126. if hasattr(group, '_spritegroup'):
  127. if has(group):
  128. group.remove_internal(self)
  129. self.remove_internal(group)
  130. else:
  131. self.remove(*group)
  132. def add_internal(self, group):
  133. self.__g[group] = 0
  134. def remove_internal(self, group):
  135. del self.__g[group]
  136. def update(self, *args):
  137. """method to control sprite behavior
  138. Sprite.update(*args):
  139. The default implementation of this method does nothing; it's just a
  140. convenient "hook" that you can override. This method is called by
  141. Group.update() with whatever arguments you give it.
  142. There is no need to use this method if not using the convenience
  143. method by the same name in the Group class.
  144. """
  145. pass
  146. def kill(self):
  147. """remove the Sprite from all Groups
  148. Sprite.kill(): return None
  149. The Sprite is removed from all the Groups that contain it. This won't
  150. change anything about the state of the Sprite. It is possible to
  151. continue to use the Sprite after this method has been called, including
  152. adding it to Groups.
  153. """
  154. for c in self.__g:
  155. c.remove_internal(self)
  156. self.__g.clear()
  157. def groups(self):
  158. """list of Groups that contain this Sprite
  159. Sprite.groups(): return group_list
  160. Returns a list of all the Groups that contain this Sprite.
  161. """
  162. return list(self.__g)
  163. def alive(self):
  164. """does the sprite belong to any groups
  165. Sprite.alive(): return bool
  166. Returns True when the Sprite belongs to one or more Groups.
  167. """
  168. return truth(self.__g)
  169. def __repr__(self):
  170. return "<%s sprite(in %d groups)>" % (self.__class__.__name__, len(self.__g))
  171. class DirtySprite(Sprite):
  172. """a more featureful subclass of Sprite with more attributes
  173. pygame.sprite.DirtySprite(*groups): return DirtySprite
  174. Extra DirtySprite attributes with their default values:
  175. dirty = 1
  176. If set to 1, it is repainted and then set to 0 again.
  177. If set to 2, it is always dirty (repainted each frame;
  178. flag is not reset).
  179. If set to 0, it is not dirty and therefore not repainted again.
  180. blendmode = 0
  181. It's the special_flags argument of Surface.blit; see the blendmodes in
  182. the Surface.blit documentation
  183. source_rect = None
  184. This is the source rect to use. Remember that it is relative to the top
  185. left corner (0, 0) of self.image.
  186. visible = 1
  187. Normally this is 1. If set to 0, it will not be repainted. (If you
  188. change visible to 1, you must set dirty to 1 for it to be erased from
  189. the screen.)
  190. _layer = 0
  191. 0 is the default value but this is able to be set differently
  192. when subclassing.
  193. """
  194. def __init__(self, *groups):
  195. self.dirty = 1
  196. self.blendmode = 0 # pygame 1.8, referred to as special_flags in
  197. # the documentation of Surface.blit
  198. self._visible = 1
  199. self._layer = getattr(self, '_layer', 0) # Default 0 unless
  200. # initialized differently.
  201. self.source_rect = None
  202. Sprite.__init__(self, *groups)
  203. def _set_visible(self, val):
  204. """set the visible value (0 or 1) and makes the sprite dirty"""
  205. self._visible = val
  206. if self.dirty < 2:
  207. self.dirty = 1
  208. def _get_visible(self):
  209. """return the visible value of that sprite"""
  210. return self._visible
  211. visible = property(lambda self: self._get_visible(),
  212. lambda self, value: self._set_visible(value),
  213. doc="you can make this sprite disappear without "
  214. "removing it from the group,\n"
  215. "assign 0 for invisible and 1 for visible")
  216. def __repr__(self):
  217. return "<%s DirtySprite(in %d groups)>" % \
  218. (self.__class__.__name__, len(self.groups()))
  219. class AbstractGroup(object):
  220. """base class for containers of sprites
  221. AbstractGroup does everything needed to behave as a normal group. You can
  222. easily subclass a new group class from this or the other groups below if
  223. you want to add more features.
  224. Any AbstractGroup-derived sprite groups act like sequences and support
  225. iteration, len, and so on.
  226. """
  227. # dummy val to identify sprite groups, and avoid infinite recursion
  228. _spritegroup = True
  229. def __init__(self):
  230. self.spritedict = {}
  231. self.lostsprites = []
  232. def sprites(self):
  233. """get a list of sprites in the group
  234. Group.sprite(): return list
  235. Returns an object that can be looped over with a 'for' loop. (For now,
  236. it is always a list, but this could change in a future version of
  237. pygame.) Alternatively, you can get the same information by iterating
  238. directly over the sprite group, e.g. 'for sprite in group'.
  239. """
  240. return list(self.spritedict)
  241. def add_internal(self, sprite):
  242. self.spritedict[sprite] = 0
  243. def remove_internal(self, sprite):
  244. r = self.spritedict[sprite]
  245. if r:
  246. self.lostsprites.append(r)
  247. del self.spritedict[sprite]
  248. def has_internal(self, sprite):
  249. return sprite in self.spritedict
  250. def copy(self):
  251. """copy a group with all the same sprites
  252. Group.copy(): return Group
  253. Returns a copy of the group that is an instance of the same class
  254. and has the same sprites in it.
  255. """
  256. return self.__class__(self.sprites())
  257. def __iter__(self):
  258. return iter(self.sprites())
  259. def __contains__(self, sprite):
  260. return self.has(sprite)
  261. def add(self, *sprites):
  262. """add sprite(s) to group
  263. Group.add(sprite, list, group, ...): return None
  264. Adds a sprite or sequence of sprites to a group.
  265. """
  266. for sprite in sprites:
  267. # It's possible that some sprite is also an iterator.
  268. # If this is the case, we should add the sprite itself,
  269. # and not the iterator object.
  270. if isinstance(sprite, Sprite):
  271. if not self.has_internal(sprite):
  272. self.add_internal(sprite)
  273. sprite.add_internal(self)
  274. else:
  275. try:
  276. # See if sprite is an iterator, like a list or sprite
  277. # group.
  278. self.add(*sprite)
  279. except (TypeError, AttributeError):
  280. # Not iterable. This is probably a sprite that is not an
  281. # instance of the Sprite class or is not an instance of a
  282. # subclass of the Sprite class. Alternately, it could be an
  283. # old-style sprite group.
  284. if hasattr(sprite, '_spritegroup'):
  285. for spr in sprite.sprites():
  286. if not self.has_internal(spr):
  287. self.add_internal(spr)
  288. spr.add_internal(self)
  289. elif not self.has_internal(sprite):
  290. self.add_internal(sprite)
  291. sprite.add_internal(self)
  292. def remove(self, *sprites):
  293. """remove sprite(s) from group
  294. Group.remove(sprite, list, or group, ...): return None
  295. Removes a sprite or sequence of sprites from a group.
  296. """
  297. # This function behaves essentially the same as Group.add. It first
  298. # tries to handle each argument as an instance of the Sprite class. If
  299. # that failes, then it tries to handle the argument as an iterable
  300. # object. If that failes, then it tries to handle the argument as an
  301. # old-style sprite group. Lastly, if that fails, it assumes that the
  302. # normal Sprite methods should be used.
  303. for sprite in sprites:
  304. if isinstance(sprite, Sprite):
  305. if self.has_internal(sprite):
  306. self.remove_internal(sprite)
  307. sprite.remove_internal(self)
  308. else:
  309. try:
  310. self.remove(*sprite)
  311. except (TypeError, AttributeError):
  312. if hasattr(sprite, '_spritegroup'):
  313. for spr in sprite.sprites():
  314. if self.has_internal(spr):
  315. self.remove_internal(spr)
  316. spr.remove_internal(self)
  317. elif self.has_internal(sprite):
  318. self.remove_internal(sprite)
  319. sprite.remove_internal(self)
  320. def has(self, *sprites):
  321. """ask if group has a sprite or sprites
  322. Group.has(sprite or group, ...): return bool
  323. Returns True if the given sprite or sprites are contained in the
  324. group. Alternatively, you can get the same information using the
  325. 'in' operator, e.g. 'sprite in group', 'subgroup in group'.
  326. """
  327. return_value = False
  328. for sprite in sprites:
  329. if isinstance(sprite, Sprite):
  330. # Check for Sprite instance's membership in this group
  331. if self.has_internal(sprite):
  332. return_value = True
  333. else:
  334. return False
  335. else:
  336. try:
  337. if self.has(*sprite):
  338. return_value = True
  339. else:
  340. return False
  341. except (TypeError, AttributeError):
  342. if hasattr(sprite, '_spritegroup'):
  343. for spr in sprite.sprites():
  344. if self.has_internal(spr):
  345. return_value = True
  346. else:
  347. return False
  348. else:
  349. if self.has_internal(sprite):
  350. return_value = True
  351. else:
  352. return False
  353. return return_value
  354. def update(self, *args):
  355. """call the update method of every member sprite
  356. Group.update(*args): return None
  357. Calls the update method of every member sprite. All arguments that
  358. were passed to this method are passed to the Sprite update function.
  359. """
  360. for s in self.sprites():
  361. s.update(*args)
  362. def draw(self, surface):
  363. """draw all sprites onto the surface
  364. Group.draw(surface): return None
  365. Draws all of the member sprites onto the given surface.
  366. """
  367. sprites = self.sprites()
  368. surface_blit = surface.blit
  369. for spr in sprites:
  370. self.spritedict[spr] = surface_blit(spr.image, spr.rect)
  371. self.lostsprites = []
  372. def clear(self, surface, bgd):
  373. """erase the previous position of all sprites
  374. Group.clear(surface, bgd): return None
  375. Clears the area under every drawn sprite in the group. The bgd
  376. argument should be Surface which is the same dimensions as the
  377. screen surface. The bgd could also be a function which accepts
  378. the given surface and the area to be cleared as arguments.
  379. """
  380. if callable(bgd):
  381. for r in self.lostsprites:
  382. bgd(surface, r)
  383. for r in self.spritedict.values():
  384. if r:
  385. bgd(surface, r)
  386. else:
  387. surface_blit = surface.blit
  388. for r in self.lostsprites:
  389. surface_blit(bgd, r, r)
  390. for r in self.spritedict.values():
  391. if r:
  392. surface_blit(bgd, r, r)
  393. def empty(self):
  394. """remove all sprites
  395. Group.empty(): return None
  396. Removes all the sprites from the group.
  397. """
  398. for s in self.sprites():
  399. self.remove_internal(s)
  400. s.remove_internal(self)
  401. def __nonzero__(self):
  402. return truth(self.sprites())
  403. def __len__(self):
  404. """return number of sprites in group
  405. Group.len(group): return int
  406. Returns the number of sprites contained in the group.
  407. """
  408. return len(self.sprites())
  409. def __repr__(self):
  410. return "<%s(%d sprites)>" % (self.__class__.__name__, len(self))
  411. class Group(AbstractGroup):
  412. """container class for many Sprites
  413. pygame.sprite.Group(*sprites): return Group
  414. A simple container for Sprite objects. This class can be subclassed to
  415. create containers with more specific behaviors. The constructor takes any
  416. number of Sprite arguments to add to the Group. The group supports the
  417. following standard Python operations:
  418. in test if a Sprite is contained
  419. len the number of Sprites contained
  420. bool test if any Sprites are contained
  421. iter iterate through all the Sprites
  422. The Sprites in the Group are not ordered, so the Sprites are drawn and
  423. iterated over in no particular order.
  424. """
  425. def __init__(self, *sprites):
  426. AbstractGroup.__init__(self)
  427. self.add(*sprites)
  428. RenderPlain = Group
  429. RenderClear = Group
  430. class RenderUpdates(Group):
  431. """Group class that tracks dirty updates
  432. pygame.sprite.RenderUpdates(*sprites): return RenderUpdates
  433. This class is derived from pygame.sprite.Group(). It has an enhanced draw
  434. method that tracks the changed areas of the screen.
  435. """
  436. def draw(self, surface):
  437. spritedict = self.spritedict
  438. surface_blit = surface.blit
  439. dirty = self.lostsprites
  440. self.lostsprites = []
  441. dirty_append = dirty.append
  442. for s in self.sprites():
  443. r = spritedict[s]
  444. newrect = surface_blit(s.image, s.rect)
  445. if r:
  446. if newrect.colliderect(r):
  447. dirty_append(newrect.union(r))
  448. else:
  449. dirty_append(newrect)
  450. dirty_append(r)
  451. else:
  452. dirty_append(newrect)
  453. spritedict[s] = newrect
  454. return dirty
  455. class OrderedUpdates(RenderUpdates):
  456. """RenderUpdates class that draws Sprites in order of addition
  457. pygame.sprite.OrderedUpdates(*spites): return OrderedUpdates
  458. This class derives from pygame.sprite.RenderUpdates(). It maintains
  459. the order in which the Sprites were added to the Group for rendering.
  460. This makes adding and removing Sprites from the Group a little
  461. slower than regular Groups.
  462. """
  463. def __init__(self, *sprites):
  464. self._spritelist = []
  465. RenderUpdates.__init__(self, *sprites)
  466. def sprites(self):
  467. return list(self._spritelist)
  468. def add_internal(self, sprite):
  469. RenderUpdates.add_internal(self, sprite)
  470. self._spritelist.append(sprite)
  471. def remove_internal(self, sprite):
  472. RenderUpdates.remove_internal(self, sprite)
  473. self._spritelist.remove(sprite)
  474. class LayeredUpdates(AbstractGroup):
  475. """LayeredUpdates Group handles layers, which are drawn like OrderedUpdates
  476. pygame.sprite.LayeredUpdates(*spites, **kwargs): return LayeredUpdates
  477. This group is fully compatible with pygame.sprite.Sprite.
  478. New in pygame 1.8.0
  479. """
  480. _init_rect = Rect(0, 0, 0, 0)
  481. def __init__(self, *sprites, **kwargs):
  482. """initialize an instance of LayeredUpdates with the given attributes
  483. You can set the default layer through kwargs using 'default_layer'
  484. and an integer for the layer. The default layer is 0.
  485. If the sprite you add has an attribute _layer, then that layer will be
  486. used. If **kwarg contains 'layer', then the passed sprites will be
  487. added to that layer (overriding the sprite._layer attribute). If
  488. neither the sprite nor **kwarg has a 'layer', then the default layer is
  489. used to add the sprites.
  490. """
  491. self._spritelayers = {}
  492. self._spritelist = []
  493. AbstractGroup.__init__(self)
  494. self._default_layer = kwargs.get('default_layer', 0)
  495. self.add(*sprites, **kwargs)
  496. def add_internal(self, sprite, layer=None):
  497. """Do not use this method directly.
  498. It is used by the group to add a sprite internally.
  499. """
  500. self.spritedict[sprite] = self._init_rect
  501. if layer is None:
  502. try:
  503. layer = sprite._layer
  504. except AttributeError:
  505. layer = sprite._layer = self._default_layer
  506. elif hasattr(sprite, '_layer'):
  507. sprite._layer = layer
  508. sprites = self._spritelist # speedup
  509. sprites_layers = self._spritelayers
  510. sprites_layers[sprite] = layer
  511. # add the sprite at the right position
  512. # bisect algorithmus
  513. leng = len(sprites)
  514. low = mid = 0
  515. high = leng - 1
  516. while low <= high:
  517. mid = low + (high - low) // 2
  518. if sprites_layers[sprites[mid]] <= layer:
  519. low = mid + 1
  520. else:
  521. high = mid - 1
  522. # linear search to find final position
  523. while mid < leng and sprites_layers[sprites[mid]] <= layer:
  524. mid += 1
  525. sprites.insert(mid, sprite)
  526. def add(self, *sprites, **kwargs):
  527. """add a sprite or sequence of sprites to a group
  528. LayeredUpdates.add(*sprites, **kwargs): return None
  529. If the sprite you add has an attribute _layer, then that layer will be
  530. used. If **kwarg contains 'layer', then the passed sprites will be
  531. added to that layer (overriding the sprite._layer attribute). If
  532. neither the sprite nor **kwarg has a 'layer', then the default layer is
  533. used to add the sprites.
  534. """
  535. if not sprites:
  536. return
  537. if 'layer' in kwargs:
  538. layer = kwargs['layer']
  539. else:
  540. layer = None
  541. for sprite in sprites:
  542. # It's possible that some sprite is also an iterator.
  543. # If this is the case, we should add the sprite itself,
  544. # and not the iterator object.
  545. if isinstance(sprite, Sprite):
  546. if not self.has_internal(sprite):
  547. self.add_internal(sprite, layer)
  548. sprite.add_internal(self)
  549. else:
  550. try:
  551. # See if sprite is an iterator, like a list or sprite
  552. # group.
  553. self.add(*sprite, **kwargs)
  554. except (TypeError, AttributeError):
  555. # Not iterable. This is probably a sprite that is not an
  556. # instance of the Sprite class or is not an instance of a
  557. # subclass of the Sprite class. Alternately, it could be an
  558. # old-style sprite group.
  559. if hasattr(sprite, '_spritegroup'):
  560. for spr in sprite.sprites():
  561. if not self.has_internal(spr):
  562. self.add_internal(spr, layer)
  563. spr.add_internal(self)
  564. elif not self.has_internal(sprite):
  565. self.add_internal(sprite, layer)
  566. sprite.add_internal(self)
  567. def remove_internal(self, sprite):
  568. """Do not use this method directly.
  569. The group uses it to add a sprite.
  570. """
  571. self._spritelist.remove(sprite)
  572. # these dirty rects are suboptimal for one frame
  573. r = self.spritedict[sprite]
  574. if r is not self._init_rect:
  575. self.lostsprites.append(r) # dirty rect
  576. if hasattr(sprite, 'rect'):
  577. self.lostsprites.append(sprite.rect) # dirty rect
  578. del self.spritedict[sprite]
  579. del self._spritelayers[sprite]
  580. def sprites(self):
  581. """return a ordered list of sprites (first back, last top).
  582. LayeredUpdates.sprites(): return sprites
  583. """
  584. return list(self._spritelist)
  585. def draw(self, surface):
  586. """draw all sprites in the right order onto the passed surface
  587. LayeredUpdates.draw(surface): return Rect_list
  588. """
  589. spritedict = self.spritedict
  590. surface_blit = surface.blit
  591. dirty = self.lostsprites
  592. self.lostsprites = []
  593. dirty_append = dirty.append
  594. init_rect = self._init_rect
  595. for spr in self.sprites():
  596. rec = spritedict[spr]
  597. newrect = surface_blit(spr.image, spr.rect)
  598. if rec is init_rect:
  599. dirty_append(newrect)
  600. else:
  601. if newrect.colliderect(rec):
  602. dirty_append(newrect.union(rec))
  603. else:
  604. dirty_append(newrect)
  605. dirty_append(rec)
  606. spritedict[spr] = newrect
  607. return dirty
  608. def get_sprites_at(self, pos):
  609. """return a list with all sprites at that position
  610. LayeredUpdates.get_sprites_at(pos): return colliding_sprites
  611. Bottom sprites are listed first; the top ones are listed last.
  612. """
  613. _sprites = self._spritelist
  614. rect = Rect(pos, (0, 0))
  615. colliding_idx = rect.collidelistall(_sprites)
  616. colliding = [_sprites[i] for i in colliding_idx]
  617. return colliding
  618. def get_sprite(self, idx):
  619. """return the sprite at the index idx from the groups sprites
  620. LayeredUpdates.get_sprite(idx): return sprite
  621. Raises IndexOutOfBounds if the idx is not within range.
  622. """
  623. return self._spritelist[idx]
  624. def remove_sprites_of_layer(self, layer_nr):
  625. """remove all sprites from a layer and return them as a list
  626. LayeredUpdates.remove_sprites_of_layer(layer_nr): return sprites
  627. """
  628. sprites = self.get_sprites_from_layer(layer_nr)
  629. self.remove(*sprites)
  630. return sprites
  631. #---# layer methods
  632. def layers(self):
  633. """return a list of unique defined layers defined.
  634. LayeredUpdates.layers(): return layers
  635. """
  636. return sorted(set(self._spritelayers.values()))
  637. def change_layer(self, sprite, new_layer):
  638. """change the layer of the sprite
  639. LayeredUpdates.change_layer(sprite, new_layer): return None
  640. The sprite must have been added to the renderer already. This is not
  641. checked.
  642. """
  643. sprites = self._spritelist # speedup
  644. sprites_layers = self._spritelayers # speedup
  645. sprites.remove(sprite)
  646. sprites_layers.pop(sprite)
  647. # add the sprite at the right position
  648. # bisect algorithmus
  649. leng = len(sprites)
  650. low = mid = 0
  651. high = leng - 1
  652. while low <= high:
  653. mid = low + (high - low) // 2
  654. if sprites_layers[sprites[mid]] <= new_layer:
  655. low = mid + 1
  656. else:
  657. high = mid - 1
  658. # linear search to find final position
  659. while mid < leng and sprites_layers[sprites[mid]] <= new_layer:
  660. mid += 1
  661. sprites.insert(mid, sprite)
  662. if hasattr(sprite, 'layer'):
  663. sprite.layer = new_layer
  664. # add layer info
  665. sprites_layers[sprite] = new_layer
  666. def get_layer_of_sprite(self, sprite):
  667. """return the layer that sprite is currently in
  668. If the sprite is not found, then it will return the default layer.
  669. """
  670. return self._spritelayers.get(sprite, self._default_layer)
  671. def get_top_layer(self):
  672. """return the top layer
  673. LayeredUpdates.get_top_layer(): return layer
  674. """
  675. return self._spritelayers[self._spritelist[-1]]
  676. def get_bottom_layer(self):
  677. """return the bottom layer
  678. LayeredUpdates.get_bottom_layer(): return layer
  679. """
  680. return self._spritelayers[self._spritelist[0]]
  681. def move_to_front(self, sprite):
  682. """bring the sprite to front layer
  683. LayeredUpdates.move_to_front(sprite): return None
  684. Brings the sprite to front by changing the sprite layer to the top-most
  685. layer. The sprite is added at the end of the list of sprites in that
  686. top-most layer.
  687. """
  688. self.change_layer(sprite, self.get_top_layer())
  689. def move_to_back(self, sprite):
  690. """move the sprite to the bottom layer
  691. LayeredUpdates.move_to_back(sprite): return None
  692. Moves the sprite to the bottom layer by moving it to a new layer below
  693. the current bottom layer.
  694. """
  695. self.change_layer(sprite, self.get_bottom_layer() - 1)
  696. def get_top_sprite(self):
  697. """return the topmost sprite
  698. LayeredUpdates.get_top_sprite(): return Sprite
  699. """
  700. return self._spritelist[-1]
  701. def get_sprites_from_layer(self, layer):
  702. """return all sprites from a layer ordered as they where added
  703. LayeredUpdates.get_sprites_from_layer(layer): return sprites
  704. Returns all sprites from a layer. The sprites are ordered in the
  705. sequence that they where added. (The sprites are not removed from the
  706. layer.
  707. """
  708. sprites = []
  709. sprites_append = sprites.append
  710. sprite_layers = self._spritelayers
  711. for spr in self._spritelist:
  712. if sprite_layers[spr] == layer:
  713. sprites_append(spr)
  714. elif sprite_layers[spr] > layer:# break after because no other will
  715. # follow with same layer
  716. break
  717. return sprites
  718. def switch_layer(self, layer1_nr, layer2_nr):
  719. """switch the sprites from layer1_nr to layer2_nr
  720. LayeredUpdates.switch_layer(layer1_nr, layer2_nr): return None
  721. The layers number must exist. This method does not check for the
  722. existence of the given layers.
  723. """
  724. sprites1 = self.remove_sprites_of_layer(layer1_nr)
  725. for spr in self.get_sprites_from_layer(layer2_nr):
  726. self.change_layer(spr, layer1_nr)
  727. self.add(layer=layer2_nr, *sprites1)
  728. class LayeredDirty(LayeredUpdates):
  729. """LayeredDirty Group is for DirtySprites; subclasses LayeredUpdates
  730. pygame.sprite.LayeredDirty(*spites, **kwargs): return LayeredDirty
  731. This group requires pygame.sprite.DirtySprite or any sprite that
  732. has the following attributes:
  733. image, rect, dirty, visible, blendmode (see doc of DirtySprite).
  734. It uses the dirty flag technique and is therefore faster than
  735. pygame.sprite.RenderUpdates if you have many static sprites. It
  736. also switches automatically between dirty rect updating and full
  737. screen drawing, so you do no have to worry which would be faster.
  738. As with the pygame.sprite.Group, you can specify some additional attributes
  739. through kwargs:
  740. _use_update: True/False (default is False)
  741. _default_layer: default layer where the sprites without a layer are
  742. added
  743. _time_threshold: treshold time for switching between dirty rect mode
  744. and fullscreen mode; defaults to updating at 80 frames per second,
  745. which is equal to 1000.0 / 80.0
  746. New in pygame 1.8.0
  747. """
  748. def __init__(self, *sprites, **kwargs):
  749. """initialize group.
  750. pygame.sprite.LayeredDirty(*spites, **kwargs): return LayeredDirty
  751. You can specify some additional attributes through kwargs:
  752. _use_update: True/False (default is False)
  753. _default_layer: default layer where the sprites without a layer are
  754. added
  755. _time_threshold: treshold time for switching between dirty rect
  756. mode and fullscreen mode; defaults to updating at 80 frames per
  757. second, which is equal to 1000.0 / 80.0
  758. """
  759. LayeredUpdates.__init__(self, *sprites, **kwargs)
  760. self._clip = None
  761. self._use_update = False
  762. self._time_threshold = 1000.0 / 80.0 # 1000.0 / fps
  763. self._bgd = None
  764. for key, val in kwargs.items():
  765. if key in ['_use_update', '_time_threshold', '_default_layer']:
  766. if hasattr(self, key):
  767. setattr(self, key, val)
  768. def add_internal(self, sprite, layer=None):
  769. """Do not use this method directly.
  770. It is used by the group to add a sprite internally.
  771. """
  772. # check if all needed attributes are set
  773. if not hasattr(sprite, 'dirty'):
  774. raise AttributeError()
  775. if not hasattr(sprite, 'visible'):
  776. raise AttributeError()
  777. if not hasattr(sprite, 'blendmode'):
  778. raise AttributeError()
  779. if not isinstance(sprite, DirtySprite):
  780. raise TypeError()
  781. if sprite.dirty == 0: # set it dirty if it is not
  782. sprite.dirty = 1
  783. LayeredUpdates.add_internal(self, sprite, layer)
  784. def draw(self, surface, bgd=None):
  785. """draw all sprites in the right order onto the given surface
  786. LayeredDirty.draw(surface, bgd=None): return Rect_list
  787. You can pass the background too. If a self.bgd is already set to some
  788. value that is not None, then the bgd argument has no effect.
  789. """
  790. # speedups
  791. _orig_clip = surface.get_clip()
  792. _clip = self._clip
  793. if _clip is None:
  794. _clip = _orig_clip
  795. _surf = surface
  796. _sprites = self._spritelist
  797. _old_rect = self.spritedict
  798. _update = self.lostsprites
  799. _update_append = _update.append
  800. _ret = None
  801. _surf_blit = _surf.blit
  802. _rect = Rect
  803. if bgd is not None:
  804. self._bgd = bgd
  805. _bgd = self._bgd
  806. init_rect = self._init_rect
  807. _surf.set_clip(_clip)
  808. # -------
  809. # 0. decide whether to render with update or flip
  810. start_time = get_ticks()
  811. if self._use_update: # dirty rects mode
  812. # 1. find dirty area on screen and put the rects into _update
  813. # still not happy with that part
  814. for spr in _sprites:
  815. if 0 < spr.dirty:
  816. # chose the right rect
  817. if spr.source_rect:
  818. _union_rect = _rect(spr.rect.topleft,
  819. spr.source_rect.size)
  820. else:
  821. _union_rect = _rect(spr.rect)
  822. _union_rect_collidelist = _union_rect.collidelist
  823. _union_rect_union_ip = _union_rect.union_ip
  824. i = _union_rect_collidelist(_update)
  825. while -1 < i:
  826. _union_rect_union_ip(_update[i])
  827. del _update[i]
  828. i = _union_rect_collidelist(_update)
  829. _update_append(_union_rect.clip(_clip))
  830. if _old_rect[spr] is not init_rect:
  831. _union_rect = _rect(_old_rect[spr])
  832. _union_rect_collidelist = _union_rect.collidelist
  833. _union_rect_union_ip = _union_rect.union_ip
  834. i = _union_rect_collidelist(_update)
  835. while -1 < i:
  836. _union_rect_union_ip(_update[i])
  837. del _update[i]
  838. i = _union_rect_collidelist(_update)
  839. _update_append(_union_rect.clip(_clip))
  840. # can it be done better? because that is an O(n**2) algorithm in
  841. # worst case
  842. # clear using background
  843. if _bgd is not None:
  844. for rec in _update:
  845. _surf_blit(_bgd, rec, rec)
  846. # 2. draw
  847. for spr in _sprites:
  848. if 1 > spr.dirty:
  849. if spr._visible:
  850. # sprite not dirty; blit only the intersecting part
  851. if spr.source_rect is not None:
  852. # For possible future speed up, source_rect's data
  853. # can be prefetched outside of this loop.
  854. _spr_rect = _rect(spr.rect.topleft,
  855. spr.source_rect.size)
  856. rect_offset_x = spr.source_rect[0] - _spr_rect[0]
  857. rect_offset_y = spr.source_rect[1] - _spr_rect[1]
  858. else:
  859. _spr_rect = spr.rect
  860. rect_offset_x = -_spr_rect[0]
  861. rect_offset_y = -_spr_rect[1]
  862. _spr_rect_clip = _spr_rect.clip
  863. for idx in _spr_rect.collidelistall(_update):
  864. # clip
  865. clip = _spr_rect_clip(_update[idx])
  866. _surf_blit(spr.image,
  867. clip,
  868. (clip[0] + rect_offset_x,
  869. clip[1] + rect_offset_y,
  870. clip[2],
  871. clip[3]),
  872. spr.blendmode)
  873. else: # dirty sprite
  874. if spr._visible:
  875. _old_rect[spr] = _surf_blit(spr.image,
  876. spr.rect,
  877. spr.source_rect,
  878. spr.blendmode)
  879. if spr.dirty == 1:
  880. spr.dirty = 0
  881. _ret = list(_update)
  882. else: # flip, full screen mode
  883. if _bgd is not None:
  884. _surf_blit(_bgd, (0, 0))
  885. for spr in _sprites:
  886. if spr._visible:
  887. _old_rect[spr] = _surf_blit(spr.image,
  888. spr.rect,
  889. spr.source_rect,
  890. spr.blendmode)
  891. _ret = [_rect(_clip)] # return only the part of the screen changed
  892. # timing for switching modes
  893. # How may a good threshold be found? It depends on the hardware.
  894. end_time = get_ticks()
  895. if end_time-start_time > self._time_threshold:
  896. self._use_update = False
  897. else:
  898. self._use_update = True
  899. ## # debug
  900. ## print " check: using dirty rects:", self._use_update
  901. # emtpy dirty rects list
  902. _update[:] = []
  903. # -------
  904. # restore original clip
  905. _surf.set_clip(_orig_clip)
  906. return _ret
  907. def clear(self, surface, bgd):
  908. """use to set background
  909. Group.clear(surface, bgd): return None
  910. """
  911. self._bgd = bgd
  912. def repaint_rect(self, screen_rect):
  913. """repaint the given area
  914. LayeredDirty.repaint_rect(screen_rect): return None
  915. screen_rect is in screen coordinates.
  916. """
  917. if self._clip:
  918. self.lostsprites.append(screen_rect.clip(self._clip))
  919. else:
  920. self.lostsprites.append(Rect(screen_rect))
  921. def set_clip(self, screen_rect=None):
  922. """clip the area where to draw; pass None (default) to reset the clip
  923. LayeredDirty.set_clip(screen_rect=None): return None
  924. """
  925. if screen_rect is None:
  926. self._clip = pygame.display.get_surface().get_rect()
  927. else:
  928. self._clip = screen_rect
  929. self._use_update = False
  930. def get_clip(self):
  931. """get the area where drawing will occur
  932. LayeredDirty.get_clip(): return Rect
  933. """
  934. return self._clip
  935. def change_layer(self, sprite, new_layer):
  936. """change the layer of the sprite
  937. LayeredUpdates.change_layer(sprite, new_layer): return None
  938. The sprite must have been added to the renderer already. This is not
  939. checked.
  940. """
  941. LayeredUpdates.change_layer(self, sprite, new_layer)
  942. if sprite.dirty == 0:
  943. sprite.dirty = 1
  944. def set_timing_treshold(self, time_ms):
  945. """set the treshold in milliseconds
  946. set_timing_treshold(time_ms): return None
  947. Defaults to 1000.0 / 80.0. This means that the screen will be painted
  948. using the flip method rather than the update method if the update
  949. method is taking so long to update the screen that the frame rate falls
  950. below 80 frames per second.
  951. """
  952. self._time_threshold = time_ms
  953. class GroupSingle(AbstractGroup):
  954. """A group container that holds a single most recent item.
  955. This class works just like a regular group, but it only keeps a single
  956. sprite in the group. Whatever sprite has been added to the group last will
  957. be the only sprite in the group.
  958. You can access its one sprite as the .sprite attribute. Assigning to this
  959. attribute will properly remove the old sprite and then add the new one.
  960. """
  961. def __init__(self, sprite=None):
  962. AbstractGroup.__init__(self)
  963. self.__sprite = None
  964. if sprite is not None:
  965. self.add(sprite)
  966. def copy(self):
  967. return GroupSingle(self.__sprite)
  968. def sprites(self):
  969. if self.__sprite is not None:
  970. return [self.__sprite]
  971. else:
  972. return []
  973. def add_internal(self, sprite):
  974. if self.__sprite is not None:
  975. self.__sprite.remove_internal(self)
  976. self.remove_internal(self.__sprite)
  977. self.__sprite = sprite
  978. def __nonzero__(self):
  979. return self.__sprite is not None
  980. def _get_sprite(self):
  981. return self.__sprite
  982. def _set_sprite(self, sprite):
  983. self.add_internal(sprite)
  984. sprite.add_internal(self)
  985. return sprite
  986. sprite = property(_get_sprite,
  987. _set_sprite,
  988. None,
  989. "The sprite contained in this group")
  990. def remove_internal(self, sprite):
  991. if sprite is self.__sprite:
  992. self.__sprite = None
  993. if sprite in self.spritedict:
  994. AbstractGroup.remove_internal(self, sprite)
  995. def has_internal(self, sprite):
  996. return self.__sprite is sprite
  997. # Optimizations...
  998. def __contains__(self, sprite):
  999. return self.__sprite is sprite
  1000. # Some different collision detection functions that could be used.
  1001. def collide_rect(left, right):
  1002. """collision detection between two sprites, using rects.
  1003. pygame.sprite.collide_rect(left, right): return bool
  1004. Tests for collision between two sprites. Uses the pygame.Rect colliderect
  1005. function to calculate the collision. It is intended to be passed as a
  1006. collided callback function to the *collide functions. Sprites must have
  1007. "rect" attributes.
  1008. New in pygame 1.8.0
  1009. """
  1010. return left.rect.colliderect(right.rect)
  1011. class collide_rect_ratio:
  1012. """A callable class that checks for collisions using scaled rects
  1013. The class checks for collisions between two sprites using a scaled version
  1014. of the sprites' rects. Is created with a ratio; the instance is then
  1015. intended to be passed as a collided callback function to the *collide
  1016. functions.
  1017. New in pygame 1.8.1
  1018. """
  1019. def __init__(self, ratio):
  1020. """create a new collide_rect_ratio callable
  1021. Ratio is expected to be a floating point value used to scale
  1022. the underlying sprite rect before checking for collisions.
  1023. """
  1024. self.ratio = ratio
  1025. def __call__(self, left, right):
  1026. """detect collision between two sprites using scaled rects
  1027. pygame.sprite.collide_rect_ratio(ratio)(left, right): return bool
  1028. Tests for collision between two sprites. Uses the pygame.Rect
  1029. colliderect function to calculate the collision after scaling the rects
  1030. by the stored ratio. Sprites must have "rect" attributes.
  1031. """
  1032. ratio = self.ratio
  1033. leftrect = left.rect
  1034. width = leftrect.width
  1035. height = leftrect.height
  1036. leftrect = leftrect.inflate(width * ratio - width,
  1037. height * ratio - height)
  1038. rightrect = right.rect
  1039. width = rightrect.width
  1040. height = rightrect.height
  1041. rightrect = rightrect.inflate(width * ratio - width,
  1042. height * ratio - height)
  1043. return leftrect.colliderect(rightrect)
  1044. def collide_circle(left, right):
  1045. """detect collision between two sprites using circles
  1046. pygame.sprite.collide_circle(left, right): return bool
  1047. Tests for collision between two sprites by testing whether two circles
  1048. centered on the sprites overlap. If the sprites have a "radius" attribute,
  1049. then that radius is used to create the circle; otherwise, a circle is
  1050. created that is big enough to completely enclose the sprite's rect as
  1051. given by the "rect" attribute. This function is intended to be passed as
  1052. a collided callback function to the *collide functions. Sprites must have a
  1053. "rect" and an optional "radius" attribute.
  1054. New in pygame 1.8.0
  1055. """
  1056. xdistance = left.rect.centerx - right.rect.centerx
  1057. ydistance = left.rect.centery - right.rect.centery
  1058. distancesquared = xdistance ** 2 + ydistance ** 2
  1059. if hasattr(left, 'radius'):
  1060. leftradius = left.radius
  1061. else:
  1062. leftrect = left.rect
  1063. # approximating the radius of a square by using half of the diagonal,
  1064. # might give false positives (especially if its a long small rect)
  1065. leftradius = 0.5 * ((leftrect.width ** 2 + leftrect.height ** 2) ** 0.5)
  1066. # store the radius on the sprite for next time
  1067. setattr(left, 'radius', leftradius)
  1068. if hasattr(right, 'radius'):
  1069. rightradius = right.radius
  1070. else:
  1071. rightrect = right.rect
  1072. # approximating the radius of a square by using half of the diagonal
  1073. # might give false positives (especially if its a long small rect)
  1074. rightradius = 0.5 * ((rightrect.width ** 2 + rightrect.height ** 2) ** 0.5)
  1075. # store the radius on the sprite for next time
  1076. setattr(right, 'radius', rightradius)
  1077. return distancesquared <= (leftradius + rightradius) ** 2
  1078. class collide_circle_ratio(object):
  1079. """detect collision between two sprites using scaled circles
  1080. This callable class checks for collisions between two sprites using a
  1081. scaled version of a sprite's radius. It is created with a ratio as the
  1082. argument to the constructor. The instance is then intended to be passed as
  1083. a collided callback function to the *collide functions.
  1084. New in pygame 1.8.1
  1085. """
  1086. def __init__(self, ratio):
  1087. """creates a new collide_circle_ratio callable instance
  1088. The given ratio is expected to be a floating point value used to scale
  1089. the underlying sprite radius before checking for collisions.
  1090. When the ratio is ratio=1.0, then it behaves exactly like the
  1091. collide_circle method.
  1092. """
  1093. self.ratio = ratio
  1094. def __call__(self, left, right):
  1095. """detect collision between two sprites using scaled circles
  1096. pygame.sprite.collide_circle_radio(ratio)(left, right): return bool
  1097. Tests for collision between two sprites by testing whether two circles
  1098. centered on the sprites overlap after scaling the circle's radius by
  1099. the stored ratio. If the sprites have a "radius" attribute, that is
  1100. used to create the circle; otherwise, a circle is created that is big
  1101. enough to completely enclose the sprite's rect as given by the "rect"
  1102. attribute. Intended to be passed as a collided callback function to the
  1103. *collide functions. Sprites must have a "rect" and an optional "radius"
  1104. attribute.
  1105. """
  1106. ratio = self.ratio
  1107. xdistance = left.rect.centerx - right.rect.centerx
  1108. ydistance = left.rect.centery - right.rect.centery
  1109. distancesquared = xdistance ** 2 + ydistance ** 2
  1110. if hasattr(left, "radius"):
  1111. leftradius = left.radius * ratio
  1112. else:
  1113. leftrect = left.rect
  1114. leftradius = ratio * 0.5 * ((leftrect.width ** 2 + leftrect.height ** 2) ** 0.5)
  1115. # store the radius on the sprite for next time
  1116. setattr(left, 'radius', leftradius)
  1117. if hasattr(right, "radius"):
  1118. rightradius = right.radius * ratio
  1119. else:
  1120. rightrect = right.rect
  1121. rightradius = ratio * 0.5 * ((rightrect.width ** 2 + rightrect.height ** 2) ** 0.5)
  1122. # store the radius on the sprite for next time
  1123. setattr(right, 'radius', rightradius)
  1124. return distancesquared <= (leftradius + rightradius) ** 2
  1125. def collide_mask(left, right):
  1126. """collision detection between two sprites, using masks.
  1127. pygame.sprite.collide_mask(SpriteLeft, SpriteRight): bool
  1128. Tests for collision between two sprites by testing if their bitmasks
  1129. overlap. If the sprites have a "mask" attribute, that is used as the mask;
  1130. otherwise, a mask is created from the sprite image. Intended to be passed
  1131. as a collided callback function to the *collide functions. Sprites must
  1132. have a "rect" and an optional "mask" attribute.
  1133. New in pygame 1.8.0
  1134. """
  1135. xoffset = right.rect[0] - left.rect[0]
  1136. yoffset = right.rect[1] - left.rect[1]
  1137. try:
  1138. leftmask = left.mask
  1139. except AttributeError:
  1140. leftmask = from_surface(left.image)
  1141. try:
  1142. rightmask = right.mask
  1143. except AttributeError:
  1144. rightmask = from_surface(right.image)
  1145. return leftmask.overlap(rightmask, (xoffset, yoffset))
  1146. def spritecollide(sprite, group, dokill, collided=None):
  1147. """find Sprites in a Group that intersect another Sprite
  1148. pygame.sprite.spritecollide(sprite, group, dokill, collided=None):
  1149. return Sprite_list
  1150. Return a list containing all Sprites in a Group that intersect with another
  1151. Sprite. Intersection is determined by comparing the Sprite.rect attribute
  1152. of each Sprite.
  1153. The dokill argument is a bool. If set to True, all Sprites that collide
  1154. will be removed from the Group.
  1155. The collided argument is a callback function used to calculate if two
  1156. sprites are colliding. it should take two sprites as values, and return a
  1157. bool value indicating if they are colliding. If collided is not passed, all
  1158. sprites must have a "rect" value, which is a rectangle of the sprite area,
  1159. which will be used to calculate the collision.
  1160. """
  1161. if dokill:
  1162. crashed = []
  1163. append = crashed.append
  1164. if collided:
  1165. for s in group.sprites():
  1166. if collided(sprite, s):
  1167. s.kill()
  1168. append(s)
  1169. else:
  1170. spritecollide = sprite.rect.colliderect
  1171. for s in group.sprites():
  1172. if spritecollide(s.rect):
  1173. s.kill()
  1174. append(s)
  1175. return crashed
  1176. elif collided:
  1177. return [s for s in group if collided(sprite, s)]
  1178. else:
  1179. spritecollide = sprite.rect.colliderect
  1180. return [s for s in group if spritecollide(s.rect)]
  1181. def groupcollide(groupa, groupb, dokilla, dokillb, collided=None):
  1182. """detect collision between a group and another group
  1183. pygame.sprite.groupcollide(groupa, groupb, dokilla, dokillb):
  1184. return dict
  1185. Given two groups, this will find the intersections between all sprites in
  1186. each group. It returns a dictionary of all sprites in the first group that
  1187. collide. The value for each item in the dictionary is a list of the sprites
  1188. in the second group it collides with. The two dokill arguments control if
  1189. the sprites from either group will be automatically removed from all
  1190. groups. Collided is a callback function used to calculate if two sprites
  1191. are colliding. it should take two sprites as values, and return a bool
  1192. value indicating if they are colliding. If collided is not passed, all
  1193. sprites must have a "rect" value, which is a rectangle of the sprite area
  1194. that will be used to calculate the collision.
  1195. """
  1196. crashed = {}
  1197. SC = spritecollide
  1198. if dokilla:
  1199. for s in groupa.sprites():
  1200. c = SC(s, groupb, dokillb, collided)
  1201. if c:
  1202. crashed[s] = c
  1203. s.kill()
  1204. else:
  1205. for s in groupa:
  1206. c = SC(s, groupb, dokillb, collided)
  1207. if c:
  1208. crashed[s] = c
  1209. return crashed
  1210. def spritecollideany(sprite, group, collided=None):
  1211. """finds any sprites in a group that collide with the given sprite
  1212. pygame.sprite.spritecollideany(sprite, group): return sprite
  1213. Given a sprite and a group of sprites, this will return return any single
  1214. sprite that collides with with the given sprite. If there are no
  1215. collisions, then this returns None.
  1216. If you don't need all the features of the spritecollide function, this
  1217. function will be a bit quicker.
  1218. Collided is a callback function used to calculate if two sprites are
  1219. colliding. It should take two sprites as values and return a bool value
  1220. indicating if they are colliding. If collided is not passed, then all
  1221. sprites must have a "rect" value, which is a rectangle of the sprite area,
  1222. which will be used to calculate the collision.
  1223. """
  1224. if collided:
  1225. for s in group:
  1226. if collided(sprite, s):
  1227. return s
  1228. else:
  1229. # Special case old behaviour for speed.
  1230. spritecollide = sprite.rect.colliderect
  1231. for s in group:
  1232. if spritecollide(s.rect):
  1233. return s
  1234. return None