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'
28 # -----------------------------------------------------------------------------
36 path_to_map
= os
.path
.join(os
.pardir
, "001-1.tmx")
37 print(("usage: python %s your_map.tmx\n\nUsing default map '%s'\n" % \
38 (os
.path
.basename(__file__
), path_to_map
)))
42 demo_pygame(path_to_map
)
44 # -----------------------------------------------------------------------------
46 def demo_pygame(file_name
):
48 Example showing how to use the paralax scrolling feature.
51 # parser the map (it is done here to initialize the
52 # window the same size as the map if it is small enough)
53 world_map
= tiledtmxloader
.tmxreader
.TileMapParser().parse_decode(file_name
)
55 # init pygame and set up a screen
57 pygame
.display
.set_caption("tiledtmxloader - " + file_name
+ \
58 " - keys: arrows, 0-9")
59 screen_width
= min(1024, world_map
.pixel_width
)
60 screen_height
= min(768, world_map
.pixel_height
)
61 screen
= pygame
.display
.set_mode((screen_width
, screen_height
))
63 # load the images using pygame
64 resources
= tiledtmxloader
.helperspygame
.ResourceLoaderPygame()
65 resources
.load(world_map
)
67 # prepare map rendering
68 assert world_map
.orientation
== "orthogonal"
71 renderer
= tiledtmxloader
.helperspygame
.RendererPygame()
74 # use floats for hero position
75 hero_pos_x
= screen_width
76 hero_pos_y
= screen_height
77 hero
= create_hero(hero_pos_x
, hero_pos_y
)
79 # cam_offset is for scrolling
80 cam_world_pos_x
= hero
.rect
.centerx
81 cam_world_pos_y
= hero
.rect
.centery
83 # set initial cam position and size
84 renderer
.set_camera_position_and_size(cam_world_pos_x
, cam_world_pos_y
, \
85 screen_width
, screen_height
)
88 sprite_layers
= tiledtmxloader
.helperspygame
.get_layers_from_map(resources
)
91 sprite_layers
= [layer
for layer
in sprite_layers
if not layer
.is_object_group
]
93 # add the hero the the right layer, it can be changed using 0-9 keys
94 sprite_layers
[1].add_sprite(hero
)
96 # layer add/remove hero keys
97 num_keys
= [pygame
.K_0
, pygame
.K_1
, pygame
.K_2
, pygame
.K_3
, pygame
.K_4
, \
98 pygame
.K_5
, pygame
.K_6
, pygame
.K_7
, pygame
.K_8
, pygame
.K_9
]
100 # variables for the main loop
101 clock
= pygame
.time
.Clock()
104 # set up timer for fps printing
105 pygame
.time
.set_timer(pygame
.USEREVENT
, 1000)
112 for event
in pygame
.event
.get():
113 if event
.type == pygame
.QUIT
:
115 elif event
.type == pygame
.USEREVENT
:
116 print("fps: ", clock
.get_fps())
117 elif event
.type == pygame
.KEYDOWN
:
118 if event
.key
== pygame
.K_ESCAPE
:
120 elif event
.key
in num_keys
:
121 # find out which layer to manipulate
122 idx
= num_keys
.index(event
.key
)
123 # make sure this layer exists
124 if idx
< len(world_map
.layers
):
125 if sprite_layers
[idx
].contains_sprite(hero
):
126 sprite_layers
[idx
].remove_sprite(hero
)
127 print("removed hero sprite from layer", idx
)
129 sprite_layers
[idx
].add_sprite(hero
)
130 print("added hero sprite to layer", idx
)
132 print("no such layer or more than 10 layers: " + str(idx
))
135 direction_x
= pygame
.key
.get_pressed()[pygame
.K_RIGHT
] - \
136 pygame
.key
.get_pressed()[pygame
.K_LEFT
]
137 direction_y
= pygame
.key
.get_pressed()[pygame
.K_DOWN
] - \
138 pygame
.key
.get_pressed()[pygame
.K_UP
]
140 # make sure the hero moves with same speed in all directions (diagonal!)
141 dir_len
= math
.hypot(direction_x
, direction_y
)
142 dir_len
= dir_len
if dir_len
else 1.0
144 hero_pos_x
+= speed
* dt
* direction_x
/ dir_len
145 hero_pos_y
+= speed
* dt
* direction_y
/ dir_len
146 hero
.rect
.midbottom
= (hero_pos_x
, hero_pos_y
)
148 # adjust camera according to the hero's position, follow him
149 renderer
.set_camera_position(hero
.rect
.centerx
, hero
.rect
.centery
)
151 # clear screen, might be left out if every pixel is redrawn anyway
152 screen
.fill((0, 0, 0))
155 for sprite_layer
in sprite_layers
:
156 if sprite_layer
.is_object_group
:
157 # we dont draw the object group layers
158 # you should filter them out if not needed
161 renderer
.render_layer(screen
, sprite_layer
)
163 pygame
.display
.flip()
165 # -----------------------------------------------------------------------------
167 def create_hero(start_pos_x
, start_pos_y
):
169 Creates the hero sprite.
171 image
= pygame
.Surface((50, 70), pygame
.SRCALPHA
)
172 image
.fill((255, 0, 0, 200))
173 rect
= image
.get_rect()
174 rect
.midbottom
= (start_pos_x
, start_pos_y
)
175 return tiledtmxloader
.helperspygame
.SpriteLayer
.Sprite(image
, rect
)
177 # -----------------------------------------------------------------------------
179 if __name__
== '__main__':