A look at the first Rebirth demo game
| Published: | Comments: 6 | Filed under: Development, Rebirth
I finished a demo game yesterday, as an aid in designing the still-imaginary Rebirth API. I actually meant to post this last night, but I ended up staying up until past 2AM trying to find a way to do syntax highlighting on Mephisto, to no avail.
The game is a simple one: bounce a ball in the air by clicking it, and don't let it fall to the ground. You get a point for each time you click it, but lose a point for each time it hits the ground. If you get up to 15 points, you win. Not the most challenging thing in the world, but it has all the elements of a complete game.
The code for the entire game is about 100 lines long. Rather than cop out and just post the whole file, I'll show and explain a few highlights. Keep in mind that the code to support this doesn't exist yet; I'm just threshing it out, and imagining how it might be used. As I explained in my previous post, once I've got some sample games written, I'll codify the API in specs, and then implement it.
First up, is the class for the Ball object. Here's the entire class definition:
# Represents a bouncing ball.
class Ball < GameObject
# Set up the physical and visual properties of the ball.
def initialize( params = {} )
base_params = {
:center => v(0,0),
:radius => 15.0,
:density => 5.0,
:elast => 0.9,
:color => :red
}
add_shape Circle.new( base_params.merge(params) )
end
end
As you can see, it just creates a Circle instance with certain parameters, and adds the Circle as a shape. The hash of base parameters defines what the default ball is like, but you can override / augment them by passing a hash to the initializer. There will be other possible parameters besides just the ones shown there, but these are the ones that are important to the ball (Circle will use defaults for the rest).
Circle will be one of a handful of built-in shape classes, which provide a visual appearance and a physical behavior for game objects (i.e. for collision detection and physics). By default, objects are both visible and collidable, but you can turn either (or both) off by providing certain parameters. That way, you can have non-collidable objects, or collidable invisible objects. And, yes, all the shape classes will be part of the physics engine, powered by Chipmunk.
So far, it has a ball that we can see, and that acts physically. Later on, it adds a hook to perform a certain action when the player clicks on the ball:
$game[:ball] = Ball.new
$game[:ball].when_clicked do |click|
$game[:score] += 1
$game[:ball].shove( :from => click.pos, :strength => 30.0 )
end
Here, it makes an instance of Ball, and stores the instance in a namespace for later use ($game is an instance of Game, which I'll cover later). Then, it uses the when_clicked method to add a hook which will be triggered when the user clicks on the ball. The block adds one point to the player's score, and also applies a physical impulse to the ball to shove it away from the cursor, sending the ball back into the air. (By the way, the hook could have just as well been set up in the Ball's initialize method, for behaviors that you want every instance to have.)
Elsewhere, it sets up a when_collided hook on the floor, to call a block to remove one point from the player's score. It also creates a when_true hook on $game itself; when_true periodically evaluates a Proc to see if the statement is true, in this case to test if the player has 15 points yet.
There's other interesting code that I'd show you, but I don't want this post to get really long. I expect to put this project up on Github eventually, so you'll be able to get all the code you want. If the demand is there, I might do another post tomorrow to demo another part.
P.S. Rebirth is still strictly theoretical. I'd give it a 50:50 chance that Rebirth sees a release, and only a 1-in-20 chance that it will result in any major functionality removal from core Rubygame.
Comments