annotate 003/005.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 HeroSprite(pygame.sprite.Sprite):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
33 def __init__(self, (x, y)):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
34 super(HeroSprite, self).__init__()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
35 self.spritesheet = load_sheet('spritesheet.png', 64)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
36 self.facing = 2
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
37 self.image = self.spritesheet[self.facing]
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
38 self.rect = self.image.get_rect()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
39 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
40 self.animations = []
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 move(self, (dx, dy)):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
43 self.facing = FACING[dx, dy]
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
44 self.animations.append(self.animation_walk((dx, dy)))
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 def animation_walk(self, (dx, dy)):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
47 for step in xrange(8):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
48 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
49 self.rect.move_ip(dx * 4, dy * 4)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
50 yield
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
51
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
52 def animation_stand(self):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
53 self.image = self.spritesheet[self.facing]
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
54 return
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
55 yield
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
56
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
57 def update(self):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
58 if not self.animations:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
59 self.animations.append(self.animation_stand())
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
60 while self.animations:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
61 try:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
62 self.animations[0].next()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
63 return
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
64 except StopIteration:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
65 self.animations.pop(0)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
66
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
67 class Level(object):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
68 def __init__(self, filename):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
69 self.tiled_map = tmxloader.load_pygame(filename)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
70 self.hero_position = 1, 1
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
71
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
72 def render(self, screen):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
73 w = self.tiled_map.tilewidth
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
74 h = self.tiled_map.tileheight
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
75 get_image = self.tiled_map.getTileImage
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
76 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
77 for y in xrange(self.tiled_map.height):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
78 for x in xrange(self.tiled_map.width):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
79 image = get_image(x, y, l)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
80 if image:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
81 screen.blit(image, (x * w, y * h))
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
82 x, y = self.hero_position
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
83
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
84 def get_properties(self, (x, y)):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
85 properties = {}
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
86 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
87 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
88 return properties
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
89
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
90 def is_blocked(self, (x, y)):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
91 return self.get_properties((x, y)).get('block')
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
92
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
93 def move_hero(self, (dx, dy)):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
94 x, y = self.hero_position
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
95 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
96 self.hero_position = x + dx, y + dy
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
97 return True
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
98 return False
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
99
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
100
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
101 class Game(object):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
102 def __init__(self):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
103 pygame.display.init()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
104 pygame.font.init()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
105 self.screen = pygame.display.set_mode((800, 600))
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
106 self.screen.fill((0x66, 0x66, 0x66))
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
107 self.keys = {
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
108 pg.K_UP: (self.move_hero, (0, -1)),
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
109 pg.K_DOWN: (self.move_hero, (0, 1)),
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
110 pg.K_LEFT: (self.move_hero, (-1, 0)),
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
111 pg.K_RIGHT: (self.move_hero, (1, 0)),
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
112 pg.K_ESCAPE: (self.quit,),
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 self.sprites = pygame.sprite.RenderUpdates()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
115 self.key = None
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
116
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
117 def load_level(self, filename):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
118 self.level = Level(filename)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
119 self.hero_sprite = HeroSprite(self.level.hero_position)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
120 self.sprites.add(self.hero_sprite)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
121 self.background = self.screen.copy()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
122 self.level.render(self.background)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
123 self.screen.blit(self.background, (0, 0))
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
124 self.sprites.draw(self.screen)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
125 pygame.display.update()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
126
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
127 def quit(self):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
128 self.game_over = True
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
129
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
130 def move_hero(self, direction):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
131 if self.level.move_hero(direction):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
132 self.hero_sprite.move(direction)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
133
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
134 def control(self):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
135 for event in pygame.event.get():
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
136 if event.type == pg.QUIT:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
137 self.quit()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
138 elif event.type == pg.KEYDOWN:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
139 self.key = event.key
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
140 if not self.hero_sprite.animations:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
141 keys = pygame.key.get_pressed()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
142 for key, action in self.keys.iteritems():
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
143 if keys[key] or self.key == key:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
144 action[0](*action[1:])
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
145 break
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
146 self.key = None
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
147
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
148 def display(self):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
149 self.sprites.clear(self.screen, self.background)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
150 self.sprites.update()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
151 dirty = self.sprites.draw(self.screen)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
152 pygame.display.update(dirty)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
153
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
154 def run(self):
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
155 self.clock = pygame.time.Clock()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
156 self.game_over = False
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
157 while not self.game_over:
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
158 self.display()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
159 self.control()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
160 self.clock.tick(30)
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
161
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
162
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
163 if __name__ == '__main__':
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
164 game = Game()
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
165 game.load_level('level.tmx')
2a0f43c87b80 Fourth part of the tutorial
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
166 game.run()