Posts Tagged ‘ Python

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 :)

SohCahToa!

Time to get back to the basics.  I use trig at work, and at home in PyGame\Processing, but I like understanding the fundamentals of how it works.  So I sat down this afternoon and made up a “SohCahToa” PyGame program that really illustrates (to me at least)  what the values behind sine, cosine and tangent mean.  They are after all, ratios of the sides of a triangle.

So the mnemonic device is “Soh-Cah-Toa”, which means:

  • Sine = Opposite / Hypotenuse
  • Cosine = Adjacent / Hypotenuse
  • Tangent = Opposite / Adjacent

I wanted a visual way to see this in action, and that’s what this little program does:

sohCahToa01

Click through (twice) to larger image...

It plots a triangle defined by the opposite, adjacent, and hypotenuse sides.  As time goes by ‘degrees \ radians \ pi’ values increase, and the triangle changes.  The lengths of each side are plotted, and at the bottom, the math behind the sin, cos, and tan are shown in real-time.

The source code is online here, feel free to grab it:
http://www.akeric.com/python/sohCahToa01.py

Also, with a lot of help from this post (and several followup emails from its author), I was able to (finally) turn my PyGame apps into Windows executable.  Find the zipped version here:
http://www.akeric.com/python/sohCahToa01.zip

I built it with Python 2.6.2 and PyGame 1.9.1, but nothing fancy is going on with either, so it should work with older versions.

Open Source Sony?

Saw that Sony just opened up some of their software to the world, darn fine of them to do:

http://opensource.imageworks.com/

Includes:

  • OSL: Open Source Shading Language
  • Scala Migrations: Database Library Management
  • Field3D: Voxel Data Storage Library
  • PyString: Python string handling in C++
  • Maya Reticle: Flexible camera guilds for Maya