-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMover.cpp
77 lines (60 loc) · 1.58 KB
/
Mover.cpp
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
#ifndef MOVER_H
#define MOVER_H
#include "PVector.cpp"
#include "Arduino.h"
#include <M5Stack.h>
class Mover {
private:
float xoff, yoff;
int width = 320, height = 240, maxh = 0, maxw = 10;
public:
int r, g, b;
PVector location, velocity, mouse, dir;
M5Display* d;
Mover(M5Display* _d) {
location = PVector::createVector(random(width), random(height));
velocity = PVector::createVector(0, 0);
r = random(0, 255);
g = random(0, 255);
b = random(0, 255);
xoff = 0;
yoff = 0;
d = _d;
};
void update(int x, int y) {
mouse = PVector::createVector(x,y);
dir = PVector::sub(mouse, location);
dir.normalize();
dir.mult(0.5);
velocity.add(dir);
velocity.limit(10);
location.add(velocity);
};
void clear(int pixelSize) {
d->fillRect(location.x, location.y,pixelSize,pixelSize, M5.Lcd.color565(0, 0, 0));
};
void display(int pixelSize) {
d->fillRect(location.x, location.y,pixelSize,pixelSize, M5.Lcd.color565(r, g, b));
};
void checkEdges() {
if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
} else if (location.y < 0) {
location.y = height;
}
};
void bounce() {
if ((location.x > 128) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if ((location.y > 96) || (location.y < 0)) {
velocity.y = velocity.y * -1;
}
}
};
#endif