Archive for January, 2006

Printing a degree symbol in Python

Tuesday, January 31st, 2006

While trying to show the ball angle in PyGame I fired up charmap in Windows, copied the degree symbol, and pasted it into my code. As expected, I got this error in Komodo while trying print:

DeprecationWarning: Non-ASCII character ‘\xb0′ in file Python-1.py on line 4, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
execfile(file, globals, locals)

After taking a look at PEP 0263 and a python-tutor post, I was able to figure out how to get it to print without the warning. Here’s what you need to do:

degree_symbol = unichr(176).encode("latin-1") print degree_symbol

That should properly display the symbol (i.e. °). Here it is rendered as text in PyGame:

Degree symbol

Pretty simple if you are familiar with Unicode, but good to know.

Guido: Django 1, Cheetah 0

Tuesday, January 31st, 2006

Guido van Rossum follows up his critique of Python web frameworks with some thoughts regarding templating with Django and Cheetah.

Quoth Guido:

I finally got some time to play with Cheetah and Django templates. After maybe an hour with each, I like Django best. Hopefully people are still watching this space…

Oh, I’m sure people are watching quite closely indeed! :)

As a side note, web.py uses Cheetah for templating. It seemed pretty nice to me… for the 30 seconds I used it when running through the tutorial.

Fast scrolling with PyGame

Tuesday, January 31st, 2006

I found a PyGame mailing list thread on fast scrolling maps today. It’s from 2002, so it’s a little old. I think, however, that it might still be useful.

Quoth the original poster:

“Can anyone point me to a good example of a fast scrolling background (full-screen or nearly full-screen)?”

The subsequent thread has quite a few potential solutions and lot of advice.

100th post

Tuesday, January 31st, 2006

My first post was at 8:40:51 PM on November 23, 2005, this is already my 100th post. Amazing.

I’ve been having a lot of fun with the site, and I hope others have found it to be at least a tiny bit useful or informative as well. Thanks go to everyone who has been reading and commenting.

Bounce

Tuesday, January 31st, 2006

Tonight I finally created what could be called a true “game”, as opposed the basic PyGame proof-of-concept stuff I’ve been toying with:

PyGame Pong

Basically, you use the mouse to control the paddle (a la Breakout) and bounce the smiley face around the screen, with appropriate sound effects. If the smiley slips past you and hits the bottom, you lose a life. After three lives are lost, the game is over.

It’s probably the most simple computer game in the world, but I really feel like I’m making some progress… and PyWeek 2 is only a few weeks away.

Languages for teaching programming

Monday, January 30th, 2006

A few weeks ago Nate’s friend was asking for recommendations on which language to use when teaching children how to program. At the time, I recommended Python (but I mentioned that Logo and HTML could be useful as well).

Today I came across A Very Quick Comparison of Popular Langauges for Teaching Computer Programming. The languages covered include BASIC, C, Java, and Python. And Python seems to come out on top:

For teaching as a first language however it has some specific advantages. As can be seen from the examples above (ignoring BASIC), Python requires less time, less lines of code, and less concepts to be taught to reach a given goal. This allows more time to be spent on the important things.

I definitely think Ruby and Logo should be added to the list. And perhaps Pascal or FORTRAN might be worth looking at as well. But it’s a decent summary, and worth reading if you plan on doing some teaching.

SPyRE talk at Philadelphia PUG

Monday, January 30th, 2006

For those of you in the Philly area, David Keeney will be speaking about SPyRE at Drexel University in Philadelphia on Tuesday January 31st.

Quoth David:

SPyRE is a Simple Pythonic Rendering Engine for OpenGL 3d graphics. I will be discussing primarily OpenGL/PyOpenGL, with some discussion and examples using SPyRE, and talking about those aspects of PyGame which are useful with OpenGL graphical programs. I hope to keep it informative to people with little or no experience in PyGame or PyOpenGL.

Hopefully, we will spend a little time as well discussing various plans and prospects for Philadelphia’s PUG. This is only the second meeting, so lots of things are undecided with respect to group goals
and purpose.

Sounds like an interesting talk.

Making menus

Sunday, January 29th, 2006

Not much coding over the weekend, but I was able to spend a few minutes trying to get a game menu working:

PyGame menu test

Not much to look at, but it works. The user can mouse around and click on the options and have it perform actions based on the selection.

The code, however, is really ugly at this point. Now that I know the basic concepts, I need to clean it up into a general MainMenu class that I can reuse in the future. Ideally, it would accept any number of options (with associated image files) and then automatically place them on the screen.

That might be this week’s goal.

Guido talks about web frameworks

Saturday, January 28th, 2006

Guido van Rossum’s first project at Google involves building an internal web application, so he took a look at some the available web frameworks. He has a short post called Please Teach me Web Frameworks for Python where he talks about his experiences.

Here’s part of what he had to say about Django:

I took a brief look at Django, and while I like their website (pretty and easily navigable and chockfull of useful information), I’m not keen on the particular tools they provide (it doesn’t help that they begin every example with “from mumble.something import *”). For example, Django’s templating language is rich and powerful, but it doesn’t look very Pythonic to me — in fact, it’s so rich and powerful that it might as well be PHP. Similarly, I’m not keen on their object-relational mapping approach. There’s too much magic based on name correspondence, and the automatically generated APIs feel a bit unpythonic (e.g. lots of getter and setter methods where a normal Python object would use public attributes and perhaps properties). I imagine that it works best if you know exactly how it is mapped to SQL.

He also mentions Ruby on Rails and Quixote but, little suprisingly, there’s no mention of TurboGears.

I think it’s an honest look at the current state of Python web frameworks (and RoR) that is well worth reading. Read the whole thing.

Keeping score with PyGame

Friday, January 27th, 2006

I’ve been playing with PyGame a little bit each night this week. Here’s tonight’s creation:

Dragon attack

This one allows the player to use the mouse to control a dragon (move and attack). If the dragon attacks the purple blob, the score increases. It uses rather sloppy rectangle based collision detection, but it works:

# Shrink rect and check for collision with target hitbox = self.rect.inflate(-5, -5) return hitbox.colliderect(target.rect)

And of course, it includes sound effects and background music. I’ve been using Audacity to convert mp3s to ogg for use with pygame.mixer.music:

# Load an play background music, continuous loop pygame.mixer.music.load('data/banco.ogg') pygame.mixer.music.play(-1, 0.0)

The Line By Line Chimp tutorial was extremely helpful in getting it all to work.