-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsnake.pde
88 lines (72 loc) · 1.98 KB
/
snake.pde
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
// example from the Processing.js library
// interactive snake cursor exploring a canvas image
// store mouse position & image
float[] x = new float[20];
float[] y = new float[20];
float segLength = 10;
PImage img;
// set up the screen & background image
void setup(){
size(320, 240);
smooth();
img = loadImage("http://processingjs.org/learning/custom/snake/data/dirt.jpg");
}
// update the screen with the cursor position
void draw(){
// redraw background and image each time
background(226);
image(img, 0, 0);
// draw a snake with respect to the mouse movement
// & adjust the positions across the array
dragSegment(0, mouseX - 8, mouseY - 8);
for(int i = 0; i < (x.length - 1); i++)
dragSegment(i + 1, x[i], y[i]);
}
// draw a piece of the snake
void dragSegment(int i, float xin, float yin){
// calculate the difference between the next position and this one
float dx = xin - x[i];
float dy = yin - y[i];
// calculate the angle for this change
float angle = atan2(dy, dx);
// store this value as the current position
x[i] = xin - cos(angle) * segLength;
y[i] = yin - sin(angle) * segLength;
// save the viewing matrix
pushMatrix();
// move to the proper segment position
translate(x[i], y[i]);
rotate(angle);
// define the snake segment color (based on order)
// either black, yellow, or red!
color c;
if(i % 3 == 1)
c = color(0, 0, 0, 255);
else if(i % 3 == 2)
c = color(255, 255, 0, 255);
else
c = color(255, 0, 0, 255);
// draw the segment of the snake
stroke(c);
strokeWeight(10);
line(0, 0, segLength, 0);
// draw the snake's tail
if(i == x.length - 1){
fill( c );
noStroke();
beginShape(TRIANGLES);
vertex(0, 5);
vertex(-2 * segLength, 0);
vertex(0, -5);
endShape();
}
// draw the snake's eyes
if(i == 0){
noStroke();
fill(0, 255);
ellipse(segLength, -2, 3, 3);
ellipse(segLength, 2, 3, 3);
}
// reset the viewing matrix
popMatrix();
}