2 # -*- coding: utf-8 -*-
28 def get_files_natural_order(path
):
30 for infile
in glob
.glob( path
):
31 if os
.path
.isfile(infile
):
32 file_list
.append(infile
)
33 digits
= re
.compile(r
'(\d+)')
34 def tokenize(filename
):
35 return tuple(int(token
) if match
else token
37 ((fragment
, digits
.search(fragment
))
38 for fragment
in digits
.split(filename
)))
39 file_list
.sort(key
=tokenize
)
42 def get_images_from_files(file_list
):
44 for filename
in file_list
:
46 image
= pygame
.image
.load(filename
)
47 print 'Image: ', filename
48 image_list
.append(image
)
49 except pygame
.error
, message
:
50 print 'Cannot load image: ', filename
53 def create_image(width
, height
, red
, green
, blue
, alpha
):
54 image
= pygame
.Surface((width
, height
), pygame
.SRCALPHA
)
55 image
.fill((red
, green
, blue
, alpha
))
58 def get_images(directory
, selection
):
59 if isinstance(selection
, (list, tuple)):
61 for element
in selection
:
62 img
.extend(get_images(directory
, element
))
65 return get_images_from_files(get_files_natural_order(os
.path
.join(directory
, selection
)))
67 def get_mirror_images(source_images
, flip_horizontal
, flip_vertical
):
68 destination_images
= []
69 for image
in source_images
:
70 destination_images
.append(pygame
.transform
.flip(image
, flip_horizontal
, flip_vertical
))
71 return destination_images
74 if not EAST
in img
and WEST
in img
:
75 img
[EAST
] = get_mirror_images(img
[WEST
], True, False)
76 if not WEST
in img
and not EAST
in img
and SOUTH
in img
:
77 img
[EAST
] = img
[SOUTH
]
78 img
[WEST
] = img
[SOUTH
]
79 if not WEST
in img
and not EAST
in img
and NORTH
in img
:
80 img
[EAST
] = img
[NORTH
]
81 img
[WEST
] = img
[NORTH
]
82 if not NORTH
in img
and SOUTH
in img
:
83 img
[NORTH
] = img
[SOUTH
]
84 if not SOUTH
in img
and NORTH
in img
:
85 img
[SOUTH
] = img
[NORTH
]
86 if not WEST
in img
and EAST
in img
:
87 img
[WEST
] = get_mirror_images(img
[EAST
], True, False)
88 if not SOUTHWEST
in img
and WEST
in img
:
89 img
[SOUTHWEST
] = img
[WEST
]
90 if not SOUTHEAST
in img
and EAST
in img
:
91 img
[SOUTHEAST
] = img
[EAST
]
92 if not NORTHWEST
in img
and WEST
in img
:
93 img
[NORTHWEST
] = img
[WEST
]
94 if not NORTHEAST
in img
and EAST
in img
:
95 img
[NORTHEAST
] = img
[EAST
]
96 for dir in [ NORTH
, SOUTH
, EAST
, WEST
, NORTHEAST
, NORTHWEST
, SOUTHEAST
, SOUTHWEST
]:
98 if not 'fake_img' in locals():
99 fake_img
= create_image(25, 45, 255, 0, 0, 150)
100 img
[dir] = [ fake_img
]
103 class avatar(tiledtmxloader
.helperspygame
.SpriteLayer
.Sprite
):
104 def __init__(self
, directory
):
109 except AttributeError:
110 self
.images_standing
= { }
111 fill_images(self
.images_standing
)
113 fill_images(self
.images_walking
)
114 except AttributeError:
115 self
.images_walking
= self
.images_standing
117 fill_images(self
.images_running
)
118 except AttributeError:
119 self
.images_running
= self
.images_walking
121 self
.images
= self
.images_standing
123 image
= self
.images
[self
.direction
][self
.frame_num
]
124 rect
= image
.get_rect()
128 super(avatar
, self
).__init
__(image
, rect
)
129 def load(self
, directory
):
130 self
.images_standing
= { }
131 self
.images_walking
= { }
132 self
.images_running
= { }
133 def lookat(self
, direction
):
134 midbottom
= self
.rect
.midbottom
135 if (direction
!= CENTER
):
136 self
.direction
= direction
137 if (self
.frame_num
>= len(self
.images
[self
.direction
])):
139 image
= self
.images
[self
.direction
][self
.frame_num
]
140 rect
= image
.get_rect()
141 rect
.midbottom
= midbottom
144 def setaction(self
, action
):
145 self
.images
= self
.images_standing
147 self
.images
= self
.images_walking
148 elif (action
== RUN
):
149 self
.images
= self
.images_running
150 if (self
.frame_num
>= len(self
.images
[self
.direction
])):
152 def update(self
, inc_x
, inc_y
, action_type
):
153 direction
= [NORTHWEST
, NORTH
, NORTHEAST
, WEST
, CENTER
, EAST
, SOUTHWEST
, SOUTH
, SOUTHEAST
] \
154 [4 + cmp(inc_x
, 0) + cmp(inc_y
, 0) * 3]
156 if action_type
== STAND
or direction
== CENTER
:
157 self
.setaction(STAND
)
158 elif action_type
== RUN
:
163 self
.lookat(direction
)
164 tick
= pygame
.time
.get_ticks()
165 if (tick
- self
.last_tick
> self
.ch_speed
):
166 self
.last_tick
= tick
;
168 if (self
.frame_num
>= len(self
.images
[self
.direction
])):
170 def moveto(self
, pos_x
, pos_y
):
171 self
.rect
.midbottom
= (pos_x
, pos_y
)