Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/AnimatedSprite_0.py
blob: bfe4e5967b7e10277eb5b51984c402acbae7827b (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
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
import pygame
import os

LEFT = 1
RIGHT = 2
UP = 3
DOWN =4

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

    images: list with sprite images
    sprites: 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, sprites, next_frames, 
                 dxs, dys, xini, yini, fps = 10):
        pygame.sprite.Sprite.__init__(self)
        self._images = images

        # Track the time we started, and the time between updates.
        # Then we can figure out when we have to switch the image.
        self._start = pygame.time.get_ticks()
        self._delay = 1000 / fps
        self._last_update = 0
        self._frame = 0

        self._sprites = sprites
        self._next_frames = next_frames
        self._dxs = dxs
        self._dys = dys
        self.x = xini
        self.y = yini

        self.image = self._images[self._sprites[self._frame]]

        # Call update to set our first image.
        self.update(pygame.time.get_ticks())

    def update(self, t):
        # Note that this doesn't work if it's been more that self._delay
        # time between calls to update(); we only update the image once
        # then, but it really should be updated twice.

#        print t

#        if t - self._last_update > self._delay:
#            if self._frame >= 2: #len(self._images): 
#                self._frame = 0
        self.image = self._images[self._sprites[self._frame]]
#            self.x += self._dxs[self._frame]
#            if move == RIGHT:
#                self.x += 5
#            elif move == LEFT:
#                self.x -= 5
#            self.y += self._dys[self._frame]
        self._last_update = t

    def render(self, screen):
#        self.update(pygame.time.get_ticks())
        screen.blit(self.image, (self.x, self.y))

    def move(self, move):
        if move == RIGHT:
            self.x += 5
            self._frame = self._next_frames[self._frame]
        elif move == LEFT:
            self.x -= 5
            self._frame = self._next_frames[self._frame]
        elif move == UP:
            self.y -= 5
            self._frame = self._next_frames[self._frame]
        elif move == DOWN:
            self.y += 5
            self._frame = self._next_frames[self._frame]
        
        

    def updateFrame(self, sprite):
        self._frame = sprite

if __name__ == "__main__":

    pygame.display.init()
    screen = pygame.display.set_mode((800,600))
    clock = pygame.time.Clock()

    images = []
    master_image = pygame.image.load(os.path.join('images', 'xoxi-ninios-sprite-01_800.png')).convert_alpha()
    master_width, master_height = master_image.get_size()
    w = int(master_width/4)
    h = int(master_height/4)
    for i in xrange(4):
        images.append(master_image.subsurface((i*w,0,w,h)))
    for i in xrange(4):
        images.append(master_image.subsurface((i*w,h,w,h)))
    for i in xrange(4):
        images.append(master_image.subsurface((i*w,2*h,w,h)))
    for i in xrange(4):
        images.append(master_image.subsurface((i*w,3*h,w,h)))

    sps = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    nf = [1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12]
    dxs = [5, 5, 5, 5, 5, -5, -5, -5, -5, -5]
    dys = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    sp = AnimatedSprite(images, sps, nf, dxs, dys, 50, 400, 10)

    playing = True

    move = 0
    
    while playing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                playing = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                pass
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    move = UP
                    sp.updateFrame(0)
                if event.key == pygame.K_DOWN:
                    move = DOWN
                    sp.updateFrame(4)
                if event.key == pygame.K_LEFT:
                    move = LEFT
                    sp.updateFrame(8)
                if event.key == pygame.K_RIGHT:
                    move = RIGHT
                    sp.updateFrame(15)
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    move = 0
                if event.key == pygame.K_DOWN:
                    move = 0
                if event.key == pygame.K_LEFT:
                    move = 0
                if event.key == pygame.K_RIGHT:
                    move = 0
        
        sp.move(move)

        screen.fill((100,100,100))
        time_passed = clock.tick(30)
        print move
        sp.update(time_passed)
        sp.render(screen)
        
        pygame.display.flip()