forked from ippa/chingu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example13_high_scores.rb
97 lines (81 loc) · 2.67 KB
/
example13_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
#!/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 HighScoreList-class
# I couldn't keep myself from spicying it up some though :P
#
class Game < Chingu::Window
def initialize
super(640,400)
self.input = {:esc => :exit, :a => :add }
self.caption = "Example of Chingus HighScore class"
@title = PulsatingText.create("HIGH SCORES", :x => $window.width/2, :y => 50, :size => 70)
#
# Load a list from disk, defaults to "high_score_list.yml"
# Argument :size forces list to this size
#
@high_score_list = HighScoreList.load(:size => 10)
#
# Add some new high scores to the list. :name and :score are required but you can put whatever.
# They will mix with the old scores, automatic default sorting on :score
#
10.times do
data = {:name => "NEW", :score => rand(10000)}
position = @high_score_list.add(data)
if position
puts "#{data[:name]} - #{data[:score]} got position #{position}"
else
puts "#{data[:name]} - #{data[:score]} didn't make it"
end
end
create_text
end
def add
data = {:name => "NEW", :score => @high_score_list.high_scores.first[:score] + 10, :text => "from example13.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
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 = [Color::RED, Color::GREEN, Color::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