Coder Social home page Coder Social logo

gosu-tutorial's Introduction

Introduction to Gosu

What is Gosu?

Gosu is a 2d game development library for Ruby and C++. It is available for macOS, Windows, Linux (including Raspbian), as well as iOS. This tutorial will focus on macOS, so if you're using the other operating systems, we've included handy-dandy links to their installation ways.

Gosu is a lightweight engine with few dependencies, and provides:

  • a window and a main loop
  • 2D graphics and text, powered by OpenGL or OpenGL ES
  • sounds and music
  • keyboard, mouse, and gamepad input

Installation

Since we are using Ruby with Gosu, Ruby must be installed into your computer.

Step One

In order to install Ruby into your computer, Homebrew (a package manager for Mac) is necessary! So, let's do that first.

  • PS, the install scripts are to be pasted onto your Terminal (since you're learning Gosu, we're going to assume you know what a Terminal is), but location is irrelevent because yay global installs!
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Step Two

Now that Homebrew is installed, it's Ruby's turn!

$ brew install ruby

Step Three

We've successfully installed both Homebrew and Ruby; let's take look at Gosu again. Like we mentioned before, Gosu has few dependencies, an important one being the SDL 2 Library. In order for Gosu to actually work, let's install that, too.

brew install sdl2

Step Four

Final installation - it's time for the one we've all been waiting for: Gosu!

gem install gosu

Let's test that out real quick with this one-liner. If everything's been correctly installed, a window should pop up after hitting enter.

ruby -rgosu -e 'w = Gosu::Window.new(200, 150); w.caption = "It works!"; w.show'

If that worked, Install Fest was successful! :partyparrot:

Gosu-Examples

One of the great things about Gosu are the example games it provides. Install and run the gosu-examples (check out the code here!) gem:

gem install gosu-examples
gosu-examples

A few of these games require the following additional libraries:

gem install chipmunk
  • Chipmunk2D is a simple, lightweight, fast and portable 2D rigid body physics library written in C.
gem install rmagick
  • RMagick is an interface between the Ruby programming language and the ImageMagick® and GraphicsMagick image processing libraries.
gem install opengl
  • OpenGL is the premier environment for developing portable, interactive 2D and 3D graphics applications.

Let's Make a Thing!

The best way to learn a new framework or language is to make a simple game or app. We're going to render a ruby in our Gosu window and implement movement to it.

Step One: Gosu Window

Step One

  • class Tester inherits from Gosu::Window
  • initialize method takes three parameters: width, height, fullscreen
  • call on class Tester with Tester.new.show

Now you have a Gosu Window!

Step One A

Step Two: Rendering an Image in the Window

Step Two

  • create a class Ruby
  • initialize method takes four parameters: window, x, y, width, height
  • create instance variables that equal to those parameters! (ex: @width = width, @height = height)
  • note: the radius will be equal to the width divided by 2
  • the instance variable for your ruby image will contain three parameters- window, image location, as well as options
  • now that we've initialized our Ruby class, we'll fill out the draw method:
  • call the image instance variable and

Step Two A

Now that we've created our image class, let's implement it in class Tester!

  • we've previously created two unused method in class Tester: the draw method and the update method; we'll be using the draw method now.
  • in the draw method, refer to your image instance variable and accompany it with a ".draw"! easy peasy.
  • in the initialize method, we'll call on the image instance variable once more, and fill out the parameters we called on in the class Ruby initialize method
  • remember initialize(window, x, y, width, height)?

Yay, a ruby!

Step Two B

We're not done yet, though! Gosu is a gaming library, after all, and gaming involves movement. Our next step is to implement movement to the image.

Step Three: Movement

The following code belongs in your class Ruby

Step Three

  • here we create the following methods: move, right_wall, left_wall, bottom_wall, top_wall, h_bounce, and v_bounce

Step Three A

  • in the move method, we're going to call on the methods created in the previous image. the "if" statements are telling the ruby "if you hit this wall, do 'this'"

Step Three B (my skill in drawing circles is impeccable)

  • in this image, we're adding velocity to both the x-axis and the y-axis. this belongs in the initialize method of class Ruby

We've got movement! Go, ruby, go!

gif

Step Four: Background Image

Thus far, we've seen how easy it is to render a Gosu window, add a player to the screen, and make it movable. Now we're going to add a background image, and marvel at its' simplicity!

In the initialize method of class Tester, we provided parameters indicating the height and width of the window. The more we learn about Gosu (and Ruby, by extension), the more we realize there's different ways of doing things. Let's switch it up a bit by commenting our that line of code and placing our new height and width right next to super.

Step Four

  • Test that out!

Next up, we're going to create a new instance variable in class Tester for our background image. Indicated by my fabulous red dots, we see it created in the initialize method, then called in the draw method.

http://i.imgur.com/rFr2k7w.png

  • Note: Pay attention to the comments

Go ruby, go clouds! ruby cloud gif

Step Five: Adding Multiple Images

We're going to add multiple images to our game. In order to do that, we're going to change our @ruby instance method so it's more reuseable.

Step Five

Step Five A

  • these instance variables are created in the initialize method of class Tester

Step Five B

  • now that we've initialized an array and random instance variable, we'll incorporate that into some new methods!
  • note: we're still in class Tester for these methods

Step Five C

  • in order for the user to actually render multiple images, we'll create a button down method that says: "If the space button is pressed, create another ruby." Then we're going to throw in a little surprise turtle! "If the T button is pressed, create a turtle!"

turtle ruby gif

Look at that, guys! We've managed to create a Gosu Window, a background image, and two moveable images, and all in 120 lines of code. This just reinforces the fact that Gosu is a lightweight engine that, in conjuction with the efficiency and speed of Ruby, makes it easy for programmers to render a simple game or app. Thank you for reading our tutorial!

gosu-tutorial's People

Contributors

sgaraicoa avatar dkoreeda avatar axkaban avatar

Stargazers

Penny Gautreaux avatar

Watchers

James Cloos avatar  avatar

Forkers

jtmorales331

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.