Tuesday, February 26, 2008

Surviving London at Rush Hour


Let's face it, no city is at its best on the way to work. When the world is all sour faces either someone is giving out free lemon wedges or it is rush hour. In London, even the friendliest people turn to stone during the morning commute. I can't blame them. Queuing and jostling in the rain is an unhappy way to start your day. Here are some guidelines for surviving a rush hour in London:
  1. Don't get in the way. Even the fragile looking old women in white gloves will knock you down like a rugby player and walk across your back.
  2. Since you can't always get out of the way, apologize when you bang into somebody. If more people did, the whole experience would be better.
  3. While riding an escalator in the underground, don't stand on the left side or the women in white gloves will get you.
  4. Wear eye protection or someone's umbrella will poke yours out.
Here is my favorite London commute story: one morning on my way to work a grim little woman stomped my heel so hard that my foot came completely out of my shoe. I tried dancing on one foot to keep my white sock off the pavement, but lost my balance as she shouldered past me. As I stood in her wake, completely undone, she turned, gave me a dirty look that can best be described as "the stink eye", and scurried off towards Liverpool Street. So there I was, standing in the rain, a filthy, wet sock on one foot, my shoe a pace behind me being mercilessly trampled by women in white gloves and not a sympathetic face in sight. So alarmed was I that I wouldn't bend over to pick it up for fear someone would give me a wedgie. I kicked my shoe to the edge of the traffic before trying to put it back on. In disgust I threw my soiled sock down in the middle of the sidewalk. It lay there among the mangled umbrellas and forgotten gloves, like a corpse on a battlefield, evidence of that morning's carnage.

Thursday, February 21, 2008

Gosu! Write more games!

A few weeks ago I watched The Kite Runner, which is a film about childhood. About the same time I was reading about Gosu, a game programming framework for ruby and C++. It struck me that I couldn't remember the last time I wrote a game. What a shame! So I started writing a game based on the kite fighting action from The Kite Runner.

Gosu is a sweet little framework for writing 2D games in Ruby. To quote their wiki:

Gosu aims to contain everything that is necessary to write a game in clean C++ or Ruby code, yet not much more. Our aim is to get the annoying world of hardware and OS interaction out of your way. Think SDL with a very different, less C-ish style.... Everything in Gosu exists for the same single reason: It was necessary for an existing project. Nothing was, and will ever be, added out of boredom.

Learning enough about Gosu to write my game was a snap, which meant that in no time I was up and running... into the fact that I haven't thought about math in a looong time. And then there is all that physics stuff. I mean, F=MA is simple, but using it to model a kite on a string is tricky. Even trickier is modelling two kites with strings that interact with one another.

So after a couple days tinkering I don't have a game because in trying to get the kites to interact I ran into all sorts of problems, but I do have kites on a string, and even though I can't get them to fight, they do pal around rather spectacularly. Looping the loop is especially fun and the way the string behaves naturally is fairly entertaining.

The project website is here. Please tinker with the code and add to it. I would love to see how to model the interaction between the strings properly. So hey, if you have read this far you must be interested in some code. The following shows what a basic gosu project looks like. Subclassing the Gosu::Window Class buys the ability to draw to our window, paint images, and access the soundcard. The other main elements to note are the overridden methods update and draw. Update is where you trigger any processing or logic for game interaction. The draw method is where you update the appearance of the objects in the window. It is so simple. Here is the ruby documentation.
require 'rubygems'
require 'gosu'
require 'enumerator'
require 'kite'

class KiteRunner < Gosu::Window
attr_accessor :player1, :player2
def initialize
super(640, 480, false)
self.caption = "Kite Runner Game"
@player1 = Kite.new(self, 300, 470)
@player2 = Kite.new(self, 350, 470)
@background_image = Gosu::Image.new(self, "Sky.png", true)
end

def update
process_keypress
@player1.move
@player2.move
end

def draw
@background_image.draw(0, 0, 0)
@player1.draw
@player2.draw
end

def process_keypress()
close if button_down?(Gosu::Button::KbEscape)
@player1.send('left') if button_down?(Gosu::Button::KbLeft)
@player1.send('right') if button_down?(Gosu::Button::KbRight)
@player1.send('longer') if button_down?(Gosu::Button::KbUp)
@player1.send('shorter') if button_down?(Gosu::Button::KbDown)
@player2.send('left') if button_down?(Gosu::Button::Kb1)
@player2.send('right') if button_down?(Gosu::Button::Kb4)
@player2.send('longer') if button_down?(Gosu::Button::Kb2)
@player2.send('shorter') if button_down?(Gosu::Button::Kb3)
end

end

Wednesday, February 20, 2008

The Heroes of 2008?

I am pretty excited about the elections this year, especially the presidential one. Lawrence Lessig's presentation about why you should vote for Obama does a good job of explaining why I think I will vote Obama in 2008.



Today on his blog Lessig himself, who has recently announced a shift away from his career focus on internet law, a historic career I might add as he is founder and CEO of the creative commons. His new goal has been to address the corruptive effect that money has in Washington. A lofty goal indeed. He is considering a run for congress. If he does run, then there will be one more candidate to get excited about.



I would love to see somebody as intellectually capable, honest and open as Lessig in Washington, even if there are some good arguments against his proposed solution to the corruption. This is a person who brings a creative intelligence and energy to solving difficult problems. Go check out his website, Lessig08.org. Even if you don't support his ideas you have to admit it is refreshing to hear someone with real ideas willing to put themselves on the line for what they believe.

Tuesday, February 19, 2008

Metakoans: Are you sure those mountains are merely mountains?

Illustration by Why The Lucky Stiff(Illustration by Why The Lucky Stiff)
Metakoans is an old Ruby metaprogramming challenge from RubyQuiz, the weekly programming challenge for Ruby programmers that comes out every Friday on the RubyTalk mailing list. I first heard about metakoans during one of Neal Ford's Ruby tutorials, but only recently did I get around to solving it. If you haven't already taken the quiz you should.

Initially I struggled with the koans, because you need to know all about methods like instance_eval, class_eval, and the eigenclass. So I went back and read the metaprogramming chapter of Matz's new book The Ruby Programming Language. That, plus some dandruff commercial style head scratching was all it took to solve them.

So now I have a solution (it isn't pretty), but if I am completely honest with myself I don't think I did the exercise correctly. My solution to koan 1 made the first 5 koans pass. That made me feel like a smarty pants, but It also meant I didn't bother reading koans 2-4. Can you really say you passed a koan if you never had to look at it? No. So the real koan challenge is: Can you write 9 solutions to the metakoan quiz where each solution only passes all solutions up to the current koan you are working on? So solution 1 will just make koan 1 pass. Solution 2 will only make koan 1 and koan 2 pass, etc.

I struggled to make a good exclusive solution to koan 1. The best I have done so far uses a technique I like to call cheating. I break my attribute function so that it doesn't work if it is being called from the eigenclass. Ok, so all's fair in love, war, and metaprogramming, but it seems like there should be a more natural solution.

#Exclusively pass Koan 1.
class Module
def attribute (p)
return if self.ancestors.include?(Class)
define_method(:a?){return @a}
class_eval{attr_accessor :a}
end
end


But even if it turns out that such an exercise is only solveable through contrived answers at least you will have to look closely at each koan. And once I have done this I fully expect that mountains will once again become merely mountains. Stop reading now if you don't want to see a solution to the original metakoans problem.


#it's ugly but it works.
class Module
def attribute(p, &block)
if p.is_a? Hash
define_attribute_methods(p.keys.first, p.values.first)
else
define_method(:default){block ? instance_eval(&block) : nil}
define_attribute_methods(p, false)
end
end

def define_attribute_methods(param_name, default_value)
default = default_value ? "@@default" : "default"
class_eval("@@default = #{default_value}") if default_value
class_eval("def #{param_name}; return #{default} unless defined? @#{param_name}; @#{param_name}; end")
class_eval("def #{param_name}=(val); @#{param_name} = val; end")
class_eval("def #{param_name}?; return #{default} unless defined? @#{param_name}; @#{param_name}; end")
end
end