nfg_Day7

Day 7
<< main | course director notes

code float[] posX = { 10,20,30,40,50,60 }; float[] posY = { 10,20,30,40,50,60 };
 * 10. Arrays**

void setup{ size(640,480); }

void draw{ background(128); for(int i=0;i<posX.length;i++){ stroke(0); strokeWeight(20); point(posX[i],posY[i]); posX[i]++; } } code

code float posX1 = 0; float posY1 = 240; float circleSize1 = 40; color circleColor1 = color(255,0,0);
 * 9. Coordinates of multiple objects**

float posX2 = 0; float posY2 = 100; float circleSize2 = 140; color circleColor2 = color(0,0,255);

void setup{ size(640,480); }

void draw{ background(128);

noStroke; ellipseMode(CENTER); fill(circleColor1); ellipse(posX1,posY1,circleSize1,circleSize1); fill(circleColor2); ellipse(posX2,posY2,circleSize2,circleSize2);

if(posX2<width){ posX2 += 10; } else{ posX2 = 0; } if(posX1<width){ posX1 += 2; } else{ posX1 = 0; }

} code

code float posX = 0; float posY = 240; float s = 0; int counter = 0;
 * 8. Sine wave example**

void setup{ size(640,480); }

void draw{ if(counter<100){ stroke(0); }else{ stroke(255,0,0); } counter++;

strokeWeight(20); point(posX,posY); posX += 10; posY += 10 * sin(s); s += 0.1; }

code

code float posX = 320; float posY = 240; int counter = 0; int counterMax = 100;
 * 7. Counter example 1**

void setup{ size(640,480); }

void draw{ if(counter<counterMax){ background(128); strokeWeight(20); point(posX,posY); posX++; counter++; } }

void keyPressed{ counter=0; posX = 320; } code

code float posX = 320; float posY = 240; PImage bob; float rot = 0;
 * 6. Using images**

void setup{ size(640,480); smooth; bob = loadImage("test.png"); }

void draw{ if(mousePressed){ pushMatrix; translate(mouseX,mouseY); rotate(radians(rot)); imageMode(CENTER); image(bob,0,0,width,height); popMatrix; rot++; } } code

code float posX = 320; float posY = 240; float previousPosX = posX; float previousPosY = posY;
 * 5. More loops**

size(640,480);

for(int i=0;i<10000;i++){ posX = random(width); posY = random(height); stroke(0,20); strokeWeight(10); point(posX,posY); stroke(0,20); strokeWeight(1); line(previousPosX,previousPosY,posX,posY); previousPosX = posX; previousPosY = posY; } code

code void setup{ size(640,480); }
 * 4. Loop review**

void draw{ background(0); for(int i=0;i<1000;i++){ strokeWeight(20); stroke(100,100,100,100); point(random(width),random(height)); } } code

code float posX = 320; float posY = 240; float enemyPosX = 20; float enemyPosY = 20; float circleSize = 40; float playerEase = 16; float enemyEase = 32;
 * 3. Working with functions**

void setup{ size(640,480); }

void draw{ background(0); if(mousePressed){ posX = tween(posX,mouseX,playerEase); posY = tween(posY,mouseY,playerEase); strokeWeight(5); stroke(255,0,0); point(mouseX,mouseY); strokeWeight(1); line(posX,posY,mouseX,mouseY); } noStroke; fill(255,0,0); ellipseMode(CENTER); ellipse(posX,posY,circleSize,circleSize); enemyPosX = tween(enemyPosX,posX,enemyEase); enemyPosY = tween(enemyPosY,posY,enemyEase); strokeWeight(5); stroke(0,0,255); point(posX,posY); strokeWeight(1); line(posX,posY,enemyPosX,enemyPosY); fill(0,0,255); ellipse(enemyPosX,enemyPosY,circleSize,circleSize);

if(hitDetect(posX,posY,circleSize,circleSize,enemyPosX,enemyPosY,circleSize,circleSize)){ background(random(255),0,0); } }

float tween(float v1, float v2, float e) { v1 += (v2-v1)/e; return v1; }

boolean hitDetect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) { w1 /= 2; h1 /= 2; w2 /= 2; h2 /= 2; if(x1 + w1 >= x2 - w2 && x1 - w1 <= x2 + w2 && y1 + h1 >= y2 - h2 && y1 - h1 <= y2 + h2) { return true; } else { return false; } }

code

code <!doctype html>
 * 2. Using Processing.js**

 width:640px; height:480px; postion:relative; margin:auto; border:1px solid red; } 
 * 1) container{



code


 * [|1. Uploading to OpenProcessing.org]**