Archive for the ‘ CG ’ Category

How can I increment numbers on saved files in Python?

Originally posted on my Python Wiki.

This is a function I had made for my BubblePaint program in PyGame. In a nutshell, it will save the screen to a png image, and increment the filename with each image in the dir.  User can keep saving images, and this will keep incrementing the number of the newly saved file.

import glob, os, re

def saveImage():
    """
    Save the current image to the working directory of the program.
    """
    currentImages = glob.glob("*.png")
    numList = [0]
    for img in currentImages:
        i = os.path.splitext(img)[0]
        try:
            num = re.findall('[0-9]+$', i)[0]
            numList.append(int(num))
        except IndexError:
            pass
    numList = sorted(numList)
    newNum = numList[-1]+1
    saveName = 'bubblePaint.%04d.png' % newNum
    print "Saving %s" % saveName
    pygame.image.save(screen, saveName)

Will save out files like this:

bubblePaint.0001.png
bubblePaint.0002.png
etc...

How can I modify a Python attribute any time it is accessed?

Originally posted on my Python Wiki

I was designing a UI for a PyGame program I am working on, and needed a way to pass a value defining a “row height” into the functions that rendered my text to the screen. Since the UI could change, I didn’t want to have to hard-code positions into each element, later to modify it and have to redo all the positions.
What I came up with was a simple class with a single usable attribute, called val. Through using properties, I’m able to control how the attr behavies at time time its value is queried:

class Row(object):
    # class to store the current row location in the UI
    # Each time it is called, it will increment its value
    def __init__(self, val):
        self._val = 0
        self.orig = val
    @property
    def val(self):
        self._val = self._val + self.orig
        return self._val
row = Row(16)

print row.val, row.val, row.val, row.val
# 16 32 48 64

Properties have getter, setter, and deleter methods, but the default is getter, which I used above. So as you can see, each time I call to print, it accesses the val property (via the getter), and updates the internal counter.
This is a PyGame code snippet showing it in use:

overlay.blit(bubble, (8, row.val))
overlay.blit(toggleText, (8, row.val))
overlay.blit(lmb, (8, row.val))

Rather than having to specify a Y value for the last arg of the tuple, I can simply pass in my object, and it passes out the current new position, based on how many times it was called before.

I have no doubt there is probably some slicker way in Python, but it’s what I came up with on the spot :)

PyGame Wiki created

It was only a matter of time:  I find Tiddlywiki’s a great way to store online notes.  I currently have made them for a variety of subjects (as shown on the left sidebar of my page).  They can be hosted for free through tiddlyspot.com.  Other than the funny name, and trying to explain it to people, they’re a wonderful data-publication medium in my opinion.

Based on my previous posts on ‘what to make a game in’ (1, 2, 3, 4, 5), I finally settled on PyGame, and have had a local copy of my ‘PyGame’ wiki for a few months now.  But today, I’ve got it hosted online.  Not a huge amount of info on it yet, but no doubt it will grow over time.  Enjoy

http://pygamewiki.tiddlyspot.com/

BubblePaint v0.01

BubblePaint.001

This is the results of my latest efforts to learn PyGame using PyMunk, a Python wrapper around the Chipmunk 2D physics engine.  Eventually I hope to use this knowledge in a physics-based tank game, but right now, I’m just having fun ‘painting’ with the ‘bubbles’.  There’s no great magic going on in my opinion, just some weekend coding fun.

In a nutshell:

  • You paint with “bubbles” on the canvas using the mouse.  It expects you have a 3-button mouse with a scroll wheel in the middle (because that’s what I have).  Since its physics based, the bubbles will push each other around, and none will be overlapping (if given time to settle).
  • LMB-drag draws bubbles
  • MMB-drag up\down: changes bubble size. Bigger bubbles get darker, smaller bubbles get lighter.
  • RMB-drag up\down: change brush hue
  • Mouse-wheel: change pressure of bubbles (number applied at once, from 1->10)
  • ‘s’ will save ‘test.png’ in the install dir.

You can find the Python source here:

And you can find a zipped Windows executable here:

Future plans for this include:

  • Shapes other than circles (square, triangle, random-polygon)
  • Ability to enable gravity, and add static rigid bodies (more of a physics sandbox at that point)
  • Images for the ‘bubbles’ rather than just solid color.
  • The ability to ‘dry’ the canvas to allow for multiple layers of painting.
  • A smarter way to save images.
  • A toggleable overlay layer showing stats.
  • Change the background color.
  • User defined resolution.  Currently set to 768×768

But we’ll see how much of that happens, I have family coming into two for the next few weeks :)

Changing times…

After 7.5 years of employment at Electronic Arts, I’ve decided to change jobs.  It’s been a fantastic time, with an amazing group of people I only wish the best for.   It was one of the hardest decisions I’ve ever had to make leaving the company; they’ve treated me very well over the years and I have no major complaints.  The way I was treated by the upper management through the exit process was handled with the utmost integrity, which I greatly appreciated.  But the opportunity I am leaving it for with both grow my career, and allow me to focus on projects that are more in-line with my personality.

So it’s a very bittersweet parting, but onward and upward :)