gosu-examples 1.0.5 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/bin/gosu-examples +3 -5
- data/examples/chipmunk_and_rmagick.rb +17 -19
- data/examples/chipmunk_integration.rb +42 -44
- data/examples/cptn_ruby.rb +19 -21
- data/examples/opengl_integration.rb +59 -59
- data/examples/rmagick_integration.rb +74 -74
- data/examples/text_input.rb +26 -27
- data/examples/tutorial.rb +21 -23
- data/examples/welcome.rb +17 -19
- data/lib/gosu-examples/example.rb +18 -18
- data/lib/gosu-examples/sidebar.rb +14 -14
- metadata +9 -36
- data/examples/media/BrokenPNG.png +0 -0
- data/examples/media/Cursor.png +0 -0
- data/examples/media/JingleBells.mp3 +0 -0
- data/examples/media/JingleBells.ogg +0 -0
- data/examples/media/Loop.wav +0 -0
- data/examples/media/Sample.wav +0 -0
- data/examples/media/SquareTexture.png +0 -0
- data/examples/media/Wallpaper.png +0 -0
- data/examples/media/WallpaperXXL.png +0 -0
- data/examples/media/WhiteAlpha.png +0 -0
- data/examples/media/audio_formats/aiff_32bit_float.aiff +0 -0
- data/examples/media/audio_formats/au_16bit_pcm.au +0 -0
- data/examples/media/audio_formats/caf_be_16bit_44khz.caf +0 -0
- data/examples/media/audio_formats/caf_le_16bit_44khz.caf +0 -0
- data/examples/media/audio_formats/caf_le_8bit_44khz.caf +0 -0
- data/examples/media/audio_formats/general_midi.mid +0 -0
- data/examples/media/audio_formats/impulse_tracker.it +0 -0
- data/examples/media/audio_formats/mp3_128k_stereo.mp3 +0 -0
- data/examples/media/audio_formats/mp3_avg_96kbit_jointstereo.mp3 +0 -0
- data/examples/media/audio_formats/ogg_vorbis.ogg +0 -0
- data/examples/media/audio_formats/wav_16bit_pcm.wav +0 -0
- data/examples/media/audio_formats/wav_32bit_pcm.wav +0 -0
- data/examples/media/audio_formats/wav_4bit_ms_adpcm.wav +0 -0
- data/examples/media/image_formats/test.jpg +0 -0
- data/examples/media/image_formats/test.psd +0 -0
- data/examples/media/vera.ttf +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35dec2483af7a950e86155f3fb389c6e475ed0f00c0b15aa73cb58d14ec20003
|
4
|
+
data.tar.gz: 781bce8991c9191bae8eb976fb5bf3c072e5171b610d5b5a689b2dcf91beb99f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef736babe72e026806ce9a113b3d425f4bf637de7afcfed5ea3634f8abb46e8240288382ab0d031fcf4ea555f46e867d3d1336395476c50c69508f467a392be5
|
7
|
+
data.tar.gz: 83912efaf8d40af9f559f96d3b6e9481e970c1a16f9c4c4cf14d02f627bba285c066d26055ce0fa87a9b32f0ba7c1aa498b0b993ab539964c02dc7d9ccc4a773
|
data/README.md
CHANGED
data/bin/gosu-examples
CHANGED
@@ -4,10 +4,8 @@ require "gosu"
|
|
4
4
|
|
5
5
|
Dir.chdir "#{File.dirname __FILE__}/../examples"
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
require "example"
|
10
|
-
require "sidebar"
|
7
|
+
require_relative "../lib/gosu-examples/example"
|
8
|
+
require_relative "../lib/gosu-examples/sidebar"
|
11
9
|
|
12
10
|
Example.load_examples "*.rb"
|
13
11
|
|
@@ -89,7 +87,7 @@ class ExampleBox < Gosu::Window
|
|
89
87
|
elsif RUBY_PLATFORM =~ /mingw[0-9]*$/
|
90
88
|
`explorer "#{filename.gsub('/', '\\')}"`
|
91
89
|
else
|
92
|
-
|
90
|
+
fork { exec "xdg-open '#{filename}'" }
|
93
91
|
end
|
94
92
|
end
|
95
93
|
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# Encoding: UTF-8
|
2
|
-
|
3
1
|
# Based on the C Demo3 demonstration distributed with Chipmunk.
|
4
2
|
# Also with some help from the chipmunk_integration.rb program.
|
5
3
|
#
|
@@ -17,7 +15,7 @@ end
|
|
17
15
|
|
18
16
|
WIDTH = 640
|
19
17
|
HEIGHT = 480
|
20
|
-
TICK = 1.0/60.0
|
18
|
+
TICK = 1.0 / 60.0
|
21
19
|
NUM_POLYGONS = 80
|
22
20
|
NUM_SIDES = 4
|
23
21
|
EDGE_SIZE = 15
|
@@ -26,16 +24,16 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
26
24
|
def radians_to_vec2(radians)
|
27
25
|
CP::Vec2.new(Math::cos(radians), Math::sin(radians))
|
28
26
|
end
|
29
|
-
|
27
|
+
|
30
28
|
def initialize
|
31
29
|
super WIDTH, HEIGHT
|
32
|
-
|
30
|
+
|
33
31
|
self.caption = "Chipmunk, RMagick and Gosu"
|
34
|
-
|
32
|
+
|
35
33
|
@space = CP::Space.new
|
36
34
|
@space.iterations = 5
|
37
35
|
@space.gravity = CP::Vec2.new(0, 100)
|
38
|
-
|
36
|
+
|
39
37
|
# you can replace the background with any image with this line
|
40
38
|
# background = Magick::ImageList.new("media/space.png")
|
41
39
|
fill = Magick::TextureFill.new(Magick::ImageList.new("granite:"))
|
@@ -44,7 +42,7 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
44
42
|
@background_image = Gosu::Image.new(background, tileable: true) # turn the image into a Gosu one
|
45
43
|
@boxes = create_boxes(NUM_POLYGONS)
|
46
44
|
end
|
47
|
-
|
45
|
+
|
48
46
|
# Create all of the static triangles.
|
49
47
|
# Adds them to the space and the background image.
|
50
48
|
def setup_triangles(background)
|
@@ -56,7 +54,7 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
56
54
|
body = CP::Body.new(Float::MAX, Float::MAX)
|
57
55
|
base = 15
|
58
56
|
height = 10
|
59
|
-
shape_vertices =
|
57
|
+
shape_vertices = [CP::Vec2.new(-base, base), CP::Vec2.new(base, base), CP::Vec2.new(0, -height)]
|
60
58
|
# make shapes and images
|
61
59
|
8.times do |i|
|
62
60
|
8.times do |j|
|
@@ -67,7 +65,7 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
67
65
|
shape.e = 1
|
68
66
|
shape.u = 1
|
69
67
|
@space.add_static_shape(shape)
|
70
|
-
gc.polygon(x - base + 1, y + base - 1, x + base - 1, y + base - 1,
|
68
|
+
gc.polygon(x - base + 1, y + base - 1, x + base - 1, y + base - 1, x, y - height + 1)
|
71
69
|
end
|
72
70
|
end
|
73
71
|
# do the drawing
|
@@ -83,10 +81,10 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
83
81
|
end
|
84
82
|
return vertices
|
85
83
|
end
|
86
|
-
|
84
|
+
|
87
85
|
# Produces the image of a polygon.
|
88
86
|
def polygon_image(vertices)
|
89
|
-
box_image = Magick::Image.new(EDGE_SIZE
|
87
|
+
box_image = Magick::Image.new(EDGE_SIZE * 2, EDGE_SIZE * 2) { self.background_color = "transparent" }
|
90
88
|
gc = Magick::Draw.new
|
91
89
|
gc.stroke("red")
|
92
90
|
gc.fill("plum")
|
@@ -95,12 +93,12 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
95
93
|
gc.draw(box_image)
|
96
94
|
return Gosu::Image.new(box_image)
|
97
95
|
end
|
98
|
-
|
96
|
+
|
99
97
|
# Produces the polygon objects and adds them to the space.
|
100
98
|
def create_boxes(num)
|
101
99
|
box_vertices = polygon_vertices(NUM_SIDES, EDGE_SIZE)
|
102
100
|
box_image = polygon_image(box_vertices)
|
103
|
-
boxes =
|
101
|
+
boxes = []
|
104
102
|
num.times do
|
105
103
|
body = CP::Body.new(1, CP::moment_for_poly(1.0, box_vertices, CP::Vec2.new(0, 0))) # mass, moment of inertia
|
106
104
|
body.p = CP::Vec2.new(rand(WIDTH), rand(40) - 50)
|
@@ -109,17 +107,17 @@ class ChipmunkAndRMagick < (Example rescue Gosu::Window)
|
|
109
107
|
shape.u = 0.4
|
110
108
|
boxes << Box.new(box_image, body)
|
111
109
|
@space.add_body(body)
|
112
|
-
@space.add_shape(shape)
|
110
|
+
@space.add_shape(shape)
|
113
111
|
end
|
114
112
|
return boxes
|
115
113
|
end
|
116
|
-
|
114
|
+
|
117
115
|
# All the simulation is done here.
|
118
116
|
def update
|
119
117
|
@space.step(TICK)
|
120
118
|
@boxes.each { |box| box.check_off_screen }
|
121
119
|
end
|
122
|
-
|
120
|
+
|
123
121
|
# All the updating of the screen is done here.
|
124
122
|
def draw
|
125
123
|
@background_image.draw(0, 0, ZOrder::BACKGROUND)
|
@@ -134,7 +132,7 @@ class Box
|
|
134
132
|
@image = image
|
135
133
|
@body = body
|
136
134
|
end
|
137
|
-
|
135
|
+
|
138
136
|
# If it goes offscreen we put it back to the top.
|
139
137
|
def check_off_screen
|
140
138
|
pos = @body.p
|
@@ -142,7 +140,7 @@ class Box
|
|
142
140
|
@body.p = CP::Vec2.new(rand * WIDTH, 0)
|
143
141
|
end
|
144
142
|
end
|
145
|
-
|
143
|
+
|
146
144
|
def draw
|
147
145
|
@image.draw_rot(@body.p.x, @body.p.y, ZOrder::BOX, @body.a.radians_to_gosu)
|
148
146
|
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# Encoding: UTF-8
|
2
|
-
|
3
1
|
## File: ChipmunkIntegration.rb
|
4
2
|
## Author: Dirk Johnson
|
5
3
|
## Version: 1.0.0
|
@@ -34,32 +32,32 @@ class Player
|
|
34
32
|
@shape = shape
|
35
33
|
@shape.body.p = CP::Vec2.new(0.0, 0.0) # position
|
36
34
|
@shape.body.v = CP::Vec2.new(0.0, 0.0) # velocity
|
37
|
-
|
35
|
+
|
38
36
|
# Keep in mind that down the screen is positive y, which means that PI/2 radians,
|
39
37
|
# which you might consider the top in the traditional Trig unit circle sense is actually
|
40
38
|
# the bottom; thus 3PI/2 is the top
|
41
|
-
@shape.body.a = (3*Math::PI/2.0) # angle in radians; faces towards top of screen
|
39
|
+
@shape.body.a = (3 * Math::PI / 2.0) # angle in radians; faces towards top of screen
|
42
40
|
end
|
43
|
-
|
41
|
+
|
44
42
|
# Directly set the position of our Player
|
45
43
|
def warp(vect)
|
46
44
|
@shape.body.p = vect
|
47
45
|
end
|
48
|
-
|
46
|
+
|
49
47
|
# Apply negative Torque; Chipmunk will do the rest
|
50
48
|
# SUBSTEPS is used as a divisor to keep turning rate constant
|
51
49
|
# even if the number of steps per update are adjusted
|
52
50
|
def turn_left
|
53
|
-
@shape.body.t -= 400.0/SUBSTEPS
|
51
|
+
@shape.body.t -= 400.0 / SUBSTEPS
|
54
52
|
end
|
55
|
-
|
53
|
+
|
56
54
|
# Apply positive Torque; Chipmunk will do the rest
|
57
55
|
# SUBSTEPS is used as a divisor to keep turning rate constant
|
58
56
|
# even if the number of steps per update are adjusted
|
59
57
|
def turn_right
|
60
|
-
@shape.body.t += 400.0/SUBSTEPS
|
58
|
+
@shape.body.t += 400.0 / SUBSTEPS
|
61
59
|
end
|
62
|
-
|
60
|
+
|
63
61
|
# Apply forward force; Chipmunk will do the rest
|
64
62
|
# SUBSTEPS is used as a divisor to keep acceleration rate constant
|
65
63
|
# even if the number of steps per update are adjusted
|
@@ -67,27 +65,27 @@ class Player
|
|
67
65
|
# forward momentum by creating a vector in the direction of the facing
|
68
66
|
# and with a magnitude representing the force we want to apply
|
69
67
|
def accelerate
|
70
|
-
@shape.body.apply_force(@shape.body.rot * (3000.0/SUBSTEPS), CP::Vec2.new(0.0, 0.0))
|
68
|
+
@shape.body.apply_force(@shape.body.rot * (3000.0 / SUBSTEPS), CP::Vec2.new(0.0, 0.0))
|
71
69
|
end
|
72
|
-
|
70
|
+
|
73
71
|
# Apply even more forward force
|
74
72
|
# See accelerate for more details
|
75
73
|
def boost
|
76
74
|
@shape.body.apply_force(@shape.body.rot * (3000.0), CP::Vec2.new(0.0, 0.0))
|
77
75
|
end
|
78
|
-
|
76
|
+
|
79
77
|
# Apply reverse force
|
80
78
|
# See accelerate for more details
|
81
79
|
def reverse
|
82
|
-
@shape.body.apply_force(-@shape.body.rot * (1000.0/SUBSTEPS), CP::Vec2.new(0.0, 0.0))
|
80
|
+
@shape.body.apply_force(-@shape.body.rot * (1000.0 / SUBSTEPS), CP::Vec2.new(0.0, 0.0))
|
83
81
|
end
|
84
|
-
|
82
|
+
|
85
83
|
# Wrap to the other side of the screen when we fly off the edge
|
86
84
|
def validate_position
|
87
85
|
l_position = CP::Vec2.new(@shape.body.p.x % WIDTH, @shape.body.p.y % HEIGHT)
|
88
86
|
@shape.body.p = l_position
|
89
87
|
end
|
90
|
-
|
88
|
+
|
91
89
|
def draw
|
92
90
|
@image.draw_rot(@shape.body.p.x, @shape.body.p.y, ZOrder::Player, @shape.body.a.radians_to_gosu)
|
93
91
|
end
|
@@ -97,7 +95,7 @@ end
|
|
97
95
|
# Of course... it just sits around and looks good...
|
98
96
|
class Star
|
99
97
|
attr_reader :shape
|
100
|
-
|
98
|
+
|
101
99
|
def initialize(animation, shape)
|
102
100
|
@animation = animation
|
103
101
|
@color = Gosu::Color.new(0xff_000000)
|
@@ -110,8 +108,8 @@ class Star
|
|
110
108
|
@shape.body.a = 0.gosu_to_radians # faces towards top of screen
|
111
109
|
end
|
112
110
|
|
113
|
-
def draw
|
114
|
-
img = @animation[Gosu.milliseconds / 100 % @animation.size]
|
111
|
+
def draw
|
112
|
+
img = @animation[Gosu.milliseconds / 100 % @animation.size]
|
115
113
|
img.draw(@shape.body.p.x - img.width / 2.0, @shape.body.p.y - img.height / 2.0, ZOrder::Stars, 1, 1, @color, :add)
|
116
114
|
end
|
117
115
|
end
|
@@ -121,52 +119,52 @@ end
|
|
121
119
|
class ChipmunkIntegration < (Example rescue Gosu::Window)
|
122
120
|
def initialize
|
123
121
|
super WIDTH, HEIGHT
|
124
|
-
|
122
|
+
|
125
123
|
self.caption = "Gosu & Chipmunk Integration Demo"
|
126
|
-
|
124
|
+
|
127
125
|
@background_image = Gosu::Image.new("media/space.png", tileable: true)
|
128
126
|
|
129
127
|
# Put the beep here, as it is the environment now that determines collision
|
130
128
|
@beep = Gosu::Sample.new("media/beep.wav")
|
131
|
-
|
129
|
+
|
132
130
|
# Put the score here, as it is the environment that tracks this now
|
133
131
|
@score = 0
|
134
132
|
@font = Gosu::Font.new(20)
|
135
|
-
|
133
|
+
|
136
134
|
# Time increment over which to apply a physics "step" ("delta t")
|
137
|
-
@dt = (1.0/60.0)
|
138
|
-
|
135
|
+
@dt = (1.0 / 60.0)
|
136
|
+
|
139
137
|
# Create our Space and set its damping
|
140
138
|
# A damping of 0.8 causes the ship bleed off its force and torque over time
|
141
139
|
# This is not realistic behavior in a vacuum of space, but it gives the game
|
142
140
|
# the feel I'd like in this situation
|
143
141
|
@space = CP::Space.new
|
144
|
-
@space.damping = 0.8
|
145
|
-
|
142
|
+
@space.damping = 0.8
|
143
|
+
|
146
144
|
# Create the Body for the Player
|
147
145
|
body = CP::Body.new(10.0, 150.0)
|
148
|
-
|
146
|
+
|
149
147
|
# In order to create a shape, we must first define it
|
150
148
|
# Chipmunk defines 3 types of Shapes: Segments, Circles and Polys
|
151
149
|
# We'll use s simple, 4 sided Poly for our Player (ship)
|
152
150
|
# You need to define the vectors so that the "top" of the Shape is towards 0 radians (the right)
|
153
151
|
shape_array = [CP::Vec2.new(-25.0, -25.0), CP::Vec2.new(-25.0, 25.0), CP::Vec2.new(25.0, 1.0), CP::Vec2.new(25.0, -1.0)]
|
154
|
-
shape = CP::Shape::Poly.new(body, shape_array, CP::Vec2.new(0,0))
|
155
|
-
|
152
|
+
shape = CP::Shape::Poly.new(body, shape_array, CP::Vec2.new(0, 0))
|
153
|
+
|
156
154
|
# The collision_type of a shape allows us to set up special collision behavior
|
157
155
|
# based on these types. The actual value for the collision_type is arbitrary
|
158
156
|
# and, as long as it is consistent, will work for us; of course, it helps to have it make sense
|
159
157
|
shape.collision_type = :ship
|
160
|
-
|
158
|
+
|
161
159
|
@space.add_body(body)
|
162
160
|
@space.add_shape(shape)
|
163
161
|
|
164
162
|
@player = Player.new(shape)
|
165
163
|
@player.warp(CP::Vec2.new(320, 240)) # move to the center of the window
|
166
|
-
|
164
|
+
|
167
165
|
@star_anim = Gosu::Image.load_tiles("media/star.png", 25, 25)
|
168
166
|
@stars = Array.new
|
169
|
-
|
167
|
+
|
170
168
|
# Here we define what is supposed to happen when a Player (ship) collides with a Star
|
171
169
|
# I create a @remove_shapes array because we cannot remove either Shapes or Bodies
|
172
170
|
# from Space within a collision closure, rather, we have to wait till the closure
|
@@ -181,7 +179,7 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
|
|
181
179
|
@beep.play
|
182
180
|
@remove_shapes << star_shape
|
183
181
|
end
|
184
|
-
|
182
|
+
|
185
183
|
# Here we tell Space that we don't want one star bumping into another
|
186
184
|
# The reason we need to do this is because when the Player hits a Star,
|
187
185
|
# the Star will travel until it is removed in the update cycle below
|
@@ -207,16 +205,16 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
|
|
207
205
|
@space.remove_shape(shape)
|
208
206
|
end
|
209
207
|
@remove_shapes.clear # clear out the shapes for next pass
|
210
|
-
|
208
|
+
|
211
209
|
# When a force or torque is set on a Body, it is cumulative
|
212
210
|
# This means that the force you applied last SUBSTEP will compound with the
|
213
211
|
# force applied this SUBSTEP; which is probably not the behavior you want
|
214
212
|
# We reset the forces on the Player each SUBSTEP for this reason
|
215
213
|
@player.shape.body.reset_forces
|
216
|
-
|
214
|
+
|
217
215
|
# Wrap around the screen to the other side
|
218
216
|
@player.validate_position
|
219
|
-
|
217
|
+
|
220
218
|
# Check keyboard
|
221
219
|
if Gosu.button_down? Gosu::KB_LEFT
|
222
220
|
@player.turn_left
|
@@ -224,7 +222,7 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
|
|
224
222
|
if Gosu.button_down? Gosu::KB_RIGHT
|
225
223
|
@player.turn_right
|
226
224
|
end
|
227
|
-
|
225
|
+
|
228
226
|
if Gosu.button_down? Gosu::KB_UP
|
229
227
|
if Gosu.button_down?(Gosu::KB_RIGHT_SHIFT) or Gosu.button_down?(Gosu::KB_LEFT_SHIFT)
|
230
228
|
@player.boost
|
@@ -234,21 +232,21 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
|
|
234
232
|
elsif Gosu.button_down? Gosu::KB_DOWN
|
235
233
|
@player.reverse
|
236
234
|
end
|
237
|
-
|
235
|
+
|
238
236
|
# Perform the step over @dt period of time
|
239
237
|
# For best performance @dt should remain consistent for the game
|
240
238
|
@space.step(@dt)
|
241
239
|
end
|
242
|
-
|
240
|
+
|
243
241
|
# Each update (not SUBSTEP) we see if we need to add more Stars
|
244
242
|
if rand(100) < 4 and @stars.size < 25
|
245
243
|
body = CP::Body.new(0.0001, 0.0001)
|
246
|
-
shape = CP::Shape::Circle.new(body, 25/2, CP::Vec2.new(0.0, 0.0))
|
244
|
+
shape = CP::Shape::Circle.new(body, 25 / 2, CP::Vec2.new(0.0, 0.0))
|
247
245
|
shape.collision_type = :star
|
248
|
-
|
246
|
+
|
249
247
|
@space.add_body(body)
|
250
248
|
@space.add_shape(shape)
|
251
|
-
|
249
|
+
|
252
250
|
@stars.push(Star.new(@star_anim, shape))
|
253
251
|
end
|
254
252
|
end
|
@@ -257,7 +255,7 @@ class ChipmunkIntegration < (Example rescue Gosu::Window)
|
|
257
255
|
@background_image.draw(0, 0, ZOrder::Background)
|
258
256
|
@player.draw
|
259
257
|
@stars.each { |star| star.draw }
|
260
|
-
@font.
|
258
|
+
@font.draw_text("Score: #{@score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xff_ffff00)
|
261
259
|
end
|
262
260
|
|
263
261
|
def button_down(id)
|
data/examples/cptn_ruby.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# Encoding: UTF-8
|
2
|
-
|
3
1
|
# A simple jump-and-run/platformer game with a tile-based map.
|
4
2
|
|
5
3
|
# Shows how to
|
@@ -17,7 +15,7 @@
|
|
17
15
|
# 4) similarly, add sound effects for various events
|
18
16
|
# Exploring this game's code and Gosu:
|
19
17
|
# 5) make the player wider, so he doesn't fall off edges as easily
|
20
|
-
# 6) add background music (check if playing in Window#update to implement
|
18
|
+
# 6) add background music (check if playing in Window#update to implement
|
21
19
|
# looping)
|
22
20
|
# 7) implement parallax scrolling for the star background!
|
23
21
|
# Getting tricky:
|
@@ -43,7 +41,7 @@ class CollectibleGem
|
|
43
41
|
@image = image
|
44
42
|
@x, @y = x, y
|
45
43
|
end
|
46
|
-
|
44
|
+
|
47
45
|
def draw
|
48
46
|
# Draw, slowly rotating
|
49
47
|
@image.draw_rot(@x, @y, 0, 25 * Math.sin(Gosu.milliseconds / 133.7))
|
@@ -63,9 +61,9 @@ class Player
|
|
63
61
|
@standing, @walk1, @walk2, @jump = *Gosu::Image.load_tiles("media/cptn_ruby.png", 50, 50)
|
64
62
|
# This always points to the frame that is currently drawn.
|
65
63
|
# This is set in update, and used in draw.
|
66
|
-
@cur_image = @standing
|
64
|
+
@cur_image = @standing
|
67
65
|
end
|
68
|
-
|
66
|
+
|
69
67
|
def draw
|
70
68
|
# Flip vertically when facing to the left.
|
71
69
|
if @dir == :left
|
@@ -77,14 +75,14 @@ class Player
|
|
77
75
|
end
|
78
76
|
@cur_image.draw(@x + offs_x, @y - 49, 0, factor, 1.0)
|
79
77
|
end
|
80
|
-
|
78
|
+
|
81
79
|
# Could the object be placed at x + offs_x/y + offs_y without being stuck?
|
82
80
|
def would_fit(offs_x, offs_y)
|
83
81
|
# Check at the center/top and center/bottom for map collisions
|
84
82
|
not @map.solid?(@x + offs_x, @y + offs_y) and
|
85
83
|
not @map.solid?(@x + offs_x, @y + offs_y - 45)
|
86
84
|
end
|
87
|
-
|
85
|
+
|
88
86
|
def update(move_x)
|
89
87
|
# Select image depending on action
|
90
88
|
if (move_x == 0)
|
@@ -95,7 +93,7 @@ class Player
|
|
95
93
|
if (@vy < 0)
|
96
94
|
@cur_image = @jump
|
97
95
|
end
|
98
|
-
|
96
|
+
|
99
97
|
# Directional walking, horizontal movement
|
100
98
|
if move_x > 0
|
101
99
|
@dir = :right
|
@@ -118,13 +116,13 @@ class Player
|
|
118
116
|
(-@vy).times { if would_fit(0, -1) then @y -= 1 else @vy = 0 end }
|
119
117
|
end
|
120
118
|
end
|
121
|
-
|
119
|
+
|
122
120
|
def try_to_jump
|
123
121
|
if @map.solid?(@x, @y + 1)
|
124
122
|
@vy = -20
|
125
123
|
end
|
126
124
|
end
|
127
|
-
|
125
|
+
|
128
126
|
def collect_gems(gems)
|
129
127
|
# Same as in the tutorial game.
|
130
128
|
gems.reject! do |c|
|
@@ -136,7 +134,7 @@ end
|
|
136
134
|
# Map class holds and draws tiles and gems.
|
137
135
|
class Map
|
138
136
|
attr_reader :width, :height, :gems
|
139
|
-
|
137
|
+
|
140
138
|
def initialize(filename)
|
141
139
|
# Load 60x60 tiles, 5px overlap in all four directions.
|
142
140
|
@tileset = Gosu::Image.load_tiles("media/tileset.png", 60, 60, tileable: true)
|
@@ -152,9 +150,9 @@ class Map
|
|
152
150
|
case lines[y][x, 1]
|
153
151
|
when '"'
|
154
152
|
Tiles::Grass
|
155
|
-
when
|
153
|
+
when "#"
|
156
154
|
Tiles::Earth
|
157
|
-
when
|
155
|
+
when "x"
|
158
156
|
@gems.push(CollectibleGem.new(gem_img, x * 50 + 25, y * 50 + 25))
|
159
157
|
nil
|
160
158
|
else
|
@@ -163,7 +161,7 @@ class Map
|
|
163
161
|
end
|
164
162
|
end
|
165
163
|
end
|
166
|
-
|
164
|
+
|
167
165
|
def draw
|
168
166
|
# Very primitive drawing function:
|
169
167
|
# Draws all the tiles, some off-screen, some on-screen.
|
@@ -179,7 +177,7 @@ class Map
|
|
179
177
|
end
|
180
178
|
@gems.each { |c| c.draw }
|
181
179
|
end
|
182
|
-
|
180
|
+
|
183
181
|
# Solid at a given pixel position?
|
184
182
|
def solid?(x, y)
|
185
183
|
y < 0 || @tiles[x / 50][y / 50]
|
@@ -189,16 +187,16 @@ end
|
|
189
187
|
class CptnRuby < (Example rescue Gosu::Window)
|
190
188
|
def initialize
|
191
189
|
super WIDTH, HEIGHT
|
192
|
-
|
190
|
+
|
193
191
|
self.caption = "Cptn. Ruby"
|
194
|
-
|
192
|
+
|
195
193
|
@sky = Gosu::Image.new("media/space.png", tileable: true)
|
196
194
|
@map = Map.new("media/cptn_ruby_map.txt")
|
197
195
|
@cptn = Player.new(@map, 400, 100)
|
198
196
|
# The scrolling position is stored as top left corner of the screen.
|
199
197
|
@camera_x = @camera_y = 0
|
200
198
|
end
|
201
|
-
|
199
|
+
|
202
200
|
def update
|
203
201
|
move_x = 0
|
204
202
|
move_x -= 5 if Gosu.button_down? Gosu::KB_LEFT
|
@@ -209,7 +207,7 @@ class CptnRuby < (Example rescue Gosu::Window)
|
|
209
207
|
@camera_x = [[@cptn.x - WIDTH / 2, 0].max, @map.width * 50 - WIDTH].min
|
210
208
|
@camera_y = [[@cptn.y - HEIGHT / 2, 0].max, @map.height * 50 - HEIGHT].min
|
211
209
|
end
|
212
|
-
|
210
|
+
|
213
211
|
def draw
|
214
212
|
@sky.draw 0, 0, 0
|
215
213
|
Gosu.translate(-@camera_x, -@camera_y) do
|
@@ -217,7 +215,7 @@ class CptnRuby < (Example rescue Gosu::Window)
|
|
217
215
|
@cptn.draw
|
218
216
|
end
|
219
217
|
end
|
220
|
-
|
218
|
+
|
221
219
|
def button_down(id)
|
222
220
|
case id
|
223
221
|
when Gosu::KB_UP
|