Mercurial > tiling
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 |
| rev | line source |
|---|---|
| 0 | 1 #!/usr/bin/env python |
| 2 # -*- coding: utf-8 -*- | |
| 3 | |
| 4 import pygame | |
| 5 | |
| 6 | |
| 7 def load_atlas(filename, width=32, height=None): | |
| 8 image = pygame.image.load(filename) | |
| 9 image_width, image_height = image.get_size() | |
| 10 w, h = width, height or width | |
| 11 return [ | |
| 12 image.subsurface((x * w, y * h, w, h)) | |
| 13 for y in xrange(0, image_height // h) | |
| 14 for x in xrange(0, image_width // w) | |
| 15 ] | |
| 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 | 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 |
