annotate 002/006.py @ 12:9dc97f88e274 default tip

Added CC0 license
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sun, 08 Jul 2012 19:56:38 +0200
parents 96fffc932ba7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
1 #!/usr/bin/env python
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
3
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
4 import pygame
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
5 import pygame.locals as pg
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
6 from pytmx import tmxloader
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
7
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
8
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
9 def load_sheet(filename, width=32, height=None):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
10 if height is None:
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
11 height = width
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
12 image = pygame.image.load(filename)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
13 image = image.convert()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
14 image.set_colorkey((0xff, 0, 0xff))
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
15 w, h = image.get_size()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
16 images = []
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
17 for x in xrange(w // width):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
18 for y in xrange(h // height):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
19 rect = pygame.rect.Rect(x * width, y * height, width, height)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
20 images.append(image.subsurface(rect))
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
21 return images
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
22
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
23
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
24 class HeroSprite(pygame.sprite.Sprite):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
25 def __init__(self, (x, y)):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
26 super(HeroSprite, self).__init__()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
27 self.spritesheet = load_sheet('spritesheet.png', 64)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
28 self.image = self.spritesheet[2]
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
29 self.rect = self.image.get_rect()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
30 self.rect.midbottom = (x * 32 + 16, y * 32 + 32)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
31
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
32 def move(self, (dx, dy)):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
33 self.rect.move_ip(dx * 32, dy * 32)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
34
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
35
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
36 class Level(object):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
37 def __init__(self, filename):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
38 self.tiled_map = tmxloader.load_pygame(filename)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
39 self.hero_position = 1, 1
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
40
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
41 def render(self, screen):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
42 w = self.tiled_map.tilewidth
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
43 h = self.tiled_map.tileheight
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
44 get_image = self.tiled_map.getTileImage
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
45 for l, layer in enumerate(self.tiled_map.tilelayers):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
46 for y in xrange(self.tiled_map.height):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
47 for x in xrange(self.tiled_map.width):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
48 image = get_image(x, y, l)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
49 if image:
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
50 screen.blit(image, (x * w, y * h))
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
51 x, y = self.hero_position
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
52
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
53 def get_properties(self, (x, y)):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
54 properties = {}
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
55 for l, layer in enumerate(self.tiled_map.tilelayers):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
56 properties.update(self.tiled_map.getTileProperties((x, y, l)) or {})
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
57 return properties
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
58
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
59 def is_blocked(self, (x, y)):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
60 return self.get_properties((x, y)).get('block')
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
61
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
62 def move_hero(self, (dx, dy)):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
63 x, y = self.hero_position
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
64 if not self.is_blocked((x + dx, y + dy)):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
65 self.hero_position = x + dx, y + dy
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
66 return True
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
67 return False
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
68
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
69
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
70 class Game(object):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
71 def __init__(self):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
72 pygame.display.init()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
73 pygame.font.init()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
74 self.screen = pygame.display.set_mode((800, 600))
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
75 self.screen.fill((0x66, 0x66, 0x66))
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
76 self.keys = {
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
77 pg.K_UP: (self.move_hero, (0, -1)),
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
78 pg.K_DOWN: (self.move_hero, (0, 1)),
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
79 pg.K_LEFT: (self.move_hero, (-1, 0)),
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
80 pg.K_RIGHT: (self.move_hero, (1, 0)),
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
81 pg.K_ESCAPE: (self.quit,),
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
82 }
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
83 self.sprites = pygame.sprite.RenderUpdates()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
84
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
85 def load_level(self, filename):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
86 self.level = Level(filename)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
87 self.hero_sprite = HeroSprite(self.level.hero_position)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
88 self.sprites.add(self.hero_sprite)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
89 self.background = self.screen.copy()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
90 self.level.render(self.background)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
91 self.screen.blit(self.background, (0, 0))
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
92 self.sprites.draw(self.screen)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
93 pygame.display.update()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
94
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
95 def quit(self):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
96 self.game_over = True
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
97
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
98 def move_hero(self, direction):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
99 if self.level.move_hero(direction):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
100 self.hero_sprite.move(direction)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
101
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
102 def control(self):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
103 event = pygame.event.wait()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
104 if event.type == pg.QUIT:
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
105 self.quit()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
106 elif event.type == pg.KEYDOWN:
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
107 try:
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
108 action = self.keys[event.key]
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
109 except KeyError:
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
110 pass
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
111 else:
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
112 action[0](*action[1:])
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
113
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
114 def display(self):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
115 self.sprites.clear(self.screen, self.background)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
116 self.sprites.update()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
117 dirty = self.sprites.draw(self.screen)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
118 pygame.display.update(dirty)
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
119
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
120 def run(self):
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
121 self.game_over = False
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
122 while not self.game_over:
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
123 self.display()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
124 self.control()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
125
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
126
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
127 if __name__ == '__main__':
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
128 game = Game()
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
129 game.load_level('level.tmx')
96fffc932ba7 Add sprites and animations
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
130 game.run()