Python: Useful Utility for PGU's leveledit

I'm participating in PyWeek right now, and I'm using PGU. If you're not using PGU, you can skip this post.

If you're like me, you sometimes get confused about when you're editing the tiles and when you're editing the background. My buddy drew an entire level, and the tiles and background were totally messed up. Rather than redo the entire level, I wrote a little utility to force all the tiles into the background. It's quick-and-dirty, but quite useful when you need it:
#!/usr/bin/env python
# Copyright 2007 Adam Ulvi, Shannon Behrens
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <>.

"""Take a TGA file, and force any tiles onto the background.

This is to make up for UI interface "difficulties" in PGU's leveledit.


import os
import sys

import pygame

__docformat__ = "restructuredtext"

if (not len(sys.argv) == 3 or
not os.path.isfile(sys.argv[1])):
raise ValueError
except: # Catch all exceptions, not just ValueErrors.
print >> sys.stderr, "usage: INPUT.tga OUTPUT.tga"

in_f, out_f = sys.argv[1], sys.argv[2]
in_img = pygame.image.load(in_f)
out_img = in_img.copy()
w, h = in_img.get_width(), in_img.get_height()
for y in range(h):
for x in range(w):
(tile, bg, code, alpha) = in_img.get_at((x, y))
if tile:
bg, tile = tile, 0
out_img.set_at((x, y), (tile, bg, code, alpha)), out_f)


dm said…
hmm... your copyright license is almost as long as the code. expecting contributions or didn't want that little tutorial to slip out into the public domain?
(sorry.. on a copyright bender)
Anonymous said…
dm - It's Python, often the comments and license are longer than the code, esp for small utilities like this :-)

Additionally, this utility was written late last night while working on our Pyweek submission.. all other files in the game are stamped with the same license so it made sense for consistency to stamp it as well.