forked from CodingTrain/Logo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle.js
44 lines (37 loc) · 867 Bytes
/
turtle.js
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
class Turtle {
constructor(x, y, angle) {
this.x = x;
this.y = y;
this.homeX = x;
this.homeY = y;
this.prevX = x;
this.prevY = y;
this.dir = angle;
this.strokeColor = 255;
this.strokeWeight = 1;
}
reset() {
this.pen = true;
}
forward(amt) {
// Move the turtle
this.x += cos(this.dir) * amt;
this.y += sin(this.dir) * amt;
// If the pen is down we should draw a line from the previous position to the new position
if (this.pen) {
stroke(this.strokeColor);
strokeWeight(this.strokeWeight);
line(this.prevX, this.prevY, this.x, this.y);
}
// The current position will become the next previous position
this.prevX = this.x;
this.prevY = this.y;
}
right(angle) {
this.dir += angle;
}
home() {
this.x = this.homeX;
this.y = this.homeY;
}
}