gosu 0.8.7.2-x86-mingw32 → 0.9.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/examples/CptnRuby.rb DELETED
@@ -1,223 +0,0 @@
1
- # Basically, the tutorial game taken to a jump'n'run perspective.
2
-
3
- # Shows how to
4
- # * implement jumping/gravity
5
- # * implement scrolling using Window#translate
6
- # * implement a simple tile-based map
7
- # * load levels from primitive text files
8
-
9
- # Some exercises, starting at the real basics:
10
- # 0) understand the existing code!
11
- # As shown in the tutorial:
12
- # 1) change it use Gosu's Z-ordering
13
- # 2) add gamepad support
14
- # 3) add a score as in the tutorial game
15
- # 4) similarly, add sound effects for various events
16
- # Exploring this game's code and Gosu:
17
- # 5) make the player wider, so he doesn't fall off edges as easily
18
- # 6) add background music (check if playing in Window#update to implement
19
- # looping)
20
- # 7) implement parallax scrolling for the star background!
21
- # Getting tricky:
22
- # 8) optimize Map#draw so only tiles on screen are drawn (needs modulo, a pen
23
- # and paper to figure out)
24
- # 9) add loading of next level when all gems are collected
25
- # ...Enemies, a more sophisticated object system, weapons, title and credits
26
- # screens...
27
-
28
- require 'rubygems'
29
- require 'gosu'
30
- include Gosu
31
-
32
- module Tiles
33
- Grass = 0
34
- Earth = 1
35
- end
36
-
37
- class CollectibleGem
38
- attr_reader :x, :y
39
-
40
- def initialize(image, x, y)
41
- @image = image
42
- @x, @y = x, y
43
- end
44
-
45
- def draw
46
- # Draw, slowly rotating
47
- @image.draw_rot(@x, @y, 0, 25 * Math.sin(milliseconds / 133.7))
48
- end
49
- end
50
-
51
- # Player class.
52
- class CptnRuby
53
- attr_reader :x, :y
54
-
55
- def initialize(window, x, y)
56
- @x, @y = x, y
57
- @dir = :left
58
- @vy = 0 # Vertical velocity
59
- @map = window.map
60
- # Load all animation frames
61
- @standing, @walk1, @walk2, @jump =
62
- *Image.load_tiles(window, "media/CptnRuby.png", 50, 50, false)
63
- # This always points to the frame that is currently drawn.
64
- # This is set in update, and used in draw.
65
- @cur_image = @standing
66
- end
67
-
68
- def draw
69
- # Flip vertically when facing to the left.
70
- if @dir == :left then
71
- offs_x = -25
72
- factor = 1.0
73
- else
74
- offs_x = 25
75
- factor = -1.0
76
- end
77
- @cur_image.draw(@x + offs_x, @y - 49, 0, factor, 1.0)
78
- end
79
-
80
- # Could the object be placed at x + offs_x/y + offs_y without being stuck?
81
- def would_fit(offs_x, offs_y)
82
- # Check at the center/top and center/bottom for map collisions
83
- not @map.solid?(@x + offs_x, @y + offs_y) and
84
- not @map.solid?(@x + offs_x, @y + offs_y - 45)
85
- end
86
-
87
- def update(move_x)
88
- # Select image depending on action
89
- if (move_x == 0)
90
- @cur_image = @standing
91
- else
92
- @cur_image = (milliseconds / 175 % 2 == 0) ? @walk1 : @walk2
93
- end
94
- if (@vy < 0)
95
- @cur_image = @jump
96
- end
97
-
98
- # Directional walking, horizontal movement
99
- if move_x > 0 then
100
- @dir = :right
101
- move_x.times { if would_fit(1, 0) then @x += 1 end }
102
- end
103
- if move_x < 0 then
104
- @dir = :left
105
- (-move_x).times { if would_fit(-1, 0) then @x -= 1 end }
106
- end
107
-
108
- # Acceleration/gravity
109
- # By adding 1 each frame, and (ideally) adding vy to y, the player's
110
- # jumping curve will be the parabole we want it to be.
111
- @vy += 1
112
- # Vertical movement
113
- if @vy > 0 then
114
- @vy.times { if would_fit(0, 1) then @y += 1 else @vy = 0 end }
115
- end
116
- if @vy < 0 then
117
- (-@vy).times { if would_fit(0, -1) then @y -= 1 else @vy = 0 end }
118
- end
119
- end
120
-
121
- def try_to_jump
122
- if @map.solid?(@x, @y + 1) then
123
- @vy = -20
124
- end
125
- end
126
-
127
- def collect_gems(gems)
128
- # Same as in the tutorial game.
129
- gems.reject! do |c|
130
- (c.x - @x).abs < 50 and (c.y - @y).abs < 50
131
- end
132
- end
133
- end
134
-
135
- # Map class holds and draws tiles and gems.
136
- class Map
137
- attr_reader :width, :height, :gems
138
-
139
- def initialize(window, filename)
140
- # Load 60x60 tiles, 5px overlap in all four directions.
141
- @tileset = Image.load_tiles(window, "media/CptnRuby Tileset.png", 60, 60, true)
142
-
143
- gem_img = Image.new(window, "media/CptnRuby Gem.png", false)
144
- @gems = []
145
-
146
- lines = File.readlines(filename).map { |line| line.chomp }
147
- @height = lines.size
148
- @width = lines[0].size
149
- @tiles = Array.new(@width) do |x|
150
- Array.new(@height) do |y|
151
- case lines[y][x, 1]
152
- when '"'
153
- Tiles::Grass
154
- when '#'
155
- Tiles::Earth
156
- when 'x'
157
- @gems.push(CollectibleGem.new(gem_img, x * 50 + 25, y * 50 + 25))
158
- nil
159
- else
160
- nil
161
- end
162
- end
163
- end
164
- end
165
-
166
- def draw
167
- # Very primitive drawing function:
168
- # Draws all the tiles, some off-screen, some on-screen.
169
- @height.times do |y|
170
- @width.times do |x|
171
- tile = @tiles[x][y]
172
- if tile
173
- # Draw the tile with an offset (tile images have some overlap)
174
- # Scrolling is implemented here just as in the game objects.
175
- @tileset[tile].draw(x * 50 - 5, y * 50 - 5, 0)
176
- end
177
- end
178
- end
179
- @gems.each { |c| c.draw }
180
- end
181
-
182
- # Solid at a given pixel position?
183
- def solid?(x, y)
184
- y < 0 || @tiles[x / 50][y / 50]
185
- end
186
- end
187
-
188
- class Game < Window
189
- attr_reader :map
190
-
191
- def initialize
192
- super(640, 480, false)
193
- self.caption = "Cptn. Ruby"
194
- @sky = Image.new(self, "media/Space.png", true)
195
- @map = Map.new(self, "media/CptnRuby Map.txt")
196
- @cptn = CptnRuby.new(self, 400, 100)
197
- # The scrolling position is stored as top left corner of the screen.
198
- @camera_x = @camera_y = 0
199
- end
200
- def update
201
- move_x = 0
202
- move_x -= 5 if button_down? KbLeft
203
- move_x += 5 if button_down? KbRight
204
- @cptn.update(move_x)
205
- @cptn.collect_gems(@map.gems)
206
- # Scrolling follows player
207
- @camera_x = [[@cptn.x - 320, 0].max, @map.width * 50 - 640].min
208
- @camera_y = [[@cptn.y - 240, 0].max, @map.height * 50 - 480].min
209
- end
210
- def draw
211
- @sky.draw 0, 0, 0
212
- translate(-@camera_x, -@camera_y) do
213
- @map.draw
214
- @cptn.draw
215
- end
216
- end
217
- def button_down(id)
218
- if id == KbUp then @cptn.try_to_jump end
219
- if id == KbEscape then close end
220
- end
221
- end
222
-
223
- Game.new.show
data/examples/GosuZen.rb DELETED
@@ -1,68 +0,0 @@
1
- # Gosu Zen example based on erisdiscord's comment here:
2
- # https://github.com/jlnr/gosu/pull/120
3
-
4
- # Gosu Zen is the (inline) Sinatra of Ruby multimedia programming.
5
- # The interface is still in flux. If you want to tune the interface
6
- # a little further or provide more examples, please fork Gosu and
7
- # send a pull request. Thanks!
8
-
9
- require 'rubygems'
10
- require 'gosu/zen'
11
- include Gosu::Zen
12
-
13
- window 480, 240, :fullscreen => false
14
-
15
- button_down Gosu::KbEscape do
16
- close
17
- end
18
-
19
- update do
20
- t = Gosu.milliseconds / 1000.0
21
-
22
- @radius = [width, height].min * 0.37
23
- @angle = t * Math::PI
24
-
25
- a, b =\
26
- (Math.cos(t) + 0.5) * 0xff,
27
- (Math.sin(t) + 0.5) * 0xff
28
-
29
- c = (a + b) / 2
30
-
31
- @colors = [
32
- Gosu::Color.rgb(a, b, 0xff),
33
- Gosu::Color.rgb(a, 0x00, b),
34
- Gosu::Color.rgb(0xff, b, a),
35
- Gosu::Color.rgb(b, a, 0x00),
36
- Gosu::Color.rgb(b, 0xff, a),
37
- Gosu::Color.rgb(0x00, a, b) ]
38
-
39
- @background = Gosu::Color.rgb(c, c, c)
40
- end
41
-
42
- draw do
43
- draw_quad\
44
- 0, 0, @background,
45
- 0, height, @background,
46
- width, height, @background,
47
- width, 0, @background, 0
48
-
49
- translate width / 2, height / 2 do
50
- @colors.each.with_index do |color, i|
51
-
52
- angle = @angle + i.to_f / @colors.length * 2.0 * Math::PI
53
- x = @radius * Math.sin(angle)
54
- y = @radius * Math.cos(angle)
55
- w, h = x, y
56
-
57
- translate x, y do
58
- rotate Gosu.radians_to_degrees(angle) do
59
- draw_quad\
60
- -w, +h, color,
61
- -w, -h, color,
62
- +w, -h, color,
63
- +w, +h, color, 0
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,155 +0,0 @@
1
- # Based on the C Demo3 demonstration distributed with Chipmunk.
2
- # Also with some help from the ChipmunkIntegration.rb program.
3
- #
4
- # License: Same as for Gosu (MIT)
5
- # Created on 21/10/2007, 00:05:19 by Robert Sheehan
6
-
7
- require 'rubygems'
8
- require 'gosu'
9
- require 'chipmunk'
10
- require 'RMagick'
11
-
12
- # Convenience method for converting from radians to a Vec2 vector.
13
- class Numeric
14
- def radians_to_vec2
15
- CP::Vec2.new(Math::cos(self), Math::sin(self))
16
- end
17
- end
18
-
19
- # Layering of sprites
20
- module ZOrder
21
- Background, Box = *0..1
22
- end
23
-
24
- SCREEN_WIDTH = 640
25
- SCREEN_HEIGHT = 480
26
- TICK = 1.0/60.0
27
- NUM_POLYGONS = 80
28
- NUM_SIDES = 4
29
- EDGE_SIZE = 15
30
-
31
- # Everything appears in the Gosu::Window.
32
- class DemoWindow < Gosu::Window
33
-
34
- def initialize
35
- super(SCREEN_WIDTH, SCREEN_HEIGHT, false)
36
- self.caption = "A Chipmunk-RMagick-Gosu Demonstration"
37
- @space = CP::Space.new
38
- @space.iterations = 5
39
- @space.gravity = CP::Vec2.new(0, 100)
40
- # you can replace the background with any image with this line
41
- # background = Magick::ImageList.new( "media/Space.png")
42
- fill = Magick::TextureFill.new(Magick::ImageList.new("granite:"))
43
- background = Magick::Image.new(SCREEN_WIDTH, SCREEN_HEIGHT, fill)
44
- setup_triangles(background)
45
- @background_image = Gosu::Image.new(self, background, true) # turn the image into a Gosu one
46
- @boxes = create_boxes(NUM_POLYGONS)
47
- end
48
-
49
- # Create all of the static triangles.
50
- # Adds them to the space and the background image.
51
- def setup_triangles(background)
52
- gc = Magick::Draw.new
53
- gc.stroke_width(2)
54
- gc.stroke('red')
55
- gc.fill('blue')
56
- # all the triangles are part of the same body
57
- body = CP::Body.new(Float::MAX, Float::MAX)
58
- base = 15
59
- height = 10
60
- shape_vertices = [CP::Vec2.new(-base, base), CP::Vec2.new(base, base), CP::Vec2.new(0, -height)]
61
- # make shapes and images
62
- 9.times do |i|
63
- 6.times do |j|
64
- stagger = (j % 2) * 40
65
- x = i * 80 + stagger
66
- y = j * 70 + 80
67
- shape = CP::Shape::Poly.new(body, shape_vertices, CP::Vec2.new(x, y))
68
- shape.e = 1
69
- shape.u = 1
70
- @space.add_static_shape(shape)
71
- gc.polygon(x - base + 1, y + base - 1, x + base - 1, y + base - 1, x, y - height + 1)
72
- end
73
- end
74
- # do the drawing
75
- gc.draw(background)
76
- end
77
-
78
- # Produces the vertices of a regular polygon.
79
- def polygon_vertices(sides, size)
80
- vertices = []
81
- sides.times do |i|
82
- angle = -2 * Math::PI * i / sides
83
- vertices << angle.radians_to_vec2() * size
84
- end
85
- return vertices
86
- end
87
-
88
- # Produces the image of a polygon.
89
- def polygon_image(vertices)
90
- box_image = Magick::Image.new(EDGE_SIZE * 2, EDGE_SIZE * 2) { self.background_color = 'transparent' }
91
- gc = Magick::Draw.new
92
- gc.stroke('red')
93
- gc.fill('plum')
94
- draw_vertices = vertices.map { |v| [v.x + EDGE_SIZE, v.y + EDGE_SIZE] }.flatten
95
- gc.polygon(*draw_vertices)
96
- gc.draw(box_image)
97
- return Gosu::Image.new(self, box_image, false)
98
- end
99
-
100
- # Produces the polygon objects and adds them to the space.
101
- def create_boxes(num)
102
- box_vertices = polygon_vertices(NUM_SIDES, EDGE_SIZE)
103
- box_image = polygon_image(box_vertices)
104
- boxes = []
105
- num.times do
106
- body = CP::Body.new(1, CP::moment_for_poly(1.0, box_vertices, CP::Vec2.new(0, 0))) # mass, moment of inertia
107
- body.p = CP::Vec2.new(rand(SCREEN_WIDTH), rand(40) - 50)
108
- shape = CP::Shape::Poly.new(body, box_vertices, CP::Vec2.new(0, 0))
109
- shape.e = 0.0
110
- shape.u = 0.4
111
- boxes << Box.new(box_image, body)
112
- @space.add_body(body)
113
- @space.add_shape(shape)
114
- end
115
- return boxes
116
- end
117
-
118
- # All the simulation is done here.
119
- def update
120
- @space.step(TICK)
121
- @boxes.each { |box| box.check_off_screen }
122
- end
123
-
124
- # All the updating of the screen is done here.
125
- def draw
126
- @background_image.draw(0, 0, ZOrder::Background)
127
- @boxes.each { |box| box.draw }
128
- end
129
-
130
- end
131
-
132
- # The falling boxes class.
133
- # Nothing more than a body and an image.
134
- class Box
135
-
136
- def initialize(image, body)
137
- @image = image
138
- @body = body
139
- end
140
-
141
- # If it goes offscreen we put it back to the top.
142
- def check_off_screen
143
- pos = @body.p
144
- if pos.y > SCREEN_HEIGHT + EDGE_SIZE or pos.x > SCREEN_WIDTH + EDGE_SIZE or pos.x < -EDGE_SIZE
145
- @body.p = CP::Vec2.new(rand * SCREEN_WIDTH, 0)
146
- end
147
- end
148
-
149
- def draw
150
- @image.draw_rot(@body.p.x, @body.p.y, ZOrder::Box, @body.a.radians_to_gosu)
151
- end
152
- end
153
-
154
- window = DemoWindow.new
155
- window.show
@@ -1,226 +0,0 @@
1
- # The tutorial game over a landscape rendered with OpenGL.
2
- # Basically shows how arbitrary OpenGL calls can be put into
3
- # the block given to Window#gl, and that Gosu Images can be
4
- # used as textures using the gl_tex_info call.
5
-
6
- require 'rubygems'
7
- require 'gosu'
8
- require 'gl'
9
- require 'glu'
10
-
11
- include Gl
12
- include Glu
13
-
14
- module ZOrder
15
- Stars, Player, UI = *0..3
16
- end
17
-
18
- # The only really new class here.
19
- # Draws a scrolling, repeating texture with a randomized height map.
20
- class GLBackground
21
- # Height map size
22
- POINTS_X = 7
23
- POINTS_Y = 7
24
- # Scrolling speed
25
- SCROLLS_PER_STEP = 50
26
-
27
- def initialize(window)
28
- @image = Gosu::Image.new(window, "media/Earth.png", true)
29
- @scrolls = 0
30
- @height_map = Array.new(POINTS_Y) { Array.new(POINTS_X) { rand } }
31
- end
32
-
33
- def scroll
34
- @scrolls += 1
35
- if @scrolls == SCROLLS_PER_STEP then
36
- @scrolls = 0
37
- @height_map.shift
38
- @height_map.push Array.new(POINTS_X) { rand }
39
- end
40
- end
41
-
42
- def exec_gl
43
- # Get the name of the OpenGL texture the Image resides on, and the
44
- # u/v coordinates of the rect it occupies.
45
- # gl_tex_info can return nil if the image was too large to fit onto
46
- # a single OpenGL texture and was internally split up.
47
- info = @image.gl_tex_info
48
- return unless info
49
-
50
- # Pretty straightforward OpenGL code.
51
-
52
- glDepthFunc(GL_GEQUAL)
53
- glEnable(GL_DEPTH_TEST)
54
- glEnable(GL_BLEND)
55
-
56
- glMatrixMode(GL_PROJECTION)
57
- glLoadIdentity
58
- glFrustum(-0.10, 0.10, -0.075, 0.075, 1, 100)
59
-
60
- glMatrixMode(GL_MODELVIEW)
61
- glLoadIdentity
62
- glTranslate(0, 0, -4)
63
-
64
- glEnable(GL_TEXTURE_2D)
65
- glBindTexture(GL_TEXTURE_2D, info.tex_name)
66
-
67
- offs_y = 1.0 * @scrolls / SCROLLS_PER_STEP
68
-
69
- 0.upto(POINTS_Y - 2) do |y|
70
- 0.upto(POINTS_X - 2) do |x|
71
- glBegin(GL_TRIANGLE_STRIP)
72
- z = @height_map[y][x]
73
- glColor4d(1, 1, 1, z)
74
- glTexCoord2d(info.left, info.top)
75
- glVertex3d(-0.5 + (x - 0.0) / (POINTS_X-1), -0.5 + (y - offs_y - 0.0) / (POINTS_Y-2), z)
76
-
77
- z = @height_map[y+1][x]
78
- glColor4d(1, 1, 1, z)
79
- glTexCoord2d(info.left, info.bottom)
80
- glVertex3d(-0.5 + (x - 0.0) / (POINTS_X-1), -0.5 + (y - offs_y + 1.0) / (POINTS_Y-2), z)
81
-
82
- z = @height_map[y][x + 1]
83
- glColor4d(1, 1, 1, z)
84
- glTexCoord2d(info.right, info.top)
85
- glVertex3d(-0.5 + (x + 1.0) / (POINTS_X-1), -0.5 + (y - offs_y - 0.0) / (POINTS_Y-2), z)
86
-
87
- z = @height_map[y+1][x + 1]
88
- glColor4d(1, 1, 1, z)
89
- glTexCoord2d(info.right, info.bottom)
90
- glVertex3d(-0.5 + (x + 1.0) / (POINTS_X-1), -0.5 + (y - offs_y + 1.0) / (POINTS_Y-2), z)
91
- glEnd
92
- end
93
- end
94
- end
95
- end
96
-
97
- # Roughly adapted from the tutorial game. Always faces north.
98
- class Player
99
- Speed = 7
100
-
101
- attr_reader :score
102
-
103
- def initialize(window, x, y)
104
- @image = Gosu::Image.new(window, "media/Starfighter.bmp", false)
105
- @beep = Gosu::Sample.new(window, "media/Beep.wav")
106
- @x, @y = x, y
107
- @score = 0
108
- end
109
-
110
- def move_left
111
- @x = [@x - Speed, 0].max
112
- end
113
-
114
- def move_right
115
- @x = [@x + Speed, 800].min
116
- end
117
-
118
- def accelerate
119
- @y = [@y - Speed, 50].max
120
- end
121
-
122
- def brake
123
- @y = [@y + Speed, 600].min
124
- end
125
-
126
- def draw
127
- @image.draw(@x - @image.width / 2, @y - @image.height / 2, ZOrder::Player)
128
- end
129
-
130
- def collect_stars(stars)
131
- stars.reject! do |star|
132
- if Gosu::distance(@x, @y, star.x, star.y) < 35 then
133
- @score += 10
134
- @beep.play
135
- true
136
- else
137
- false
138
- end
139
- end
140
- end
141
- end
142
-
143
- # Also taken from the tutorial, but drawn with draw_rot and an increasing angle
144
- # for extra rotation coolness!
145
- class Star
146
- attr_reader :x, :y
147
-
148
- def initialize(animation)
149
- @animation = animation
150
- @color = Gosu::Color.new(0xff000000)
151
- @color.red = rand(255 - 40) + 40
152
- @color.green = rand(255 - 40) + 40
153
- @color.blue = rand(255 - 40) + 40
154
- @x = rand * 800
155
- @y = 0
156
- end
157
-
158
- def draw
159
- img = @animation[Gosu::milliseconds / 100 % @animation.size];
160
- img.draw_rot(@x, @y, ZOrder::Stars, @y, 0.5, 0.5, 1, 1, @color, :add)
161
- end
162
-
163
- def update
164
- # Move towards bottom of screen
165
- @y += 3
166
- # Return false when out of screen (gets deleted then)
167
- @y < 650
168
- end
169
- end
170
-
171
- class GameWindow < Gosu::Window
172
- def initialize
173
- super(800, 600, false)
174
- self.caption = "Gosu & OpenGL Integration Demo"
175
-
176
- @gl_background = GLBackground.new(self)
177
-
178
- @player = Player.new(self, 400, 500)
179
-
180
- @star_anim = Gosu::Image::load_tiles(self, "media/Star.png", 25, 25, false)
181
- @stars = Array.new
182
-
183
- @font = Gosu::Font.new(self, Gosu::default_font_name, 20)
184
- end
185
-
186
- def update
187
- @player.move_left if button_down? Gosu::KbLeft or button_down? Gosu::GpLeft
188
- @player.move_right if button_down? Gosu::KbRight or button_down? Gosu::GpRight
189
- @player.accelerate if button_down? Gosu::KbUp or button_down? Gosu::GpUp
190
- @player.brake if button_down? Gosu::KbDown or button_down? Gosu::GpDown
191
-
192
- @player.collect_stars(@stars)
193
-
194
- @stars.reject! { |star| !star.update }
195
-
196
- @gl_background.scroll
197
-
198
- @stars.push(Star.new(@star_anim)) if rand(20) == 0
199
- end
200
-
201
- def draw
202
- # gl will execute the given block in a clean OpenGL environment, then reset
203
- # everything so Gosu's rendering can take place again.
204
-
205
- gl do
206
- glClearColor(0.0, 0.2, 0.5, 1.0)
207
- glClearDepth(0)
208
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
209
-
210
- @gl_background.exec_gl
211
- end
212
-
213
- @player.draw
214
- @stars.each { |star| star.draw }
215
- @font.draw("Score: #{@player.score}", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffffff00)
216
- end
217
-
218
- def button_down(id)
219
- if id == Gosu::KbEscape
220
- close
221
- end
222
- end
223
- end
224
-
225
- window = GameWindow.new
226
- window.show