forked from ippa/chingu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example15_trait_timer2.rb
57 lines (47 loc) · 1.23 KB
/
example15_trait_timer2.rb
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
#!/usr/bin/env ruby
require 'rubygems' rescue nil
$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
require 'chingu'
include Gosu
include Chingu
#
# Testing advanced options of timer-trait
#
class Game < Chingu::Window
def initialize
super(640,400)
self.input = {:esc => :exit}
switch_game_state(Stuff)
end
end
class Stuff < GameState
trait :timer
def initialize(options = {})
super
self.input = {:s => :stop, :"1" => :fast, :"2" => :slow}
@thing = Thing.create(:x => $window.width/2, :y => $window.height / 2 )
every(500, :name => :blink) { @thing.visible? ? @thing.hide! : @thing.show! }
p timer_exists?(:blink)
$window.caption = "Timer-usage. 's' to stop timer, '1' for fast timer, '2' for slow timer"
end
def stop
stop_timer(:blink)
end
def fast
every(200, :name => :blink) { @thing.visible? ? @thing.hide! : @thing.show! }
end
def slow
every(1000, :name => :blink) { @thing.visible? ? @thing.hide! : @thing.show! }
end
def update
super
game_objects.destroy_if { |object| object.outside_window? }
end
end
class Thing < GameObject
def initialize(options = {})
super
@image = Image["circle.png"]
end
end
Game.new.show