Skip to content

Commit 623abba

Browse files
author
Fela Winkelmolen
committed
turtle documentation, and some little "API" change
1 parent f866b1d commit 623abba

4 files changed

Lines changed: 91 additions & 9 deletions

File tree

h-ety-h/turtlescript.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
class Shoes::TurtleCanvas < Shoes::Widget
44
# default values
5-
WIDTH = 400
6-
HEIGHT = 400
5+
WIDTH = 500
6+
HEIGHT = 500
77
SPEED = 4 # power of two
88

99
include Math
@@ -12,6 +12,7 @@ class Shoes::TurtleCanvas < Shoes::Widget
1212
# para with the next command written on it
1313
attr_writer :next_command, :pen_info
1414
attr_accessor :speed # powers of two
15+
attr_reader :width, :height
1516

1617
def initialize
1718
@width = WIDTH
@@ -87,15 +88,15 @@ def pendown
8788
is_step
8889
@pendown = true
8990
end
90-
def isdown?
91+
def pendown?
9192
return @pendown
9293
end
93-
def go x, y
94+
def goto x, y
9495
is_step
9596
update_position(x, y)
9697
end
9798
def center
98-
go(200, 200)
99+
go(width/2, height/2)
99100
end
100101
def setx x
101102
is_step
@@ -115,7 +116,9 @@ def getposition
115116
[@x, @y]
116117
end
117118
def getheading
118-
@heading
119+
degs = @heading/DEG
120+
degs += 180
121+
degs % 360
119122
end
120123

121124
### user commands already in shoes (the first two with another name ###

samples/turtle_barbwire.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
background yellow
66
pencolor brown
77
pensize 2
8-
go 30, 200
8+
goto 30, 200
99
setheading 180
1010
1000.times do
1111
forward 20

samples/turtle_stars.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
pensize 2
66
pencolor yellow
77
30.times do
8-
go rand(400), rand(400)
8+
goto rand(width), rand(height)
99
setheading rand(360)
1010
len = 5 + rand(30)
1111
5.times do
1212
forward len
1313
turnleft 180-360/10
1414
end
1515
end
16-
end
16+
end

turtle-graphics

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Turtle Graphics support (http://en.wikipedia.org/wiki/Turtle_graphics)
2+
3+
Turtle code can be run from the HacketyHack editor, two modes are available:
4+
5+
1. interactive mode, allowing step by step execution
6+
7+
Turtle.start do
8+
# turtle code here
9+
end
10+
11+
12+
2. drawing mode that execute the whole code at once
13+
14+
Turtle.draw do
15+
# turtle code here
16+
end
17+
18+
19+
== Available Commands ==
20+
penup
21+
after this command gets called the pen is up and nothing is drawn
22+
23+
pendown
24+
after this command the pen is dawn and lines are drawn as the turtle moves
25+
26+
pendown?
27+
returns true if the pen is down, (default is true)
28+
29+
pensize x
30+
set the width of the line drawn by the turtle to x
31+
32+
pencolor c
33+
set the color of the line drawn by the turtle to c, use the shoes color system
34+
35+
background c
36+
set the background color to c, use the shoes color system
37+
38+
forward x
39+
moves the turtle forward by x pixels
40+
when no options are given a default argument of 50 pixels is used
41+
42+
backward x
43+
moves the turtle forward by x pixels, equivalent to forward -x
44+
when no options are given a default argument of 50 pixels is used
45+
46+
turnleft a
47+
turns the turtle heading a degrees to the left, default heading is 0 degrees
48+
when no options are given a default argument of 90 degrees is used
49+
50+
turnright a
51+
turns the turtle heading a degrees to the right, equivalent to turnleft -a
52+
when no options are given a default argument of 90 degrees is used
53+
54+
setheading a
55+
sets the current heading to a
56+
57+
getheading
58+
retuns the current heading in degrees
59+
60+
setx x
61+
moves the turtle horizontally to (current_y, x)
62+
doesn't draw anything
63+
64+
sety y
65+
moves the turtle vertically to (y, current_x)
66+
doesn't draw anything
67+
68+
goto x, y
69+
moves the turtle to (x, y)
70+
doesn't draw anything
71+
72+
getposition
73+
retuns [x, y] (as an array)
74+
75+
clear
76+
removes all content (but does not change the turtle attributes)
77+
78+
reset
79+
removes all content and resets all attributes (including turtle position)

0 commit comments

Comments
 (0)