From: Greg McIntyre Date: 2003-11-21T10:37:20+09:00 Subject: Re: learning the "Ruby way" "Zachary P. Landau" wrote: > > In Ruby I've only written small programs which transform data and > > interesting little toy scripts. But I skim this list every day and I > > have a vapourware Ruby roguelike game in the works which is fun to > > design and code random small bits when I have time away from > > assignments, thesis and (hopefully soon) work. > > A roguelike game written in Ruby would be very interesting to see. > Especially if it was easy to add new elements to the game in ruby. If > you ever get something releasable, be sure to let us know. I think Ruby's a great language for it. I chose it because I wanted others, possibly non-professional programmers, to be able to "write" for it (create levels, dialogue, etc.). Ruby's so intuitive and easy that I thought it would be good. That's why I find the long raging discussion on interfaces interesting -- novice programmers are going to get things wrong so it'd be nice if I could make their code adhere to an interface, or at least warn them when it doesn't. I mucked about with doing this in Ruby 1.8 and ended up with modules which assert certain methods exist in the classes in which they are included, at the point of their inclusion. It's a simple and not-perfect check but it is transparent check from the point of the "writer" but it does combat forgetfulness. Here's a taste... This is a map definition. Do I store it in XML? No. Do I store it as my own special file format? No. It's in Ruby! Yey! :) Doing this makes map definitions very flexible (e.g. it could be fully or partially randomly generated upon definition). The map definition is a class with a constant for layout and a constant for a "key" (in the cartography sense). Each tile on the map contains a bunch of stuff with no certain arrangement (I'm currently unsure if that's desirable... still thinking about it). Map::inherited is defined to collect map definitions into a list. I have a demo in which you can walk around this map, bump into things and set off the trap. Very simple, but I'm still thinking about the design of the fundamentals, so I didn't want to get carried away (and because I'm a hobbyist/hacker). Anyway, I'm posting it because it's a nice example of Ruby's power and flexibility being used. :) ------------- class DemoMap < Map class SpecialTrap def triggered_by(actor) end include Triggerable # asserts triggered_by is defined end LAYOUT =< Ground, '#' => BorderWall, '=' => [ Ground, Table, ], '0' => [ Ground, Marker.new(0), Teleport.new(DemoMap, 1), Door.new(open=true), ], 't' => [ Ground, SpecialTrap, ], '1' => [ Ground, Marker.new(1), Teleport.new(DemoMap, 0), Door.new(open=false), ], } end -- Greg McIntyre ======[ greg@puyo.cjb.net ]===[ http://puyo.cjb.net ]===