Mercurial > pygame-tutorial
annotate 003/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 | 2a0f43c87b80 |
| children |
| rev | line source |
|---|---|
|
7
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
3 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
4 import pygame |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
5 import pygame.locals as pg |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
6 from pytmx import tmxloader |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
7 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
8 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
9 FACING = { |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
10 (0, -1): 0, |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
11 (-1, 0): 1, |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
12 (0, 1): 2, |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
13 (1, 0): 3, |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
14 } |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
15 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
16 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
17 def load_sheet(filename, width=32, height=None): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
18 if height is None: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
19 height = width |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
20 image = pygame.image.load(filename) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
21 image = image.convert() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
22 image.set_colorkey((0xff, 0, 0xff)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
23 w, h = image.get_size() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
24 images = [] |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
25 for x in xrange(w // width): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
26 for y in xrange(h // height): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
27 rect = pygame.rect.Rect(x * width, y * height, width, height) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
28 images.append(image.subsurface(rect)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
29 return images |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
30 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
31 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
32 class ShadowSprite(pygame.sprite.Sprite): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
33 def __init__(self, sprite): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
34 super(ShadowSprite, self).__init__() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
35 self.spritesheet = load_sheet('effects.png', 32) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
36 self.image = self.spritesheet[0] |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
37 self.rect = self.image.get_rect() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
38 self.image.set_alpha(0.6 * 255) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
39 self.sprite = sprite |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
40 self.rect.midbottom = self.sprite.rect.midbottom |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
41 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
42 def update(self): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
43 self.rect.midbottom = self.sprite.rect.midbottom |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
44 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
45 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
46 class HeroSprite(pygame.sprite.Sprite): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
47 def __init__(self, (x, y)): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
48 super(HeroSprite, self).__init__() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
49 self.spritesheet = load_sheet('spritesheet.png', 64) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
50 self.facing = 2 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
51 self.image = self.spritesheet[self.facing] |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
52 self.rect = self.image.get_rect() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
53 self.rect.midbottom = (x * 32 + 16, y * 32 + 32) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
54 self.animations = [] |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
55 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
56 def move(self, (dx, dy)): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
57 self.facing = FACING[dx, dy] |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
58 self.animations.append(self.animation_walk((dx, dy))) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
59 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
60 def animation_walk(self, (dx, dy)): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
61 for step in xrange(8): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
62 self.image = self.spritesheet[self.facing + step * 4 + 4] |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
63 self.rect.move_ip(dx * 4, dy * 4) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
64 yield |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
65 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
66 def animation_stand(self): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
67 self.image = self.spritesheet[self.facing] |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
68 return |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
69 yield |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
70 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
71 def update(self): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
72 if not self.animations: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
73 self.animations.append(self.animation_stand()) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
74 while self.animations: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
75 try: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
76 self.animations[0].next() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
77 return |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
78 except StopIteration: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
79 self.animations.pop(0) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
80 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
81 class Level(object): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
82 def __init__(self, filename): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
83 self.tiled_map = tmxloader.load_pygame(filename) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
84 self.hero_position = 1, 1 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
85 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
86 def render(self, screen): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
87 w = self.tiled_map.tilewidth |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
88 h = self.tiled_map.tileheight |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
89 get_image = self.tiled_map.getTileImage |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
90 for l, layer in enumerate(self.tiled_map.tilelayers): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
91 for y in xrange(self.tiled_map.height): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
92 for x in xrange(self.tiled_map.width): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
93 image = get_image(x, y, l) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
94 if image: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
95 screen.blit(image, (x * w, y * h)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
96 x, y = self.hero_position |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
97 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
98 def get_properties(self, (x, y)): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
99 properties = {} |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
100 for l, layer in enumerate(self.tiled_map.tilelayers): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
101 properties.update(self.tiled_map.getTileProperties((x, y, l)) or {}) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
102 return properties |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
103 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
104 def is_blocked(self, (x, y)): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
105 return self.get_properties((x, y)).get('block') |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
106 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
107 def move_hero(self, (dx, dy)): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
108 x, y = self.hero_position |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
109 if not self.is_blocked((x + dx, y + dy)): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
110 self.hero_position = x + dx, y + dy |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
111 return True |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
112 return False |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
113 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
114 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
115 class Game(object): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
116 def __init__(self): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
117 pygame.display.init() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
118 pygame.font.init() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
119 self.screen = pygame.display.set_mode((800, 600)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
120 self.screen.fill((0x66, 0x66, 0x66)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
121 self.keys = { |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
122 pg.K_UP: (self.move_hero, (0, -1)), |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
123 pg.K_DOWN: (self.move_hero, (0, 1)), |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
124 pg.K_LEFT: (self.move_hero, (-1, 0)), |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
125 pg.K_RIGHT: (self.move_hero, (1, 0)), |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
126 pg.K_ESCAPE: (self.quit,), |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
127 } |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
128 self.shadow_sprites = pygame.sprite.RenderUpdates() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
129 self.sprites = pygame.sprite.RenderUpdates() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
130 self.key = None |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
131 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
132 def load_level(self, filename): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
133 self.level = Level(filename) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
134 self.hero_sprite = HeroSprite(self.level.hero_position) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
135 self.sprites.add(self.hero_sprite) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
136 self.shadow_sprites.add(ShadowSprite(self.hero_sprite)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
137 self.background = self.screen.copy() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
138 self.level.render(self.background) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
139 self.screen.blit(self.background, (0, 0)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
140 self.sprites.draw(self.screen) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
141 pygame.display.update() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
142 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
143 def quit(self): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
144 self.game_over = True |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
145 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
146 def move_hero(self, direction): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
147 if self.level.move_hero(direction): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
148 self.hero_sprite.move(direction) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
149 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
150 def control(self): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
151 for event in pygame.event.get(): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
152 if event.type == pg.QUIT: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
153 self.quit() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
154 elif event.type == pg.KEYDOWN: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
155 self.key = event.key |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
156 if not self.hero_sprite.animations: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
157 keys = pygame.key.get_pressed() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
158 for key, action in self.keys.iteritems(): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
159 if keys[key] or self.key == key: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
160 action[0](*action[1:]) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
161 break |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
162 self.key = None |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
163 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
164 def display(self): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
165 self.sprites.clear(self.screen, self.background) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
166 self.shadow_sprites.clear(self.screen, self.background) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
167 self.sprites.update() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
168 self.shadow_sprites.update() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
169 dirty = [] |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
170 dirty.extend(self.shadow_sprites.draw(self.screen)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
171 dirty.extend(self.sprites.draw(self.screen)) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
172 pygame.display.update(dirty) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
173 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
174 def run(self): |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
175 self.clock = pygame.time.Clock() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
176 self.game_over = False |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
177 while not self.game_over: |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
178 self.display() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
179 self.control() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
180 self.clock.tick(30) |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
181 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
182 |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
183 if __name__ == '__main__': |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
184 game = Game() |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
185 game.load_level('level.tmx') |
|
2a0f43c87b80
Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff
changeset
|
186 game.run() |
