-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
96 lines (86 loc) · 2.56 KB
/
sketch.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
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
let res = [400, 400];
let framerate = 60;
let backgroundColor = 220;
class Square {
constructor(pos, size, vel, squareColor, lineWeight, lineColor) {
this.prevPos = [...pos];
this.pos = pos;
this.size = size;
this.vel = vel;
for (let i in this.vel)
this.vel[i] /= framerate;
this.squareColor = squareColor;
this.lineColor = lineColor;
}
apply_vel() {
this.prevPos = [...this.pos];
for (let i in this.pos)
this.pos[i] += this.vel[i];
}
collision() {
for (let i in this.pos) {
if (this.pos[i] < 0) {
this.pos[i] = 0;
this.vel[i] = -this.vel[i];
}
else if (this.pos[i] + this.size[i] > res[0]) {
this.pos[i] = res[0] - this.size[i];
this.vel[i] = -this.vel[i];
}
}
}
draw() {
fill(this.squareColor);
rect(this.pos[0], this.pos[1], this.size[0], this.size[1]);
beginLayer();
stroke(this.lineColor);
line(this.prevPos[0] + this.size[0] / 2, this.prevPos[1] + this.size[1] / 2,
this.pos[0] + this.size[0] / 2, this.pos[1] + this.size[1] / 2);
endLayer();
}
play() {
this.apply_vel();
this.collision();
this.draw();
}
}
let squares = new LinkedList();
function SpawnSquare() {
squares.push(new Square([Number(prompt("Enter the x position of the rectangle")),
Number(prompt("Enter the y position of the rectangle"))],
[Number(prompt("Enter the width of the rectangle")),
Number(prompt("Enter the height of the rectangle"))],
[Number(prompt("Enter the x velocity of the rectangle")),
Number(prompt("Enter the y velocity of the rectangle"))],
prompt("Enter the color of the rectangle"),
Number(prompt("Enter the weight of the line")),
prompt("Enter the color of the line")));
}
function setup() {
SpawnSquare();
createCanvas(res[0], res[1]);
frameRate(framerate);
background(backgroundColor);
}
function draw() {
background(backgroundColor);
beginLayer();
endLayer();
if (squares.length != 0)
for (let i = squares.head; i != null; i = i.next)
i.data.play();
}
function keyPressed() {
switch (keyCode) {
case 83: // s to spawn
SpawnSquare();
break;
case 67: // c to clear rectangles
if (prompt("Are you sure that you want to clear all rectangles? (yes/no)") == "yes")
squares.clear();
break;
case 80: // p to pause
prompt("Paused!");
break;
}
}