Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Tutorial] Using Scenes

The "Scene" style workings of RPG maker are even easier to implement into Gosu.

First you need a scene:

Code:
class Scene_Start

  def update

 

  end

end

We don't need to update Graphics like in RPG Maker; nor do we need to run a loop (see below).

We need our game window:

Code:
class Game < Window

  def initialize

    super(640, 480, false)

    self.caption = "Scene Changing Demo"

    $scene = Scene_Start.new

  end

  def update

    $scene.update

  end

end

The method initialize is called when the game is first opened. As such this can be used to call our initial scene (here, Scene_Start).

The method update is ran once per frame, automatically. Therefore, we can call our scene update methods here for convenience.

From before you'll note that super(x, y, fullscreen?) here makes the window 640x480, with no fullscreen mode.


Now all that remains is to call the script:

Code:
 

$game = Game.new

$game.show


Demo

Code:
require 'rubygems'

require 'gosu'

include Gosu

 

class Scene_End

  def update

    $game.draw_text('Scene_End (Press Down to close)', 0, 0, 0)

    if $game.button_down?(Button::KbDown)

      exit

    end

  end

end

 

class Scene_Start

  def update

    $game.draw_text('Scene_Start (Press Up to continue)', 0, 0, 0)

    if $game.button_down?(Button::KbUp)

      $scene = Scene_End.new

    end

  end

end

 

class Game < Window

  def initialize

    super(640, 480, false)

    self.caption = "Scene Changing Demo"

    $scene = Scene_Start.new

    @font = Font.new(self, "Times New Roman", 24)

  end

  def update

    $scene.update

  end

  def draw_text(text, x, y, z)

    @font.draw(text, x, y, z, 1, 1)

  end

end

 

$game = Game.new

$game.show


You'll noticed I added a draw_text method; this was just for ease and probably wasn't needed. Nonetheless, play around with the "demo" some and yeah.


This probably didn't need a tutorial; nonetheless if you have any problems with this post here.
 

e

Sponsor

You don't need to make the scene a global variable; it could be a simple class variable, and then if you want to access it from outside you could just have it attr_accessor'd or something.
 
button_down!!!!
<.<

the correct use is:
def button_down(id)
if id == #key
#something
end
end

Gosu have the Input in another way than rgss so you need:

Ruby:
class Game < Window

      def initialize

        super(640, 480, false)

        self.caption = "Scene Changing Demo"

        $scene = Scene_Start.new

      end

 

      def button_down(id)

         $scene.button_down(id)

      end

 

      def update

        $scene.update

      end

 

      def draw

        $scene.draw

      end

   end

You can draw things in the update, but by default Gosu calls update to do the operations and after calls draw() to draw the things, to have all more organizated.
You dont need to use it, but is called anyway.
 

e

Sponsor

Don't forget about button_up; button_down will be called every tick for as long as the button's pressed, whereas button_up will only be called when it's released, so it depends on what you want to do.

button_down? is useful if you want to check in the update method if the button is currently pressed, but you don't want to actually deal with it (i.e.: check for combos or whatever).
 
thats not true :3

you have 3 methods

button_down()

button_up()

window_object.button_down?(id)

the two button_down(?) are different.

button_down is called every time a key is pressed and only returns true one time

.button_down? is called all the ticks while you are pressing the button, thats the difference

see it like:

button_down() -> Input.trigger?()
button_down?() -> Input.press?()
 

e

Sponsor

Um...if you read the doc carefully, it says button_down is called every time before update, which itself called on every tick; so button_down will always be called on every tick if a button is pressed, regardless of whether its the same or not.

button_down? is never called by the engine; it is explicitly called by the user. Check the doc, the examples, etc; it doesn't make sense otherwise.
 
i checked all and I know how it works, i had some problems with these at the beginning, asked in #gosu (freenode) and in the forums, and now works perfect for me, im sure of what im saying.

button_down can be called in all the frames, but only return one true for key, only one time :3
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top