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

The documentation/ folder is a great resource for how to use the engine’s primitives

General rendering rules

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:

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:

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:

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

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]