annotate tiling.py @ 1:17e9c6f3a0dd default tip

isolate into a function
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sun, 10 Mar 2013 20:45:29 +0100
parents 88a19853b1ac
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
1 #!/usr/bin/env python
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
3
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
4 import pygame
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
5
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
6
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
7 def load_atlas(filename, width=32, height=None):
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
8 image = pygame.image.load(filename)
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
9 image_width, image_height = image.get_size()
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
10 w, h = width, height or width
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
11 return [
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
12 image.subsurface((x * w, y * h, w, h))
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
13 for y in xrange(0, image_height // h)
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
14 for x in xrange(0, image_width // w)
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
15 ]
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
16
1
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
17 def render_grid(surf, grid, tiles):
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
18 for y, (row, nrow) in enumerate(zip(grid, grid[1:])):
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
19 for x, (tl, tr, bl, br) in enumerate(zip(row, row[1:], nrow, nrow[1:])):
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
20 surf.blit(grass[tl + tr * 2 + bl * 4 + br * 8], (x * 48, y * 32))
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
21
0
88a19853b1ac Initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
22
1
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
23 if __name__ == '__main__':
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
24 screen = pygame.display.set_mode((480, 480))
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
25 grass = load_atlas('grass_on_ground.png', 48, 32)
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
26 grid = [
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
27 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
28 [0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
29 [0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
30 [0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
31 [0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
32 [0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
33 [0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
34 [0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
35 [0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
36 [0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
37 [0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
38 [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
39 [0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
40 [0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
41 [0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
42 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
43 ]
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
44 render_grid(screen, grid, grass)
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
45 pygame.display.flip()
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
46 while pygame.event.wait().type not in {pygame.QUIT, pygame.KEYDOWN}:
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
47 pass
17e9c6f3a0dd isolate into a function
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 0
diff changeset
48