rubyhop 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Manifest.txt +1 -0
  3. data/lib/rubyguy.png +0 -0
  4. data/lib/rubyhop.rb +179 -27
  5. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3a7bd3e2a2d85f16e6554902f36a25ce939a603
4
- data.tar.gz: f5eac8301dbc469bd5f4fcd1cf5ad1aa20021ee3
3
+ metadata.gz: 957ec92efc1036be06cb60d23aedad68cf0ad1da
4
+ data.tar.gz: f7aac31a776fa91c8451d67322acf0f86f09bd2c
5
5
  SHA512:
6
- metadata.gz: ccf0c6b7733f1d431509398cce633582cfcdc40ca1d88a544ba83b3a5831040f10c918bfcbb8c3fb22b677a8fc4313f6e9488d7e48a300cf937a40d6ddae1ad3
7
- data.tar.gz: 2d3a1a8cb3f33865d9c73ffa45151fc4fd3ec6cdd32774f3f23c654cade645272ff2ee4b70a6ee53b251a964676321d896a5d7205757f67ba539ebb727bfc7f3
6
+ metadata.gz: 903d1ebedb4f768e933a46c1e01ccf7b88c5aa14ed53717e5eb781ac02dd320f323dfdc805fc9ebe2a1fa7646427bb472d86d9056c16a767853f2fb6d33f77e8
7
+ data.tar.gz: fd21aa49f77ccba3f7397692a5200f314f9978ab7d5216030696133cc66936c87e0797610e51b6a5fdc093c48ada750d4007083635f1404af31bd0267f482c0f
data/Manifest.txt CHANGED
@@ -12,5 +12,6 @@ lib/music.mp3
12
12
  lib/rubyguy-dead.png
13
13
  lib/rubyguy-fall.png
14
14
  lib/rubyguy-rise.png
15
+ lib/rubyguy.png
15
16
  lib/rubyhop.rb
16
17
  test/test_rubyhop.rb
data/lib/rubyguy.png ADDED
Binary file
data/lib/rubyhop.rb CHANGED
@@ -6,22 +6,20 @@ end
6
6
 
7
7
  class Player
8
8
  attr_accessor :x, :y, :alive
9
- def initialize window
10
- @window = window
11
- @alive = true
9
+ def initialize level
10
+ @level = level
11
+ @window = @level.window
12
12
  # position
13
- @x = window.width/2
14
- @y = window.height/2
15
- @velocity = 0.0
13
+ start!
16
14
  @gravity = -0.25
17
15
  @hop = 7.5
18
16
  # sounds
19
17
  @sound = Gosu::Sample.new @window, get_my_file("hop.mp3")
20
18
  @gameover = Gosu::Sample.new @window, get_my_file("gameover.mp3")
21
19
  # images
22
- @rise = Gosu::Image.new window, get_my_file("rubyguy-rise.png")
23
- @fall = Gosu::Image.new window, get_my_file("rubyguy-fall.png")
24
- @dead = Gosu::Image.new window, get_my_file("rubyguy-dead.png")
20
+ @rise = Gosu::Image.new @window, get_my_file("rubyguy-rise.png")
21
+ @fall = Gosu::Image.new @window, get_my_file("rubyguy-fall.png")
22
+ @dead = Gosu::Image.new @window, get_my_file("rubyguy-dead.png")
25
23
  end
26
24
  def hop
27
25
  if @alive
@@ -29,6 +27,12 @@ class Player
29
27
  @velocity += @hop
30
28
  end
31
29
  end
30
+ def start!
31
+ @x = @window.width/2
32
+ @y = @window.height/2
33
+ @velocity = 0.0
34
+ @alive = true
35
+ end
32
36
  def die!
33
37
  if @alive
34
38
  # Set velocity to one last hop
@@ -41,11 +45,11 @@ class Player
41
45
  @velocity += @gravity
42
46
  @y -= @velocity
43
47
  if @alive && (@y < 32 || @y > @window.height - 32)
44
-
48
+ die!
45
49
  end
46
- if @y > 5000
50
+ if @y > 1000
47
51
  # kick out to loading screen to try again?
48
- @window.close
52
+ @level.fail!
49
53
  end
50
54
  end
51
55
  def draw
@@ -66,11 +70,11 @@ end
66
70
 
67
71
  class Hoop
68
72
  attr_accessor :x, :y, :active
69
- def initialize window
70
- @window = window
71
- @hoop = Gosu::Image.new window, get_my_file("hoop.png")
73
+ def initialize level
74
+ @level = level
75
+ @window = @level.window
76
+ @hoop = Gosu::Image.new @window, get_my_file("hoop.png")
72
77
  # center of screen
73
- @movement = 2
74
78
  @x = @y = 0
75
79
  @active = true
76
80
  end
@@ -83,26 +87,53 @@ class Hoop
83
87
  false
84
88
  end
85
89
  def update
86
- @movement += 0.003
87
- @x -= @movement
90
+ @x -= @level.movement
88
91
  end
89
92
  def draw
90
93
  @hoop.draw @x - 66, @y - 98, 1000 - @x
91
94
  end
92
95
  end
93
96
 
94
- class RubyhopGame < Gosu::Window
95
- VERSION = "1.1.0"
96
- def initialize width=800, height=600, fullscreen=false
97
- super
98
- self.caption = "Ruby Hop"
99
- @music = Gosu::Song.new self, get_my_file("music.mp3")
97
+ class HopLevel
98
+ attr_accessor :window, :movement, :score
99
+ def initialize window
100
+ @window = window
101
+ @window.caption = "Ruby Hop"
102
+ @music = Gosu::Song.new @window, get_my_file("music.mp3")
100
103
  @music.play true
101
- @background = Gosu::Image.new self, get_my_file("background.png")
104
+ @background = Gosu::Image.new @window, get_my_file("background.png")
102
105
  @player = Player.new self
103
106
  @hoops = 6.times.map { Hoop.new self }
104
107
  init_hoops!
108
+ @font = Gosu::Font.new @window, Gosu::default_font_name, 20
109
+ @movement = 2
110
+
111
+ # Add callback holders
112
+ @fail_callbacks = []
113
+ @quit_callbacks = []
114
+ end
115
+
116
+ def on_fail &block
117
+ @fail_callbacks << block
118
+ end
119
+
120
+ def on_quit &block
121
+ @quit_callbacks << block
122
+ end
123
+
124
+ def start!
105
125
  @score = 0
126
+ @movement = 2
127
+ @player.start!
128
+ init_hoops!
129
+ end
130
+
131
+ def fail!
132
+ @fail_callbacks.each { |c| c.call }
133
+ end
134
+
135
+ def quit!
136
+ @quit_callbacks.each { |c| c.call }
106
137
  end
107
138
 
108
139
  def init_hoops!
@@ -127,11 +158,12 @@ class RubyhopGame < Gosu::Window
127
158
  end
128
159
 
129
160
  def button_down id
130
- close if id == Gosu::KbEscape
161
+ quit! if id == Gosu::KbEscape
131
162
  @player.hop if id == Gosu::KbSpace
132
163
  end
133
164
 
134
165
  def update
166
+ @movement += 0.003
135
167
  @player.update
136
168
  @hoops.each do |hoop|
137
169
  hoop.update
@@ -141,7 +173,6 @@ class RubyhopGame < Gosu::Window
141
173
  if hoop.active && @player.alive && hoop.x < @player.x
142
174
  @score += 1
143
175
  hoop.active = false
144
- self.caption = "Ruby Hop: #{@score}"
145
176
  end
146
177
  end
147
178
  end
@@ -150,5 +181,126 @@ class RubyhopGame < Gosu::Window
150
181
  @background.draw 0, 0, 0
151
182
  @player.draw
152
183
  @hoops.each &:draw
184
+ @font.draw "Score: #{@score}", 700, 10, 1, 1.0, 1.0, Gosu::Color::RED
185
+ end
186
+ end
187
+
188
+ class FailLevel
189
+ attr_accessor :window
190
+ def initialize window
191
+ @window = window
192
+ @background = Gosu::Image.new @window, get_my_file("background.png")
193
+ @rubyguy = Gosu::Image.new @window, get_my_file("rubyguy.png")
194
+
195
+ create_image!
196
+
197
+ # Add callback holders
198
+ @continue_callbacks = []
199
+ @quit_callbacks = []
200
+ end
201
+
202
+ def create_image!
203
+ @msg = Gosu::Image.from_text @window,
204
+ "You scored #{@window.score}.\n" +
205
+ "Your high score is #{@window.high_score}.\n" +
206
+ "Press SPACE if you dare to continue...\n" +
207
+ "Or ESCAPE if it is just too much for you.",
208
+ Gosu::default_font_name, 24
209
+ @msg_x = @window.width/2 - @msg.width/2
210
+ @msg_y = @window.height * 2 / 3
211
+ end
212
+
213
+ def on_continue &block
214
+ @continue_callbacks << block
215
+ end
216
+
217
+ def on_quit &block
218
+ @quit_callbacks << block
219
+ end
220
+
221
+ def continue!
222
+ @continue_callbacks.each { |c| c.call }
223
+ end
224
+
225
+ def quit!
226
+ @quit_callbacks.each { |c| c.call }
227
+ end
228
+
229
+ def start!
230
+ create_image!
231
+ end
232
+
233
+ def update
234
+ quit! if @window.button_down? Gosu::KbEscape
235
+ continue! if ( @window.button_down?(Gosu::KbSpace) ||
236
+ @window.button_down?(Gosu::KbReturn) ||
237
+ @window.button_down?(Gosu::KbEnter) )
238
+ end
239
+
240
+ def draw
241
+ @background.draw 0, 0, 0
242
+ c = Math.cos(@window.time*4)
243
+ @rubyguy.draw_rot(((@window.width)/2), ((@window.height)/2 - 80), 1, 0,
244
+ 0.5, 0.5, 1.0+c*0.1, 1.0+c*0.1)
245
+ s = Math.sin @window.time
246
+ @msg.draw_rot( ((@window.width)/2 + (100*(s)).to_i),
247
+ ((@window.height)/2 + 160 + s*s*s.abs*50),
248
+ 1, s*5, 0.5, 0.5,
249
+ 1.0+(0.1*s*s*s.abs), 1.0+(0.1*s*s*s.abs),
250
+ Gosu::Color::RED )
251
+ end
252
+ end
253
+
254
+ class RubyhopGame < Gosu::Window
255
+ VERSION = "1.2.0"
256
+ attr_reader :time, :sounds, :score, :high_score
257
+ def initialize width=800, height=600, fullscreen=false
258
+ super
259
+
260
+ self.caption = 'Ruby Hop'
261
+
262
+ # Scores
263
+ @score = @high_score = 0
264
+
265
+ # Levels
266
+ @hop = HopLevel.new self
267
+ @fail = FailLevel.new self
268
+
269
+ @hop.on_fail { fail! }
270
+ @hop.on_quit { close }
271
+
272
+ @fail.on_continue { play! }
273
+ @fail.on_quit { close }
274
+
275
+ play!
276
+ end
277
+
278
+ def play!
279
+ @level = @hop
280
+ @level.start!
281
+ end
282
+
283
+ def fail!
284
+ @score = @hop.score
285
+ @high_score = @score if @score > @high_score
286
+ @level = @fail
287
+ @level.start!
288
+ end
289
+
290
+ def button_down id
291
+ @level.button_down id if @level.respond_to? :button_down
292
+ end
293
+
294
+ def button_up id
295
+ @level.button_up id if @level.respond_to? :button_up
296
+ end
297
+
298
+ def update
299
+ @time = Time.now.to_f
300
+ @level.update
301
+ end
302
+
303
+ def draw
304
+ @level.draw
153
305
  end
154
306
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyhop
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -68,6 +68,7 @@ files:
68
68
  - lib/rubyguy-dead.png
69
69
  - lib/rubyguy-fall.png
70
70
  - lib/rubyguy-rise.png
71
+ - lib/rubyguy.png
71
72
  - lib/rubyhop.rb
72
73
  - test/test_rubyhop.rb
73
74
  homepage: http://blowmage.com/rubyhop