nottobe, hi hi

Retour

PROCESSING

1. Installer Processing: www.processing.org

2. Dessiner un carré

void setup() {
  size(400, 400); // zone de dessin
  stroke(255); // couleur tracé (blanc)
  background(0); // fond (noir)
}

void draw() {
  rect(190, 190, 20, 20);
}
      

3. Déplacer un carré

// on se place au milieu pour commencer
int x = 190;
int y = 190;

void setup() {
  size(400, 400);
  stroke(255);
}

void draw() {
  background(0);

  if (key == CODED && keyPressed == true) {
    if (keyCode == UP) y-= 5;
    else if (keyCode == DOWN) y += 5;
    else if (keyCode == RIGHT) x += 5;
    else if (keyCode == LEFT) x -= 5;

    // Gestion des bords
    if (x < 0) x = 0;
    else if (x > 376) x = 376;
    if (y < 0) y = 0;
    else if (y > 376) y = 376;
  }

  rect(x, y, 20, 20);
}
      

4. Déplacer un personnage
Placer le PNG (megaman.png dans le dossier data du script)

// on se place au milieu pour commencer
int x = 188;
int y = 188;
PImage sprite;

void setup() {
  size(400, 400);
  stroke(255);
  sprite = loadImage("megaman.png");
}

void draw() {
  background(0);
  if (key == CODED && keyPressed == true) {
    if (keyCode == UP) y-= 5;
    else if (keyCode == DOWN) y += 5;
    else if (keyCode == RIGHT) x += 5;
    else if (keyCode == LEFT) x -= 5;

    // Gestion des bords
    if (x < 0) x = 0;
    else if (x > 376) x = 376;
    if (y < 0) y = 0;
    else if (y > 376) y = 376;
  }
  image(sprite, x, y);
}
      

5. Ciel étoilé

void setup() {
  size(400, 400);
  background(0);

  for (int i = 0; i < 100; i++) {
    stroke(55 + random(100));
    rect(random(400), random(400), 2, 2);
  }
  for (int i = 0; i < 100; i++) {
    stroke(155 + random(100));
    rect(random(400), random(400), 1, 1);
  }
}

void draw() {}
      

6. Obstacle
Placer le PNG (megaman.png dans le dossier data du script)

// on se place au milieu pour commencer
int x = 188;
int oldX = 188;
int y = 188;
int oldY = 188;
PImage sprite;

void setup() {
  size(400, 400);
  sprite = loadImage("megaman.png");
  stroke(255);
  fill(255);
}

void draw() {
  background(0);
  if (key == CODED && keyPressed == true) {
    if (keyCode == UP) y-= 5;
    else if (keyCode == DOWN) y += 5;
    else if (keyCode == RIGHT) x += 5;
    else if (keyCode == LEFT) x -= 5;

    // Gestion des bords
    if (x < 0) x = 0;
    else if (x > 376) x = 376;
    if (y < 0) y = 0;
    else if (y > 376) y = 376;

    // Gestion obstacle
    if (x > 300-24 && x < 300+20 && y > 300-24 && y < 300+20) {
     x = oldX;
     y = oldY;
    }
  }
  oldX = x;
  oldY = y;
  image(sprite, x, y);
  rect(300, 300, 20, 20);
}

      

7. Obstacles randomisés
Placer le PNG (megaman.png dans le dossier data du script)

// on se place au milieu pour commencer
int x = 0;
int oldX = 0;
int y = 0;
int oldY = 0;
PImage sprite;
int NbObs = 40;
int[] ObsX = new int[NbObs];
int[] ObsY = new int[NbObs];

void setup() {
  size(400, 400);
  sprite = loadImage("megaman.png");
  stroke(255);
  fill(255);
  for (int i = 0; i < NbObs; i++) {
    ObsX[i] = 24 + int(random(400));
    ObsY[i] = 24 + int(random(400));
  }
}

void draw() {
  background(0);
  if (key == CODED && keyPressed == true) {
    if (keyCode == UP) y-= 5;
    else if (keyCode == DOWN) y += 5;
    else if (keyCode == RIGHT) x += 5;
    else if (keyCode == LEFT) x -= 5;

    // Gestion des bords
    if (x < 0) x = 0;
    else if (x > 376) x = 376;
    if (y < 0) y = 0;
    else if (y > 376) y = 376;

    // Gestion obstacles
    for (int i = 0; i < NbObs; i++) {
      if (x > ObsX[i]-24 && x < ObsX[i]+20 && y > ObsY[i]-24 && y < ObsY[i]+20) {
        x = oldX;
        y = oldY;
      }
    }
  }
  oldX = x;
  oldY = y;
  image(sprite, x, y);
  for (int i = 0; i < NbObs; i++) rect(ObsX[i], ObsY[i], 20, 20);
}
      

8. Obstacles-TheHulk randomisés
Placer les PNG (megaman.png & thehulk.png dans le dossier data du script)

// on se place au milieu pour commencer
int x = 0;
int oldX = 0;
int y = 0;
int oldY = 0;
PImage megaman;
PImage thehulk;
int NbObs = 40;
int[] ObsX = new int[NbObs];
int[] ObsY = new int[NbObs];

void setup() {
  size(400, 400);
  megaman = loadImage("megaman.png");
  thehulk = loadImage("thehulk.png");
  stroke(255);
  fill(255);
  for (int i = 0; i < NbObs; i++) {
    ObsX[i] = 24 + int(random(400));
    ObsY[i] = 24 + int(random(400));
  }
}

void draw() {
  background(0);
   if (key == CODED && keyPressed == true) {
     if (keyCode == UP) y-= 2;
     else if (keyCode == DOWN) y += 2;
     else if (keyCode == RIGHT) x += 2;
     else if (keyCode == LEFT) x -= 2;

     // Gestion des bords
     if (x < 0) x = 0;
     else if (x > 376) x = 376;
     if (y < 0) y = 0;
     else if (y > 376) y = 376;

     // Gestion obstacle
     for (int i = 0; i < NbObs; i++) {
       if (x > ObsX[i]-24 && x < ObsX[i]+30 && y > ObsY[i]-24 && y < ObsY[i]+30) {
         x = oldX;
         y = oldY;
       }
     }
   }
  oldX = x;
  oldY = y;
  image(megaman, x, y);
  for (int i = 0; i < NbObs; i++) {
    image(thehulk, ObsX[i], ObsY[i]);
    ObsX[i] += random(-1, 2);
    ObsY[i] += random(-1, 2);
  }
}
      

9. MegaMan vs. The TheHulk
Placer les PNG (megaman.png & thehulk.png dans le dossier data du script)

// on se place au milieu pour commencer
int x = 0;
int oldX = 0;
int y = 0;
int oldY = 0;
PImage megaman;
PImage thehulk;
int NbObs = 40;
int[] ObsX = new int[NbObs];
int[] ObsY = new int[NbObs];

void setup() {
  size(400, 400);
  megaman = loadImage("megaman.png");
  thehulk = loadImage("thehulk.png");
  stroke(255);
  fill(255);
  for (int i = 0; i < NbObs; i++) {
    ObsX[i] = 24 + int(random(400));
    ObsY[i] = 24 + int(random(400));
  }
}

void draw() {
  background(0);
   if (key == CODED && keyPressed == true) {
     if (keyCode == UP) y-= 2;
     else if (keyCode == DOWN) y += 2;
     else if (keyCode == RIGHT) x += 2;
     else if (keyCode == LEFT) x -= 2;

     // Gestion des bords
     if (x < 0) x = 0;
     else if (x > 376) x = 376;
     if (y < 0) y = 0;
     else if (y > 376) y = 376;

     // Gestion obstacle
     for (int i = 0; i < NbObs; i++) {
       if (x > ObsX[i]-24 && x < ObsX[i]+30 && y > ObsY[i]-24 && y < ObsY[i]+30) {
         x = oldX;
         y = oldY;
       }
     }
   }
  oldX = x;
  oldY = y;
  image(megaman, x, y);
  for (int i = 0; i < NbObs; i++) {
    image(thehulk, ObsX[i], ObsY[i]);
    ObsX[i] += random(-1, 2);
    ObsY[i] += random(-1, 2);
  }
}

void keyPressed() {println(key);
  if (key == ENTER || key == RETURN) {
    for (int i = 0; i < 100; i = i + 5) {
      frameRate(2);
      circle(x+12, y+12, i);
    }
    for (int i = 0; i < NbObs; i++) {
      if (dist(x, y, ObsX[i], ObsY[i]) < 100) ObsX[i] = ObsY[i] = -100;
    }
    frameRate(30);
  }
}
      

10. PONG

float bX = 100;
float bY = 100;

float depX = 6;
float depY = 4;

float raqY = 0;
float raqDepY = 0;
float qarY = 0;
float qarDepY = 0;


void setup() {
  size(1024, 640);
  noStroke();

  fill(255);
}

void draw() {
  background(0);



  bX = bX + depX;
  bY = bY + depY;

  if (10 < bX && bX < 20 && raqY < bY && bY < raqY+50) depX =-depX;
  if (width-20 < bX && bX < width-10 && qarY < bY && bY < qarY+50) depX =-depX;

  if (bY > height-10 || bY < 0) depY = -depY;

  raqY = raqY + raqDepY;
  qarY = qarY + qarDepY;

  if (raqY < 0) raqY = 0;
  if (raqY > height-50) raqY = height-50;

  if (qarY < 0) qarY = 0;
  if (qarY > height-50) qarY = height-50;

  if (bX > width-10 || bX < 0) {
    bX = 100+random(300);
    bY = 100+random(300);
    depX = 6;
    depY = 4;
  }

  rect(bX, bY, 10, 10);
  rect(10, raqY, 10, 50);
  rect(width-20, qarY, 10, 50);
}

void keyPressed() {
  if (key == 'a') raqDepY = -5;
  if (key == 'q') raqDepY = 5;
  if (key == 'p') qarDepY = -5;
  if (key == 'm') qarDepY = 5;
}

void keyReleased() {
  if (key == 'a') raqDepY = 0;
  if (key == 'q') raqDepY = 0;
  if (key == 'p') qarDepY = 0;
  if (key == 'm') qarDepY = 0;
}

11. ANALYSE AUDIO

import processing.sound.*;

AudioIn input;
Amplitude analyzer;
FFT fft;
int bands = 512;
float[] spectrum = new float[bands];

int bassLimit = 20;
int mediumLimit = 120;

int multiplicator = 1000;

void setup() {
  size(1080, 720);
  background(0);
  strokeWeight(4);

  input = new AudioIn(this, 0);
  input.start();
  analyzer = new Amplitude(this);
  analyzer.input(input);
  fft = new FFT(this, bands);
  fft.input(input);
}

void draw() {
  fill(0, 20);
  strokeWeight(20);

  float bass = 0;
  float medium = 0;
  float treble = 0;
  float vol = analyzer.analyze();
  fft.analyze(spectrum);
  for(int i = 0; i < bands; i++){
    if (i < bassLimit) bass += spectrum[i];
    else if (i < mediumLimit) medium += spectrum[i];
    else treble += spectrum[i];
  }

  stroke(
    map(treble*multiplicator, 0, 255, 0, 150),
    map(medium*multiplicator, 0, 255, 0, 150),
    map(bass*multiplicator, 0, 255, 0, 150));

  circle(width/2, height/2, int(vol*multiplicator));

}


void mousePressed() {
 saveFrame("output/frame_####.png");
}