Text Example

require 'gosu'

class TextExample < Gosu::Window
	def initialize
		super 640, 480
		self.caption = "Text Example"

		@font = Gosu::Font.new(32, name: "Nimbus Doggo L")

	def draw
		@font.draw("Otter.Tech Rocks My Socks!", 10, 20, 0)

window = TextExample.new.show

Similar to images, fonts have two parts- loading and painting.

The loading part is relatively straightforward. The first parameter tells Gosu what size to make the font (height in pixels), and the second tells it what font to use.

Now you may be wondering where the font actually comes from- there’s no “Nimbus Mono L” file in the snippet repository, so how does Gosu know what the font looks like? There are three ways:

name: "Font Name" tells Gosu to get that font name from your operating system. If it can’t find it, it uses a default font.

name: "./font/path.ttf" tells Gosu to load a TrueType Font file from disk. This is helpful if you have a font which might not be installed on your player’s system. Note that Gosu expects to see a / or ./ at the beginning so that it knows this is a file.

Or you can just omit the argument and Gosu will pick a default font. This is helpful for prototyping.

Our drawing is also very similar to images. In fact, the only difference is that the font also requires some text to draw. The Font class can take the same additional parameters for additional effect:

draw(text, x, y, z, scale_x = 1, scale_y = 1, color = 0xff_ffffff, mode = :default)

  • x: The X-Coordinate of your image (the upper side)
  • y: The Y-Coordinate of your image(the left side)
  • z: The Z-Index or Z-Order which determines which images are drawn on top
  • scale_x: Factor that gets multiplied against the width of your image to grow or shrink it (e.g. 0.5 halves the image size, 2 doubles it, 1 keeps it the same)
  • scale_y: Factor that gets multiplied against the height of your image to grow or shrink it (e.g. 0.5 halves the image size, 2 doubles it, 1 keeps it the same)
  • color: Shades the image with the color specified (as a hexidecimal value)
  • mode: Determines how the image is “blended” with the images underneath it.

As always, you can find more details in the official documentation.