Tag Archives: python

My first foray into python

This project had a lot of firsts for me. First time contributing to open source, first time with git, and first time using python.

Along with learning the very basics of python, I was able to expand on PyRunner’s use of the configparser. With it you can quickly create and access sections and variables in a text file.

#If the section does not exist, create it.
config = ConfigParser.ConfigParser()
if not ( config.has_section(score_type) ):
# Creating the needed section.
config.set(score_type,"1", "0")
config.set(score_type,"2", "0")
config.set(score_type,"10", "0")
with open('pyRunner.cfg', 'wb') as configfile:

As you can see, there are convenient functions built in that let you, for example, check for the existence of a section in your file.

And here is what the config file that is generated (and updated in this case) looks like.

10 = 0
1 = 202
3 = 0
2 = 0

The other piece I expanded on was the score keeping system. We wanted to have a top 10 list rather than just the single top score. Here is some recursion I used to do it.

for i in range(1, 11):
if ( int(score) > int(config.get(score_type, str(i))) ):
#store the value I am about to replace
replaced_score = config.get(score_type, str(i))
#replace the value in the list
config.set(score_type, str(i), str(int(score)))
with open('pyRunner.cfg', 'wb') as configfile:
#find the replaced score's new place in the list
#immediatly end the loop when the recursion unwinds

Yes it is simple. But I did learn what happens in python when a recursive function gets away from you. Initially, I forgot to type cast the score variable above leading to this…
RuntimeError: maximum recursion depth exceeded while calling a Python object
Rather than forcing you to kill the process manually, python does it for you and lets you know that it did. Nice.


From June 28 through July 12, we worked on an arcade-style game built by dhatch with Python and the PyGame framework: PyRunner.

Python and PyGame are cross-platform, although we had some issues running it on OSX (which we fixed–kinda).

One of my additions was to randomize the block sizes. Here is a bit of code that did that:

class block(scroller):
    def __init__(self, y):
        self.side1 = random.randint(20,100)
        self.side2 = 20
        self.dimensions = random.choice([(self.side1,self.side2),(self.side2,self.side1)])
        self.image = pygame.Surface(self.dimensions)
        self.rect = self.image.get_rect()
        self.y = y
        pygame.draw.rect(self.image, (28,55,183), self.rect)
        self.rect.right = pygame.display.get_surface().get_width()
        self.rect.centery = y

You can see one of Python’s cool built-in methods at work: random.choice(…). This method randomly returns one of the options passed in to it. Here, I’m using it to determine if a block will be wide or tall.

Pygame is also used in the code snippet above. pygame.surface(…) prepares an image object, which pygame.draw.rect(…) then draws on the page.

On July 18th, dhatch pulled our changes into a branch of the project: http://github.com/dhatch/PyRunner/tree/1.2pre

See all our changes here: http://github.com/dhatch/PyRunner/pull/4