Tag Archives: project

First workshop of Spring 2012

Pianobar

Our first workshop of 2012 will introduce an open source project called Pianobar. Pianobar is a Pandora client written in C that allows users to listen to and interact with the Pandora.com music service via the command-line. Attendees will be encouraged to work collaboratively on new features, unit tests, documentation, and bugs. This workshop offers a chance to learn more of the C language, version control, and programming in general.

The goal of the workshop is to help participants learn the skills they need in order to contribute to Pianobar.

This workshop is open to anyone who has some programming experience. Guest wifi will be provided for non-students.

Pianobar Workshop
Wednesday, February 8th
OCNL 237
Time: 4:00pm

***Bring your laptop***

Tetris in CoffeeScript

From July 26th through Aug 9th, the team conributed to a Tetris clone written in CoffeeScript: coffee-canvas-tetris

CoffeeScript is unique in that it is compiled into Javascript. Although the syntax is a bit unusual, I found it to be much more concise and easier to read than Javascript. Here is an example of something I contributed to the project:

$(canvas).click -> pause_or_unpause()

The compiled JS looks like this:

$(canvas).click(function() {
  return pause_or_unpause();
});

So although you’re probably confused about the arrow and dollar sign, I’m sure you can see how much nicer it is to read in CoffeeScript. If you take a look at the project source code, you’ll see just how much of a difference it makes. CoffeeScript was inspired by Ruby and Python, which is why the syntax is so concise.

Beyond just making the code look pretty, CoffeeScript offers a lot of built-in functionality that Javascript lacks. For one, it allows you to define classes, rather than forcing you to use prototypical inheritance. It also offers comprehensions, which are loops but treated as expressions– meaning they can be assigned like a variable.

The most difficult part of this project was setting up the development environment. Just about everyone on the team had difficulties with the dependencies required to run this project. The easiest way (in linux) turned out to be to just download the source and run “sudo bin/cake install” from within the downloaded folder.

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()
config.readfp(open('pyRunner.cfg'))
if not ( config.has_section(score_type) ):
# Creating the needed section.
config.add_section(score_type)
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:
config.write(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.

[endurance]
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:
config.write(configfile)
#find the replaced score's new place in the list
ranking(replaced_score)
#immediatly end the loop when the recursion unwinds
break

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.
–Dennis

PyRunner

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):
        scroller.__init__(self)
        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