kame 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "gosu"
4
+
5
+ group :development do
6
+ gem "bundler", "~> 1.0.0"
7
+ gem "jeweler", "~> 1.6.4"
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ gosu (0.7.38)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.0.0)
17
+ gosu
18
+ jeweler (~> 1.6.4)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Andy Pike
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ kame (Japanese for turtle)
2
+ ==========================
3
+
4
+ Kame is an implementation of the Logo programming language built as a challenge to myself. I wanted a simple way to introduce programming in Ruby to my daughter and thought this would be fun.
5
+
6
+ With Kame, you control the movement of a turtle around a piece of paper by programming commands. With these simple commands your turtle will draw you a picture.
7
+
8
+ It's a nice introduction to programming for kids and will also help their maths :o)
9
+
10
+ What it does
11
+ ------------
12
+
13
+ You create a Ruby file with commands that instructs a turtle (kame) to move around. You can tell the turtle to move forward or backward, rotate, put his pen down on the paper or pick it up. You can also change the colour of the pen and background.
14
+
15
+ The turtle starts in the top left corner of the kame window and will move from there depending on your commands.
16
+
17
+ Each command is on a new line and they are executed in order.
18
+
19
+ As this is using ruby, you also have access to Ruby constructs such as loops and functions.
20
+
21
+ Install
22
+ -------
23
+
24
+ gem install kame
25
+
26
+ Simple Sample
27
+ -------------
28
+
29
+ Create a new text file called square.rb and paste the below code in and save it:
30
+
31
+ require "kame"
32
+
33
+ Kame.new do
34
+ forward 100
35
+ turn_right 90
36
+ forward 100
37
+
38
+ colour :red
39
+ pen_down
40
+
41
+ 4.times do
42
+ turn_right 90
43
+ forward 50
44
+ end
45
+ end
46
+
47
+ Running
48
+ -------
49
+
50
+ In the console use this:
51
+
52
+ ruby square.rb
53
+
54
+ Commands
55
+ --------
56
+
57
+ To move the turtle forward by 50 pixels from it's current position, you can use the `forward` command:
58
+
59
+ forward 50
60
+
61
+ To move the turtle backward by 75 pixels from it's current position, you can use the `backward` command:
62
+
63
+ backward 75
64
+
65
+ To tell the turtle to put his pen down onto the paper use the `pen_down` command:
66
+
67
+ pen_down
68
+
69
+ To tell the turtle to pick his pen up again, use the `pen_up` command:
70
+
71
+ pen_up
72
+
73
+ You can ask the turtle to rotate 90 degrees to the left (anti-clockwise) on the spot without moving by using the `turn_left` command:
74
+
75
+ turn_left 90
76
+
77
+ You can ask the turtle to rotate 90 degrees to the right (clockwise) on the spot without moving by using the `turn_right` command:
78
+
79
+ turn_right 90
80
+
81
+ You can change the colour of the turtle's pen at any time (on the paper or not) with the `colour` command:
82
+
83
+ colour :pink
84
+
85
+ Colours
86
+ -------
87
+
88
+ Here is a list of the available colours:
89
+
90
+ :white
91
+ :black
92
+ :red
93
+ :green
94
+ :blue
95
+ :yellow
96
+ :pink
97
+ :grey
98
+
99
+ Options
100
+ -------
101
+
102
+ In addition to commanding the turtle, there are some options you can specify. You pass these options to the Kame constructor like so:
103
+
104
+ Kame.new(:width => 800, :height => 600) do
105
+ ...
106
+ end
107
+
108
+ Here is a list of all options with their default values (all are optional):
109
+
110
+ :width => 640 # The width of the paper in pixels
111
+ :height => 480 # The height of the paper in pixels
112
+ :paper => :white # The colour of the paper (from the list above)
113
+ :title => 'Kame' # The window title
114
+ :speed => 10 # The speed that the turtle draws. This is how many lines per second should be drawn
115
+
116
+
117
+ Contributing to kame
118
+ --------------------
119
+
120
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
121
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
122
+ * Fork the project
123
+ * Start a feature/bugfix branch
124
+ * Commit and push until you are happy with your contribution
125
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
126
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
127
+
128
+ Copyright
129
+ ---------
130
+
131
+ Copyright (c) 2011 Andy Pike. See LICENSE.txt for further details.
132
+
133
+
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "kame"
18
+ gem.homepage = "http://github.com/andypike/kame"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Kame (Japanese for turtle) is an implementation of the Logo programming language for kids}
21
+ gem.description = %Q{Kame (Japanese for turtle) is an implementation of the Logo programming language built as a challenge to myself. I wanted a simple way to introduce programming in Ruby to my daughter and thought this would be fun. With Kame, you control the movement of a turtle around the screen by programming commands. With these simple commands your turtle will draw you a picture. It's a nice introduction to programming for kids and will also help their maths :o)}
22
+ gem.authors = ["Andy Pike"]
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ task :default => :test
35
+
36
+ require 'rake/rdoctask'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "kame #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
data/lib/kame.rb ADDED
@@ -0,0 +1,77 @@
1
+ require 'kame/kame_window'
2
+ require 'kame/kame_colours'
3
+
4
+ class Kame
5
+ def initialize(options = {}, &block)
6
+ @lines = []
7
+ @state = {
8
+ :pen_down => false,
9
+ :x => 0,
10
+ :y => 0,
11
+ :rotation => 0,
12
+ :line_colour => KameColours::lookup[:white]
13
+ }
14
+
15
+ opts = {
16
+ :width => 640,
17
+ :height => 480,
18
+ :paper => :white,
19
+ :title => 'Kame',
20
+ :speed => 10
21
+ }.merge(options)
22
+
23
+ instance_eval(&block)
24
+
25
+ window = KameWindow.new(@lines, opts).show
26
+ end
27
+
28
+ def pen_up
29
+ @state[:pen_down] = false
30
+ end
31
+
32
+ def pen_down
33
+ @state[:pen_down] = true
34
+ end
35
+
36
+ def pen_down?
37
+ @state[:pen_down]
38
+ end
39
+
40
+ def turn_left(degrees)
41
+ @state[:rotation] -= degrees
42
+ end
43
+
44
+ def turn_right(degrees)
45
+ @state[:rotation] += degrees
46
+ end
47
+
48
+ def colour(colour)
49
+ @state[:line_colour] = KameColours::lookup[colour]
50
+ end
51
+
52
+ def forward(distance)
53
+ theta = @state[:rotation].degrees_to_radians
54
+ x = @state[:x] + distance * Math::cos(theta)
55
+ y = @state[:y] + distance * Math::sin(theta)
56
+
57
+ if pen_down?
58
+ @lines << {
59
+ :colour => @state[:line_colour],
60
+ :from => { :x => @state[:x], :y => @state[:y] },
61
+ :to => { :x => x, :y => y }
62
+ }
63
+ end
64
+
65
+ @state[:x] = x
66
+ @state[:y] = y
67
+ end
68
+
69
+ def backward(distance)
70
+ self.forward(-distance)
71
+ end
72
+
73
+ def method_missing(method_name, *args)
74
+ puts "Sorry, but I don't know what '#{method_name}' means?"
75
+ end
76
+ end
77
+
@@ -0,0 +1,14 @@
1
+ class KameColours
2
+ def self.lookup
3
+ {
4
+ :white => 0xffffffff,
5
+ :black => 0xff000000,
6
+ :red => 0xffff0000,
7
+ :green => 0xff00ff00,
8
+ :blue => 0xff0000ff,
9
+ :yellow => 0xffffff00,
10
+ :pink => 0xffff00ff,
11
+ :grey => 0xff808080
12
+ }
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ require 'gosu'
2
+
3
+ class KameWindow < Gosu::Window
4
+ def initialize(lines, options)
5
+ @width = options[:width]
6
+ @height = options[:height]
7
+ @background_colour = Gosu::Color.argb(KameColours::lookup[options[:paper]])
8
+ @speed = options[:speed]
9
+ @lines = lines
10
+ @first_frame = Gosu::milliseconds
11
+
12
+ super(@width, @height, false)
13
+ self.caption = options[:title]
14
+ end
15
+
16
+ def draw_background
17
+ draw_quad(0, 0, @background_colour,
18
+ @width, 0, @background_colour,
19
+ 0, @height, @background_colour,
20
+ @width, @height, @background_colour,
21
+ 0)
22
+ end
23
+
24
+ def update
25
+ @this_frame = Gosu::milliseconds
26
+ @seconds_since_last_frame = (@this_frame - @first_frame) / 1000.0
27
+ end
28
+
29
+ def draw
30
+ self.draw_background
31
+ return if @lines.nil? || @lines.count == 0
32
+
33
+ max = (@seconds_since_last_frame * @speed).round
34
+ if max > @lines.count - 1
35
+ max = @lines.count - 1
36
+ end
37
+
38
+ 0.upto(max).each do |i|
39
+ line = @lines[i]
40
+ line_colour = Gosu::Color.argb(line[:colour])
41
+ draw_line(line[:from][:x], line[:from][:y], line_colour, line[:to][:x], line[:to][:y], line_colour, 1)
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kame
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.3.0
6
+ platform: ruby
7
+ authors:
8
+ - Andy Pike
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-11-10 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: gosu
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.6.4
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ description: Kame (Japanese for turtle) is an implementation of the Logo programming language built as a challenge to myself. I wanted a simple way to introduce programming in Ruby to my daughter and thought this would be fun. With Kame, you control the movement of a turtle around the screen by programming commands. With these simple commands your turtle will draw you a picture. It's a nice introduction to programming for kids and will also help their maths :o)
49
+ email:
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - LICENSE.txt
56
+ - README.md
57
+ files:
58
+ - .document
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - LICENSE.txt
62
+ - README.md
63
+ - Rakefile
64
+ - VERSION
65
+ - lib/kame.rb
66
+ - lib/kame/kame_colours.rb
67
+ - lib/kame/kame_window.rb
68
+ homepage: http://github.com/andypike/kame
69
+ licenses:
70
+ - MIT
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: -1997320679435807352
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ requirements: []
92
+
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.10
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Kame (Japanese for turtle) is an implementation of the Logo programming language for kids
98
+ test_files: []
99
+