OneHourProcessing 十三日目

OneHourProcessing 十三日目

ぷよぷよ計画三日目です

今日かかった時間は「49分11秒」
トータル時間は「3時間51分32秒」
バグが…不具合が…

コード

Controller controller;

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

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

void keyPressed()
{
  controller.keyPress();
}

void keyReleased()
{
  controller.keyRelease();
}

void mousePressed()
{
  reset();
}

void reset()
{
  controller = null;
  controller = new Controller(new PVector (8, 15), 30);
}

Controllerクラス

class Controller
{
  PVector squareNum;
  float squareSize;
  ArrayList<Puyo> puyos = new ArrayList<Puyo>();
  ArrayList<Puyo> movePuyos = new ArrayList<Puyo>();
  Puyo[][] squares;
  Puyo operatePuyo;
  Puyo subPuyo;

  Controller(PVector squareNum, float squareSize)
  {
    this.squareNum  = squareNum;
    this.squareSize = squareSize;
    squares = new Puyo[int(squareNum.x)][int(squareNum.y)];

    addPuyo(true);
    addPuyo(false);
  }

  void update()
  {
    for (Puyo puyo : movePuyos)
      puyo.update();

    for (int i = 0; i < movePuyos.size(); i++)  
      if (movePuyos.get(i).pos.y % squareSize > squareSize / 2)
      {
        Puyo puyo = movePuyos.get(i);
        int puyoX = puyo.returnX(squareSize);
        int puyoY = puyo.returnY(squareSize);
        boolean isStop = false;
        if (puyoY >= squareNum.y - 1)
        {
          isStop = true;
          squares[puyoX][int(squareNum.y - 1)] = puyo;
          puyo.pos.y = squareSize * (squareNum.y - 0.5);
        } else if (squares[puyoX][puyoY + 1] != null)
        {
          isStop = true;
          squares[puyoX][puyoY] = puyo;
          puyo.pos.y = squareSize * (puyoY + 0.5);
        }
        if (isStop)
        {
          puyo.setStatus("STOP");
          movePuyos.remove(puyo);
          if (puyo == operatePuyo)
          {
            operatePuyo = subPuyo;
            subPuyo = null;
          } else if (puyo == subPuyo)
            subPuyo = null;

          i--;
        }
      }

    if (movePuyos.size() > 0)
      return;


    operatePuyo = null;
    subPuyo = null;

    for (Puyo puyo : puyos)
      puyo.flag = true;

    for (Puyo puyo : puyos)
      if (puyo.flag)
        checkPuyo(puyo, null);

    for (int i = 0; i < puyos.size(); i++)
    {
      Puyo puyo = puyos.get(i);
      if (puyo.connectNum + 1 >= 4)
      {
        erasePuyo(puyo);
        i--;
      }
      println(puyo.pos.x + " : " + puyo.pos.y);
    }

    for (Puyo puyo : puyos)
    {
      int puyoX = puyo.returnX(squareSize);
      int puyoY = puyo.returnY(squareSize);
      if (puyoY < squareNum.y - 1)
        if (squares[puyoX][puyoY + 1] == null)
        {
          puyo.setStatus("FALL");
          squares[puyoX][puyoY] = null;
          movePuyos.add(puyo);
        }
    }

    if (movePuyos.size() > 0)
      return;

    addPuyo(true);
    addPuyo(false);
  }

  void display()
  {
    for (Puyo puyo : puyos)
      puyo.display();
  }

  void keyPress()
  {
    if (operatePuyo == null)
      return;

    int rotate = 1;

    switch(keyCode)
    {
    case LEFT:
      if (operatePuyo.returnX(squareSize) <= 0)
        break;
      if (subPuyo != null)
        if (subPuyo.returnX(squareSize) <= 0)
          break;
      operatePuyo.pos.x -= squareSize;
      if (subPuyo != null)
        subPuyo.pos.x -= squareSize;
      break;

    case RIGHT:
      if (operatePuyo.returnX(squareSize) >= squareNum.x - 1)
        break;
      if (subPuyo != null)
        if (subPuyo.returnX(squareSize) >= squareNum.x - 1)
          break;
      operatePuyo.pos.x += squareSize;
      if (subPuyo != null)
        subPuyo.pos.x += squareSize;
      break;

    case DOWN:
      operatePuyo.vec.y = 6;
      if (subPuyo != null)
        subPuyo.vec.y = 6;
      break;

    case ENTER:
      rotate = -1;
    case SHIFT:
      if (subPuyo == null)
        break;

      int operateX = operatePuyo.returnX(squareSize);
      int operateY = operatePuyo.returnY(squareSize);
      int subX = subPuyo.returnX(squareSize);
      int subY = subPuyo.returnY(squareSize);
      int nextX, nextY;

      if (operateX == subX)
      {
        nextX = (operateY - subY) * rotate;
        nextY = 0;
      } else
      {
        nextY = -(operateX - subX) * rotate;
        nextX = 0;
      }

      boolean cannotRotate = false;
      cannotRotate |= operateX + nextX < 0;
      cannotRotate |= operateY + nextY < 0;
      cannotRotate |= operateX + nextX > squareNum.x - 1;
      cannotRotate |= operateY + nextY > squareNum.y - 1;

      if (cannotRotate)
        break;

      if (squares[operateX + nextX][operateY + nextY] != null)
        break;

      subPuyo.pos.x = operatePuyo.pos.x + nextX * squareSize;
      subPuyo.pos.y = operatePuyo.pos.y + nextY * squareSize;
      break;
    }
  }

  void keyRelease()
  {
    if (operatePuyo != null)
      operatePuyo.vec.y = 3;
    if (subPuyo != null)
      subPuyo.vec.y = 3;
  }

  void addPuyo(boolean isSub)
  {
    PVector puyoPos;
    PVector puyoVec = new PVector (0, 3);
    float   puyoRad = squareSize;
    String  puyoSta = "FALL";
    String  puyoTyp;
    int random = int(random(3));

    if (isSub)
      puyoPos = new PVector (squareNum.x / 2 * squareSize - squareSize / 2, squareSize / 2);
    else
      puyoPos = new PVector (squareNum.x / 2 * squareSize - squareSize / 2, squareSize * 3 / 2);

    if (random == 0)
      puyoTyp = "RED";
    else if (random == 1)
      puyoTyp = "YELLOW";
    else if (random == 2)
      puyoTyp = "GREEN";
    else if (random == 3)
      puyoTyp = "BLUE";
    else
      puyoTyp = "PURPLE";

    Puyo puyo = new Puyo(puyoPos, puyoVec, puyoRad, puyoSta, puyoTyp);
    if (isSub)
      operatePuyo = puyo;
    else
      subPuyo = puyo;
    puyos.add(puyo);
    movePuyos.add(puyo);

    if (squares[puyo.returnX(squareSize)][puyo.returnY(squareSize)] != null)
      reset();
  }

  void checkPuyo(Puyo puyo, Puyo beforePuyo)
  {
    int puyoX = puyo.returnX(squareSize);
    int puyoY = puyo.returnY(squareSize);
    puyo.connectNum = 0;
    puyo.flag = false;

    for (int x = -1; x <= 1; x += 2)
      if (puyoX + x >= 0 && puyoX + x < squareNum.x)
      {
        Puyo checkPuyo = squares[puyoX + x][puyoY];
        if (checkPuyo != null)
          if (puyo.checkParents(checkPuyo))
          {
            checkPuyo(checkPuyo, puyo);
            puyo.connectNum += checkPuyo.connectNum + 1;
          }
      }
    for (int y = -1; y <= 1; y += 2)
      if (puyoY + y >= 0 && puyoY + y < squareNum.y)
      {
        Puyo checkPuyo = squares[puyoX][puyoY + y];
        if (checkPuyo != null && checkPuyo != beforePuyo)
          if (puyo.checkParents(checkPuyo))
          {
            checkPuyo(checkPuyo, puyo);
            puyo.connectNum += checkPuyo.connectNum + 1;
          }
      }
  }

  void erasePuyo(Puyo puyo)
  {
    println(puyo.parents.size());
    for (int i = 0; i < puyo.parents.size(); i++)
    {
      erasePuyo(puyo.parents.get(i));
    }
    puyos.remove(puyo);
    movePuyos.remove(puyo);
    squares[puyo.returnX(squareSize)][puyo.returnY(squareSize)] = null;
  }

  void resetPuyo()
  {
    for (Puyo puyo : puyos)
    {
      int puyoX = puyo.returnX(squareSize);
      int puyoY = puyo.returnY(squareSize);

      if (puyoY <= squareNum.y - 2)
        if (squares[puyoX][puyoY + 1] == null)
        {
          puyo.vec = new PVector(0, 3);
          puyo.status = "FALL";
          puyo.connectNum = 0;
          puyo.parents.clear();
          squares[puyoX][puyoY] = null;
        }
    }
  }
}

Puyoクラス

class Puyo
{
  PVector pos;
  PVector vec;
  float radius;
  String status; // STOP, FALL
  String type; //RED, YELLOW, GREEN, BLUE, PURPLE
  boolean flag;
  ArrayList<Puyo> parents = new ArrayList<Puyo>();
  int connectNum;

  Puyo(PVector pos, PVector vec, float radius, String status, String type)
  {
    this.pos    = pos;
    this.vec    = vec;
    this.radius = radius;
    this.status = status;
    this.type   = type;
    flag = true;
    connectNum = 0;
  }

  void update()
  {
    pos.add(vec);
  }

  void display()
  {
    if (type.equals("RED"))
      fill(255, 0, 0);
    else if (type.equals("YELLOW"))
      fill(255, 255, 0);
    else if (type.equals("GREEN"))
      fill(0, 255, 0);
    else if (type.equals("BLUE"))
      fill(0, 0, 255);
    else if (type.equals("PURPLE"))
      fill(255, 0, 255);

    ellipse(pos.x, pos.y, radius, radius);
  }

  void setStatus(String nextStatus)
  {
    switch(nextStatus)
    {
    case "STOP":
      status = "STOP";
      vec = new PVector(0, 0);
      break;

    case "FALL":
      status = "FALL";
      vec = new PVector(0, 3);
      break;
    }
  }

  void setParents(Puyo puyo)
  {
    parents.add(puyo);
  }

  boolean checkParents(Puyo puyo)
  {
    if (!type.equals(puyo.type))
      return false;
    if (!puyo.flag)
      return false;
    parents.add(puyo);
    return true;
  }

  int returnX(float squareSize)
  {
    return int(pos.x / squareSize);
  }

  int returnY(float squareSize)
  {
    return int(pos.y / squareSize);
  }
}

OneHourProcessing 十二日目

OneHourProcessing 十二日目

十二日目の作品(昨日の続き)はこちら

今日かかった時間は「1時間36分21秒」
ここまでのトータルは「3時間2分21秒」

コード

Controller controller;

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

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

void keyPressed()
{
  controller.keyPress();
}

void mousePressed()
{
  reset();
}

void reset()
{
  controller = null;
  controller = new Controller(new PVector (8, 15), 30);
}

Controllerクラス

class Controller
{
  PVector squareNum;
  float squareSize;
  ArrayList<Puyo> puyos = new ArrayList<Puyo>();
  ArrayList<Puyo> movePuyos = new ArrayList<Puyo>();
  Puyo[][] squares;
  Puyo operatePuyo;

  Controller(PVector squareNum, float squareSize)
  {
    this.squareNum  = squareNum;
    this.squareSize = squareSize;
    squares = new Puyo[int(squareNum.x)][int(squareNum.y)];
    addPuyo();
  }

  void update()
  {
    for (Puyo puyo : movePuyos)
      puyo.update();

    for (int i = 0; i < movePuyos.size(); i++)  
      if (movePuyos.get(i).pos.y % squareSize > squareSize / 2)
      {
        Puyo puyo = movePuyos.get(i);
        int puyoX = puyo.returnX(squareSize);
        int puyoY = puyo.returnY(squareSize);
        if (puyoY >= squareNum.y - 1)
        {
          puyo.setStatus("STOP");
          squares[puyoX][int(squareNum.y - 1)] = puyo;
          puyo.pos.y = squareSize * (squareNum.y - 0.5);
          movePuyos.remove(puyo);
          i--;
        } else if (squares[puyoX][puyoY + 1] != null)
        {
          puyo.setStatus("STOP");
          squares[puyoX][puyoY] = puyo;
          puyo.pos.y = squareSize * (puyoY + 0.5);
          movePuyos.remove(puyo);
          i--;
        }
      }

    if (movePuyos.size() > 0)
      return;

    for (Puyo puyo : puyos)
      puyo.flag = true;

    for (Puyo puyo : puyos)
      if (puyo.flag)
        checkPuyo(puyo, null);

    for (int i = 0; i < puyos.size(); i++)
    {
      Puyo puyo = puyos.get(i);
      if (puyo.connectNum + 1 >= 4)
      {
        erasePuyo(puyo);
        i--;
      }
      println(puyo.pos.x + " : " + puyo.pos.y);
    }

    for (Puyo puyo : puyos)
    {
      int puyoX = puyo.returnX(squareSize);
      int puyoY = puyo.returnY(squareSize);
      if (puyoY < squareNum.y - 1)
        if (squares[puyoX][puyoY + 1] == null)
        {
          puyo.setStatus("FALL");
          squares[puyoX][puyoY] = null;
          movePuyos.add(puyo);
        }
    }

    if (movePuyos.size() > 0)
      return;

    addPuyo();
  }

  void display()
  {
    for (Puyo puyo : puyos)
      puyo.display();
  }

  void keyPress()
  {
    if (operatePuyo == null)
      return;

    switch(keyCode)
    {
    case LEFT:
      if (operatePuyo.returnX(squareSize) > 0)
        operatePuyo.pos.x -= squareSize;
      break;
    case RIGHT:
      if (operatePuyo.returnX(squareSize) < squareNum.x - 1)
        operatePuyo.pos.x += squareSize;
      break;
    }
  }

  void addPuyo()
  {
    PVector puyoPos = new PVector (squareNum.x / 2 * squareSize + squareSize / 2, squareSize / 2);
    PVector puyoVec = new PVector (0, 3);
    float   puyoRad = squareSize;
    String  puyoSta = "FALL";
    String  puyoTyp;
    int random = int(random(3));

    if (random == 0)
      puyoTyp = "RED";
    else if (random == 1)
      puyoTyp = "YELLOW";
    else if (random == 2)
      puyoTyp = "GREEN";
    else if (random == 3)
      puyoTyp = "BLUE";
    else
      puyoTyp = "PURPLE";

    Puyo puyo = new Puyo(puyoPos, puyoVec, puyoRad, puyoSta, puyoTyp);
    //Puyo puyo = new Puyo(puyoPos, puyoVec, puyoRad, puyoSta, "PURPLE");
    operatePuyo = puyo;
    puyos.add(puyo);
    movePuyos.add(puyo);
  }

  void checkPuyo(Puyo puyo, Puyo beforePuyo)
  {
    int puyoX = puyo.returnX(squareSize);
    int puyoY = puyo.returnY(squareSize);
    puyo.connectNum = 0;
    puyo.flag = false;

    for (int x = -1; x <= 1; x += 2)
      if (puyoX + x >= 0 && puyoX + x < squareNum.x)
      {
        Puyo checkPuyo = squares[puyoX + x][puyoY];
        if (checkPuyo != null)
          if (puyo.checkParents(checkPuyo))
          {
            checkPuyo(checkPuyo, puyo);
            puyo.connectNum += checkPuyo.connectNum + 1;
          }
      }
    for (int y = -1; y <= 1; y += 2)
      if (puyoY + y >= 0 && puyoY + y < squareNum.y)
      {
        Puyo checkPuyo = squares[puyoX][puyoY + y];
        if (checkPuyo != null && checkPuyo != beforePuyo)
          if (puyo.checkParents(checkPuyo))
          {
            checkPuyo(checkPuyo, puyo);
            puyo.connectNum += checkPuyo.connectNum + 1;
          }
      }
  }

  void erasePuyo(Puyo puyo)
  {
    println(puyo.parents.size());
    for (int i = 0; i < puyo.parents.size(); i++)
    {
      erasePuyo(puyo.parents.get(i));
    }
    puyos.remove(puyo);
    movePuyos.remove(puyo);
    squares[puyo.returnX(squareSize)][puyo.returnY(squareSize)] = null;
  }

  void resetPuyo()
  {
    for (Puyo puyo : puyos)
    {
      int puyoX = puyo.returnX(squareSize);
      int puyoY = puyo.returnY(squareSize);

      if (puyoY <= squareNum.y - 2)
        if (squares[puyoX][puyoY + 1] == null)
        {
          puyo.vec = new PVector(0, 3);
          puyo.status = "FALL";
          puyo.connectNum = 0;
          puyo.parents.clear();
          squares[puyoX][puyoY] = null;
        }
    }
  }
}

Puyoクラス

class Puyo
{
  PVector pos;
  PVector vec;
  float radius;
  String status; // MASTER, SLAVE, STOP, FALL
  String type; //RED, YELLOW, GREEN, BLUE, PURPLE
  boolean flag;
  ArrayList<Puyo> parents = new ArrayList<Puyo>();
  int connectNum;

  Puyo(PVector pos, PVector vec, float radius, String status, String type)
  {
    this.pos    = pos;
    this.vec    = vec;
    this.radius = radius;
    this.status = status;
    this.type   = type;
    flag = true;
    connectNum = 0;
  }

  void update()
  {
    pos.add(vec);
  }

  void display()
  {
    if (type.equals("RED"))
      fill(255, 0, 0);
    else if (type.equals("YELLOW"))
      fill(255, 255, 0);
    else if (type.equals("GREEN"))
      fill(0, 255, 0);
    else if (type.equals("BLUE"))
      fill(0, 0, 255);
    else if (type.equals("PURPLE"))
      fill(255, 0, 255);

    ellipse(pos.x, pos.y, radius, radius);
  }

  void setStatus(String nextStatus)
  {
    switch(nextStatus)
    {
    case "STOP":
      status = "STOP";
      vec = new PVector(0, 0);
      break;

    case "FALL":
      status = "FALL";
      vec = new PVector(0, 3);
      break;
    }
  }

  void setParents(Puyo puyo)
  {
    parents.add(puyo);
  }

  boolean checkParents(Puyo puyo)
  {
    if (!type.equals(puyo.type))
      return false;
    if (!puyo.flag)
      return false;
    parents.add(puyo);
    return true;
  }

  int returnX(float squareSize)
  {
    return int(pos.x / squareSize);
  }

  int returnY(float squareSize)
  {
    return int(pos.y / squareSize);
  }
}

OneHourProcessing 十一日目

OneHourProcessing 十一日目

今日作ったのはこちら

まだ未完成です…
作るのにかかった時間は「1時間26分」

コード

Controller controller;

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

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

void keyPressed()
{
  controller.keyPress();
}

void reset()
{
  controller = null;
  controller = new Controller(new PVector (8, 15), 30);
}

Controllerクラス

class Controller
{
  PVector squareNum;
  float squareSize;
  ArrayList<Puyo> puyos = new ArrayList<Puyo>();
  Puyo[][] squares;
  Puyo operatePuyo;

  Controller(PVector squareNum, float squareSize)
  {
    this.squareNum  = squareNum;
    this.squareSize = squareSize;
    squares = new Puyo[int(squareNum.x)][int(squareNum.y)];
    addPuyo();
  }

  void update()
  {
    for (Puyo puyo : puyos)
      puyo.update();

    if (operatePuyo.pos.y % squareSize > squareSize / 2)
    {
      int puyoX = int(operatePuyo.pos.x / squareSize);
      int puyoY = int(operatePuyo.pos.y / squareSize);

      if (puyoY <= squareNum.y - 2)
        if (squares[puyoX][puyoY + 1] == null)
          return;

      operatePuyo.vec = new PVector(0, 0);
      operatePuyo.status = "STOP";
      squares[puyoX][puyoY] = operatePuyo;

      for (int x = -1; x <= 1; x += 2)
        if (puyoX + x >= 0 && puyoX + x < squareNum.x)
        {
          Puyo puyo = squares[puyoX + x][puyoY];
          if (puyo != null)
            operatePuyo.checkParents(puyo);
        }
      for (int y = -1; y <= 1; y += 2)
        if (puyoY + y >= 0 && puyoY + y < squareNum.y)
        {
          Puyo puyo = squares[puyoX][puyoY + y];
          if (puyo != null)
            operatePuyo.checkParents(puyo);
        }

      println(operatePuyo.connectNum + 1);
      if (operatePuyo.connectNum + 1 >= 4)
      {
        removePuyo(operatePuyo);
      }

      addPuyo();
    }
  }

  void display()
  {
    for (Puyo puyo : puyos)
      puyo.display();
  }

  void keyPress()
  {
    if (operatePuyo == null)
      return;

    switch(keyCode)
    {
    case LEFT:
      if (operatePuyo.returnX(squareSize) > 0)
        operatePuyo.pos.x -= squareSize;
      break;
    case RIGHT:
      if (operatePuyo.returnX(squareSize) < squareNum.x - 1)
        operatePuyo.pos.x += squareSize;
      break;
    }
  }

  void addPuyo()
  {
    PVector puyoPos = new PVector (squareNum.x / 2 * squareSize + squareSize / 2, squareSize / 2);
    PVector puyoVec = new PVector (0, 3);
    float   puyoRad = squareSize;
    String  puyoSta = "FALL";
    String  puyoTyp;
    int random = int(random(5));

    if (random == 0)
      puyoTyp = "RED";
    else if (random == 1)
      puyoTyp = "YELLOW";
    else if (random == 2)
      puyoTyp = "GREEN";
    else if (random == 3)
      puyoTyp = "BLUE";
    else
      puyoTyp = "PURPLE";

    Puyo puyo = new Puyo(puyoPos, puyoVec, puyoRad, puyoSta, "PURPLE");
    operatePuyo = puyo;
    puyos.add(puyo);
  }

  void removePuyo(Puyo puyo)
  {
    for (int i = 0; i < puyo.parents.size(); i++)
    {
      removePuyo(puyo.parents.get(i));
    }
    puyos.remove(puyo);
    squares[puyo.returnX(squareSize)][puyo.returnY(squareSize)] = null;
  }

  void resetPuyo()
  {
    for (Puyo puyo : puyos)
    {
      int puyoX = puyo.returnX(squareSize);
      int puyoY = puyo.returnY(squareSize);

      if (puyoY <= squareNum.y - 2)
        if (squares[puyoX][puyoY + 1] == null)
        {
          puyo.vec = new PVector(0, 3);
          puyo.status = "FALL";
          puyo.connectNum = 0;
          puyo.parents.clear();
          squares[puyoX][puyoY] = null;
        }
    }
  }
}

Puyoクラス

class Puyo
{
  PVector pos;
  PVector vec;
  float radius;
  String status; // MASTER, SLAVE, STOP, FALL
  String type; //RED, YELLOW, GREEN, BLUE, PURPLE
  ArrayList<Puyo> parents = new ArrayList<Puyo>();
  int connectNum;

  Puyo(PVector pos, PVector vec, float radius, String status, String type)
  {
    this.pos    = pos;
    this.vec    = vec;
    this.radius = radius;
    this.status = status;
    this.type   = type;
    connectNum = 0;
  }

  void update()
  {
    pos.add(vec);
  }

  void display()
  {
    if (type.equals("RED"))
      fill(255, 0, 0);
    else if (type.equals("YELLOW"))
      fill(255, 255, 0);
    else if (type.equals("GREEN"))
      fill(0, 255, 0);
    else if (type.equals("BLUE"))
      fill(0, 0, 255);
    else if (type.equals("PURPLE"))
      fill(255, 0, 255);

    ellipse(pos.x, pos.y, radius, radius);
  }

  void setParents(Puyo puyo)
  {
    parents.add(puyo);
  }

  void checkParents(Puyo puyo)
  {
    if (!type.equals(puyo.type))
      return;
    connectNum += puyo.connectNum + 1;
    parents.add(puyo);
  }
  
  int returnX(float squareSize)
  {
    return int(pos.x / squareSize);
  }
  
  int returnY(float squareSize)
  {
    return int(pos.y / squareSize);
  }
}

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;
  }
}

OneHourProcessing 九日目

OneHourProcessing 九日目

今日作ったのはこちら

作るのにかかった時間は「41分30秒」
久々の時間以内+最速

コード

CA cellular_automata;

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

void draw()
{
  cellular_automata.display();
  cellular_automata.update();
}

void mousePressed()
{
  reset();
}

void reset()
{
  cellular_automata = null;
  cellular_automata = new CA(char(int(random(256))));
}
class CA
{
  int[] rule  = new int[8];
  int[] cells = new int[width];
  int hierarchy;
  int direction = 1;

  CA(char rule)
  {
    for (int i = 0; i < 8; i++)
    {
      this.rule[i] = (rule >> i) % 2;
    print(((rule >> i) % 2) + " : ");
    }
    println(int(rule));
    
    for (int i = 0; i < cells.length; i++)
      cells[i] = 0;

    cells[int(cells.length / 2)] = 1;

    hierarchy = 0;

    background(0);
  }

  void display()
  {
    loadPixels();

    for (int i = 0; i < cells.length; i++)
      if (cells[i] == 1)
        pixels[i + hierarchy * width] = color(0, 255, 0);  
      else
        pixels[i + hierarchy * width] = color(0); 

    updatePixels();
  }

  void update()
  {
    int[] nextCells = new int[width];

    for (int i = 1; i < cells.length - 1; i++)
    {
      int neighborCells = cells[i - 1] * 4 + cells[i] * 2 + cells[i + 1];
      nextCells[i] = rule[neighborCells];
    }

    nextCells[0] = cells[0];
    nextCells[cells.length - 1] = cells[cells.length - 1];

    cells = nextCells;

    hierarchy += direction;

    if (hierarchy == 0)
      direction = 1;
    else if (hierarchy == height - 1)
      direction = -1;
  }
}

OneHourProcessing 八日目

OneHourProcessing 八日目

今日作ったのはこちら

作るのにかかった時間は「1時間20分」

コード

PVector Gravity = new PVector(0, 0.05);
ArrayList<Firework> fireworks = new ArrayList<Firework>();

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

void draw()
{
  background(0);
  for (Firework firework : fireworks)
  {
    firework.update(Gravity);
    firework.display();
  }
  for (int i = 0; i < fireworks.size(); i++)
    if (fireworks.get(i).life < -10 && fireworks.get(i).particles.size() == 0)
    {
      fireworks.remove(i);
      i--;
    }
}

void setFirework()
{
  Firework firework = new Firework(new PVector(random(width), height), 5, 35, color(255), new PVector(0, -10));
  firework.setParticle(int(random(10, 20)), 3, int(random(50, 70)), color(int(random(150, 255)), int(random(150, 255)), int(random(150, 255))), random(3, 4));
  fireworks.add(firework);
}

void mouseReleased()
{
  setFirework();
}

Fireworkクラス

class Firework extends Particle
{
  ArrayList<Particle> particles = new ArrayList<Particle>();
  int particle_num;
  float particle_radius;
  float particle_initial;
  color particle_color;
  int particle_life;

  Firework(PVector pos, float radius, int life, color col, PVector initial)
  {
    super(pos, radius, life, col);
    vec = initial;
  }

  void setParticle(int particle_num, float particle_radius, int particle_life, color particle_color, float particle_initial)
  {
    this.particle_num     = particle_num;
    this.particle_radius  = particle_radius;
    this.particle_initial = particle_initial;
    this.particle_color   = particle_color;
    this.particle_life    = particle_life;
  }

  void update(PVector Gravity)
  {
    addVector(Gravity);
    super.update();

    makeParticle();

    if (particles.size() == 0)
      return;

    for (Particle particle : particles)
    {
      particle.addVector(Gravity);
      particle.update();
    }

    for (int i = 0; i < particles.size(); i++)
      if (particles.get(i).life < 0)
      {
        particles.remove(i);
        i--;
      }
  }

  void display()
  {
    super.display();
    if (particles.size() == 0)
      return;

    for (Particle particle : particles)
      particle.display();
  }

  void makeParticle()
  {
    if (life != 0)
      return;

    float rad = PI / particle_num;

    for (int i = 0; i <= particle_num; i++)
      for (int j = 0; j <= particle_num; j++)
      {
        Particle particle = new Particle(new PVector(pos.x, pos.y), particle_radius, particle_life, particle_color);
        particle.vec = new PVector(cos(i * rad) * particle_initial, sin(i * rad) * cos(j * rad) * particle_initial);
        particles.add(particle);
      }
  }
}

Particleクラス

class Particle
{
  PVector pos;
  PVector vec;
  PVector acc;
  float radius;
  int life;
  color col;
  
  Particle(PVector pos, float radius, int life, color col)
  {
    this.pos    = pos;
    this.radius = radius;
    this.life   = life;
    this.col    = col;
    
    vec = new PVector(0, 0);
    acc = new PVector(0, 0);
  }
  
  void update()
  {
    life--;
    vec.add(acc);
    pos.add(vec);
    acc.mult(0);
  }
  
  void display()
  {
    //fill(col);
    fill(col, life * 10);
    ellipse(pos.x, pos.y, radius * 2, radius * 2);
  }
  
  void addVector(PVector addVec)
  {
    acc.add(addVec);
  }
}

OneHourProcessing 七日目

OneHourProcessing 七日目

一週間経った…だと…?
今日作ったのは「ライフゲーム
内容はこんな感じ

作るのにかかった時間は「52分58秒」
久々の時間内…やったぜ

コード

Board board;

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

void draw()
{
  background(0);
  board.game();
}

void reset()
{
  board = null;
  board = new Board(80, 45);
  board.addPerturn(int(random(40)) + 20, int(random(15)) + 15, "RANDOM");
  board.addPerturn(int(random(40)) + 20, int(random(15)) + 15, "RANDOM");
  board.addPerturn(int(random(40)) + 20, int(random(15)) + 15, "RANDOM");
}

void mousePressed()
{
  if (mouseButton == LEFT)
    board.addPerturn(2, mouseY / (height / 45), "SPACESHIP");
  else
    reset();
}

Boardクラス

class Board
{
  int Xsize, Ysize;
  Cell[][] cells;

  Board(int Xsize, int Ysize)
  {
    this.Xsize = Xsize;
    this.Ysize = Ysize;
    cells = new Cell[Xsize][Ysize];

    for (int x = 0; x < Xsize; x++)
      for (int y = 0; y < Ysize; y++)
        cells[x][y] = new Cell(x, y, height / Ysize);
  }

  void game()
  {
    update();
    display();
    finish();
  }

  void display()
  {
    for (int x = 0; x < Xsize; x++)
      for (int y = 0; y < Ysize; y++)
        cells[x][y].display();
  }

  void update()
  {
    for (int x = 1; x < Xsize - 1; x++)
      for (int y = 1; y < Ysize - 1; y++)
        checkNeighbor(x, y);
  }

  void finish()
  {
    for (int x = 0; x < Xsize; x++)
      for (int y = 0; y < Ysize; y++)
        cells[x][y].finish();
  }

  void checkNeighbor(int x, int y)
  {
    int neighbor = 0;
    for (int addX = -1; addX <= 1; addX++)
      for (int addY = -1; addY <= 1; addY++)
        neighbor += cells[x + addX][y + addY].state;

    neighbor -= cells[x][y].state;

    switch(neighbor)
    {
    case 2:
      if (cells[x][y].state == 0)
        break;
    case 3:
      cells[x][y].nextState = 1;
      break;

    default:
      cells[x][y].nextState = 0;
    }
  }

  void addPerturn(int x, int y, String perturn)
  {
    if (x < 1 || y < 1 || x > Xsize - 2 || y > Ysize - 2)
      return;

    switch(perturn)
    {
    case "RANDOM":
      for (int addX = 0; addX < 3; addX++)
        for (int addY = 0; addY < 3; addY++)
          cells[x + addX][y + addY].state = int(random(2));
      break;
      
    case "SPACESHIP":
          cells[x][y].state = 1;
          cells[x][y + 2].state = 1;
          cells[x + 1][y + 3].state = 1;
          cells[x + 2][y + 3].state = 1;
          cells[x + 3][y + 3].state = 1;
          cells[x + 4][y + 3].state = 1;
          cells[x + 4][y + 2].state = 1;
          cells[x + 4][y + 1].state = 1;
          cells[x + 3][y].state = 1;
      break;
    }
  }
}

Cellクラス

class Cell
{
  int x, y, state, nextState;
  float size;

  Cell(int x, int y, float size)
  {
    this.x = x;
    this.y = y;
    state = 0;
    nextState = 0;
    this.size = size;
  }
  
  void finish()
  {
    state = nextState;
  }

  void display()
  {
    if (state == 1)
      fill(0, 255, 0);
    else
      fill(0, 0);

    rect(x * size, y * size, size, size);
  }
}