Ruby, OpenGL and Pixels

I used my favorite language of all time, Ruby. With 1.9 Ruby has picked up speed and with Ocra you easily make a ~1-2 meg exe-file out of it. Also the excellent (and german precision engineered)  Gosu enables OpenGL accelerated 2D. On top of that I used my own Ruby/Gosu-specific framework Chingu to get game states, re-usable game logic and a boiler plate “game object”. To get a speedy get_pixel() I used the awesome Texplay by John Mair.

I have a pixel fetish. I love retro-looks in games, and I love pixel-perfect-collisions, a fetish that probably came into existence playing hours and hours of Lemmings 2-player mode on the classic Amiga 500.

So, the most time was spent up building a good pixel-perfect-collision algo, and it came out very playable, but not perfect. I ranted about it in my last blogpost.

With Ruby and Chingu my level-building got very clean, I think even ppl not into Ruby could understand what’s happening here:

class Level7 < Level
   def setup
     self.background = "level7.bmp"
     every(1000) { Enemy.create(:image => "acid_drop.bmp", x => 150, :velocity_y => 5) }
 every(1500) { Enemy.create(:image => "acid_drop.bmp", x => 250, :velocity_y => 5) }
   end
 end

or here (this is from the mainloop):

player.each_collision(Enemy) do |player, enemy|
 unless player.paused?
 player.pause!
 Sound["die.wav"].play(0.3)
 after(1000) {  player.x = @entry_x; player.y = @entry_y; player.unpause!; }
 end
 end

It’s nice when it’s silly simple building readable levels.

I also got a very basic and simple Map-class going so I could build on my matrix of screens that made up the game:

map = [
 [LevelUp, nil, nil, nil],
 [Level1, Level2,Level22, nil],
 [LevelAir, nil, Level3, nil],
 [LevelAir2, Level5, Level4, nil],
 [nil, Level6, Level7, nil],
 [nil, Level8, Level9, End],
 ]

Basically if you exited a screen to the right I would increment a column_counter and fetch a new screen from the matrix. If exiting downwards, increment row_counter an fetch a new screen.

If you like OO, short readable code and scripting languages in general and haven’t checked out Ruby or Gosu yet, now is the time to do it.

Comments

sfernald
15. Dec 2009 · 16:41 UTC
I liked this game so much I think I’m going to try to make my own level. Since the level code is so clean, I expect this to be quite easy. We’ll see.
sfernald
15. Dec 2009 · 22:41 UTC
Hey, I made a new level for your game. I has given me a chance to play around with Chingu. Must say it’s pretty cool.
sfernald
17. Dec 2009 · 20:52 UTC
I finished my little sequel of sorts. Here it is:
ippa
18. Dec 2009 · 07:50 UTC
I love it :D.. you’ve made a better game then the original! Played through it, damn it was hard.
ippa
18. Dec 2009 · 08:33 UTC
Blogpost + Windows EXE:
sfernald
18. Dec 2009 · 14:53 UTC
Very cool.