forked from CodingTrain/Logo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtle.js
More file actions
39 lines (34 loc) · 701 Bytes
/
turtle.js
File metadata and controls
39 lines (34 loc) · 701 Bytes
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
class Turtle {
constructor(x, y, angle) {
this.x = x;
this.y = y;
this.homeX = x;
this.homeY = y;
this.dir = angle;
this.strokeColor = 255;
this.strokeWeight = 1;
}
reset() {
translate(this.x, this.y);
rotate(this.dir);
this.pen = true;
}
forward(amt) {
if (this.pen) {
stroke(this.strokeColor);
strokeWeight(this.strokeWeight);
line(0, 0, amt, 0);
}
translate(amt, 0);
this.x += Math.cos(this.dir * Math.PI / 180) * amt;
this.y += Math.sin(this.dir * Math.PI / 180) * amt;
}
right(angle) {
rotate(angle);
this.dir += angle;
}
home() {
this.x = this.homeX;
this.y = this.homeY;
}
}