Skip to content

Tutorial: write a gosu game

Garoe Dorta edited this page May 11, 2015 · 19 revisions

Goal

Write a small game using Gosu.

Prerequisites

You should have completed the Getting started with Ruboto tutorial.

This tutorial has been tested with the following setups

Platform JDK Ruby Ruboto Ruboto Core Gosu Device API level Tester
OS X 10.9.2 1.8.0 2.1.1 1.0.3 1.0.0 0.0.5 Emulator 15 donV
Ubuntu 12.04 6b27-1.12.3 1.8.7 0.10.2 0.5.3 0.0.5 Point of View, Mobii 10.1 11 Garoe

Further reading

http://www.libgosu.org

https://github.com/Garoe/gosu-android/wiki

Create the app and install it

Connect your device or start the emulator.

ruboto gen app --package org.ruboto.example.tutorial_touch
cd tutorial_touch
rake install start

You should see the standard "What hath Matz wrought?" screen.

A fresh app

Add the gosu_android gem to your app

Create a new file Gemfile.apk with the following content

source 'https://rubygems.org/'

gem 'gosu_android'

Add the Tutorial_touch activity from the gosu_android repository

Copy the contents of the Tutorial_touch example from the gosu_android examples directory:

https://github.com/Garoe/gosu-android/blob/master/examples/tutorial_touch_activity.rb

The contents should replace the content of the src/tutorial_touch_activity.rb file.

For convenience, we have included the file here:

require 'gosu'

module ZOrder
  Background, Stars, Player, UI = *0..3
end

class Player
  attr_reader :score

  def initialize(window)
    @image = Gosu::Image.new(window, Ruboto::R::drawable::star_fighter, false)
    @beep = Gosu::Sample.new(window, Ruboto::R::raw::beep)
    @x = @y = @vel_x = @vel_y = @angle = 0.0
    @score = 0
  end

  def warp(x, y)
    @x, @y = x, y
  end

  def draw
    @image.draw_rot(@x, @y, ZOrder::Player, @angle)
  end
  
  def collect_stars(stars)
    stars.reject! do |star|
      if Gosu::distance(@x, @y, star.x, star.y) < 35 then
        @score += 10
        @beep.play
        true
      else
        false
      end
    end
  end
end

class Star
  attr_reader :x, :y
  
  def initialize(animation)
    @animation = animation
    @color = Gosu::Color.new(0xff000000)
    @color.red = rand(256 - 40) + 40
    @color.green = rand(256 - 40) + 40
    @color.blue = rand(256 - 40) + 40
    @x = rand * 640
    @y = rand * 480
  end

  def draw
    img = @animation[Gosu::milliseconds / 100 % @animation.size]
    img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
        ZOrder::Stars, 1, 1, @color, :add)
  end
end

class GameWindow < Gosu::Window
  def initialize
    super(640, 480, false)
    self.caption = "Gosu Tutorial Game"
    
    @background_image = Gosu::Image.new(self, Ruboto::R::drawable::space, true)
    
    @player = Player.new(self)
    @player.warp(320, 240)

    @star_anim = Gosu::Image::load_tiles(self, Ruboto::R::drawable::star, 25, 25, false)
    @stars = Array.new
    
    @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
  end

  def update
    @player.collect_stars(@stars)
    #Normally 25 stars
    if rand(100) < 4 and @stars.size < 5 then
      @stars.push(Star.new(@star_anim))
    end
  end

  def touch_moved(touch)
    @player.warp(touch.x, touch.y)
  end

  def draw
    @background_image.draw(0, 0, ZOrder::Background)
    @player.draw
    @stars.each { |star| star.draw }
    @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
  end

  def button_down(id)
    if id == Gosu::KbEscape then
      close
    end
  end
end

class TutorialTouchActivity
  def on_create(bundle)
    super(bundle)
    Gosu::AndroidInitializer.instance.start(self)
    rescue Exception => e
      puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
  end
  
  def on_ready
    window = GameWindow.new
    window.show
    rescue Exception => e
      puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
  end
end

Add the game resources

Download these images to your res/drawable folder:

Download this sound file to your res/raw folder:

Try it!

Build, install and run the application

rake install start

You should now see a simple game with a star-fighter that must collect stars.

Enjoy!

Comment I had to fetch the png character_atlas8.png and copy it to res/drawable to get it run. FJ https://github.com/Garoe/gosu-android/blob/master/res/drawable-nodpi/character_atlas8.png

Clone this wiki locally