gosu 0.7.7.2-universal-darwin-8 → 0.7.8-universal-darwin-8

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,7 @@
4
4
  ## Date: 2007-10-05
5
5
  ## License: Same as for Gosu (MIT)
6
6
  ## Comments: Based on the Gosu Ruby Tutorial, but incorporating the Chipmunk Physics Engine
7
+ ## See http://code.google.com/p/gosu/wiki/RubyChipmunkIntegration for the accompanying text.
7
8
 
8
9
  require 'rubygems'
9
10
  require 'gosu'
@@ -0,0 +1,159 @@
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 between radians, Gosu degrees and Vec2 vectors.
13
+ class Numeric
14
+ def radians_to_gosu
15
+ self * 180.0 / Math::PI + 90
16
+ end
17
+
18
+ def radians_to_vec2
19
+ CP::Vec2.new(Math::cos(self), Math::sin(self))
20
+ end
21
+ end
22
+
23
+ # Layering of sprites
24
+ module ZOrder
25
+ Background, Box = *0..1
26
+ end
27
+
28
+ SCREEN_WIDTH = 640
29
+ SCREEN_HEIGHT = 480
30
+ TICK = 1.0/60.0
31
+ NUM_POLYGONS = 80
32
+ NUM_SIDES = 4
33
+ EDGE_SIZE = 15
34
+
35
+ # Everything appears in the Gosu::Window.
36
+ class DemoWindow < Gosu::Window
37
+
38
+ def initialize
39
+ super(SCREEN_WIDTH, SCREEN_HEIGHT, false)
40
+ self.caption = "A Chipmunk-RMagick-Gosu Demonstration"
41
+ @space = CP::Space.new
42
+ @space.iterations = 5
43
+ @space.gravity = CP::Vec2.new(0, 100)
44
+ # you can replace the background with any image with this line
45
+ # background = Magick::ImageList.new( "media/Space.png")
46
+ fill = Magick::TextureFill.new(Magick::ImageList.new("granite:"))
47
+ background = Magick::Image.new(SCREEN_WIDTH, SCREEN_HEIGHT, fill)
48
+ setup_triangles(background)
49
+ @background_image = Gosu::Image.new(self, background, true) # turn the image into a Gosu one
50
+ @boxes = create_boxes(NUM_POLYGONS)
51
+ end
52
+
53
+ # Create all of the static triangles.
54
+ # Adds them to the space and the background image.
55
+ def setup_triangles(background)
56
+ gc = Magick::Draw.new
57
+ gc.stroke_width(2)
58
+ gc.stroke('red')
59
+ gc.fill('blue')
60
+ # all the triangles are part of the same body
61
+ body = CP::Body.new(Float::MAX, Float::MAX)
62
+ base = 15
63
+ height = 10
64
+ shape_vertices = [CP::Vec2.new(-base, base), CP::Vec2.new(base, base), CP::Vec2.new(0, -height)]
65
+ # make shapes and images
66
+ 9.times do |i|
67
+ 6.times do |j|
68
+ stagger = (j % 2) * 40
69
+ x = i * 80 + stagger
70
+ y = j * 70 + 80
71
+ shape = CP::Shape::Poly.new(body, shape_vertices, CP::Vec2.new(x, y))
72
+ shape.e = 1
73
+ shape.u = 1
74
+ @space.add_static_shape(shape)
75
+ gc.polygon(x - base + 1, y + base - 1, x + base - 1, y + base - 1, x, y - height + 1)
76
+ end
77
+ end
78
+ # do the drawing
79
+ gc.draw(background)
80
+ end
81
+
82
+ # Produces the vertices of a regular polygon.
83
+ def polygon_vertices(sides, size)
84
+ vertices = []
85
+ sides.times do |i|
86
+ angle = -2 * Math::PI * i / sides
87
+ vertices << angle.radians_to_vec2() * size
88
+ end
89
+ return vertices
90
+ end
91
+
92
+ # Produces the image of a polygon.
93
+ def polygon_image(vertices)
94
+ box_image = Magick::Image.new(EDGE_SIZE * 2, EDGE_SIZE * 2) { self.background_color = 'transparent' }
95
+ gc = Magick::Draw.new
96
+ gc.stroke('red')
97
+ gc.fill('plum')
98
+ draw_vertices = vertices.map { |v| [v.x + EDGE_SIZE, v.y + EDGE_SIZE] }.flatten
99
+ gc.polygon(*draw_vertices)
100
+ gc.draw(box_image)
101
+ return Gosu::Image.new(self, box_image, false)
102
+ end
103
+
104
+ # Produces the polygon objects and adds them to the space.
105
+ def create_boxes(num)
106
+ box_vertices = polygon_vertices(NUM_SIDES, EDGE_SIZE)
107
+ box_image = polygon_image(box_vertices)
108
+ boxes = []
109
+ num.times do
110
+ body = CP::Body.new(1, CP::moment_for_poly(1.0, box_vertices, CP::Vec2.new(0, 0))) # mass, moment of inertia
111
+ body.p = CP::Vec2.new(rand(SCREEN_WIDTH), rand(40) - 50)
112
+ shape = CP::Shape::Poly.new(body, box_vertices, CP::Vec2.new(0, 0))
113
+ shape.e = 0.0
114
+ shape.u = 0.4
115
+ boxes << Box.new(box_image, body)
116
+ @space.add_body(body)
117
+ @space.add_shape(shape)
118
+ end
119
+ return boxes
120
+ end
121
+
122
+ # All the simulation is done here.
123
+ def update
124
+ @space.step(TICK)
125
+ @boxes.each { |box| box.check_off_screen }
126
+ end
127
+
128
+ # All the updating of the screen is done here.
129
+ def draw
130
+ @background_image.draw(0, 0, ZOrder::Background)
131
+ @boxes.each { |box| box.draw }
132
+ end
133
+
134
+ end
135
+
136
+ # The falling boxes class.
137
+ # Nothing more than a body and an image.
138
+ class Box
139
+
140
+ def initialize(image, body)
141
+ @image = image
142
+ @body = body
143
+ end
144
+
145
+ # If it goes offscreen we put it back to the top.
146
+ def check_off_screen
147
+ pos = @body.p
148
+ if pos.y > SCREEN_HEIGHT + EDGE_SIZE or pos.x > SCREEN_WIDTH + EDGE_SIZE or pos.x < -EDGE_SIZE
149
+ @body.p = CP::Vec2.new(rand * SCREEN_WIDTH, 0)
150
+ end
151
+ end
152
+
153
+ def draw
154
+ @image.draw_rot(@body.p.x, @body.p.y, ZOrder::Box, @body.a.radians_to_gosu)
155
+ end
156
+ end
157
+
158
+ window = DemoWindow.new
159
+ window.show
data/lib/gosu.bundle CHANGED
Binary file
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: gosu
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.7.7.2
7
- date: 2007-11-10 00:00:00 +01:00
6
+ version: 0.7.8
7
+ date: 2007-12-11 00:00:00 +01:00
8
8
  summary: 2D game development library.
9
9
  require_paths:
10
10
  - lib
@@ -35,6 +35,7 @@ files:
35
35
  - LICENSE
36
36
  - examples/ChipmunkIntegration.rb
37
37
  - examples/CptnRuby.rb
38
+ - examples/MoreChipmunkAndRMagick.rb
38
39
  - examples/OpenGLIntegration.rb
39
40
  - examples/RMagickIntegration.rb
40
41
  - examples/Tutorial.rb