All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

Ruby library Gosu for creating 2D games

Post cover: Ruby library Gosu for creating 2D games
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
Gosu is a library for developing 2D games and graphical applications in the Ruby programming language (as well as C++). It simplifies game creation by providing a set of convenient and efficient tools for working with graphics, sound, text, and input management.

Gosu Features

  • Gosu supports loading and displaying images in PNG, JPEG, and other formats. Animation is also supported using sprite sheets.
  • The library allows displaying text using system fonts or loaded font files.
  • Gosu supports playing sounds and music in WAV and MP3 formats, making it easy to add sound effects and background music to the game.
  • The library provides convenient handling of input from the keyboard and mouse, simplifying the control of characters and other game elements.
  • Gosu allows creating smooth animations using timers and frame cycles.
  • With open-source code and support for extensions, Gosu easily integrates with other libraries and allows adding new functionalities.
In the threads related to this post, I will gradually add small notes on how to work with Gosu. I also attached a video with a demo that is packaged in a Ruby gem.
Let's install the Gosu demo:
gem install gosu-examples
Let's run the demo:
gosu-examples
gosu-examples
gosu-examples
If everything is installed correctly, a window with demos will open, which you can explore. Here is the translation of the text from the main screen:
Welcome to the Gosu Example Box!

This little tool lets you launch any of Gosu’s example games from the list on the right hand side of the screen.

Every example can be run both from this tool and from the terminal/command line as a stand-alone Ruby script.

Keyboard shortcuts:

• To see the source code of an example or feature demo, press E.
• To open the ‘examples’ folder, press O.
• To quit this tool, press Esc.
• To toggle fullscreen mode, press Alt+Enter (Windows, Linux) or cmd+F (macOS).

Why not take a look at the code for this example right now? Simply press E.
If you press E right away, the part corresponding to this first screen will open in your code editor:
require "gosu"

WIDTH, HEIGHT = 640, 480

class Welcome < (Example rescue Gosu::Window)
  PADDING = 20

  def initialize
    super WIDTH, HEIGHT

    self.caption = "Welcome!"

    text =
      "<b>Welcome to the Gosu Example Box!</b>

      This little tool lets you launch any of Gosu’s example games from the list on the right hand side of the screen.

      Every example can be run both from this tool <i>and</i> from the terminal/command line as a stand-alone Ruby script.

      Keyboard shortcuts:

      • To see the source code of an example or feature demo, press <b>E</b>.
      • To open the ‘examples’ folder, press <b>O</b>.
      • To quit this tool, press <b>Esc</b>.
      • To toggle fullscreen mode, press <b>Alt+Enter</b> (Windows, Linux) or <b>cmd+F</b> (macOS).

      Why not take a look at the code for this example right now? Simply press <b>E</b>."

    # Remove all leading spaces so the text is left-aligned
    text.gsub! /^ +/, ""

    @text = Gosu::Image.from_markup text, 20, width: WIDTH - 2 * PADDING

    @background = Gosu::Image.new "media/space.png"
  end

  def draw
    draw_rotating_star_backgrounds

    @text.draw PADDING, PADDING, 0
  end

  def draw_rotating_star_backgrounds
    # Disregard the math in this method, it doesn't look as good as I thought it
    # would. =(

    angle = Gosu.milliseconds / 50.0
    scale = (Gosu.milliseconds % 1000) / 1000.0

    [1, 0].each do |extra_scale|
      @background.draw_rot WIDTH * 0.5, HEIGHT * 0.75, 0, angle, 0.5, 0.5,
        scale + extra_scale, scale + extra_scale
    end
  end
end

Welcome.new.show if __FILE__ == $0
And by choosing cptn_ruby.rb you can play a simple platformer (I added a gameplay video to this post) ^_^
In the next post, I will discuss the process of writing/creating a game a bit.

This post doesn't have any additions from the author yet.

What is immutability and mutability?
19 Jun 07:48

What is immutability and mutability?

meme code
meme code@memecode
What will be the result of adding 10.5 and 10?
23 Jun 13:23

What will be the result of adding 10.5 and 10?

meme code
meme code@memecode
[Ruby] What is the difference between variables that start with @, @@, and $?
23 Jun 14:00

[Ruby] What is the difference between variables that start with @, @@, and $?

meme code
meme code@memecode
What is a function in programming?
24 Jun 18:15

What is a function in programming?

meme code
meme code@memecode
[Fix] extconf.rb failed during the installation of the Ruby library Gosu
27 Jun 16:38

[Fix] extconf.rb failed during the installation of the Ruby library Gosu

meme code
meme code@memecode
How to make an empty git commit?
28 Jun 08:33

How to make an empty git commit?

meme code
meme code@memecode
Gosu Ruby Tutorial - пройдемось по офіційній документації
03 Jul 11:50

Gosu Ruby Tutorial - пройдемось по офіційній документації

meme code
meme code@memecode
We are writing a demo game Drones vs Zombies (Gosu / Ruby)
12 Jul 12:17

We are writing a demo game Drones vs Zombies (Gosu / Ruby)

meme code
meme code@memecode
How to fix a Windows crash caused by CrowdStrike?
19 Jul 13:53

How to fix a Windows crash caused by CrowdStrike?

meme code
meme code@memecode
What does .map(&:name) mean in Ruby?
28 Jul 11:18

What does .map(&:name) mean in Ruby?

meme code
meme code@memecode
How does the map method work in Ruby? Overview of the method's operation with examples
30 Jul 07:33

How does the map method work in Ruby? Overview of the method's operation with examples

meme code
meme code@memecode
What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?
02 Aug 13:15

What does a dot at the beginning of a file (.gitignore, .DS_Store, .bashrc, etc.) mean?

meme code
meme code@memecode