-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.pde
More file actions
319 lines (257 loc) · 5.41 KB
/
sketch.pde
File metadata and controls
319 lines (257 loc) · 5.41 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
Serpent s;
GestureDetector g;
Rat r;
void setup () {
fullScreen();
background(0);
s= new Serpent();
g= new GestureDetector();
r= new Rat();
}
void draw() {
if (s.moving) {
s.move();
}
//background(0);
fill(255);
s.display();
r.display();
if (s.getSegments()!=null) {
for (Segment seg : s.getSegments()) {
fill(color(255, 175, 150));
seg.display();
seg.move();
noFill();
}
}
}
void mousePressed() {
g.initial();
}
void mouseReleased() {
g.fin();
if (s.moving == false) {
s.moving = true;
}
}
class Serpent {
PVector location;
PVector velocity;
int speed = 10;
boolean moving = false;
int lastSwipe = 0;
ArrayList<Pivot> pivots;
ArrayList<Segment> segments;
Serpent() {
location = new PVector(width/2, height/2);
velocity = new PVector(0, 0);
segments = new ArrayList();
pivots = new ArrayList();
}
void display() {
rect(this.location.x,
this.location.y,
50,
50);
}
void eat( ) {
//todo add segment parameters
Segment segment = new Segment(reportHeadLocation());
segments.add(segment);
// text("segment added", width-500, 100);
r.move();
}
void move() {
//todo handle arraylist of segments
switch(lastSwipe) {
case 0:
case 1:
velocity = new PVector(0, -speed);
this.location.add(velocity);
// s.pivots.add( new Pivot(s.reportHeadLocation(), s.reportHeadVelocity()));
text("pivot added", 100, 200);
break;
case 2:
velocity = new PVector(0, speed);
this.location.add(velocity);
// s.pivots.add( new Pivot(s.reportHeadLocation(), s.reportHeadVelocity()));
text("pivot added", 100, 200);
break;
case 3:
velocity = new PVector(-speed, 0);
this.location.add(velocity);
// s.pivots.add( new Pivot(s.reportHeadLocation(), s.reportHeadVelocity()));
text("pivot added", 100, 200);
break;
case 4:
velocity = new PVector(speed, 0);
this.location.add(velocity);
// s.pivots.add( new Pivot(s.reportHeadLocation(), s.reportHeadVelocity()));
text("pivot added", 100, 200);
break;
}
if ( closeEnough())
{
eat();
}
}
PVector reportHeadLocation() {
return location;
}
PVector reportHeadVelocity() {
return velocity;
}
boolean closeEnough() {
PVector head, rat;
head = reportHeadLocation();
rat = r.reportLocation();
if ((abs(head.x - rat.x)<50) && (abs(head.y - rat.y) < 50)) {
return true;
} else
{
return false;
}
}
int lastSwipe () {
return lastSwipe;
}
ArrayList<Pivot> getPivots() {
return pivots;
}
ArrayList<Segment> getSegments() {
return segments;
}
}
class Rat {
PVector location;
Rat() {
move();
// display();
}
void display() {
rect(location.x, location.y, 50, 50);
}
void move() {
float x = random(0, width);
float y = random(0, height);
location = new PVector(x, y);
}
PVector reportLocation() {
return location;
}
}
class GestureDetector {
int x, y, i, j;
int TEXT_LOCATION= 100;
GestureDetector () {
}
void initial() {
x=mouseX;
y=mouseY;
}
void fin() {
i=mouseX;
j=mouseY;
calculateDrag();
}
void calculateDrag() {
textSize(70);
//background(0);
if ((abs(x-i)>abs(y-j))) {
if (x>i) {
fill(255);
text("left swipe",
TEXT_LOCATION,
TEXT_LOCATION);
s.lastSwipe=3;
} else {
fill(255);
text("right swipe",
TEXT_LOCATION,
TEXT_LOCATION);
s.lastSwipe=4;
}
} else {
if (y>j) {
fill(255);
text("up swipe",
TEXT_LOCATION,
TEXT_LOCATION);
s.lastSwipe=1;
} else {
fill(255);
text("down swipe",
TEXT_LOCATION,
TEXT_LOCATION);
s.lastSwipe=2;
}
}
}
}
class Segment {
PVector location;
PVector velocity;
PVector lastLocation;
ArrayList<Pivot> pivots;
public Segment(PVector l) {
float x = l.x;
float y=l.y;
location= new PVector(x, y);
pivots = new ArrayList();
}
void display() {
switch(s.lastSwipe()) {
case 0:
case 1:
rect(location.x, location.y+50, 50, 50);
break;
case 2:
rect(location.x, location.y-50, 50, 50);
break;
case 3:
rect(location.x+50, location.y, 50, 50);
break;
case 4:
rect(location.x-50, location.y, 50, 50);
break;
}
// rect(location.x-50, location.y-50, 50, 50);
}
void move() {
setLastLocation(location);
if (getPivots()!=null){
for (Pivot p : getPivots()) {
Float x = p.pivotLocation.x-location.x;
Float y = p.pivotLocation.y-location.y;
velocity = new PVector(x, y);
location.add(velocity);
}
}
}
void setLastLocation(PVector last) {
lastLocation=last;
}
PVector getLastLocation() {
return lastLocation;
}
void addPivot(PVector l){
pivots.add(new Pivot(l));
}
ArrayList<Pivot> getPivots(){
return pivots;
}
}
class Pivot {
PVector pivotLocation;
// PVector pivotVelocity;
public Pivot(PVector l) {
this.pivotLocation = l;
// this.pivotVelocity = v;
}
PVector getPivotLocation() {
return pivotLocation;
}
//PVector getPivotVelocity() {
// return pivotVelocity;
//}
}