Write a little game in Ruby!
Created by Patrick Huesler, @phuesler & Floor Drees, @floordrees for Rails Girls The Hague
Gosu is a 2D game development library. Gosu features easy to use and game-friendly interfaces to 2D graphics and text (accelerated by 3D hardware), sound samples and music as well as keyboard, mouse and gamepad/joystick input. It also includes demos for integration with RMagick, Chipmunk and OpenGL.
The actual source code, wiki, issue tracker etc. are all hosted on GitHub. The best entry point into Gosu’s documentation is the wiki home page.
Install the Ruby gem
Run the following command in the Terminal app:
or add gem "gosu", "~> 0.7.48"
to your Gemfile and run bundle install
.
Play around with the example games:
and then: ruby CptnRuby.rb
or: ruby RMagickIntegration.rb
or: ruby Tutorial.rb
Enough with those examples already!
Copy the repository by opening (a new tab in your) terminal. If you were working on the Rails Girls app in the mean time, make sure you’re in your home directory by running cd
.
Now run
aaand change into the proper directory using
Run!
To play our little game, open it using the terminal:
So how does this work?
Let’s inspect some code, shall we? Open game.rb
in your texteditor. See the
… right at the top of your file? Here we make sure we ‘call’ the necessary gem, so we can move on to our class
(or multiple classes).
So we have our
… thing going on. The def
’s you see within this Game class, are methods. Here we define which instructions the program should follow. Just take a look at the following snippet:
Want to play around a bit? Copy the contents of game.rb
in a new .rb
file. Save it and name it as you’d like. Now try and change some stuff in the game and run it in your terminal to see the changes.
Think you have more graphic skills than Patrick (you probably do)? Then you can try and create a new sprites.png
! Don’t forget to call it here:
And see the Game.new.show
? That creates a new instance. It has no memory, so when you get stuck in the game, you can just start a new game. Have fun!
Credits
The assets used in vim adventures, you can find them here
The sounds usedm you’ll find over at Matthew Klingensmith (www.matthewklingensmith.com)
Want to learn more? View more guides!