つよくなりたいなあ

地に足をつけず世界を生きぬくブログです

デイリーコーディング #1

f:id:heyassy:20171231184139j:plain

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

Minim minim;
ArrayList<Particle> particles;

void setup() {
  size(640, 640);
  stroke(0);
  minim = new Minim(this);
  particles = new ArrayList<Particle>();

  for (int i = 0; i < 10; i++) {
    particles.add(new Particle());
  }
}

void draw() {
  background(255);

  for (Particle p: particles) {
    p.update();
    p.display();
    if (p.isCollided) {
      p.playSound();
    }
  }
}

class Particle {
  PVector loc, vel;
  float radius = 25;
  boolean isCollided = false;
  AudioPlayer player;

  Particle() {
    loc = new PVector(random(radius, width - radius), random(radius, height - radius));
    vel = new PVector(random(-2, 2), random(-2, 2));
    player = minim.loadFile("Clap.mp3");
  }

  void display() {
    if (isCollided) {
      noStroke();
      fill(255, 0, 175);
    } else {
      stroke(1);
      noFill();
    }
    ellipse(loc.x, loc.y, radius*2, radius*2);
  }

  void update() {
    loc.add(vel);
    if (loc.x <= radius || loc.x >= width - radius) {
      vel.x *= -1;
      loc.x += vel.x;
      isCollided = true;
    }
    if (loc.y <= radius || loc.y >= height - radius) {
      vel.y *= -1;
      loc.y += vel.y;
      isCollided = true;
    }
  }

  void playSound() {
    player.play();
    player.rewind();
    isCollided = false;
  }
}

つづけるぞ〜