top of page
  • Writer's pictureRob George

Week 12: Noise + Functions + Key Pressed + Silly Poetry Generator

In Class Task 1: Noise Review

The task was to create two boxes randomly move in a specific section of the screen while changing size and color. All while using noise.

//week 12 task 1


float x, y, w, h;

float x2, y2, w2, h2;

float hue, hue2, s, s2, b, b2;

color col;


void setup() {

size(400, 400);

rectMode(CENTER);

colorMode(HSB);

col=color(random(255), random(255), random(255));

}



void draw() {

background(col);


noStroke();

fill(hue, s, b);

x=map(noise(0.01*frameCount), 0, 1, 100, 250);

y=map(noise(0.01*frameCount+5), 0, 1, 100, 250);

w=map(noise(0.01*frameCount+25), 0, 1, 10, 150);

h=map(noise(0.01*frameCount+45), 0, 1, 20, 150);

hue=map(noise(0.01*frameCount+55), 0, 1, 0, 255);

s=255;

b=255;

rect(x, y, w, h);


noStroke();

fill(hue2, s2, b2);

x2=map(noise(0.01*frameCount+70), 0, 1, 100, 350);

y2=map(noise(0.01*frameCount+80), 0, 1, 100, 350);

w2=map(noise(0.01*frameCount+90), 0, 1, 10, 150);

h2=map(noise(0.01*frameCount+95), 0, 1, 20, 250);

hue2=map(noise(0.01*frameCount+100), 0, 1, 0, 255);

s2=255;

b2=255;

rect(x2, y2, w2, h2);

stroke(255, 200);

line(x, 0, x, height);

line(x2, 0, x2, height);

line(0, y, width, y);

line(0, y2, width, y2);

}


void keyPressed() {

if (key=='s') {

int random_number=int(random(1000));

save(random_number+"robert.george"+"_week12task1"+".png");

}

}


In Class Lesson 1: Function

Something new I learned was how to use functions. Functions are an easier way to group together a block of code under one name. Here is the simple ball bounce animation but using functions instead of having all the code written out in draw.

// bouncing ball - functions


float x=0;

float y=0;

float xSpeed=5;

float ySpeed=3;


void setup() {

size(400, 300);

}



void draw() {

background(255);


//display ball function

displayBall();

//move ball function

moveBall();

//check edges function

checkEdges();

}



void displayBall() {

stroke(0);

fill(0);

ellipse(x, y, 30, 30);

}


void moveBall() {

x=x+xSpeed;

y=y+ySpeed;

}


void checkEdges() {

if ( (x>width) || (x<0) ) {

xSpeed=xSpeed*-1;

}


if ( (y>height) || (y<0) ) {

ySpeed=ySpeed*-1;

}

}


In Class Lesson 2: Fading Typewriter

I also learned how to use keyPressed() to create a typing effect that fades each letter after it has been typed.


//Fading type writer


void setup(){

size(400, 400);

background(#E3D427);

}



void draw(){

//rect same size and color as canvas and with opacity

fill(#E3D427, 20);

noStroke();

rect(0, 0, width, height);

}



void keyPressed(){

fill(#2770E3);

textSize(random(20, 200));

text(key, random(width), random(100, height));

}


In Class Lesson 3: Silly Poem Generator

Next I learned how to create a poem generator by using string arrays and functions. These will be used to generate my own poem generator in the next couple posts.

// the simple silly poem generator


//string arrays count from [ 0, 1, 2, 3, 4, etc. ]


String[] art={"the", "this", "that", "one"};

String[] adj={"green", "red", "unbelievable", "infinite", "pouting"};

String[] noun={"tree", "forest", "dog", "sky", "grass", "mountain"};

String[] verb={"is weeping", "runs", "is thinking", "observes"};

String[] pre={"over", "above", "behind", "in front of"};

int y = 50;


void setup() {

size(500, 400);

background (255);

fill(#CC27E3);

new_sentence();

}



//the green tree is weeping over a pouting dog



void new_sentence() { //a function

background (255); //reset the background

y=50; //reset the y so the poem will restart

write_word(art);

write_word(adj);

write_word(noun);

write_word(verb);

write_word(pre);

write_word(art);

write_word(adj);

write_word(noun);

}


void draw() {

}



void mousePressed() {

new_sentence();

}



void write_word(String[]words) { //a function

int n=(int)random(words.length);

textSize(random(20,40));

text(words[n], random(80, 300), y);

y=y+40; //sets the leading of the words

}




< Return to Coding Page

4 views0 comments

Recent Posts

See All
bottom of page