chingu 0.7.7.5 → 0.8rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/History.txt +1 -1
  2. data/Rakefile +4 -2
  3. data/chingu.gemspec +25 -10
  4. data/examples/example10_traits_retrofy.rb +3 -2
  5. data/examples/example11_animation.rb +3 -2
  6. data/examples/example12_trait_timer.rb +3 -2
  7. data/examples/example13_high_scores.rb +3 -2
  8. data/examples/example14_bounding_box_circle.rb +6 -5
  9. data/examples/example15_trait_timer2.rb +3 -2
  10. data/examples/example16_online_high_scores.rb +3 -2
  11. data/examples/example17_gosu_tutorial.rb +104 -103
  12. data/examples/example18_animation_trait.rb +3 -2
  13. data/examples/example19_edit_viewport.rb +3 -2
  14. data/examples/example1_basics.rb +3 -2
  15. data/examples/example20_trait_inheritence_test.rb +3 -2
  16. data/examples/example21.yml +306 -306
  17. data/examples/example21_sidescroller_with_edit.rb +3 -2
  18. data/examples/example22_text.rb +3 -2
  19. data/examples/example23_chipmunk.rb +3 -2
  20. data/examples/example24_enter_name.rb +19 -0
  21. data/examples/example24_input_codes.rb +41 -0
  22. data/examples/example25.yml +625 -0
  23. data/examples/example25_fibers_state_machine.rb +167 -0
  24. data/examples/example2_gamestate_basics.rb +3 -2
  25. data/examples/example3_parallax.rb +3 -2
  26. data/examples/example4_gamestates.rb +3 -2
  27. data/examples/example5_gamestates_in_pure_gosu.rb +3 -2
  28. data/examples/example6_transitional_game_state.rb +3 -2
  29. data/examples/example7_gfx_helpers.rb +3 -2
  30. data/examples/example8_traits.rb +3 -2
  31. data/examples/example9_collision_detection.rb +3 -2
  32. data/examples/game1.rb +3 -2
  33. data/examples/game_of_life.rb +291 -290
  34. data/lib/chingu.rb +6 -1
  35. data/lib/chingu/animation.rb +3 -2
  36. data/lib/chingu/game_object.rb +7 -1
  37. data/lib/chingu/game_object_map.rb +6 -12
  38. data/lib/chingu/game_states/edit.rb +63 -66
  39. data/lib/chingu/game_states/enter_name.rb +97 -0
  40. data/lib/chingu/helpers/game_object.rb +10 -21
  41. data/lib/chingu/input.rb +2 -0
  42. data/lib/chingu/online_high_score_list.rb +1 -4
  43. data/lib/chingu/rect.rb +2 -1
  44. data/lib/chingu/window.rb +10 -1
  45. data/spec/chingu/game_object_spec.rb +22 -22
  46. data/spec/chingu/input_spec.rb +22 -0
  47. metadata +48 -22
  48. data/spec/chingu/rect_20x20.png +0 -0
@@ -0,0 +1,167 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require File.join(File.dirname($0), "..", "lib", "chingu")
4
+ include Gosu
5
+ include Chingu
6
+ Velocity = Struct.new(:x, :y, :z)
7
+
8
+ #
9
+ # Press 'E' when demo is running to edit the playfield!
10
+ #
11
+ class Game < Chingu::Window
12
+ def initialize
13
+ p RUBY_VERSION
14
+ super(1000,600)
15
+ end
16
+
17
+ def setup
18
+ retrofy
19
+ switch_game_state(Example25)
20
+ end
21
+ end
22
+
23
+ class Example25 < GameState
24
+ def setup
25
+ self.input = { :escape => :exit, :e => :edit }
26
+ load_game_objects
27
+ @droid = Droid.create(:x => 100, :y => 400)
28
+ end
29
+
30
+ def edit
31
+ push_game_state(GameStates::Edit.new(:grid => [32,32], :classes => [Block]))
32
+ end
33
+
34
+ def update
35
+ super
36
+ $window.caption = "x/y: #{@droid.x.to_i}/#{@droid.y.to_i} - FPS: #{$window.fps}"
37
+ end
38
+
39
+ def draw
40
+ super
41
+ #fill_rect(Rect.new(0,400,$window.width, 200), Color::White)
42
+ end
43
+ end
44
+
45
+ #
46
+ # DROID
47
+ #
48
+ class Droid < Chingu::GameObject
49
+ trait :bounding_box, :scale => 0.80
50
+ traits :timer, :collision_detection , :timer, :velocity
51
+
52
+ attr_reader :jumpign
53
+
54
+ def setup
55
+ self.input = { [:holding_left, :holding_a] => :holding_left,
56
+ [:holding_right, :holding_d] => :holding_right,
57
+ [:up, :w] => :jump,
58
+ }
59
+
60
+ # Load the full animation from tile-file media/droid.bmp
61
+ @animations = Chingu::Animation.new(:file => "droid_11x15.bmp")
62
+ @animations.frame_names = { :scan => 0..5, :up => 6..7, :down => 8..9, :left => 10..11, :right => 12..13 }
63
+
64
+ @animation = @animations[:scan]
65
+ @speed = 3
66
+ @jumping = false
67
+
68
+ self.zorder = 300
69
+ self.factor = 3
70
+ self.acceleration_y = 0.5
71
+ self.max_velocity = 10
72
+ self.rotation_center = :bottom_center
73
+
74
+ @fiber = Fiber.new do |command|
75
+ puts "got #{command}"; loop { v = Fiber.yield(); puts "got #{v}" }
76
+ end
77
+ @fiber.resume :one
78
+ @fiber.resume :two
79
+ #exit
80
+
81
+ #fiber = Fiber.new do |command|
82
+ #while true
83
+ # Fiber.yield
84
+ #end
85
+ #velocity ||= Velocity.new
86
+
87
+ #if command == :left
88
+ # move(-@speed, 0)
89
+ # @animation = @animations[:left]
90
+ #elsif command == :right
91
+ # move(@speed, 0)
92
+ # @animation = @animations[:right]
93
+ #end
94
+
95
+ #velocity
96
+ #end
97
+
98
+ update
99
+ end
100
+
101
+ def holding_left
102
+ @fiber.resume :left
103
+ end
104
+
105
+ def holding_right
106
+ @fiber.resume :right
107
+ end
108
+
109
+ def jump
110
+ return if @jumping
111
+ @jumping = true
112
+ self.velocity_y = -10
113
+ @animation = @animations[:up]
114
+ end
115
+
116
+ def move(x,y)
117
+ self.x += x
118
+ self.each_collision(Block) do |me, stone_wall|
119
+ self.x = previous_x
120
+ break
121
+ end
122
+
123
+ self.y += y
124
+ end
125
+
126
+ def update
127
+ @fiber.resume :jump# if holding?(:a)
128
+
129
+ @image = @animation.next
130
+
131
+ self.each_collision(Block) do |me, stone_wall|
132
+ if self.velocity_y < 0 # Hitting the ceiling
133
+ me.y = stone_wall.bb.bottom + me.image.height * self.factor_y
134
+ self.velocity_y = 0
135
+ else # Land on ground
136
+ @jumping = false
137
+ me.y = stone_wall.bb.top-1
138
+ end
139
+ end
140
+
141
+ @animation = @animations[:scan] unless moved?
142
+ end
143
+ end
144
+
145
+ #
146
+ # BLOCK, our basic level building block
147
+ #
148
+ class Block < GameObject
149
+ trait :bounding_box, :debug => false
150
+ trait :collision_detection
151
+
152
+ def self.solid
153
+ all.select { |block| block.alpha == 255 }
154
+ end
155
+
156
+ def self.inside_viewport
157
+ all.select { |block| block.game_state.viewport.inside?(block) }
158
+ end
159
+
160
+ def setup
161
+ @image = Image["black_block.png"]
162
+ @color = Color.new(0xff808080)
163
+ end
164
+ end
165
+
166
+
167
+ Game.new.show
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require File.join(File.dirname($0), "..", "lib", "chingu")
2
+ require 'rubygems' rescue nil
3
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
4
+ require 'chingu'
4
5
  include Gosu
5
6
 
6
7
  #
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require File.join(File.dirname($0), "..", "lib", "chingu")
2
+ require 'rubygems' rescue nil
3
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
4
+ require 'chingu'
4
5
  include Gosu
5
6
 
6
7
  #
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require File.join(File.dirname($0), "..", "lib", "chingu")
2
+ require 'rubygems' rescue nil
3
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
4
+ require 'chingu'
4
5
  include Gosu
5
6
 
6
7
  #
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require File.join(File.dirname($0), "..", "lib", "chingu")
2
+ require 'rubygems' rescue nil
3
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
4
+ require 'chingu'
4
5
  include Gosu
5
6
 
6
7
  #
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require File.join(File.dirname($0), "..", "lib", "chingu")
2
+ require 'rubygems' rescue nil
3
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
4
+ require 'chingu'
4
5
  include Gosu
5
6
 
6
7
  #
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require File.join(File.dirname($0), "..", "lib", "chingu")
2
+ require 'rubygems' rescue nil
3
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
4
+ require 'chingu'
4
5
  include Gosu
5
6
  include Chingu
6
7
 
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require File.join(File.dirname($0), "..", "lib", "chingu")
2
+ require 'rubygems' rescue nil
3
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
4
+ require 'chingu'
4
5
  include Gosu
5
6
  include Chingu
6
7
 
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require File.join(File.dirname($0), "..", "lib", "chingu")
2
+ require 'rubygems' rescue nil
3
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
4
+ require 'chingu'
4
5
  include Gosu
5
6
  include Chingu
6
7
 
data/examples/game1.rb CHANGED
@@ -4,8 +4,9 @@
4
4
  # A simple game in Chingu, using GameState, GameObject, Paralaxx, has_traits etc
5
5
  #
6
6
  #
7
- require 'rubygems'
8
- require File.join(File.dirname($0), "..", "lib", "chingu")
7
+ require 'rubygems' rescue nil
8
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
9
+ require 'chingu'
9
10
  require 'texplay' # adds Image#get_pixel
10
11
 
11
12
  include Gosu
@@ -1,291 +1,292 @@
1
- #
2
- # Conway's game of life in Gosu/Chingu
3
- # http://toastymofo.blogspot.com/2010/06/conways-game-of-life-in-ruby-gosu.html
4
- #
5
- # Developed by r.kachowski ( http://www.toastymofo.net/ )
6
- # Additions by ippa ( http://ippa.se/gaming )
7
- #
8
- require 'chingu'
9
- require 'gosu'
10
-
11
- class Main < Chingu::Window
12
- def initialize
13
- super(640,480,false)
14
- self.input={:esc=>:exit}
15
- push_game_state(GameOfLife)
16
- end
17
- def draw
18
- super
19
- fill_rect([0,0,640,480], 0xffffffff, -2)
20
- end
21
- end
22
-
23
- class GameOfLife < Chingu::GameState
24
- CELL_SIZE = 4
25
- @@tick =0
26
- def initialize
27
- super
28
- @grid = generate_grid
29
-
30
- self.input={ :left_mouse_button => :start_painting,
31
- :released_left_mouse_button => :stop_painting,
32
- :right_mouse_button => :start_erasing,
33
- :releasedright_mouse_button => :stop_erasing,
34
- :z => :reset,
35
- :n => :update_grid,
36
- :space => :toggle_running,
37
- [:mouse_wheel_up,:left_arrow] => :prev_pattern,
38
- [:mouse_wheel_down,:right_arrow] => :next_pattern
39
- }
40
-
41
- @pattern = :pixel
42
- @pattern_nr = 0
43
- @painting = false
44
- @erasing = false
45
- @running = false
46
-
47
- @pattern_info = Chingu::Text.create(:x => 1, :y => 1, :size => 16, :color => Gosu::Color::BLACK)
48
- update_pattern_info
49
- end
50
-
51
- def update_pattern_info
52
- @pattern_info.text = "Current pattern: #{@pattern}. Change patterns with mousewheel."
53
- end
54
-
55
- def prev_pattern
56
- @pattern_nr -= 1
57
- @pattern_nr = PATTERNS.keys.size-1 if @pattern_nr < 0
58
- @pattern = PATTERNS.keys[@pattern_nr]
59
- update_pattern_info
60
- end
61
-
62
- def next_pattern
63
- @pattern_nr += 1
64
- @pattern_nr = 0 if @pattern_nr >= PATTERNS.keys.size
65
- @pattern = PATTERNS.keys[@pattern_nr]
66
- update_pattern_info
67
- end
68
-
69
- def draw_pattern_at_mouse(pattern = :pixel, to_grid = false)
70
- start_x = ($window.mouse_x/CELL_SIZE).floor
71
- y = ($window.mouse_y/CELL_SIZE).floor - 1
72
-
73
- PATTERNS[pattern].each_line do |line|
74
- x = start_x
75
- line.each_char do |char|
76
- @grid[x][y] = true if char == "o" && to_grid
77
- draw_cell(x, y) if char == "o"
78
- x += 1
79
- end
80
-
81
- y += 1
82
- end
83
- end
84
-
85
- def update
86
- super
87
-
88
- if @painting
89
- draw_pattern_at_mouse(@pattern, true)
90
- @painting = false if @running # Only put out pattern Once if game is running
91
- else
92
- draw_pattern_at_mouse(@pattern)
93
- end
94
-
95
- update_grid if @running
96
-
97
- $window.caption = "Conway Generation #{@@tick}. Start/Stop with 'Space'. Run 1 generation with 'N'. Reset with 'Z'."
98
- end
99
-
100
- def draw
101
- super
102
- draw_grid
103
- end
104
-
105
- private
106
-
107
- def generate_grid
108
- width = $window.width/CELL_SIZE
109
- height = $window.height/CELL_SIZE
110
-
111
- grid = Array.new(width)
112
- col = Array.new(height)
113
- col.map!{false}
114
- grid.map!{Array.new(col)}
115
- grid
116
- end
117
-
118
- def draw_grid
119
- @grid.each_with_index do |a,x|
120
- a.each_with_index do |c,y|
121
- if c === true
122
- $window.fill_rect([x*CELL_SIZE,y*CELL_SIZE,CELL_SIZE,CELL_SIZE],0xff000000,0)
123
- end
124
- end
125
- end
126
- end
127
-
128
- def reset
129
- @grid = generate_grid
130
- @@tick = 0
131
- @running = false
132
- end
133
-
134
- def update_grid
135
- @new_grid = Marshal.load(Marshal.dump(@grid))
136
-
137
- @grid.each_with_index do |a,x|
138
- a.each_with_index do |c,y|
139
- minus_x =x-1
140
- minus_y = y-1
141
- plus_x = x+1
142
- plus_y = y+1
143
- minus_x = @grid.length-1 if minus_x <0
144
- minus_y = a.length-1 if minus_y <0
145
- plus_y = 0 if plus_y >= a.length
146
- plus_x = 0 if plus_x >= @grid.length
147
-
148
- live_neighbours = 0
149
-
150
- @grid[minus_x][y] == true ? live_neighbours+=1 : nil
151
- @grid[plus_x][y] == true ? live_neighbours+=1 : nil
152
- @grid[x][minus_y] == true ? live_neighbours+=1 : nil
153
- @grid[x][plus_y] == true ? live_neighbours+=1 : nil
154
- @grid[minus_x][plus_y] == true ? live_neighbours+=1 : nil
155
- @grid[plus_x][minus_y] == true ? live_neighbours+=1 : nil
156
- @grid[minus_x][minus_y] == true ? live_neighbours+=1 : nil
157
- @grid[plus_x][plus_y] == true ? live_neighbours+=1 : nil
158
-
159
- case live_neighbours
160
- when 0..1 then @new_grid[x][y] = false
161
- when 2 then @new_grid[x][y] = true if @new_grid[x][y] == true
162
- when 3 then @new_grid[x][y] = true
163
- when 4..8 then @new_grid[x][y] = false
164
- end
165
-
166
- end
167
- end
168
-
169
- @grid = @new_grid
170
- @@tick+=1
171
- end
172
-
173
- def toggle_running
174
- @running = !@running
175
- end
176
-
177
- def start_painting; @painting = true; end
178
- def stop_painting; @painting = false; end
179
- def start_erasing; @erasing = true; end
180
- def stop_erasing; @erasing = false; end
181
-
182
- def draw_cell(x, y, color = 0xaaff0000)
183
- $window.fill_rect([x*CELL_SIZE,y*CELL_SIZE,CELL_SIZE,CELL_SIZE],0xaa0000ff,1)
184
- end
185
-
186
- end
187
-
188
-
189
- PATTERNS = Hash.new
190
-
191
- PATTERNS[:pixel] = %q{
192
- o
193
- }
194
-
195
- #
196
- # Spaceships
197
- #
198
- PATTERNS[:glider] = %q{
199
- ---o
200
- -o-o
201
- --oo
202
- }
203
-
204
- PATTERNS[:lightweight_spaceship] = %q{
205
- -oooo
206
- o---o
207
- ----o
208
- o--o-
209
- }
210
-
211
- #
212
- # Oscillators
213
- #
214
- PATTERNS[:blinker] = %q{
215
- ooo
216
- }
217
-
218
- PATTERNS[:beacon] = %q{
219
- -ooo
220
- ooo-
221
- }
222
-
223
- PATTERNS[:toad] = %q{
224
- oo--
225
- o---
226
- ---o
227
- --oo
228
- }
229
-
230
- PATTERNS[:pulsar] = %q{
231
- ---ooo---ooo--
232
- --------------
233
- -o----o-o----o
234
- -o----o-o----o
235
- -o----o-o----o
236
- ---ooo---ooo--
237
- --------------
238
- ---ooo---ooo--
239
- -o----o-o----o
240
- -o----o-o----o
241
- -o----o-o----o
242
- --------------
243
- ---ooo---ooo--
244
- }
245
-
246
- #
247
- # Guns
248
- #
249
- PATTERNS[:gospel_glider_gun] = %q{
250
- ------------------------o-----------
251
- ----------------------o-o-----------
252
- ------------oo------oo------------oo
253
- -----------o---o----oo------------oo
254
- oo--------o-----o---oo--------------
255
- oo--------o---o-oo----o-o-----------
256
- ----------o-----o-------o-----------
257
- -----------o---o--------------------
258
- ------------oo----------------------
259
- }
260
-
261
- PATTERNS[:block_laying_switch_engine] = %q{
262
- ----------o-o--
263
- oo-------o-----
264
- oo--------o--o-
265
- ------------ooo
266
- }
267
-
268
-
269
-
270
- #
271
- # Long lived patterns
272
- #
273
- PATTERNS[:rpentomino] = %q{
274
- --oo
275
- -oo
276
- --o
277
- }
278
-
279
- PATTERNS[:diehard] = %q{
280
- oo---o-o
281
- oo----o-
282
- ------o-
283
- }
284
-
285
- PATTERNS[:acorn] = %q{
286
- --o-----
287
- ----o---
288
- -oo--ooo
289
- }
290
-
1
+ #
2
+ # Conway's game of life in Gosu/Chingu
3
+ # http://toastymofo.blogspot.com/2010/06/conways-game-of-life-in-ruby-gosu.html
4
+ #
5
+ # Developed by r.kachowski ( http://www.toastymofo.net/ )
6
+ # Additions by ippa ( http://ippa.se/gaming )
7
+ #
8
+ require 'rubygems' rescue nil
9
+ $LOAD_PATH.unshift File.join(File.expand_path(__FILE__), "..", "..", "lib")
10
+ require 'chingu'
11
+
12
+ class Main < Chingu::Window
13
+ def initialize
14
+ super(640,480,false)
15
+ self.input={:esc=>:exit}
16
+ push_game_state(GameOfLife)
17
+ end
18
+ def draw
19
+ super
20
+ fill_rect([0,0,640,480], 0xffffffff, -2)
21
+ end
22
+ end
23
+
24
+ class GameOfLife < Chingu::GameState
25
+ CELL_SIZE = 4
26
+ @@tick =0
27
+ def initialize
28
+ super
29
+ @grid = generate_grid
30
+
31
+ self.input={ :left_mouse_button => :start_painting,
32
+ :released_left_mouse_button => :stop_painting,
33
+ :right_mouse_button => :start_erasing,
34
+ :released_right_mouse_button => :stop_erasing,
35
+ :z => :reset,
36
+ :n => :update_grid,
37
+ :space => :toggle_running,
38
+ [:mouse_wheel_up,:left_arrow] => :prev_pattern,
39
+ [:mouse_wheel_down,:right_arrow] => :next_pattern
40
+ }
41
+
42
+ @pattern = :pixel
43
+ @pattern_nr = 0
44
+ @painting = false
45
+ @erasing = false
46
+ @running = false
47
+
48
+ @pattern_info = Chingu::Text.create(:x => 1, :y => 1, :size => 16, :color => Gosu::Color::BLACK)
49
+ update_pattern_info
50
+ end
51
+
52
+ def update_pattern_info
53
+ @pattern_info.text = "Current pattern: #{@pattern}. Change patterns with mousewheel."
54
+ end
55
+
56
+ def prev_pattern
57
+ @pattern_nr -= 1
58
+ @pattern_nr = PATTERNS.keys.size-1 if @pattern_nr < 0
59
+ @pattern = PATTERNS.keys[@pattern_nr]
60
+ update_pattern_info
61
+ end
62
+
63
+ def next_pattern
64
+ @pattern_nr += 1
65
+ @pattern_nr = 0 if @pattern_nr >= PATTERNS.keys.size
66
+ @pattern = PATTERNS.keys[@pattern_nr]
67
+ update_pattern_info
68
+ end
69
+
70
+ def draw_pattern_at_mouse(pattern = :pixel, to_grid = false)
71
+ start_x = ($window.mouse_x/CELL_SIZE).floor
72
+ y = ($window.mouse_y/CELL_SIZE).floor - 1
73
+
74
+ PATTERNS[pattern].each_line do |line|
75
+ x = start_x
76
+ line.each_char do |char|
77
+ @grid[x][y] = true if char == "o" && to_grid
78
+ draw_cell(x, y) if char == "o"
79
+ x += 1
80
+ end
81
+
82
+ y += 1
83
+ end
84
+ end
85
+
86
+ def update
87
+ super
88
+
89
+ if @painting
90
+ draw_pattern_at_mouse(@pattern, true)
91
+ @painting = false if @running # Only put out pattern Once if game is running
92
+ else
93
+ draw_pattern_at_mouse(@pattern)
94
+ end
95
+
96
+ update_grid if @running
97
+
98
+ $window.caption = "Conway Generation #{@@tick}. Start/Stop with 'Space'. Run 1 generation with 'N'. Reset with 'Z'."
99
+ end
100
+
101
+ def draw
102
+ super
103
+ draw_grid
104
+ end
105
+
106
+ private
107
+
108
+ def generate_grid
109
+ width = $window.width/CELL_SIZE
110
+ height = $window.height/CELL_SIZE
111
+
112
+ grid = Array.new(width)
113
+ col = Array.new(height)
114
+ col.map!{false}
115
+ grid.map!{Array.new(col)}
116
+ grid
117
+ end
118
+
119
+ def draw_grid
120
+ @grid.each_with_index do |a,x|
121
+ a.each_with_index do |c,y|
122
+ if c === true
123
+ $window.fill_rect([x*CELL_SIZE,y*CELL_SIZE,CELL_SIZE,CELL_SIZE],0xff000000,0)
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ def reset
130
+ @grid = generate_grid
131
+ @@tick = 0
132
+ @running = false
133
+ end
134
+
135
+ def update_grid
136
+ @new_grid = Marshal.load(Marshal.dump(@grid))
137
+
138
+ @grid.each_with_index do |a,x|
139
+ a.each_with_index do |c,y|
140
+ minus_x =x-1
141
+ minus_y = y-1
142
+ plus_x = x+1
143
+ plus_y = y+1
144
+ minus_x = @grid.length-1 if minus_x <0
145
+ minus_y = a.length-1 if minus_y <0
146
+ plus_y = 0 if plus_y >= a.length
147
+ plus_x = 0 if plus_x >= @grid.length
148
+
149
+ live_neighbours = 0
150
+
151
+ @grid[minus_x][y] == true ? live_neighbours+=1 : nil
152
+ @grid[plus_x][y] == true ? live_neighbours+=1 : nil
153
+ @grid[x][minus_y] == true ? live_neighbours+=1 : nil
154
+ @grid[x][plus_y] == true ? live_neighbours+=1 : nil
155
+ @grid[minus_x][plus_y] == true ? live_neighbours+=1 : nil
156
+ @grid[plus_x][minus_y] == true ? live_neighbours+=1 : nil
157
+ @grid[minus_x][minus_y] == true ? live_neighbours+=1 : nil
158
+ @grid[plus_x][plus_y] == true ? live_neighbours+=1 : nil
159
+
160
+ case live_neighbours
161
+ when 0..1 then @new_grid[x][y] = false
162
+ when 2 then @new_grid[x][y] = true if @new_grid[x][y] == true
163
+ when 3 then @new_grid[x][y] = true
164
+ when 4..8 then @new_grid[x][y] = false
165
+ end
166
+
167
+ end
168
+ end
169
+
170
+ @grid = @new_grid
171
+ @@tick+=1
172
+ end
173
+
174
+ def toggle_running
175
+ @running = !@running
176
+ end
177
+
178
+ def start_painting; @painting = true; end
179
+ def stop_painting; @painting = false; end
180
+ def start_erasing; @erasing = true; end
181
+ def stop_erasing; @erasing = false; end
182
+
183
+ def draw_cell(x, y, color = 0xaaff0000)
184
+ $window.fill_rect([x*CELL_SIZE,y*CELL_SIZE,CELL_SIZE,CELL_SIZE],0xaa0000ff,1)
185
+ end
186
+
187
+ end
188
+
189
+
190
+ PATTERNS = Hash.new
191
+
192
+ PATTERNS[:pixel] = %q{
193
+ o
194
+ }
195
+
196
+ #
197
+ # Spaceships
198
+ #
199
+ PATTERNS[:glider] = %q{
200
+ ---o
201
+ -o-o
202
+ --oo
203
+ }
204
+
205
+ PATTERNS[:lightweight_spaceship] = %q{
206
+ -oooo
207
+ o---o
208
+ ----o
209
+ o--o-
210
+ }
211
+
212
+ #
213
+ # Oscillators
214
+ #
215
+ PATTERNS[:blinker] = %q{
216
+ ooo
217
+ }
218
+
219
+ PATTERNS[:beacon] = %q{
220
+ -ooo
221
+ ooo-
222
+ }
223
+
224
+ PATTERNS[:toad] = %q{
225
+ oo--
226
+ o---
227
+ ---o
228
+ --oo
229
+ }
230
+
231
+ PATTERNS[:pulsar] = %q{
232
+ ---ooo---ooo--
233
+ --------------
234
+ -o----o-o----o
235
+ -o----o-o----o
236
+ -o----o-o----o
237
+ ---ooo---ooo--
238
+ --------------
239
+ ---ooo---ooo--
240
+ -o----o-o----o
241
+ -o----o-o----o
242
+ -o----o-o----o
243
+ --------------
244
+ ---ooo---ooo--
245
+ }
246
+
247
+ #
248
+ # Guns
249
+ #
250
+ PATTERNS[:gospel_glider_gun] = %q{
251
+ ------------------------o-----------
252
+ ----------------------o-o-----------
253
+ ------------oo------oo------------oo
254
+ -----------o---o----oo------------oo
255
+ oo--------o-----o---oo--------------
256
+ oo--------o---o-oo----o-o-----------
257
+ ----------o-----o-------o-----------
258
+ -----------o---o--------------------
259
+ ------------oo----------------------
260
+ }
261
+
262
+ PATTERNS[:block_laying_switch_engine] = %q{
263
+ ----------o-o--
264
+ oo-------o-----
265
+ oo--------o--o-
266
+ ------------ooo
267
+ }
268
+
269
+
270
+
271
+ #
272
+ # Long lived patterns
273
+ #
274
+ PATTERNS[:rpentomino] = %q{
275
+ --oo
276
+ -oo
277
+ --o
278
+ }
279
+
280
+ PATTERNS[:diehard] = %q{
281
+ oo---o-o
282
+ oo----o-
283
+ ------o-
284
+ }
285
+
286
+ PATTERNS[:acorn] = %q{
287
+ --o-----
288
+ ----o---
289
+ -oo--ooo
290
+ }
291
+
291
292
  Main.new.show