gosu-examples 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8eb4367ee4c008b79167bda5845113c7513eedd4
4
- data.tar.gz: 9b6fe668a5ed7f24da588abdb4f6f9dcbe600f81
3
+ metadata.gz: 888d1aceda91b77fb517e8b8bcf29c0465b0db10
4
+ data.tar.gz: b078a35fb710a436a9c1a0f1df81a01e3483a28e
5
5
  SHA512:
6
- metadata.gz: cacab6fb2e19440bb1b1b84043f4a3936c5b84c48734809a3e8452252ac5509cd9b743d117afc81fa2d7a300c5593a35e32dfb13a0d030ffeb8bef0efe5367eb
7
- data.tar.gz: aed9c85c7dc6c65a7bf585867f77344a6617273310bcba784f1a15ca0c932ab8eb9984fe3900c3bbf1d693371e10b8fb58e13a7b7325f23d52c14a5ca363869f
6
+ metadata.gz: 90eddc66bbd574cf91b55c8c61c7796ad4146612d14c5f72c092643ae03bcacfe0dbda96e6296c9c75051ba99c1880d088d8dd049f7d06bfde4ea5da71c7f658
7
+ data.tar.gz: 9ca2489faf897835cd19a248c23f500d250ecf95a385873b8ee728ccdef028a47fba38b172870837a0bb94cab5ea924d0bc0e51a9fb77ca6134d907bdc7d24f7
data/README.md CHANGED
@@ -1,14 +1,30 @@
1
1
  Gosu Examples
2
2
  =============
3
3
 
4
- This is a collection of Ruby example games for the [Gosu media library](https://libgosu.org/).
4
+ <img src="screenshot.png" width="526">
5
5
 
6
- To run the examples, simply install the `gosu-examples` Ruby gem and then run `gosu-examples` in the terminal.
6
+ This is a collection of Ruby example games for the [Gosu library](https://www.libgosu.org/).
7
7
 
8
- Some examples require the following libraries to be installed:
8
+ (You can find C++ examples in the [main gosu repository](https://github.com/gosu/gosu/tree/master/examples).)
9
9
 
10
- `gem install chipmunk`
11
- `gem install rmagick`
12
- `gem install opengl`
10
+ To install and run the `gosu-examples` gem:
13
11
 
14
- (This is a very rough first version of this gem. Feel free to contribute to all parts of this repository via Pull Requests - media files, more examples, cleaning up code... - as long as we can use your contributions under the MIT license.)
12
+ ```bash
13
+ gem install gosu-examples
14
+ gosu-examples
15
+ ```
16
+
17
+ You can also [download and unpack this repository](https://github.com/gosu/gosu-examples/archive/master.zip) and then run individual examples from the terminal:
18
+
19
+ ```bash
20
+ cd gosu-examples-master/examples
21
+ ruby tutorial.rb
22
+ ```
23
+
24
+ Some examples require the following additional libraries:
25
+
26
+ ```bash
27
+ gem install chipmunk
28
+ gem install rmagick
29
+ gem install opengl
30
+ ```
@@ -10,25 +10,20 @@ $LOAD_PATH << "#{File.dirname __FILE__}/../lib/gosu-examples"
10
10
  require 'example'
11
11
  require 'sidebar'
12
12
 
13
- Example.load_examples "{.,../features}/*.rb" # TODO - should be *.rb
13
+ Example.load_examples "*.rb"
14
14
 
15
15
  class ExampleBox < Gosu::Window
16
- # TODO - the ExampleWindow should resize to fit once Gosu::Window#resize has been added.
17
- # See https://github.com/jlnr/gosu/issues/255
18
- EXAMPLE_WIDTH = 600
19
- EXAMPLE_HEIGHT = 600
20
-
21
16
  def initialize
22
- super Sidebar::WIDTH + EXAMPLE_WIDTH, EXAMPLE_HEIGHT, :fullscreen => ARGV.include?('--fullscreen')
17
+ super Sidebar::WIDTH, Sidebar::HEIGHT, :fullscreen => ARGV.include?('--fullscreen')
23
18
 
24
19
  @sidebar = Sidebar.new { |example| change_example(example) }
25
20
 
26
21
  welcome_class = Example.examples.find { |example| example.name =~ /::Welcome$/ }
27
- @current_example = welcome_class.new
22
+ change_example(welcome_class)
28
23
  end
29
24
 
30
25
  def update
31
- self.caption = "Gosu Example Box - #{@current_example.caption} (#{Gosu::fps} FPS)"
26
+ self.caption = "Gosu Example Box - #{@current_example.caption} (#{Gosu.fps} FPS)"
32
27
 
33
28
  @current_example.update
34
29
  end
@@ -36,9 +31,9 @@ class ExampleBox < Gosu::Window
36
31
  def draw
37
32
  @current_example.draw
38
33
 
39
- flush
34
+ Gosu.flush
40
35
 
41
- translate(EXAMPLE_WIDTH, 0) do
36
+ Gosu.translate(@current_example.width, 0) do
42
37
  current_filename = @current_example.class.source_file
43
38
  @sidebar.draw(current_filename)
44
39
  end
@@ -46,23 +41,26 @@ class ExampleBox < Gosu::Window
46
41
 
47
42
  def button_down(id)
48
43
  case id
49
- when Gosu::KbEscape
44
+ when Gosu::KB_ESCAPE
50
45
  close
51
- when char_to_button_id('S')
52
- if filename = @current_example.class.source_file then
46
+ when Gosu.char_to_button_id('E')
47
+ if filename = @current_example.class.source_file
53
48
  open_file_or_folder filename
54
49
  end
55
- when char_to_button_id('O')
56
- if filename = @current_example.class.source_file then
50
+ when Gosu.char_to_button_id('O')
51
+ if filename = @current_example.class.source_file
57
52
  open_file_or_folder File.dirname(filename)
58
53
  end
59
54
  else
60
- if id == Gosu::MsLeft and mouse_x >= EXAMPLE_WIDTH then
61
- @sidebar.click(mouse_x - EXAMPLE_WIDTH, mouse_y)
55
+ if id == Gosu::MS_LEFT and mouse_x >= @current_example.width
56
+ @sidebar.click(mouse_x - @current_example.width, mouse_y)
62
57
  else
63
58
  @current_example.button_down(id)
64
59
  end
65
60
  end
61
+
62
+ # Call super to enable alt+enter/cmd+F for toggling fullscreen mode.
63
+ super
66
64
  end
67
65
 
68
66
  def button_up(id)
@@ -76,18 +74,20 @@ class ExampleBox < Gosu::Window
76
74
  private
77
75
 
78
76
  def change_example(example)
79
- if @current_example.class != example then
77
+ if @current_example.class != example
80
78
  @current_example = nil
81
79
  GC.start
82
80
  @current_example = example.new
81
+ self.width = @current_example.width + Sidebar::WIDTH
82
+ self.height = @current_example.height
83
83
  end
84
84
  end
85
85
 
86
86
  def open_file_or_folder(filename)
87
- if RUBY_PLATFORM =~ /darwin[0-9]*$/ then
87
+ if RUBY_PLATFORM =~ /darwin[0-9]*$/
88
88
  `open '#{filename}'`
89
- elsif RUBY_PLATFORM =~ /mingw[0-9]*$/ then
90
- `explorer '#{filename}'`
89
+ elsif RUBY_PLATFORM =~ /mingw[0-9]*$/
90
+ `explorer "#{filename.gsub('/', '\\')}"`
91
91
  else
92
92
  `xdg-open '#{filename}'`
93
93
  end
@@ -13,11 +13,11 @@ require 'rmagick'
13
13
 
14
14
  # Layering of sprites
15
15
  module ZOrder
16
- Background, Box = *0..1
16
+ BACKGROUND, BOX = *0..1
17
17
  end
18
18
 
19
- WIDTH = 600
20
- HEIGHT = 600
19
+ WIDTH = 640
20
+ HEIGHT = 480
21
21
  TICK = 1.0/60.0
22
22
  NUM_POLYGONS = 80
23
23
  NUM_SIDES = 4
@@ -63,7 +63,7 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
63
63
  8.times do |j|
64
64
  stagger = (j % 2) * 40
65
65
  x = i * 80 + stagger
66
- y = j * 70 + 80
66
+ y = j * 70 + 80
67
67
  shape = CP::Shape::Poly.new(body, shape_vertices, CP::Vec2.new(x, y))
68
68
  shape.e = 1
69
69
  shape.u = 1
@@ -123,7 +123,7 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
123
123
 
124
124
  # All the updating of the screen is done here.
125
125
  def draw
126
- @background_image.draw(0, 0, ZOrder::Background)
126
+ @background_image.draw(0, 0, ZOrder::BACKGROUND)
127
127
  @boxes.each { |box| box.draw }
128
128
  end
129
129
  end
@@ -145,7 +145,7 @@ class Box
145
145
  end
146
146
 
147
147
  def draw
148
- @image.draw_rot(@body.p.x, @body.p.y, ZOrder::Box, @body.a.radians_to_gosu)
148
+ @image.draw_rot(@body.p.x, @body.p.y, ZOrder::BOX, @body.a.radians_to_gosu)
149
149
  end
150
150
  end
151
151
 
@@ -12,8 +12,8 @@ require 'rubygems'
12
12
  require 'gosu'
13
13
  require 'chipmunk'
14
14
 
15
- WIDTH = 600
16
- HEIGHT = 600
15
+ WIDTH = 640
16
+ HEIGHT = 480
17
17
 
18
18
  # The number of steps to process every Gosu update
19
19
  # The Player ship can get going so fast as to "move through" a
@@ -68,19 +68,19 @@ class Player
68
68
  # forward momentum by creating a vector in the direction of the facing
69
69
  # and with a magnitude representing the force we want to apply
70
70
  def accelerate
71
- @shape.body.apply_force((radians_to_vec2(@shape.body.a) * (3000.0/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
71
+ @shape.body.apply_force(@shape.body.rot * (3000.0/SUBSTEPS), CP::Vec2.new(0.0, 0.0))
72
72
  end
73
73
 
74
74
  # Apply even more forward force
75
75
  # See accelerate for more details
76
76
  def boost
77
- @shape.body.apply_force((radians_to_vec2(@shape.body.a) * (3000.0)), CP::Vec2.new(0.0, 0.0))
77
+ @shape.body.apply_force(@shape.body.rot * (3000.0), CP::Vec2.new(0.0, 0.0))
78
78
  end
79
79
 
80
80
  # Apply reverse force
81
81
  # See accelerate for more details
82
82
  def reverse
83
- @shape.body.apply_force(-(radians_to_vec2(@shape.body.a) * (1000.0/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
83
+ @shape.body.apply_force(-@shape.body.rot * (1000.0/SUBSTEPS), CP::Vec2.new(0.0, 0.0))
84
84
  end
85
85
 
86
86
  # Wrap to the other side of the screen when we fly off the edge
@@ -92,13 +92,6 @@ class Player
92
92
  def draw
93
93
  @image.draw_rot(@shape.body.p.x, @shape.body.p.y, ZOrder::Player, @shape.body.a.radians_to_gosu)
94
94
  end
95
-
96
- private
97
-
98
- # Convenience method for converting from radians to a Vec2 vector.
99
- def radians_to_vec2(radians)
100
- CP::Vec2.new(Math::cos(radians), Math::sin(radians))
101
- end
102
95
  end
103
96
 
104
97
  # See how simple our Star is?
@@ -115,11 +108,11 @@ class Star
115
108
  @shape = shape
116
109
  @shape.body.p = CP::Vec2.new(rand * WIDTH, rand * HEIGHT) # position
117
110
  @shape.body.v = CP::Vec2.new(0.0, 0.0) # velocity
118
- @shape.body.a = (3*Math::PI/2.0) # angle in radians; faces towards top of screen
111
+ @shape.body.a = 0.gosu_to_radians # faces towards top of screen
119
112
  end
120
113
 
121
114
  def draw
122
- img = @animation[Gosu::milliseconds / 100 % @animation.size];
115
+ img = @animation[Gosu.milliseconds / 100 % @animation.size];
123
116
  img.draw(@shape.body.p.x - img.width / 2.0, @shape.body.p.y - img.height / 2.0, ZOrder::Stars, 1, 1, @color, :add)
124
117
  end
125
118
  end
@@ -226,20 +219,20 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
226
219
  @player.validate_position
227
220
 
228
221
  # Check keyboard
229
- if Gosu::button_down? Gosu::KbLeft
222
+ if Gosu.button_down? Gosu::KB_LEFT
230
223
  @player.turn_left
231
224
  end
232
- if Gosu::button_down? Gosu::KbRight
225
+ if Gosu.button_down? Gosu::KB_RIGHT
233
226
  @player.turn_right
234
227
  end
235
228
 
236
- if Gosu::button_down? Gosu::KbUp
237
- if Gosu::button_down?(Gosu::KbRightShift) or Gosu::button_down?(Gosu::KbLeftShift)
229
+ if Gosu.button_down? Gosu::KB_UP
230
+ if Gosu.button_down?(Gosu::KB_RIGHT_SHIFT) or Gosu.button_down?(Gosu::KB_LEFT_SHIFT)
238
231
  @player.boost
239
232
  else
240
233
  @player.accelerate
241
234
  end
242
- elsif Gosu::button_down? Gosu::KbDown
235
+ elsif Gosu.button_down? Gosu::KB_DOWN
243
236
  @player.reverse
244
237
  end
245
238
 
@@ -249,7 +242,7 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
249
242
  end
250
243
 
251
244
  # Each update (not SUBSTEP) we see if we need to add more Stars
252
- if rand(100) < 4 and @stars.size < 25 then
245
+ if rand(100) < 4 and @stars.size < 25
253
246
  body = CP::Body.new(0.0001, 0.0001)
254
247
  shape = CP::Shape::Circle.new(body, 25/2, CP::Vec2.new(0.0, 0.0))
255
248
  shape.collision_type = :star
@@ -269,8 +262,10 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
269
262
  end
270
263
 
271
264
  def button_down(id)
272
- if id == Gosu::KbEscape
265
+ if id == Gosu::KB_ESCAPE
273
266
  close
267
+ else
268
+ super
274
269
  end
275
270
  end
276
271
  end
@@ -30,7 +30,7 @@
30
30
  require 'rubygems'
31
31
  require 'gosu'
32
32
 
33
- WIDTH, HEIGHT = 600, 600
33
+ WIDTH, HEIGHT = 640, 480
34
34
 
35
35
  module Tiles
36
36
  Grass = 0
@@ -47,7 +47,7 @@ class CollectibleGem
47
47
 
48
48
  def draw
49
49
  # Draw, slowly rotating
50
- @image.draw_rot(@x, @y, 0, 25 * Math.sin(Gosu::milliseconds / 133.7))
50
+ @image.draw_rot(@x, @y, 0, 25 * Math.sin(Gosu.milliseconds / 133.7))
51
51
  end
52
52
  end
53
53
 
@@ -69,7 +69,7 @@ class Player
69
69
 
70
70
  def draw
71
71
  # Flip vertically when facing to the left.
72
- if @dir == :left then
72
+ if @dir == :left
73
73
  offs_x = -25
74
74
  factor = 1.0
75
75
  else
@@ -91,18 +91,18 @@ class Player
91
91
  if (move_x == 0)
92
92
  @cur_image = @standing
93
93
  else
94
- @cur_image = (Gosu::milliseconds / 175 % 2 == 0) ? @walk1 : @walk2
94
+ @cur_image = (Gosu.milliseconds / 175 % 2 == 0) ? @walk1 : @walk2
95
95
  end
96
96
  if (@vy < 0)
97
97
  @cur_image = @jump
98
98
  end
99
99
 
100
100
  # Directional walking, horizontal movement
101
- if move_x > 0 then
101
+ if move_x > 0
102
102
  @dir = :right
103
103
  move_x.times { if would_fit(1, 0) then @x += 1 end }
104
104
  end
105
- if move_x < 0 then
105
+ if move_x < 0
106
106
  @dir = :left
107
107
  (-move_x).times { if would_fit(-1, 0) then @x -= 1 end }
108
108
  end
@@ -112,16 +112,16 @@ class Player
112
112
  # jumping curve will be the parabole we want it to be.
113
113
  @vy += 1
114
114
  # Vertical movement
115
- if @vy > 0 then
115
+ if @vy > 0
116
116
  @vy.times { if would_fit(0, 1) then @y += 1 else @vy = 0 end }
117
117
  end
118
- if @vy < 0 then
118
+ if @vy < 0
119
119
  (-@vy).times { if would_fit(0, -1) then @y -= 1 else @vy = 0 end }
120
120
  end
121
121
  end
122
122
 
123
123
  def try_to_jump
124
- if @map.solid?(@x, @y + 1) then
124
+ if @map.solid?(@x, @y + 1)
125
125
  @vy = -20
126
126
  end
127
127
  end
@@ -202,8 +202,8 @@ class CptnRuby < (Example rescue Gosu::Window)
202
202
 
203
203
  def update
204
204
  move_x = 0
205
- move_x -= 5 if Gosu::button_down? Gosu::KbLeft
206
- move_x += 5 if Gosu::button_down? Gosu::KbRight
205
+ move_x -= 5 if Gosu.button_down? Gosu::KB_LEFT
206
+ move_x += 5 if Gosu.button_down? Gosu::KB_RIGHT
207
207
  @cptn.update(move_x)
208
208
  @cptn.collect_gems(@map.gems)
209
209
  # Scrolling follows player
@@ -213,15 +213,20 @@ class CptnRuby < (Example rescue Gosu::Window)
213
213
 
214
214
  def draw
215
215
  @sky.draw 0, 0, 0
216
- Gosu::translate(-@camera_x, -@camera_y) do
216
+ Gosu.translate(-@camera_x, -@camera_y) do
217
217
  @map.draw
218
218
  @cptn.draw
219
219
  end
220
220
  end
221
221
 
222
222
  def button_down(id)
223
- if id == Gosu::KbUp then
223
+ case id
224
+ when Gosu::KB_UP
224
225
  @cptn.try_to_jump
226
+ when Gosu::KB_ESCAPE
227
+ close
228
+ else
229
+ super
225
230
  end
226
231
  end
227
232
  end
@@ -9,7 +9,7 @@ require 'rubygems'
9
9
  require 'gosu'
10
10
  require 'gl'
11
11
 
12
- WIDTH, HEIGHT = 600, 600
12
+ WIDTH, HEIGHT = 640, 480
13
13
 
14
14
  module ZOrder
15
15
  Background, Stars, Player, UI = *0..3
@@ -32,7 +32,7 @@ class GLBackground
32
32
 
33
33
  def scroll
34
34
  @scrolls += 1
35
- if @scrolls == SCROLLS_PER_STEP then
35
+ if @scrolls == SCROLLS_PER_STEP
36
36
  @scrolls = 0
37
37
  @height_map.shift
38
38
  @height_map.push Array.new(POINTS_X) { rand }
@@ -42,7 +42,7 @@ class GLBackground
42
42
  def draw(z)
43
43
  # gl will execute the given block in a clean OpenGL environment, then reset
44
44
  # everything so Gosu's rendering can take place again.
45
- Gosu::gl(z) { exec_gl }
45
+ Gosu.gl(z) { exec_gl }
46
46
  end
47
47
 
48
48
  private
@@ -143,7 +143,7 @@ class Player
143
143
 
144
144
  def collect_stars(stars)
145
145
  stars.reject! do |star|
146
- if Gosu::distance(@x, @y, star.x, star.y) < 35 then
146
+ if Gosu.distance(@x, @y, star.x, star.y) < 35
147
147
  @score += 10
148
148
  @beep.play
149
149
  true
@@ -170,7 +170,7 @@ class Star
170
170
  end
171
171
 
172
172
  def draw
173
- img = @animation[Gosu::milliseconds / 100 % @animation.size];
173
+ img = @animation[Gosu.milliseconds / 100 % @animation.size];
174
174
  img.draw_rot(@x, @y, ZOrder::Stars, @y, 0.5, 0.5, 1, 1, @color, :add)
175
175
  end
176
176
 
@@ -199,10 +199,10 @@ class OpenGLIntegration < (Example rescue Gosu::Window)
199
199
  end
200
200
 
201
201
  def update
202
- @player.move_left if Gosu::button_down? Gosu::KbLeft or Gosu::button_down? Gosu::GpLeft
203
- @player.move_right if Gosu::button_down? Gosu::KbRight or Gosu::button_down? Gosu::GpRight
204
- @player.accelerate if Gosu::button_down? Gosu::KbUp or Gosu::button_down? Gosu::GpUp
205
- @player.brake if Gosu::button_down? Gosu::KbDown or Gosu::button_down? Gosu::GpDown
202
+ @player.move_left if Gosu.button_down? Gosu::KB_LEFT or Gosu.button_down? Gosu::GP_LEFT
203
+ @player.move_right if Gosu.button_down? Gosu::KB_RIGHT or Gosu.button_down? Gosu::GP_RIGHT
204
+ @player.accelerate if Gosu.button_down? Gosu::KB_UP or Gosu.button_down? Gosu::GP_UP
205
+ @player.brake if Gosu.button_down? Gosu::KB_DOWN or Gosu.button_down? Gosu::GP_DOWN
206
206
 
207
207
  @player.collect_stars(@stars)
208
208
 
@@ -17,7 +17,7 @@ require 'rubygems'
17
17
  require 'gosu'
18
18
  require 'rmagick'
19
19
 
20
- WIDTH, HEIGHT = 600, 600
20
+ WIDTH, HEIGHT = 640, 480
21
21
 
22
22
  NULL_PIXEL = Magick::Pixel.from_color('none')
23
23
 
@@ -156,7 +156,7 @@ class Player
156
156
  end
157
157
 
158
158
  def draw
159
- if dead then
159
+ if dead
160
160
  # Poor, broken soldier.
161
161
  @@images[0].draw_rot(x, y, 0, 290 * @dir, 0.5, 0.65, @dir * 0.5, 0.5, @color)
162
162
  @@images[2].draw_rot(x, y, 0, 160 * @dir, 0.95, 0.5, 0.5, @dir * 0.5, @color)
@@ -164,7 +164,7 @@ class Player
164
164
  # Was moved last frame?
165
165
  if @show_walk_anim
166
166
  # Yes: Display walking animation.
167
- frame = Gosu::milliseconds / 200 % 2
167
+ frame = Gosu.milliseconds / 200 % 2
168
168
  else
169
169
  # No: Stand around (boring).
170
170
  frame = 0
@@ -185,7 +185,7 @@ class Player
185
185
  # Gravity.
186
186
  @vy += 1
187
187
 
188
- if @vy > 1 then
188
+ if @vy > 1
189
189
  # Move upwards until hitting something.
190
190
  @vy.times do
191
191
  if @window.map.solid?(x, y + 1)
@@ -240,7 +240,7 @@ class Player
240
240
  end
241
241
 
242
242
  def hit_by? missile
243
- if Gosu::distance(missile.x, missile.y, x, y) < 30 then
243
+ if Gosu.distance(missile.x, missile.y, x, y) < 30
244
244
  # Was hit :(
245
245
  @dead = true
246
246
  return true
@@ -260,7 +260,7 @@ class Missile
260
260
 
261
261
  def initialize(window, x, y, angle)
262
262
  # Horizontal/vertical velocity.
263
- @vx, @vy = Gosu::offset_x(angle, 20).to_i, Gosu::offset_y(angle, 20).to_i
263
+ @vx, @vy = Gosu.offset_x(angle, 20).to_i, Gosu.offset_y(angle, 20).to_i
264
264
 
265
265
  @window, @x, @y = window, x + @vx, y + @vy
266
266
  end
@@ -271,7 +271,7 @@ class Missile
271
271
  @y += @vy
272
272
  @vy += 1
273
273
  # Hit anything?
274
- if @window.map.solid?(x, y) or @window.objects.any? { |o| o.hit_by?(self) } then
274
+ if @window.map.solid?(x, y) or @window.objects.any? { |o| o.hit_by?(self) }
275
275
  # Create great particles.
276
276
  5.times { @window.objects << Particle.new(@window, x - 25 + rand(51), y - 25 + rand(51)) }
277
277
  @window.map.blast(x, y)
@@ -285,7 +285,7 @@ class Missile
285
285
 
286
286
  def draw
287
287
  # Just draw a small rectangle.
288
- Gosu::draw_rect x-2, y-2, 4, 4, 0xff_800000
288
+ Gosu.draw_rect x-2, y-2, 4, 4, 0xff_800000
289
289
  end
290
290
 
291
291
  def hit_by?(missile)
@@ -370,7 +370,7 @@ class RMagickIntegration < (Example rescue Gosu::Window)
370
370
  cur_text = @player_instructions[@current_player] if not @waiting
371
371
  cur_text = @player_won_messages[1 - @current_player] if @players[@current_player].dead
372
372
 
373
- if cur_text then
373
+ if cur_text
374
374
  x, y = 0, 30
375
375
  cur_text.draw(x - 1, y, 0, 1, 1, 0xff_000000)
376
376
  cur_text.draw(x + 1, y, 0, 1, 1, 0xff_000000)
@@ -389,22 +389,24 @@ class RMagickIntegration < (Example rescue Gosu::Window)
389
389
  @objects.reject! { |o| o.update == false }
390
390
 
391
391
  # If it's a player's turn, forward controls.
392
- if not @waiting and not @players[@current_player].dead then
392
+ if not @waiting and not @players[@current_player].dead
393
393
  player = @players[@current_player]
394
- player.aim_up if Gosu::button_down? Gosu::KbUp
395
- player.aim_down if Gosu::button_down? Gosu::KbDown
396
- player.try_walk(-1) if Gosu::button_down? Gosu::KbLeft
397
- player.try_walk(+1) if Gosu::button_down? Gosu::KbRight
398
- player.try_jump if Gosu::button_down? Gosu::KbReturn
394
+ player.aim_up if Gosu.button_down? Gosu::KB_UP
395
+ player.aim_down if Gosu.button_down? Gosu::KB_DOWN
396
+ player.try_walk(-1) if Gosu.button_down? Gosu::KB_LEFT
397
+ player.try_walk(+1) if Gosu.button_down? Gosu::KB_RIGHT
398
+ player.try_jump if Gosu.button_down? Gosu::KB_RETURN
399
399
  end
400
400
  end
401
401
 
402
402
  def button_down(id)
403
- if id == Gosu::KbSpace and not @waiting and not @players[@current_player].dead then
403
+ if id == Gosu::KB_SPACE and not @waiting and not @players[@current_player].dead
404
404
  # Shoot! This is handled in button_down because holding space shouldn't auto-fire.
405
405
  @players[@current_player].shoot
406
406
  @current_player = 1 - @current_player
407
407
  @waiting = true
408
+ else
409
+ super
408
410
  end
409
411
  end
410
412
  end
@@ -3,10 +3,8 @@
3
3
  require 'rubygems'
4
4
  require 'gosu'
5
5
 
6
- WIDTH, HEIGHT = 600, 600
7
-
8
6
  module ZOrder
9
- Background, Stars, Player, UI = *0..3
7
+ BACKGROUND, STARS, PLAYER, UI = *0..3
10
8
  end
11
9
 
12
10
  class Player
@@ -32,27 +30,27 @@ class Player
32
30
  end
33
31
 
34
32
  def accelerate
35
- @vel_x += Gosu::offset_x(@angle, 0.5)
36
- @vel_y += Gosu::offset_y(@angle, 0.5)
33
+ @vel_x += Gosu.offset_x(@angle, 0.5)
34
+ @vel_y += Gosu.offset_y(@angle, 0.5)
37
35
  end
38
36
 
39
37
  def move
40
38
  @x += @vel_x
41
39
  @y += @vel_y
42
- @x %= WIDTH
43
- @y %= HEIGHT
40
+ @x %= 640
41
+ @y %= 480
44
42
 
45
43
  @vel_x *= 0.95
46
44
  @vel_y *= 0.95
47
45
  end
48
46
 
49
47
  def draw
50
- @image.draw_rot(@x, @y, ZOrder::Player, @angle)
48
+ @image.draw_rot(@x, @y, ZOrder::PLAYER, @angle)
51
49
  end
52
50
 
53
51
  def collect_stars(stars)
54
52
  stars.reject! do |star|
55
- if Gosu::distance(@x, @y, star.x, star.y) < 35 then
53
+ if Gosu.distance(@x, @y, star.x, star.y) < 35
56
54
  @score += 10
57
55
  @beep.play
58
56
  true
@@ -68,25 +66,24 @@ class Star
68
66
 
69
67
  def initialize(animation)
70
68
  @animation = animation
71
- @color = Gosu::Color.new(0xff_000000)
69
+ @color = Gosu::Color::BLACK.dup
72
70
  @color.red = rand(256 - 40) + 40
73
71
  @color.green = rand(256 - 40) + 40
74
72
  @color.blue = rand(256 - 40) + 40
75
- @x = rand * WIDTH
76
- @y = rand * HEIGHT
73
+ @x = rand * 640
74
+ @y = rand * 480
77
75
  end
78
76
 
79
77
  def draw
80
- img = @animation[Gosu::milliseconds / 100 % @animation.size]
78
+ img = @animation[Gosu.milliseconds / 100 % @animation.size]
81
79
  img.draw(@x - img.width / 2.0, @y - img.height / 2.0,
82
- ZOrder::Stars, 1, 1, @color, :add)
80
+ ZOrder::STARS, 1, 1, @color, :add)
83
81
  end
84
82
  end
85
83
 
86
84
  class Tutorial < (Example rescue Gosu::Window)
87
85
  def initialize
88
- super WIDTH, HEIGHT
89
-
86
+ super 640, 480
90
87
  self.caption = "Tutorial Game"
91
88
 
92
89
  @background_image = Gosu::Image.new("media/space.png", :tileable => true)
@@ -101,28 +98,36 @@ class Tutorial < (Example rescue Gosu::Window)
101
98
  end
102
99
 
103
100
  def update
104
- if Gosu::button_down? Gosu::KbLeft or Gosu::button_down? Gosu::GpLeft then
101
+ if Gosu.button_down? Gosu::KB_LEFT or Gosu.button_down? Gosu::GP_LEFT
105
102
  @player.turn_left
106
103
  end
107
- if Gosu::button_down? Gosu::KbRight or Gosu::button_down? Gosu::GpRight then
104
+ if Gosu.button_down? Gosu::KB_RIGHT or Gosu.button_down? Gosu::GP_RIGHT
108
105
  @player.turn_right
109
106
  end
110
- if Gosu::button_down? Gosu::KbUp or Gosu::button_down? Gosu::GpButton0 then
107
+ if Gosu.button_down? Gosu::KB_UP or Gosu.button_down? Gosu::GP_BUTTON_0
111
108
  @player.accelerate
112
109
  end
113
110
  @player.move
114
111
  @player.collect_stars(@stars)
115
112
 
116
- if rand(100) < 4 and @stars.size < 25 then
113
+ if rand(100) < 4 and @stars.size < 25
117
114
  @stars.push(Star.new(@star_anim))
118
115
  end
119
116
  end
120
117
 
121
118
  def draw
122
- @background_image.draw(0, 0, ZOrder::Background)
119
+ @background_image.draw(0, 0, ZOrder::BACKGROUND)
123
120
  @player.draw
124
121
  @stars.each { |star| star.draw }
125
- @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xff_ffff00)
122
+ @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, Gosu::Color::YELLOW)
123
+ end
124
+
125
+ def button_down(id)
126
+ if id == Gosu::KB_ESCAPE
127
+ close
128
+ else
129
+ super
130
+ end
126
131
  end
127
132
  end
128
133
 
@@ -3,7 +3,7 @@
3
3
  require 'rubygems'
4
4
  require 'gosu'
5
5
 
6
- WIDTH, HEIGHT = 600, 600
6
+ WIDTH, HEIGHT = 640, 480
7
7
 
8
8
  class Welcome < (Example rescue Gosu::Window)
9
9
  PADDING = 20
@@ -20,11 +20,12 @@ class Welcome < (Example rescue Gosu::Window)
20
20
 
21
21
  Every example can be run both from this tool <i>and</i> from the terminal/command line as a stand-alone Ruby script.
22
22
 
23
- Shortcuts:
23
+ Keyboard shortcuts:
24
24
 
25
- • To see the source code of an example or feature demo, press <b>S</b>.
25
+ • To see the source code of an example or feature demo, press <b>E</b>.
26
26
  • To open the ‘examples’ folder, press <b>O</b>.
27
- • To quit this tool, press <b>Esc</b>. This is useful if you are running in fullscreen mode. ($ gosu-examples --fullscreen)
27
+ • To quit this tool, press <b>Esc</b>.
28
+ • To toggle fullscreen mode, press <b>Alt+Enter</b> (Windows, Linux) or <b>cmd+F</b> (macOS).
28
29
 
29
30
  Why not take a look at the code for this example right now? Simply press <b>S</b>."
30
31
 
@@ -46,8 +47,8 @@ class Welcome < (Example rescue Gosu::Window)
46
47
  # Disregard the math in this method, it doesn't look as good as I thought it
47
48
  # would. =(
48
49
 
49
- angle = Gosu::milliseconds / 50.0
50
- scale = (Gosu::milliseconds % 1000) / 1000.0
50
+ angle = Gosu.milliseconds / 50.0
51
+ scale = (Gosu.milliseconds % 1000) / 1000.0
51
52
 
52
53
  [1, 0].each do |extra_scale|
53
54
  @background.draw_rot WIDTH * 0.5, HEIGHT * 0.75, 0, angle, 0.5, 0.5,
@@ -17,24 +17,11 @@ class Example
17
17
 
18
18
  def button_up(id)
19
19
  end
20
- end
21
-
22
- class Feature < Example
23
- def self.inherited(subclass)
24
- @@features ||= {}
25
- @@features[subclass] = self.current_source_file
26
- end
27
20
 
28
- def self.features
29
- @@features.keys
21
+ def close
22
+ # no-op, examples cannot close the containing window.
30
23
  end
31
-
32
- def self.source_file
33
- @@examples[self]
34
- end
35
- end
36
24
 
37
- class Example
38
25
  def self.current_source_file
39
26
  @current_source_file
40
27
  end
@@ -49,7 +36,7 @@ class Example
49
36
  end
50
37
 
51
38
  def self.examples
52
- @@examples.keys - [Feature]
39
+ @@examples.keys
53
40
  end
54
41
 
55
42
  def self.source_file
@@ -59,18 +46,14 @@ class Example
59
46
  def self.load_examples(pattern)
60
47
  Dir.glob(pattern) do |file|
61
48
  begin
62
- # Remember that all examples and features being loaded now must come from the
63
- # next file.
64
- #
49
+ # Remember which file we are loading.
65
50
  Example.current_source_file = File.expand_path(file)
66
51
 
67
- # Load the example/feature in a sandbox module (second parameter). This way,
68
- # several examples can define a Player class without colliding.
69
- #
70
- # load() does not let us refer to the anonymous module it creates, but we
71
- # can enumerate all loaded examples and features using Example.examples and
72
- # Feature.features afterwards.
52
+ # Load the example in a sandbox module (second parameter to load()). This way, examples can
53
+ # define classes and constants with the same names, and they will not collide.
73
54
  #
55
+ # load() does not let us refer to the anonymous module it creates, but we can enumerate all
56
+ # loaded examples using Example.examples thanks to the "inherited" callback above.
74
57
  load file, true
75
58
  rescue Exception => e
76
59
  puts "*** Cannot load #{file}:"
@@ -1,13 +1,13 @@
1
1
  class Sidebar
2
- WIDTH = 213
2
+ WIDTH = 300
3
3
  HEIGHT = 600
4
4
  FONT = Gosu::Font.new(20)
5
- HEADER = Gosu::Image.new("media/header.psd", :tileable => true)
5
+ HEADER = Gosu::Image.new("media/header@2x.psd", :tileable => true)
6
6
 
7
7
  class Button
8
8
  HEIGHT = 25
9
9
  SPACING = 5
10
- TOP_Y = HEADER.height + 15
10
+ TOP_Y = HEADER.height / 2 + 15
11
11
 
12
12
  attr_reader :filename
13
13
 
@@ -19,7 +19,7 @@ class Sidebar
19
19
  text_color = Gosu::Color::BLACK
20
20
 
21
21
  if is_current
22
- Gosu::draw_rect 0, @top, Sidebar::WIDTH, HEIGHT, 0xff_1565e5 if is_current
22
+ Gosu.draw_rect 0, @top, Sidebar::WIDTH, HEIGHT, 0xff_1565e5 if is_current
23
23
  text_color = Gosu::Color::WHITE
24
24
  end
25
25
 
@@ -44,8 +44,8 @@ class Sidebar
44
44
  end
45
45
 
46
46
  def draw(current_filename)
47
- Gosu::draw_rect 0, 0, WIDTH, HEIGHT, Gosu::Color::WHITE
48
- HEADER.draw 0, 0, 0
47
+ Gosu.draw_rect 0, 0, WIDTH, HEIGHT, Gosu::Color::WHITE
48
+ HEADER.draw 0, 0, 0, 0.5, 0.5
49
49
 
50
50
  @buttons.each do |button|
51
51
  is_current = (button.filename == current_filename)
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gosu-examples
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Raschke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-17 00:00:00.000000000 Z
11
+ date: 2017-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 0.9.0
19
+ version: 0.11.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 0.9.0
26
+ version: 0.11.0
27
27
  description: The `gosu-examples` tool provides an easy way to run and inspect example
28
28
  games written for the Gosu game development library.
29
29
  email: julian@raschke.de
@@ -32,18 +32,22 @@ executables:
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
- - bin/gosu-examples
36
35
  - LICENSE
37
36
  - README.md
38
- - lib/gosu-examples/example.rb
39
- - lib/gosu-examples/sidebar.rb
37
+ - bin/gosu-examples
40
38
  - examples/chipmunk_and_rmagick.rb
41
39
  - examples/chipmunk_integration.rb
42
40
  - examples/cptn_ruby.rb
43
- - examples/opengl_integration.rb
44
- - examples/rmagick_integration.rb
45
- - examples/tutorial.rb
46
- - examples/welcome.rb
41
+ - examples/media/BrokenPNG.png
42
+ - examples/media/Cursor.png
43
+ - examples/media/JingleBells.mp3
44
+ - examples/media/JingleBells.ogg
45
+ - examples/media/Loop.wav
46
+ - examples/media/Sample.wav
47
+ - examples/media/SquareTexture.png
48
+ - examples/media/Wallpaper.png
49
+ - examples/media/WallpaperXXL.png
50
+ - examples/media/WhiteAlpha.png
47
51
  - examples/media/audio_formats/aiff_32bit_float.aiff
48
52
  - examples/media/audio_formats/au_16bit_pcm.au
49
53
  - examples/media/audio_formats/caf_be_16bit_44khz.caf
@@ -58,33 +62,29 @@ files:
58
62
  - examples/media/audio_formats/wav_32bit_pcm.wav
59
63
  - examples/media/audio_formats/wav_4bit_ms_adpcm.wav
60
64
  - examples/media/beep.wav
61
- - examples/media/BrokenPNG.png
62
65
  - examples/media/cptn_ruby.png
63
66
  - examples/media/cptn_ruby_map.txt
64
- - examples/media/Cursor.png
65
67
  - examples/media/earth.png
66
68
  - examples/media/explosion.wav
67
69
  - examples/media/gem.png
68
- - examples/media/header.psd
70
+ - examples/media/header@2x.psd
69
71
  - examples/media/image_formats/test.jpg
70
72
  - examples/media/image_formats/test.psd
71
- - examples/media/JingleBells.mp3
72
- - examples/media/JingleBells.ogg
73
73
  - examples/media/landscape.svg
74
74
  - examples/media/large_star.png
75
- - examples/media/Loop.wav
76
- - examples/media/Sample.wav
77
75
  - examples/media/smoke.png
78
76
  - examples/media/soldier.png
79
77
  - examples/media/space.png
80
- - examples/media/SquareTexture.png
81
78
  - examples/media/star.png
82
79
  - examples/media/starfighter.bmp
83
80
  - examples/media/tileset.png
84
81
  - examples/media/vera.ttf
85
- - examples/media/Wallpaper.png
86
- - examples/media/WallpaperXXL.png
87
- - examples/media/WhiteAlpha.png
82
+ - examples/opengl_integration.rb
83
+ - examples/rmagick_integration.rb
84
+ - examples/tutorial.rb
85
+ - examples/welcome.rb
86
+ - lib/gosu-examples/example.rb
87
+ - lib/gosu-examples/sidebar.rb
88
88
  homepage: http://www.libgosu.org/
89
89
  licenses: []
90
90
  metadata: {}
@@ -94,19 +94,18 @@ require_paths:
94
94
  - lib
95
95
  required_ruby_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
- - - '>='
97
+ - - ">="
98
98
  - !ruby/object:Gem::Version
99
99
  version: 1.8.2
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - '>='
102
+ - - ">="
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
106
  rubyforge_project:
107
- rubygems_version: 2.0.14
107
+ rubygems_version: 2.6.8
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Ruby examples for the Gosu library
111
111
  test_files: []
112
- has_rdoc:
Binary file