Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/AnimatedSprite.py
blob: 76960c8222e7acba76029a0ff18fa4ad09dd054f (plain)
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
import pygame
#import os
#import random

class AnimatedSprite(pygame.sprite.Sprite):
    """
    AnimatedSprite is a custom Sprite class for animated objects

    images: list with sprite images
    frames: list with image# for every frame
    next_frames: list with next frame from current one
    dxs: list with dx for current frame
    dys: list with dy for current frame
    xini: initial x position
    yini: initial y position
    fps: fps
    """

    def __init__(self, images, frames, next_frames, 
                 dxs, dys, xini, yini, fps = 10):
        pygame.sprite.Sprite.__init__(self)
        self._images = images

        self._start = pygame.time.get_ticks()
        self._delay = 1000 / fps
        self._last_update = 0
        self._frame = 0

        self._frames = frames
        self._next_frames = next_frames
        self._dxs = dxs
        self._dys = dys
        self.x = xini
        self.y = yini
        self.move = 0

        self.image = self._images[self._frames[self._frame]]
        (self.width, self.height) = self.image.get_size() # assuming same size 4 all

#        self.update(pygame.time.get_ticks())

    def update(self, t, grid):

#        if t - self._last_update > self._delay:
#            print("Can't reach fps.")
        
        self.do_move(grid)
        self.image = self._images[self._frames[self._frame]]
        self._last_update = t

    def render(self, screen):
        screen.blit(self.image, (self.x, self.y))

    def do_move(self, grid):
        # by default, update according to sequence, override for more complex stuff
        self.x += self._dxs[self._frame]
        self.y += self._dys[self._frame]
        self._frame = self._next_frames[self._frame]

    def update_frame(self, fr):
        self._frame = fr

    def update_move(self,move):
        self.move = move

    def upper_left_tile(self):
        return ( self.x/50, self.y/50 )

    def draw_rect(self, screen):
        pygame.draw.rect(screen,(0,0,0),self.collision_rect(),1)

    def collision_rect(self):
        # by default use the same as image, override for more accurate collisions
        return self.rect()

    def rect(self):
        return (self.x, self.y, self.width, self.height)


if __name__ == "__main__":

    print("You can't run this file.")
    print("You have to import AnimatedSprite from another Python file.")