forked from ippa/chingu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example16_online_high_scores.rb
107 lines (88 loc) · 2.99 KB
/
example16_online_high_scores.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
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
#!/usr/bin/env ruby
require 'rubygems' rescue nil
$LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
require 'chingu'
include Gosu
include Chingu
#
# Demonstrating Chingus OnlineHighScoreList-class
#
# Syncs against http://www.gamercv.com/games/1-test
#
class Game < Chingu::Window
def initialize
super(640,400)
self.caption = "OnlineHighScoreList example. Press 'A' to add a top-score, 'R' to add a random."
push_game_state(HighScore)
end
end
class HighScore < GameState
def setup
self.input = {:esc => :exit, :a => :enter_name, :r => :add_random}
PulsatingText.create("class OnlineHighScoreList & www.gamvercv.com", :x => $window.width/2, :y => 50, :size => 30)
Text.create("Syncs from/to http://www.gamercv.com/games/1-test", :x => $window.width/2, :y => 80, :size => 24, :rotation_center => :center)
#
# Load a remote high score list
#
@high_score_list = OnlineHighScoreList.load(:game_id => 1, :login => "chingu", :password => "chingu", :limit => 10)
create_text
end
def enter_name
push_game_state GameStates::EnterName.new(:callback => method(:add) )
end
def add(name = nil)
return unless name
return unless name.size > 0
data = {:name => name, :score => (@high_score_list[0][:score].to_i + 10), :text => "Hello from Chingus example16.rb"}
position = @high_score_list.add(data)
puts "Got position: #{position.to_s}"
create_text
end
def add_random
data = {:name => "RND", :score => @high_score_list[0][:score]-rand(100), :text => "Random from Chingus example16.rb"}
position = @high_score_list.add(data)
puts "Got position: #{position.to_s}"
create_text
end
def create_text
Text.destroy_if { |text| text.size == 20 }
#
# Iterate through all high scores and create the visual represenation of it
#
@high_score_list.each_with_index do |high_score, index|
y = index * 25 + 100
Text.create(high_score[:name], :x => 200, :y => y, :size => 20)
Text.create(high_score[:score], :x => 400, :y => y, :size => 20)
end
end
end
#
# colorful pulsating text...
#
class PulsatingText < Text
traits :timer, :effect
@@red = Color.new(0xFFFF0000)
@@green = Color.new(0xFF00FF00)
@@blue = Color.new(0xFF0000FF)
def initialize(text, options = {})
super(text, options)
options = text if text.is_a? Hash
@pulse = options[:pulse] || false
self.rotation_center(:center_center)
every(20) { create_pulse } if @pulse == false
end
def create_pulse
pulse = PulsatingText.create(@text, :x => @x, :y => @y, :height => @height, :pulse => true, :image => @image, :zorder => @zorder+1)
colors = [@@red, @@green, @@blue]
pulse.color = colors[rand(colors.size)].dup
pulse.mode = :additive
pulse.alpha -= 150
pulse.scale_rate = 0.002
pulse.fade_rate = -3 + rand(2)
pulse.rotation_rate = rand(2)==0 ? 0.05 : -0.05
end
def update
destroy if self.alpha == 0
end
end
Game.new.show