Example scripts

Here is a small collection of snippets using tml. You can find the scripts in the “examples” directory shipped with tml. Feel free to copy and modify them to fit your needs!

Note

For portability reasons, this scripts use tml.constants.TML_DIR to access the default maps. You can of course simply pass any path without needing to use the TML_DIR constant.

Count the pickups

Code:

import os

from tml.tml import Teemap
from tml.constants import TML_DIR, TILEINDEX

map_path = os.sep.join([TML_DIR, '/maps/dm1'])
t = Teemap(map_path)
pickups = {
    'shotgun': 0,
    'grenade': 0,
    'rifle': 0,
    'ninja': 0,
    'health': 0,
    'armor': 0,
    # 'solid': 0,
    # 'air': 0,
    # 'death': 0,
    # 'nohook': 0,
}
for tile in t.gamelayer.tiles:
    for key, value in pickups.iteritems():
        if tile.index == TILEINDEX[key]:
            pickups[key] += 1

for k, v in pickups.iteritems():
    print '{value:3}x {key}'.format(value=v, key=k)

Output:

 9x armor
 2x shotgun
10x health
 0x rifle
 1x ninja
 2x grenade

Extract images

Code:

import os

from tml.tml import Teemap
from tml.constants import TML_DIR

map_path = os.sep.join([TML_DIR, '/maps/dm1'])
t = Teemap(map_path)
try:
    os.mkdir('images')
except OSError, e:
    if e.errno != 17:
        raise e
for image in t.images:
    image.save(os.sep.join(['images', image.name]))
print 'Extracted {0} images.'.format(len(t.images))

Output: Look into the created “images” directory, you will find all images saved as png.

Table Of Contents

Previous topic

Teemap

Next topic

Excursion: The Teeworlds map format

This Page