Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 167 lines (144 sloc) 4.984 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#!/usr/bin/env ruby
require 'rubygems' rescue nil
$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
require 'chingu'
include Gosu
include Chingu

#
# GFXHelpers example - demonstrating Chingus GFX
#
class Game < Chingu::Window
  @@number_of_game_states = 7
  def initialize
    super(640,400)
    self.input = {:space => :next_effect, :esc => :exit}
    self.caption = "Example of Chingus GFX Helpers"
    load_game_states
  end
  
  def next_effect
    @@number_of_game_states -= 1
    if @@number_of_game_states == 0
      load_game_states
      @@number_of_game_states = 7
    else
      pop_game_state
    end
  end
  
  def load_game_states
    push_game_state(Fill)
    push_game_state(FillRect)
    push_game_state(FillGradient)
    push_game_state(FillGradientRect)
    push_game_state(FillGradientMultipleColors)
    push_game_state(DrawCircle)
    push_game_state(Particles)
  end
end



class DrawCircle < Chingu::GameState
  def draw
    $window.caption = "circles and arcs (press space to continue)"
    draw_circle(0, 0, 300, Color::RED)
    fill_circle($window.width, $window.height, 200, Color::RED)
    
    colors = [Color::RED, Color::YELLOW, Color::GREEN, Color::BLUE]
    0.step(360, 90).each_cons(2).zip(colors).each do |(a1, a2), color|
      fill_arc(400, 100, 100, a1, a2, color)
    end
    
  end
end

class Fill < Chingu::GameState
  def draw
    $window.caption = "fill (press space to continue)"
    fill(Color::RED)
  end
end

class FillRect < Chingu::GameState
  def draw
    $window.caption = "fill_rect (press space to continue)"
    fill_rect([10,10,100,100], Color::WHITE)
  end
end

class FillGradient < Chingu::GameState
  def setup
    @pinkish = Color.new(0xFFCE17B6)
    @blueish = Color.new(0xFF6DA9FF)
  end
  
  def draw
    $window.caption = "fill_gradient (press space to continue)"
    fill_gradient(:from => @pinkish, :to => @blueish, :orientation => :vertical)
  end
end

class FillGradientMultipleColors < Chingu::GameState
  def setup
    @colors = [ 0xffff0000, 0xff00ff00, 0xff0000ff ].map { |c| Color.new(c) }
  end
  
  def draw
    $window.caption = "fill_gradient with more than two colors (press space to continue)"
    fill_gradient(:colors => @colors, :orientation => :horizontal)
  end
end

class FillGradientRect < Chingu::GameState
  def setup
    @color1 = Color.new(0xFFFFEA02)
    @color2 = Color.new(0xFF078B20)
  end
  
  def draw
    $window.caption = "fill_gradient with :rect-option (press space to continue)"
    fill_gradient(:from => @color1, :to => @color2, :rect => [100,100,200,200], :orientation => :horizontal)
  end
end

class Particles < Chingu::GameState
  def setup
    @color1 = Color.new(0xFFFFEA02)
    @color2 = Color.new(0xFF078B20)
    @blue_laserish = Color.new(0xFF86EFFF)
    @red = Color.new(0xFFFF0000)
    @white = Color.new(0xFFFFFFFF)
    @yellow = Color.new(0xFFF9F120)
    
    # Thanks jsb in #gosu of Encave-fame for fireball.png :)
    @fireball_animation = Chingu::Animation.new(:file => media_path("fireball.png"), :size => [32,32])
    @ground_y = $window.height * 0.95
  end
  
  def update
    #
    # Fire 1. Dies quickly (big :fade). Small in size (small :zoom)
    #
    Chingu::Particle.create( :x => 100,
                          :y => @ground_y,
                          :animation => @fireball_animation,
                          :scale_rate => +0.05,
                          :fade_rate => -10,
                          :rotation_rate => +1,
                          :mode => :default
                        )

    #
    # Fire 2. Higher flame, :fade only -4. Wide Flame with bigger :zoom.
    #
    Chingu::Particle.create( :x => 300,
                          :y => @ground_y,
                          :animation => @fireball_animation,
                          :scale_rate => +0.2,
                          :fade_rate => -4,
                          :rotation_rate => +3,
                          :mode => :default
                        )
    #
    # Fire 3. Blue plasma with smooth particle.png and color-overlay.
    #
    Chingu::Particle.create( :x => 500,
                          :y => @ground_y,
                          :image => "particle.png",
                          :color => @blue_laserish,
                          :mode => :additive
                        )

    Particle.each { |particle| particle.y -= 5; particle.x += 2 - rand(4) }
    game_objects.destroy_if { |object| object.outside_window? || object.color.alpha == 0 }
    super
  end
  
  def draw
    $window.caption = "particle example (space to continue) [particles#: #{game_objects.size} - framerate: #{$window.fps}]"
    fill_gradient(:from => Color.new(255,0,0,0), :to => Color.new(255,60,60,80), :rect => [0,0,$window.width,@ground_y])
    fill_gradient(:from => Color.new(255,100,100,100), :to => Color.new(255,50,50,50), :rect => [0,@ground_y,$window.width,$window.height-@ground_y])
    super
  end
end

Game.new.show
Something went wrong with that request. Please try again.