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