Saturday, March 29, 2008

Learning Visual Python

This last January I went to CodeMash in Sandusky OH. It was a great two days of sessions and keynotes. Thanks to everyone who put it together.

Anyway, my goal at the conference was to attend some sessions that improved my C# understanding and also to attend some sessions that would expand my overall programming experience.

I was most impressed by the F# and Python presentations. I went to 3 seperate sessions on Python. One of them was presented by Catherine Devlin and showed how to use Visual with Python to do some planetary simulations very easily.

It looked so easy to do some pretty incredible things, so I decided to try it out for myself. Here is what I came up with.

I made a Rubik's Cube Simulation and a Magnetic Disc simulation.

I was able to make the Rubik's Cube into an executable using py2exe. It ended up being 40 files in all (YIKES). I then made that exe into a Windows screen saver by changing the extension to .SCR and copying all 40 files into the Windows\System32 sub directory. It's pretty neat, but kinda messy to have to copy so many files.

Here is the source:

Descriptions of the programs:
Program 1: - Rubiks cube simulation.

215 lines in all!! Amazing. It works, but it probably isn't
very python-like. I know there has to be better Python ways to do some of what I did in this program.

Keys: Q - Toggles Demo Mode. F - Toggles flying the camera around.
Click on the edges of the face pieces to make them rotate.

Program 2: - Simulation of Magnets which are fixed on a rotating disc.

I'm studying the interactions and forces between the discs. The forces between magnets are shown as gold cylinders shooting out of the front of the magnets.

I think my force equations are correct, but I'm having trouble converting the force between the magnets into the angular momentum of the disc. The discs also have a friction equation built into them,
so they should slow down and stop. They seem to work except when they are at their closest point. At which point the large disc spins out of control.

You can drag and drop the disc to move them around. Also left/right clicking on a disc increases it's speed clockwise or counter-clockwise. It's only 150 lines so far. WOW.