Here's an example on Chingu on how to change from one GameState ("Scene") to another one. You can go forward or backward as needed.
The image I included as a character bitmap was a modified PNG file, Actors1.png, that only included Ralph...
Edit...
For an updated version you might visit my topic named Chingu On Action...
[rgss]#!/usr/bin/env ruby
# by Kyonides
# tools : Chingu, Gosu & Ruby on a Linux OS
# Title Picture taken from http://www.thoe.net/img/
# Game Over Picture taken from
http://www.lpi.usra.edu/education/timel ... ry/images/
require 'rubygems'
require 'chingu'
require 'modules'
require 'player_attr'
include Gosu, Chingu, KyoVocab
class Chingu::Window < Gosu::Window
alias kyon_chwin_init initialize
def initialize(width=800, height=600, fullscreen=false, update_interval=16.666666)
KyoVocab.window = super(width, height, fullscreen, update_interval)
kyon_chwin_init(width, height, fullscreen, update_interval)
end
end
class Chingu::Text
def create_image
@max_width=KyoVocab.window.width if @max_width == nil
@image = Gosu::Image.from_text(KyoVocab.window, @text, @font, @height,
@line_spacing=8, @max_width, @align)
end
end
class Kyon < Chingu::Window
attr_reader :factor
def initialize
super(640,480,false)
self.caption = 'KyoGame'
@factor = 2
switch_game_state(Title)
end
end
class Title < Chingu::GameState
def setup
super
self.input = {:escape => :exit, :enter => :new_scene}
@title = Image['images/nightsky640.png']
welcome = 'Welcome to Game'
@text = Text.new(welcome, :x=>0, :y=>50, :zorder=>55, :size=>28, :align=>:center)
end
def draw
@title.draw(0, 0, 0)
@text.draw
super
end
def new_scene
switch_game_state(Scene)
end
end
class Scene < Chingu::GameState
has_trait :debug => true
def setup
super
self.input = {:escape => :exit,
:enter => :new_scene,
:backspace => :title,
:f1 => :debug}
@night = Image['images/nightsky640.png']
msg = "The Scene\ninsert the code in some\nChingu::GameState class like this one"
@text = Text.new(msg, :x => 0, :y => 50, :align => :center,
:zorder => 55, :size => 30, :max_width => 250)
@hp = Text.new('HP', :x => 10, :y => 437, :zorder => 200, :size => 20,
:align => :left, :max_width => 100)
@mp = Text.new('MP', :x => 10, :y => 455, :zorder => 200, :size => 20,
:align => :left, :max_width => 100)
@bg0, @bg1, @bg2 = Color.new(0xFF000000), Color.new(0xFFCE28FF), Color.new(0xFF013E87)
@rect = Rect[5,434,160,41]
@hp_rect = [40,KyoVocab.window.height-40,100,10]
@mp_rect = [40,KyoVocab.window.height-20,100,10]
@player = Hero.create#(Image['ralph.png']) # the parent class includes an image filename
# but this allows the scripter to test if the parameters are passed correctly
end
def draw
@night.draw(0, 0, 0)
@text.draw
@hp.draw
@mp.draw
fill_gradient
rect => @rect,
rientation => :horizontal, :zorder => 150)
fill_gradient
from => @bg1, :to => @bg2, :rect => @hp_rect,
rientation => :horizontal,
:zorder => 150)
fill_gradient
from => @bg2, :to => @bg1, :rect => @mp_rect,
rientation => :horizontal,
:zorder => 150)
super
end
def update
super
KyoVocab.window.caption = "City Battle! Player x/y: #{@player.x}/#{@player.y}"
end
def new_scene
push_game_state(GameOver)
end
def title
switch_game_state(Title)
end
# def debug # ignore it, it doesn't work
# push_game_state(GameState::Debug)
# end
end
class GameOver < Chingu::GameState
def setup
super
self.input = {:escape => :exit, :return => :retry, :enter => :new_scene}
@over = Image['images/desert.png']
@gameover = Text.new('GAME OVER', :x=>0, :y=>50, :align=>:center, :zorder=>55, :size=>36)
@gameover.color = 0xFF000000
@tryagain = Text.new('Press Return to try again', :x => 0, :y => 250,
:align => :center, :zorder => 55, :size => 36)
end
def draw
@over.draw(0, 0, 0)
@gameover.draw
@tryagain.draw
super
end
def retry
pop_game_state
end
def new_scene
KyoVocab.window.close
end
end
class Hero < Game::Hero
has_traits :velocity
attr_accessor :x, :y
def initialize(image=nil, options={})
super(image=nil, options)
self.factor = KyoVocab.window.factor/1.5
if options == {}
@x, @y = 300, 200
else
@x, @y = options[:x], options[:y]
@hp, @mp, @ap = options[:hp], options[:mp], options[:ap]
end
self.input = {
:holding_left => :left,
:holding_right => :right,
:holding_up => :up,
:holding_down => :down}
@max_velocity = 2
end
def up; self.velocity_y = -@max_velocity; end
def down; self.velocity_y = @max_velocity; end
def right; self.velocity_x = @max_velocity; end
def left; self.velocity_x = -@max_velocity; end
def update
super
self.velocity_y *= 0.6
self.velocity_x *= 0.6
@x = @last_x if @x < 0 || @x > KyoVocab.window.width
@y = @last_y if @y < 0 || @y > KyoVocab.window.height
@last_x, @last_y = @x, @y
@image.draw(@x, @y, 0)
end
end
Kyon.new.show # Should be the last line in the main script
[/rgss]