So, I’ve decided that every time I use a new language or framework for the first time, I will write a little first impressions post about it.
This spring term I am taking a computer graphics class, CS 455 at BYU for anyone interested. It has been a really interesting class, and the projects so far have been both fun and informative. The latest project we were assigned to do was an Inverse Kinematics solver. If you don’t know what Inverse Kinematics means and you want to, you can read about it on wikipedia, but that’s not the point of this post.
We were given a framework, basically a starting point, using XNA that we could use for the project. Well, I ran into a little hitch there when the CPU in my nice gaming machine died. My gaming computer was also my only windows machine (I run Fedora on my school laptop). So… now what?
I decided I would need to learn and use a different environment, language and library to do the assignment. Being a big fan of Python I decided to try out PyGame, a library designed to help make 2d games.
This is, of course, a first impression, I have only spent about 8 hours total working with the system, but here are some initial thoughts after completing my first project in PyGame. First, a screenshot:
Well, considering that this is a library for Python the syntax is, well, Python. That is a huge plus. Even if you are not familiar with Python syntax it is pretty simple to learn (unless you are an experienced C-related language programmer, then the lack of brackets and semi-colons might drive you nuts). It is a library that is simple to use for both experienced an inexperienced programmers. The entirety of the IK solver took only 155 lines of code.
PyGame itself plugs into Python like any other module. It provides a bunch of classes, methods and constants that make interacting with the game window really simple. Here is my displayPlanks method, which is used to draw all of the planks that make up the arm.
for angle, position, scalar in zip(self.worldAngles,
stretchedPlank = pygame.transform.scale(self.plankImg, scalar)
rotatedPlank = pygame.transform.rotate(stretchedPlank, angle)
rotRect = rotatedPlank.get_rect()
rotRect.center = self.pointDisplay(position)
endRect = self.endImg.get_rect()
endRect.center = self.pointDisplay(self.plankEnds[-1])
Before calling this method we already know where each plank is located as well as how much to rotate the image of the plank. We also know the position of the end-effector (represented graphically as a baseball). This method simply takes the image of the plank, scales and rotates it for each segment, and then prints all the planks and the baseball to the screen. All of that happens quickly and without a lot of code.
The PyGame module is pretty well documented, and everything I needed was pretty easy to find as well. However, I must say that the color scheme used for their online documentation is pretty hard on my eyes.
Additionally, I was able to find some examples of some of my problems easily enough on google. There is also this free online book that is a pretty good resource to learn how to use PyGame, I only read chapter 2 and half of chapter 3, but it is easy to understand and is a pretty good resource for anyone interested to learn PyGame.
Power and Capability
This was a simply project, and I haven’t done a lot of research on this subject yet, but as far as I’m aware, PyGame is not designed to be a heavy-duty game development environment, so I doubt we’ll be seeing any best seller’s coming from it, but it’s simplicity definitely makes it worthwhile and capable of being a good way to prototype and make smaller games.
Again, this is a first impression, so if I have preached false doctrine, I claim ignorance as my defense. That being said, though, I was impressed at how simple it was to learn and build something with PyGame, and I plan to use it in the future for personal projects.
Here is a demo of the IK solver in action:
You can check out the source code for my IK solver on github.