PicoTurtle Java program to draw a groovy tunnel

I’ve also added Java support to picoturtle. Here’s a program to draw a rather groovy tunnel. Java code follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import in.abhishekmishra.picoturtle.Turtle;
import in.abhishekmishra.picoturtle.TurtleState;

public class Tunnel
{
public static void cuboid(Turtle t, double b, double w, double h) {
t.forward(b);

t.right(-45);
t.forward(h);
t.back(h);
t.left(-45);

t.left(90);
t.forward(w);

t.left(-45);
t.forward(h);
t.back(h);
t.right(-45);

t.left(90);
t.forward(b);

t.left(-135);
t.forward(h);
t.back(h);
t.right(-135);

t.left(90);
t.forward(w);

t.right(-135);
t.forward(h);
t.left(-135);

t.left(90);
t.forward(b);
t.left(90);
t.forward(w);
t.left(90);
t.forward(b);
t.left(90);
t.forward(w);
t.pendown();
}

public static void main(String[] args)
{
// Create the turtle before using
Turtle t = Turtle.CreateTurtle(args);

if (t != null) {
// Your code goes here

t.penup();
t.setx(300);
t.sety(200);
t.heading(100);
TurtleState ts = t.state();

for (double i = 1; i < 50; i+=1.5) {
double b = i*8, w = i*8, h = i/2;
t.right(90);
t.back(b/2);

t.left(90);
t.back(w/2);

t.heading(i*0.7);
t.pendown();
t.penwidth(1);
t.pencolour((int)(4*i), (int)(255 - 3*i), (int)(5*i));
cuboid(t, b, w, h);

t.penup();
t.heading(ts.angle);
t.setx(ts.location.x);
t.sety(ts.location.y);
}

// Your code ends here

// Always stop the turtle
t.stop();
}
else {
System.out.println("Error: Unable to create a turtle.");
System.exit(-1);
}
}
}