2 # -*- coding: utf-8 -*-
6 This is the pygame minimal example.
11 __revision__
= "$Rev$"
12 __version__
= "3.0.0." + __revision__
[6:-2]
13 __author__
= 'DR0ID @ 2009-2011'
27 # -----------------------------------------------------------------------------
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
)))
41 demo_pygame(path_to_map
)
43 # -----------------------------------------------------------------------------
45 def demo_pygame(file_name
):
47 This is the demo showing the usage of the collapse feature.
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
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"
70 renderer
= tiledtmxloader
.helperspygame
.RendererPygame()
72 # cam_offset is for scrolling
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")
81 sprite_layers
= tiledtmxloader
.helperspygame
.get_layers_from_map(resources
)
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()
90 # set up timer for fps printing
91 pygame
.time
.set_timer(pygame
.USEREVENT
, 1000)
98 for event
in pygame
.event
.get():
99 if event
.type == pygame
.QUIT
:
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
:
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()))
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))
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
141 renderer
.render_layer(screen
, sprite_layer
)
143 pygame
.display
.flip()
145 # -----------------------------------------------------------------------------
147 if __name__
== '__main__':