chingu 0.7.7.5 → 0.8rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +1 -1
- data/Rakefile +4 -2
- data/chingu.gemspec +25 -10
- data/examples/example10_traits_retrofy.rb +3 -2
- data/examples/example11_animation.rb +3 -2
- data/examples/example12_trait_timer.rb +3 -2
- data/examples/example13_high_scores.rb +3 -2
- data/examples/example14_bounding_box_circle.rb +6 -5
- data/examples/example15_trait_timer2.rb +3 -2
- data/examples/example16_online_high_scores.rb +3 -2
- data/examples/example17_gosu_tutorial.rb +104 -103
- data/examples/example18_animation_trait.rb +3 -2
- data/examples/example19_edit_viewport.rb +3 -2
- data/examples/example1_basics.rb +3 -2
- data/examples/example20_trait_inheritence_test.rb +3 -2
- data/examples/example21.yml +306 -306
- data/examples/example21_sidescroller_with_edit.rb +3 -2
- data/examples/example22_text.rb +3 -2
- data/examples/example23_chipmunk.rb +3 -2
- data/examples/example24_enter_name.rb +19 -0
- data/examples/example24_input_codes.rb +41 -0
- data/examples/example25.yml +625 -0
- data/examples/example25_fibers_state_machine.rb +167 -0
- data/examples/example2_gamestate_basics.rb +3 -2
- data/examples/example3_parallax.rb +3 -2
- data/examples/example4_gamestates.rb +3 -2
- data/examples/example5_gamestates_in_pure_gosu.rb +3 -2
- data/examples/example6_transitional_game_state.rb +3 -2
- data/examples/example7_gfx_helpers.rb +3 -2
- data/examples/example8_traits.rb +3 -2
- data/examples/example9_collision_detection.rb +3 -2
- data/examples/game1.rb +3 -2
- data/examples/game_of_life.rb +291 -290
- data/lib/chingu.rb +6 -1
- data/lib/chingu/animation.rb +3 -2
- data/lib/chingu/game_object.rb +7 -1
- data/lib/chingu/game_object_map.rb +6 -12
- data/lib/chingu/game_states/edit.rb +63 -66
- data/lib/chingu/game_states/enter_name.rb +97 -0
- data/lib/chingu/helpers/game_object.rb +10 -21
- data/lib/chingu/input.rb +2 -0
- data/lib/chingu/online_high_score_list.rb +1 -4
- data/lib/chingu/rect.rb +2 -1
- data/lib/chingu/window.rb +10 -1
- data/spec/chingu/game_object_spec.rb +22 -22
- data/spec/chingu/input_spec.rb +22 -0
- metadata +48 -22
- 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
|
-
|
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/example8_traits.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require 'rubygems'
|
3
|
-
|
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
|
-
|
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
|
-
|
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
|
data/examples/game_of_life.rb
CHANGED
@@ -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 '
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
:
|
33
|
-
:
|
34
|
-
:
|
35
|
-
:
|
36
|
-
:
|
37
|
-
|
38
|
-
[:
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
@
|
43
|
-
@
|
44
|
-
@
|
45
|
-
@
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
@pattern_nr
|
58
|
-
@
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
@pattern_nr
|
65
|
-
@
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
x
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
@
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
col.
|
114
|
-
|
115
|
-
grid
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
@grid[
|
152
|
-
@grid[
|
153
|
-
@grid[x][
|
154
|
-
@grid[
|
155
|
-
@grid[
|
156
|
-
@grid[
|
157
|
-
@grid[
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
when
|
162
|
-
when
|
163
|
-
when
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
def
|
179
|
-
def
|
180
|
-
def
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
#
|
197
|
-
#
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
o
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
#
|
213
|
-
#
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
ooo
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
---
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
-o----o-o----o
|
235
|
-
-o----o-o----o
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
-o----o-o----o
|
241
|
-
-o----o-o----o
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
#
|
248
|
-
#
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
oo--------o---
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
oo
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
#
|
272
|
-
#
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
oo
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
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
|