OneHourProcessing 十日目

OneHourProcessing 十日目

今日作った作品はこちら

作るのにかかった時間は「50分40秒」

コード

PVector Gravity = new PVector(0, 0.01);
Control controller;

void setup()
{
  size(800, 450);
  reset();
}

void draw()
{
  background(0);
  controller.update();
  controller.display();
}

void reset()
{
  controller = null;
  controller = new Control(10, 5, new PVector(width / 2, height - 15), width / 2);
}

void mousePressed()
{
  reset();
}

Ballクラス

class Ball
{
  PVector pos;
  PVector vec;
  PVector acc;
  float radius;
  
  Ball(PVector pos, PVector vec, float radius)
  {
    this.pos = pos;
    this.vec = vec;
    this.radius = radius;
    acc = new PVector(0, 0);
  }
  
  void update()
  {
    acc.add(Gravity);
    vec.add(acc);
    pos.add(vec);
    acc.mult(0);
  }
  
  void display()
  {
    ellipse(pos.x, pos.y, radius, radius);
  }
}

Blockクラス

class Block
{
  PVector pos;
  PVector start;
  PVector end;
  PVector size;
  
  Block(int x, int y, float Xsize, float Ysize)
  {
    pos   = new PVector(x, y);
    start = new PVector(x * Xsize, y * Ysize);
    end   = new PVector(x * Xsize + Xsize, y * Ysize + Ysize);
    size  = new PVector(Xsize, Ysize);
  }
  
  void display()
  {
    rect(start.x + 2, start.y + 2, size.x - 4, size.y - 4);
  }
  
  boolean isClash(Ball ball)
  {
    boolean isClash = true;
    
    isClash &= (ball.pos.x + ball.radius) > start.x;
    isClash &= (ball.pos.y + ball.radius) > start.y;
    isClash &= (ball.pos.x - ball.radius) < end.x;
    isClash &= (ball.pos.y - ball.radius) < end.y;
    
    return isClash;
  }
}

Controlクラス

class Control
{
  ArrayList<Block> blocks = new ArrayList<Block>();
  Ball ball;

  int Xsize, Ysize;
  PVector operateBoard;
  float boardSize;

  Control(int Xsize, int Ysize, PVector operateBoard, float boardSize)
  {
    ball = new Ball(new PVector(operateBoard.x, operateBoard.y), new PVector(random(0.5, 2.5), -5), 10);

    for (int x = 0; x < Xsize; x++)
      for (int y = 0; y < Ysize; y++)
        blocks.add(new Block(x, y, width / Xsize, height / Ysize / 3));

    this.Xsize        = Xsize;
    this.Ysize        = Ysize;
    this.operateBoard = operateBoard;
    this.boardSize    = boardSize;

    noFill();
    stroke(255);
  }

  void update()
  {
    updateDisplay();
    checkClash();
    checkWall();
    checkBoard();
    ball.update();
    checkBoard();
  }

  void display()
  {
    for (Block block : blocks)
      block.display();
    ball.display();
    displayBoard();
  }

  void checkClash()
  {
    for (int i = 0; i < blocks.size(); i++)
      if (blocks.get(i).isClash(ball))
      {
        blocks.remove(i);
        i--;
        ball.vec.y *= -1;
      }
  }

  void checkWall()
  {
    if (ball.pos.x < 0 || ball.pos.x > width)
      ball.vec.x *= -1;
  }

  void checkBoard()
  {
    boolean check = true;
    check &= ball.pos.y < operateBoard.y;
    check &= ball.pos.y + ball.vec.y > operateBoard.y;
    check &= ball.pos.x > operateBoard.x - boardSize / 2;
    check &= ball.pos.x < operateBoard.x + boardSize / 2;

    if (check)
      ball.vec.y *= -1;
  }
  
  void displayBoard()
  {
    rect(operateBoard.x - boardSize / 2, operateBoard.y - 5, boardSize, 10);
  }
  
  void updateDisplay()
  {
    operateBoard.x = mouseX;
  }
}