moved decoding of layers up to TileMap
[2dworld.git] / tiledtmxloader3 / examples / 02_render_features / 02_4_collapse.py
blobfdb5e7171e62a1b8e14feec5af6296b2dfb7b777
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 """
6 This is the pygame minimal example.
8 """
11 __revision__ = "$Rev$"
12 __version__ = "3.0.0." + __revision__[6:-2]
13 __author__ = 'DR0ID @ 2009-2011'
15 import sys
16 import os
18 import pygame
20 try:
21 import _path
22 except:
23 pass
25 import tiledtmxloader
27 # -----------------------------------------------------------------------------
29 def main():
30 """
31 Main method.
32 """
33 args = sys.argv[1:]
34 if len(args) < 1:
35 path_to_map = os.path.join(os.pardir, "001-1.tmx")
36 print(("usage: python %s your_map.tmx\n\nUsing default map '%s'\n" % \
37 (os.path.basename(__file__), path_to_map)))
38 else:
39 path_to_map = args[0]
41 demo_pygame(path_to_map)
43 # -----------------------------------------------------------------------------
45 def demo_pygame(file_name):
46 """
47 This is the demo showing the usage of the collapse feature.
48 """
50 # parser the map (it is done here to initialize the
51 # window the same size as the map if it is small enough)
52 world_map = tiledtmxloader.tmxreader.TileMapParser().parse_decode(file_name)
54 # init pygame and set up a screen
55 pygame.init()
56 pygame.display.set_caption("tiledtmxloader - " + file_name + \
57 " - keys: arrows, 0-9, r")
58 screen_width = min(1024, world_map.pixel_width)
59 screen_height = min(768, world_map.pixel_height)
60 screen = pygame.display.set_mode((screen_width, screen_height))
62 # load the images using pygame
63 resources = tiledtmxloader.helperspygame.ResourceLoaderPygame()
64 resources.load(world_map)
66 # prepare map rendering
67 assert world_map.orientation == "orthogonal"
69 # renderer
70 renderer = tiledtmxloader.helperspygame.RendererPygame()
72 # cam_offset is for scrolling
73 cam_world_pos_x = 0
74 cam_world_pos_y = 0
76 # set initial cam position and size
77 renderer.set_camera_position_and_size(cam_world_pos_x, cam_world_pos_y, \
78 screen_width, screen_height, "topleft")
80 # retrieve the layers
81 sprite_layers = tiledtmxloader.helperspygame.get_layers_from_map(resources)
83 # layer on/off keys
84 num_keys = [pygame.K_0, pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, \
85 pygame.K_5, pygame.K_6, pygame.K_7, pygame.K_8, pygame.K_9]
87 # variables for the main loop
88 clock = pygame.time.Clock()
89 running = True
90 # set up timer for fps printing
91 pygame.time.set_timer(pygame.USEREVENT, 1000)
93 # mainloop
94 while running:
95 clock.tick()
97 # event handling
98 for event in pygame.event.get():
99 if event.type == pygame.QUIT:
100 running = False
101 elif event.type == pygame.USEREVENT:
102 print("fps: ", clock.get_fps())
103 elif event.type == pygame.KEYDOWN:
104 if event.key == pygame.K_ESCAPE:
105 running = False
106 elif event.key == pygame.K_UP:
107 cam_world_pos_y -= world_map.tileheight
108 elif event.key == pygame.K_DOWN:
109 cam_world_pos_y += world_map.tileheight
110 elif event.key == pygame.K_RIGHT:
111 cam_world_pos_x += world_map.tilewidth
112 elif event.key == pygame.K_LEFT:
113 cam_world_pos_x -= world_map.tilewidth
114 elif event.key == pygame.K_r:
115 print("resetting layers!")
116 sprite_layers = tiledtmxloader.helperspygame.get_layers_from_map(resources)
117 elif event.key in num_keys:
118 # find out which layer to manipulate
119 idx = num_keys.index(event.key)
120 # make sure this layer exists
121 if idx < len(world_map.layers):
122 sprite_layers[idx] = tiledtmxloader.helperspygame.SpriteLayer.collapse(sprite_layers[idx])
123 print("layer %s has collapse level: %s" % \
124 (idx, sprite_layers[idx].get_collapse_level()))
125 else:
126 print("no such layer or more than 10 layers: " + str(idx))
128 # adjust camera to position according to the keypresses
129 renderer.set_camera_position(cam_world_pos_x, cam_world_pos_y, "topleft")
131 # clear screen, might be left out if every pixel is redrawn anyway
132 screen.fill((0, 0, 0))
134 # render the map
135 for sprite_layer in sprite_layers:
136 if sprite_layer.is_object_group:
137 # we dont draw the object group layers
138 # you should filter them out if not needed
139 continue
140 else:
141 renderer.render_layer(screen, sprite_layer)
143 pygame.display.flip()
145 # -----------------------------------------------------------------------------
147 if __name__ == '__main__':
149 main()