drawille 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c3a60513c65dfe590e9d25f8b6929b8fb432d27f
4
- data.tar.gz: 96efb58c4b3f7065034eb85896b6d65112d0093d
3
+ metadata.gz: 5e6311a9b8b5dd753dffe89b683848908ccc50df
4
+ data.tar.gz: 64893d8e4442064594b8d7dd70390157146a3f1b
5
5
  SHA512:
6
- metadata.gz: 122d8f9f4acf3098296f3750fadc27ec21531b9fa8045ec109791cf3ac4ceff91321f512ef34dee6567dbaa1680d347bd41bb70b52a534df54fd2ba844de9e9c
7
- data.tar.gz: 5f8f46911c99626d64c1ed150714031c98fb7acf7ab000b43aed174c92e87ffc2855fd023a3d5dde7945c7b491f4b5f03961a92b130711b7259bf2eebff2b886
6
+ metadata.gz: 0a8f0fee2e35301f93f456cca16790aee012034788d2f575973c62ef48d91778dbf7183c5ee7ffe4c43686a93a712a13cd2c3bcce78ee9bb4a1abf083b607307
7
+ data.tar.gz: 76071e523e1059943658349ae1c268c7ecc6641f09f872f1d0cbed0ed738239b21e9177f795be1defd685134fcf0e3728850b93a7ad44a6f70412ec759ce4fc4
data/README.md CHANGED
@@ -69,6 +69,10 @@ puts canvas.frame
69
69
 
70
70
  ![Stencil](docs/images/stencil.gif)
71
71
 
72
+ With a "flipbook" you can also create animations on your terminal.
73
+
74
+ ![Conway](docs/images/conway.gif)
75
+
72
76
  This implementation also includes a [Turtle graphics](http://en.wikipedia.org/wiki/Turtle_graphics) API for all your beloved fractals:
73
77
 
74
78
  ```ruby
@@ -190,7 +194,9 @@ Saves a snapshot of the current state of the canvas.
190
194
 
191
195
  ``FlipBook#snapshot#play``
192
196
 
193
- Will render the animation on the terminal. The method also takes an option hash with the options ``:repeat`` ``:fps``.
197
+ Will render the animation on the terminal. The method also takes an option hash with the options ``:repeat`` ``:fps``.
198
+
199
+ As an alternative to snapshots it is possible to pass a block which will be called consecutively and should return a canvas which will be rendered as a frame in the animation or ``nil`` to stop the animation.
194
200
 
195
201
  ## License
196
202
 
Binary file
@@ -0,0 +1,82 @@
1
+ require 'drawille'
2
+ require 'curses'
3
+
4
+ class Conway
5
+
6
+ OFFSET_MATRIX = [[-1,-1], [0, -1], [1, -1],
7
+ [-1, 0], [1, 0],
8
+ [-1, 1], [0, 1], [1, 1]]
9
+
10
+ def initialize height, width
11
+ @height = height
12
+ @width = width
13
+
14
+ @world = initialize_world { |x, y| rand(0..1) }
15
+ end
16
+
17
+ def initialize_world
18
+ Array.new(@width) { |x| Array.new(@height) { |y| yield x, y }}
19
+ end
20
+
21
+ def alive_neighbours x, y
22
+ OFFSET_MATRIX.reduce(0) do |memo, offset|
23
+ x_offset = x + offset[0]
24
+ y_offset = y + offset[1]
25
+
26
+ memo += @world[x_offset][y_offset] if inside_range? x_offset, y_offset
27
+ memo
28
+ end
29
+ end
30
+
31
+ def evolve
32
+ next_world = initialize_world { |x, y| 0 }
33
+ each do |x, y|
34
+ alive_neighbours = alive_neighbours x, y
35
+ next_world[x][y] =
36
+ if cell_alive? x, y
37
+ (2..3).include?(alive_neighbours) ? 1 : 0
38
+ else
39
+ alive_neighbours == 3 ? 1 : 0
40
+ end
41
+ end
42
+ @world = next_world
43
+ end
44
+
45
+ def cell_alive? x, y
46
+ @world[x][y] == 1
47
+ end
48
+
49
+ def inside_range? x, y
50
+ (0...@height).include?(y) && (0...@width).include?(x)
51
+ end
52
+
53
+ def to_canvas
54
+ canvas = Drawille::Canvas.new
55
+ each do |x, y|
56
+ canvas.set(x, y) if @world[x][y] > 0
57
+ end
58
+ canvas
59
+ end
60
+
61
+ private
62
+
63
+ def each
64
+ (0...@height).each do |y|
65
+ (0...@width).each do |x|
66
+ yield x, y
67
+ end
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ Curses::init_screen()
74
+
75
+ conway = Conway.new Curses.lines * 4, Curses.cols * 2 - 2
76
+ flipbook = Drawille::FlipBook.new
77
+
78
+ flipbook.play do
79
+ canvas = conway.to_canvas
80
+ conway.evolve
81
+ canvas
82
+ end
@@ -21,7 +21,7 @@ module Drawille
21
21
  end
22
22
 
23
23
  def each_frame options={}
24
- return enum_for(:each_frame) unless block_given?
24
+ return enum_for(__callee__) unless block_given?
25
25
  @snapshots.each do |frame|
26
26
  yield frame
27
27
  end
@@ -1,6 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
- require 'pry'
4
3
  module Drawille
5
4
 
6
5
  module Frameable
@@ -1,3 +1,3 @@
1
1
  module Drawille
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drawille
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Skirzynski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-04 00:00:00.000000000 Z
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: curses
@@ -49,10 +49,12 @@ files:
49
49
  - LICENSE.txt
50
50
  - README.md
51
51
  - Rakefile
52
+ - docs/images/conway.gif
52
53
  - docs/images/mn-gon.gif
53
54
  - docs/images/sinus.gif
54
55
  - docs/images/stencil.gif
55
56
  - drawille.gemspec
57
+ - examples/conway.rb
56
58
  - examples/mn-gon.rb
57
59
  - examples/stencil-1.png
58
60
  - examples/stencil-2.jpg
@@ -86,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
88
  version: '0'
87
89
  requirements: []
88
90
  rubyforge_project:
89
- rubygems_version: 2.2.2
91
+ rubygems_version: 2.4.5
90
92
  signing_key:
91
93
  specification_version: 4
92
94
  summary: Drawing in terminal with Unicode Braille characters.