Source

easypg / easypg / sprites.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Copyright (c) 2007-2012 Nick Efford <nick.efford (at) gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.


"""Classes to simplify the use of sprites in Pygame."""


__author__ = 'Nick Efford'
__version__ = '0.4'


import collections
import io
import os
import re
import zipfile
import pygame


class SpriteError(Exception):
    pass


class SimpleSprite(pygame.sprite.Sprite):
    """A basic sprite represented by a single, static image.

       A display surface and the path to the image file must be
       supplied when creating a SimpleSprite.  The background
       colour (which will be transparent when rendering the sprite)
       and the initial position can be specified if the defaults
       are not suitable.
    """

    def __init__(self, screen, image_path, background=None, position=None):
        """Creates a SimpleSprite for the given Pygame display surface,
           using the image at the given location.

           By default, the colour of the pixel in the top-left corner of
           the sprite image is assumed to be a background colour that
           should be transparent when the sprite is rendered on screen.
           If some other specific colour represents the background, this
           can be provided as a 3-tuple using the 'background' argument.

           By default, the sprite will be positioned in the centre of the
           the display surface on which it is drawn. Other coordinates can
           be specified as a 2-tuple using the 'position' argument.
        """

        super().__init__()

        self.screen = screen

        # Load image and make background transparent

        self.image = pygame.image.load(image_path).convert()

        if not background:
            background = self.image.get_at((0, 0))
        self.image.set_colorkey(background)

        # Position the sprite

        self.rect = self.image.get_rect()
        if position:
            self.rect.center = position
        else:
            w, h = self.screen.get_size()
            self.rect.center = (w / 2, h / 2)

    def update(self):
        """Updates this sprite.

           The implementation provided here simply invokes the move
           and check_bounds methods.
        """

        self.move()
        self.check_bounds()

    def move(self):
        """Moves this sprite.

           The implementation provided here is a stub that can be
           overridden in subclasses.
        """

    def check_bounds(self):
        """Checks whether this sprite is within bounds and modifies
           sprite properties if necessary.

           The implementation provided here is a stub that can be
           overridden in subclasses.
        """


class Sprite(pygame.sprite.Sprite):
    """A sprite consisting of (potentially) multiple images.

       It is assumed that the sprite can be in different states.  For each
       state, there can be different sets of sprite images representing
       the eight compass directions, and for each direction there can be a
       stack of images representing an animation sequence.

       The images themselves can be in a single directory or Zip archive
       and the main part of the filename (excluding the extension) should
       have the following format:

           [<state>]_[<direction>]_<num>

       <state> is optional.  If present, it consists of a short string of
       ASCII alphabetic characters.

       <direction> is also optional.  If present, it must be one of the
       following strings: 'n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'

       <num> is two decimal digits.  An image numbered 00 is required,
       but images numbered 01...99 are optional.

       Images are loaded into a dictionary called 'images'.  Note that this
       dictionary is not defined here.  IT MUST BE DEFINED IN A SUBCLASS,
       ideally as a class variable so that the loaded images can be shared
       by all instances of that subclass.

       Keys of this dictionary are states, with '-' representing an
       undefined state; values are themselves dictionaries, each of which
       has compass directions as keys (with '-' representing an undefined
       direction) and a corresponding list of Pygame surfaces as the
       value associated with each key.  This list of Pygame surfaces is
       used as an animation sequence for a particular state/direction.
    """

    image_regexp = re.compile('([a-zA-Z]*)_([a-zA-Z]*)_(\d\d)')

    def __init__(self, screen, path, background=None, position=None,
                 state='-', direction='-'):
        """Creates a Sprite object for the given Pygame display surface,
           using the images at the given location.

           The required 'path' argument is either a path to a directory
           or a path to a Zip archive, containing sprite images.

           By default, the colour of the pixel in the top-left corner of
           each image is assumed to be a background colour that should
           be transparent when the sprite is rendered on screen.  If some
           other specific colour represents the background, this can be
           provided as a 3-tuple using the 'background' argument.

           By default, the sprite will be positioned in the centre of the
           the display surface on which it is drawn. Other coordinates can
           be specified as a 2-tuple using the 'position' argument.

           An initial state and direction can be specified if needed;
           they default to '-', meaning 'undefined'.
        """

        super().__init__()

        self.screen = screen
        self.background = background
        self.state = state
        self.direction = direction

        if not hasattr(self, 'images'):
            raise SpriteError('no image store defined')
        elif not isinstance(self.images, dict):
            raise SpriteError('image store must be a dictionary')

        if os.path.isfile(path) and path.endswith('.zip'):
            self._load_from_zip(path)
        elif os.path.isdir(path):
            self._load_from_dir(path)
        else:
            raise SpriteError('invalid image location')

        self.frame = 0
        self.counter = self.anim_delay = 3
        self.sequence = self.images[self.state][self.direction]
        self.image = self.sequence[self.frame]

        self.rect = self.image.get_rect()
        if position:
            self.rect.center = position
        else:
            w, h = self.screen.get_size()
            self.rect.center = (w / 2, h / 2)

    def _load_from_dir(self, path):
        contents = sorted(os.listdir(path))
        for (state, direction, _), name in self._find_images(contents):
            image_path = os.path.join(path, name)
            image = pygame.image.load(image_path).convert()
            self._set_background(image)
            self.images[state][direction].append(image)

    def _load_from_zip(self, path):
        archive = zipfile.ZipFile(path)
        contents = sorted(archive.namelist())
        for (state, direction, _), name in self._find_images(contents):
            data = archive.open(name).read()
            source = io.BytesIO(data)
            image = pygame.image.load(source).convert()
            self._set_background(image)
            self.images[state][direction].append(image)

    def _find_images(self, names):
        found = collections.OrderedDict()

        for name in names:
            match = self.image_regexp.match(name)
            if match:
                # Valid image file, so record details in dictionary

                state = match.group(1) or '-'
                direction = match.group(2) or '-'
                num = match.group(3)
                found[(state, direction, num)] = name

                # Prepare image store to hold loaded images

                if state not in self.images:
                    self.images[state] = {}
                if direction not in self.images[state]:
                    self.images[state][direction] = []

        return found.items()

    def _set_background(self, image):
        if self.background:
            image.set_colorkey(self.background)
        else:
            colour = image.get_at((0, 0))
            image.set_colorkey(colour)

    def update(self):
        """Updates this sprite.

           The implementation provided here simply invokes the check_state,
           animate, move and check_bounds methods.
        """

        self.check_state()
        self.animate()
        self.move()
        self.check_bounds()

    def check_state(self):
        """Checks this sprite's state, changing it if necessary.

           The implementation provided here is a stub that can be
           overridden in subclasses.
        """

    def animate(self):
        """Animates this sprite by changing its image at regular intervals."""

        self.counter -= 1
        if self.counter == 0:
            self.counter = self.anim_delay
            self.frame += 1
            if self.frame >= len(self.sequence):
                self.frame = 0
            self.image = self.sequence[self.frame]

    def move(self):
        """Moves this sprite.

           The implementation provided here is a stub that can be
           overridden in subclasses.
        """

    def check_bounds(self):
        """Checks whether this sprite is within bounds and modifies
           sprite properties if necessary.

           The implementation provided here is a stub that can be
           overridden in subclasses.
        """


class Label(pygame.sprite.Sprite):
    """A sprite for a text label - e.g., a score or timer.

       A display surface, some initial text, a Font object, a colour
       (RGB 3-tuple) and a position (2-tuple) must be provided when
       creating a Label object.  An anchor point may optionally be
       specified (this is the point on the label boundary corresponding
       to the given position).

       To update a Label object, simply assign a string to its 'text'
       field; the assigned text will be rendered on the next update of
       the sprite.
    """

    def __init__(self, screen, text, font, colour, position, anchor='c'):
        """Creates a Label for the given Pygame display surface, using
           the string, font, colour and position provided.

           By default, the Label will be centered at the given position
           (anchor='c'), but other anchor points are possible:

               'tr' - top right
               'mr' - middle right
               'br' - bottom right
               'mb' - middle bottom
               'bl' - bottom left
               'ml' - middle left
               'tl' - top left
               'mt' - middle top
        """

        super().__init__()

        self.screen = screen
        self._text = text
        self.font = font
        self.colour = colour
        self.position = position
        self.anchor = anchor.lower()

        self._create_image()
        self.refresh = False

    @property
    def text(self):
        return self._text

    @text.setter
    def text(self, new_text):
        self._text = new_text
        self.refresh = True

    def _create_image(self):
        self.image = self.font.render(self.text, True, self.colour)
        self.rect = self.image.get_rect()

        if self.anchor == 'tr':
            self.rect.topright = self.position
        elif self.anchor == 'mr':
            self.rect.midright = self.position
        elif self.anchor == 'br':
            self.rect.bottomright = self.position
        elif self.anchor == 'mb':
            self.rect.midbottom = self.position
        elif self.anchor == 'bl':
            self.rect.bottomleft = self.position
        elif self.anchor == 'ml':
            self.rect.midleft = self.position
        elif self.anchor == 'tl':
            self.rect.topleft = self.position
        elif self.anchor == 'mt':
            self.rect.midtop = self.position
        else:
            # default to centre
            self.rect.center = self.position

        self.refresh = False

    def update(self):
        """Updates this Label, rendering its image again if the text
           has changed since it was last rendered.
        """

        if self.refresh:
            self._create_image()


class Integer(Label):
    """A specialisation of Label, better suited to integer scores, etc.

       This class adds a 'value' field, to which an integer can be assigned.
       The field can also be incremented & decremented using += & -=.
       The formatting used to convert the value into text can be specified
       when creating the label, or by assigning a format to the 'fmt' field.
    """

    def __init__(
      self, screen, value, font, colour, position, anchor='c', fmt='d'):
        """Creates an Integer for the given Pygame display surface,
           using the value, font, colour and position provided.

           As with Label, an optional anchor point can be specified.
           A format can also be specified if required, using standard
           Python format specifier syntax.
        """

        self._value = value
        self.fmt = fmt
        text = format(value, fmt)

        super().__init__(screen, text, font, colour, position, anchor)

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, new_value):
        self._value = new_value
        self.text = format(new_value, self.fmt)