During this week’s recitation, I learned a lot about how modulation makes just about everything much easier in processing. For my in class exercise, I created a bouncing rectangle that grows or shrinks depending on where it is on the screen. I also gave users the ability to randomly teleport it to somewhere else on the screen. Coding the teleportation was a little difficult at first, because the rectangle would constantly just teleport ten times all over the screen from a single button click. To fix this, Tristan told me to check out the keyReleased function, which was super helpful.
Code (Sorry for some reason my snipping tool is not working):
float speedX,speedY;
float x,y;
float size = 20;
void setup() {
size(600,600);
speedX = random(15);
speedY = random(15);
}
void draw() {
background(0);
rectMode(RADIUS);
rect(x, y, size, size);
x += speedX;
y += speedY;
RectBounce();
RectSize();
}
void RectBounce() {
if ((x > width) || (x < 0)) {
speedX *= -1;
RectColour();
}
if ((y > height) || (y < 0)) {
speedY *= -1;
RectColour();
}
}
void RectColour() {
rect(x, y, size, size);
fill(random(50,255),random(50,255),random(50,255));
}
void keyPressed(){
TeleportRect();
}
void TeleportRect() {
if (key == ‘t’) {
x = random(width);
y = random(height);
speedX = random(5,15);
speedY = random(5,15);
rect(x, y, size, size);
}
}
void RectSize() {
if ((x < width/2) || (y < height/2)) {
size–;
}
if ((x > width/2) || (y > height/2)) {
size++;
}
}
Video:
Homework:
I had a lot of problems with figuring out the coloring for the homework for this recitation. I couldn’t get the colors to smoothly transition no matter what I did. I thought that you could apply separate delays with modulation, but it turns out that it putting a delay anywhere effects the frame rate of he program.
code:
boolean Shrinking;
int x = 300;
void setup() {
size(600, 600);
smooth(2);
background(255);
ellipseMode(CENTER);
frameRate(120);
}
void draw() {
background(255);
ellipse(mouseX, mouseY, x, x);
Color();
if (Shrinking) {
x–;
}
if (!Shrinking) {
x++;
}
if (x < 100) {
Shrinking = !Shrinking;
}
if (x > 300) {
Shrinking = true;
}
}
void Color() {
strokeWeight(20);
colorMode(RGB);
stroke(random(255),random(255),random(255));
}
video: