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