Posts Tagged ‘ pygame

How can I blur a PyGame Surface?

From over on my PyGame Wiki…

There doesn’t seem to be a built in way in PyGame to ‘blur’ a surface, but you can fake it bay scaling the surface twice: Scale it small once, then scale it back to normal size.

def blurSurf(surface, amt):
    """
    Blur the given surface by the given 'amount'.  Only values 1 and greater
    are valid.  Value 1 = no blur.
    """
    if amt < 1.0:
        raise ValueError("Arg 'amt' must be greater than 1.0, passed in value is %s"%amt)
    scale = 1.0/float(amt)
    surf_size = surface.get_size()
    scale_size = (int(surf_size[0]*scale), int(surf_size[1]*scale))
    surf = pygame.transform.smoothscale(surface, scale_size)
    surf = pygame.transform.smoothscale(surf, surf_size)
    return surf

You can also use pygame.transform.rotozoom() in place of smoothscale. However, they have different effects: smoothscale appears to blur (and push) the pixels ‘outward’ based on the center of the surface, while rotozoom blurs (and pushes) them outward based on the top left corner of the image (0,0), effectively making them translate down and to the right in the process.
http://www.pygame.org/docs/ref/transform.html#pygame.transform.smoothscale
http://www.pygame.org/docs/ref/transform.html#pygame.transform.rotozoom

Other methods I haven’t investigated would involve running a convolve routine on the surface pixels.  As is, I’m not sure how could the performance would be though.

I’d be interested to know what methods others have come up with to solve this problem.  My searching hasn’t come up with much.

The Chaos Game

After getting the book “INTRODUCING Fractal Geometry“, I thought it’d make a good learning project to tackle “The Chaos Game“, as coined by the mathematician Michael Barnsley. Seemed simple enough to grasp, and would be fun to code up in Python\PyGame.

What attracted me to it was that while it was a seemingly simple problem, a few things outside my realm of knowledge were needed: Find a random point inside of a triangle. After a bit of web searching, I ran across the post on Wolfram Mathworld describing ‘Triangle Point Picking‘. It shows how to find points based on a quadrilateral (2x triangle). Using that code, and a Python specific post called “Deciding if a Point is Inside a Polygon“, I was able to combine them with the game rules to achieve success, the Sierpinski triangle!

While the end result is nothing special to look at, Sierpinski triangles have been around for a long time, it was the fun of actually making the whole thing work through Python that kept me at it on Sunday afternoon 😉

  • Python source code HERE.
    • Its expecting to use this Vec2D class.
  • .zip with Windows executable HERE.

Now has its own page on the blog.

The Matrix

Since I never ‘went to school’ for any kind of programming, I’ve had to pick it up as needed throughout my career.  One of the ‘great mysteries’ has been matrix math.  I’m a visual person, and that includes learning math.  Last night, while reading chapter 9 in my PyGame book ‘Beginning Game Development with Python and PyGame: From Novice to Professional‘, the whole ‘matrix thing’ finally made sense in my brain.  So much in fact, I couldn’t fall asleep I was mulling it over so much.  Waking up, I decided to dive into it, and made some psuedo-code showing how matrix multiplication happens.  But it was still really hard to physically visualize what was going on.  So, working in GIMP, I made a graphical layout of what is going on.  It is a work of beauty :)

Couple things are presumed:  It is a description of multiplying two 4×4 matrices, with their translation along the bottom row, not the right column.  In each of the sixteen blocks, the sub-matrices (‘matrixA’ on the left, ‘matrixB’ on the right) are multiplied left to right, and added top to bottom to get the final result of each block.  Time for a t-shirt!

visualMatrixMult02Click through (twice) for a larger version.
You can find it on flickr here as well.

Bubblepaint 0.1.3

bubblePaint.0016Based on my previous post, I’ve got a usable version of Bubblepaint ready for the masses.

In a nutshell, it’s a simple ‘paint program’ using 2d physics.  You ‘paint’ with ‘bubbles’ that collide with each other.  Good fun.  It’s implemented via Python through PyGame (graphics) using PyMunk (physics).  It’s documentation is on a toggleable layer on the main screen once it starts.

You can find a zipped Windows executable here:

You can find the updated Python source here:

Put a few pics on up Flickr here.

I’d love to hear some feedback.  I don’t have many plans to develop it much further, but you never know 😉

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...