diff options
Diffstat (limited to 'AnimatedSprite.py~')
-rw-r--r-- | AnimatedSprite.py~ | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/AnimatedSprite.py~ b/AnimatedSprite.py~ new file mode 100644 index 0000000..968124d --- /dev/null +++ b/AnimatedSprite.py~ @@ -0,0 +1,189 @@ +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 + print self._frame + 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, grilla): + print "%d , %d" % (self.y/50,self.x/50) + if move == RIGHT: + if grilla[(self.y+50)/50][(self.x+70)/50] == 0: # arreglar + self.x += 8 + self._frame = self._next_frames[self._frame] + elif move == LEFT: + if grilla[(self.y+50)/50][(self.x-20)/50] == 0: # arreglar + self.x -= 8 + self._frame = self._next_frames[self._frame] + elif move == UP: + if grilla[(self.y)/50][(self.x+50)/50] == 0: # arreglar + self.y -= 6 + self._frame = self._next_frames[self._frame] + elif move == DOWN: + if grilla[(self.y+80)/50][(self.x+50)/50] == 0: # arreglar + self.y += 6 + 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((1200,900)) + clock = pygame.time.Clock() + + images = [] + master_image = pygame.image.load(os.path.join('images', 'xoxi-ninios-sprite-01.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))) + + bg_image = pygame.image.load(os.path.join('images', 'derechos-plaza-fondo.jpg')).convert_alpha() + + sps = [0, 1, 0, 2, 0, 1, 0, 3, 4, 5, 4, 6, 4, 5, 4, 7, 8, 9, 8, 10, + 8, 9, 8, 11, 15, 13, 15, 14, 15, 13, 15, 12] + nf = [1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15, 8, 17, 18, 19, + 20, 21, 22, 23, 16, 25, 26, 27, 28, 29, 30, 31, 24] + 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) + + grilla = [[0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1], + [0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0], + [0,0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0,0], + [0,0,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1], + [0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1],# aca mal + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1], + [0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1,1,0,0,1,1]] + + 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(8) + if event.key == pygame.K_LEFT: + move = LEFT + sp.updateFrame(16) + if event.key == pygame.K_RIGHT: + move = RIGHT + sp.updateFrame(24) + elif event.type == pygame.KEYUP: + if event.key == pygame.K_UP: + move = 0 + sp.updateFrame(0) + if event.key == pygame.K_DOWN: + move = 0 + sp.updateFrame(8) + if event.key == pygame.K_LEFT: + move = 0 + sp.updateFrame(16) + if event.key == pygame.K_RIGHT: + move = 0 + sp.updateFrame(24) + + sp.move(move, grilla) + +# screen.fill((100,100,100)) + screen.blit(bg_image, (0, 0)) + time_passed = clock.tick(15) + sp.update(time_passed) + sp.render(screen) + + pygame.display.flip() |