DragonRuby Guide
I’m reading through the sample code and writing some simple games, making notes as I go along. Hopefully this will become a useful guide for others as they experiment with DragonRuby
You can learn more about DragonRuby here
Table of Contents
- Documentation for primitives
- General rendering rules
- Okay, so how do I make a pretty game in DragonRuby?
- Printing the framerate
- The Virtual Grid
- Numeric shifting (helpers for positioning)
- Are lines & solids useful?
- Using the game’s uptime
- Collision mapping
Documentation for primitives
The documentation/
folder is a great resource for how to use the engine’s primitives
General rendering rules
- The resolution is always 1280x720 (1280 wide, 720 high). DragonRuby will scale the content using that aspect ratio.
- However, you’re better off (and safer off!) using the Virtual Grid API provided by DragonRuby!
- As you add more entities to the game, you’ll run into performance issues with the
args.outputs.sprites << [...]
pattern. This is a good time to refactor to a dedicated Sprite class (check outsamples/14_sprite_limits_static_references/
)
Okay, so how do I make a pretty game in DragonRuby?
Sprites are the answer to everything, followed by labels. The other rendering primitives (solids & lines) are only useful from a debugging standpoint. DragonRuby’s design philosophy is:
- Use a separate workflow to generate the assets you need for the game (eg: sprites & sounds)
- Reference all those assets inside your DragonRuby code
- Use DragonRuby as the “glue”. It’s a framework, and will handle rendering the game, giving you simple helpers you can chain together
For all other display outputs, Sprites are your solution
Sprites import images and display them with a certain rectangular area The image can be of any usual format and should be located within the folder, similar to additional fonts.
Sprites have the following parameters:
- Rectangular area (x, y, width, height)
- The image (path)
- Rotation (angle)
- Alpha (a)
- [The tile positioning, so you can use spritesheets]
Printing the framerate
You can print the framerate using:
args.outputs.labels << [10, 30, args.gtk.current_framerate]
The Virtual Grid
DragonRuby offers some constants that define the “grid” of the renderable area:
args.grid.left
args.grid.right
args.grid.top
args.grid.bottm
Pixel value for the boundaries of the virtual 720 p screen (Dragon Ruby Game Toolkits’s virtual resolution is always 1280x720).
Since DragonRuby is always rendering at 1280x720, these values are fixed, but it’s always good to use the API-provided constants in case something changes in the future
Numeric shifting (helpers for positioning)
DragonRuby monkey-patches the Numeric class with new methods that help clarify positioning on the grid:
Numeric#shift_left
Numeric#right
Numeric#up
Numeric#down
Shifts the Numeric in the correct direction by adding or subracting.
Applying Numeric shifting with the virtual grid
Combined, the virtual grid and these numeric shifting values make it a lot easier to position elements based on the grid’s boundaries:
args.outputs.labels << [args.grid.left.shift_right(5), args.grid.top.shift_down(5), "This is a label located at the top left."]
args.outputs.labels << [args.grid.left.shift_right(5), args.grid.bottom.shift_up(30), "This is a label located at the bottom left."]
args.outputs.labels << [args.grid.right.shift_left(420), args.grid.top.shift_down(5), "This is a label located at the top right."]
args.outputs.labels << [args.grid.right.shift_left(440), args.grid.bottom.shift_up(30), "This is a label located at the bottom right."]
Potential practical applications
- Positioning UI elements
- Defining the bounding box of the viewport within a game level
- Animation functions
Are lines & solids useful?
Good question! They might be useful for debugging purposes or non-sprite games, but since they’re straight lines/rectangles, their usability is fairly limited (unless someone can give me some concrete examples!)
Using the game’s uptime
The game’s uptime is measured in “tick-count”. This number will keep increasing with every frame that is painted:
args.state.tick_count
: This property contains an integer value that represents the current frame.GTK renders at 60 FPS.
A value of 0 for args.state.tick_count represents the initial load of the game.
Collision mapping
However you end up building a collision map, the inside_rect?
method is one of the most essential methods DragonRuby has for checking if there’s a collision.
# [x, y].inside_rect? [rect_x, rect_y, rect_width, rect_height]
[100, 100].inside_rect? [0, 0, 500, 500]