Skip to content

Commit 52e5737

Browse files
author
Marc Bickel
committed
Added week4 sketches
1 parent 5f561bc commit 52e5737

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Mover mover;
2+
void settings() {
3+
size(800, 200);
4+
}
5+
void setup() {
6+
mover = new Mover();
7+
}
8+
void draw() {
9+
background(255);
10+
mover.update();
11+
mover.checkEdges();
12+
mover.display();
13+
}

FirstGravitySketch/mover.pde

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Mover {
2+
PVector location;
3+
PVector velocity;
4+
PVector gravity = new PVector(0, 0.1);
5+
float reboundCoeff = 0.8f;
6+
Mover() {
7+
location = new PVector(width/2f, height/2f);
8+
velocity = new PVector(1f, 1f);
9+
}
10+
void update() {
11+
location.add(velocity);
12+
velocity.add(gravity);
13+
}
14+
void display() {
15+
stroke(0);
16+
strokeWeight(2);
17+
fill(127);
18+
ellipse(location.x, location.y, 48, 48);
19+
}
20+
void checkEdges() {
21+
if (location.x + 24 > width) {
22+
velocity.x = velocity.x * -1 * reboundCoeff;
23+
location.x = width - 24;
24+
}
25+
else if (location.x - 24 < 0) {
26+
velocity.x = velocity.x * -1* reboundCoeff;
27+
location.x = 24;
28+
}
29+
if (location.y + 24 > height) {
30+
velocity.y = velocity.y * -1 * reboundCoeff;
31+
location.y = height - 24;
32+
}
33+
else if (location.y - 24 < 0) {
34+
velocity.y = velocity.y * -1 * reboundCoeff;
35+
location.y = 24;
36+
}
37+
}
38+
}

NiceLittleDraw/NiceLittleDraw.pde

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// The Nature of Code
2+
// Daniel Shiffman
3+
// http://natureofcode.com
4+
PVector location;
5+
PVector velocity;
6+
void setup() {
7+
size(200, 200);
8+
background(255);
9+
location = new PVector(100, 100);
10+
velocity = new PVector(2.5, 5);
11+
}
12+
void draw() {
13+
noStroke();
14+
fill(255, 10); // 10 is the level of opacity (to get the "trace" effect)
15+
rect(0, 0, width, height);
16+
// Add the current speed to the location.
17+
location.add(velocity);
18+
if ((location.x > width) || (location.x < 0)) {
19+
velocity.x = velocity.x * -1;
20+
}
21+
if ((location.y > height) || (location.y < 0)) {
22+
velocity.y = velocity.y * -1;
23+
}
24+
// Display circle at its location
25+
stroke(0);
26+
fill(175);
27+
ellipse(location.x, location.y, 16, 16);
28+
}

0 commit comments

Comments
 (0)