OneHourProcessing 六日目

OneHourProcessing 六日目

今日作ったのはこちら

作るのにかかった時間は「2時間20分」
うっせやろおい…

コード

昨日と同じ部分(MazeMasterクラス、Squareクラス)は省略

MazeMaker mazeMaker;
Agent agent;

boolean canEx = false;
boolean reStart = false;

void setup()
{
  size(800, 450);
  //mazeMaker = new MazeMaker(19, 15);
  mazeMaker = new MazeMaker(79, 45);
  //mazeMaker = new MazeMaker(191, 107);
}

void draw()
{
  background(0);
  mazeMaker.display();
  
  if(agent != null)
  {
    agent.secondSearch();
    agent.display();
  }

  if (canEx)
    return;

  for(int i = 0; i < 10 && !canEx; i++)
  canEx = mazeMaker.make();
}

void mouseReleased()
{
  if (mouseButton == LEFT)
  {
    if (reStart)
    {
      canEx = false;
      reStart = false;
    } else
    {
      agent = null;
      mazeMaker = null;
  //mazeMaker = new MazeMaker(19, 15);
      mazeMaker = new MazeMaker(79, 45);
      //mazeMaker = new MazeMaker(191, 107);
      canEx = true;
      reStart = true;
    }
  } else if (mouseButton == RIGHT)
  {
    if(canEx && !reStart)
    {
      agent = null;
      //agent = new Agent(mazeMaker.squares, 1, 1, 17, 13);
      agent = new Agent(mazeMaker.squares, 1, 1, 77, 43);
      agent.firstSearch();
    }
  }
}

Agentクラス

class Agent
{
  Square[][] field;
  ArrayList<SquareData> searchedSquare = new ArrayList<SquareData>();
  ArrayList<SquareData> operateSquares = new ArrayList<SquareData>();
  SquareData start;
  SquareData goal;

  Agent(Square[][] field, int startX, int startY, int goalX, int goalY)
  {
    this.field = field;
    start = new SquareData(field[startX][startY], null);
    goal  = new SquareData(field[ goalX][ goalY], null);
    start.data = "START";
    goal.data  = "GOAL";
  }

  void display()
  {
    for (int i = 0; i < searchedSquare.size(); i++)
      searchedSquare.get(i).display();

    start.display();
    goal.display();
  }

  void finish()
  {
    SquareData operateSquare = goal;
    while (operateSquare.previousSquare != start)
    {
      operateSquare.previousSquare.data = "ANSWER";
      operateSquare = operateSquare.previousSquare;
    }
    operateSquares.clear();
  }

  void firstSearch()
  {
    search(start);
  }

  void secondSearch()
  {
    int x = operateSquares.size();
    for (int i = 0; i < x; i++)
    {
      if (search(operateSquares.get(0)))
      {
        finish();
        return;
      }
    }
  }

  boolean search(SquareData operateSquare)
  {
    for (int x = -1; x <= 1; x += 2)
      if (checkCanExpansion(operateSquare, x, 0))
      {
        SquareData addData = new SquareData(field[operateSquare.x + x][operateSquare.y], operateSquare);
        if (addData.x == goal.x && addData.y == goal.y)
        {
          goal.previousSquare = operateSquare;
          return true;
        }
        searchedSquare.add(addData);
        operateSquares.add(addData);
      }
    for (int y = -1; y <= 1; y += 2)
      if (checkCanExpansion(operateSquare, 0, y))
      {
        SquareData addData = new SquareData(field[operateSquare.x][operateSquare.y + y], operateSquare);
        if (addData.x == goal.x && addData.y == goal.y)
        {
          goal.previousSquare = operateSquare;
          return true;
        }
        searchedSquare.add(addData);
        operateSquares.add(addData);
      }
      operateSquares.remove(operateSquare);
    return false;
  }

  boolean checkCanExpansion(SquareData operateSquare, int addX, int addY)
  {
    if (!operateSquare.data.equals("START"))
      if (operateSquare.previousSquare.x == operateSquare.x + addX && operateSquare.previousSquare.y == operateSquare.y + addY)
        return false;

    if (field[operateSquare.x + addX][operateSquare.y + addY].isWall)
      return false;

    return true;
  }
}

SquareDataクラス

class SquareData extends Square
{
  String data;
  SquareData previousSquare;

  SquareData(Square square, SquareData previousSquare)
  {
    super(square.x, square.y, square.size, square.isWall);
    if (square.isWall)
      data = "WALL";
    else
      data = "AISLE";
    this.previousSquare = previousSquare;
  }

  void display()
  {
    switch(data)
    {
    case "ANSWER":
      fill(0, 0, 255);
      break;
    case "WALL":
      fill(0, 0);
      break;
    case "AISLE":
      fill(150, 150, 0, 200);
      break;
    case "START":
      fill(0, 255, 0);
      break;
    case "GOAL":
      fill(200, 0, 0);
      break;
    default:
      fill(255, 255, 0);
      println(data);
    }

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

OneHourProcessing 五日目

OneHourProcessing 五日目

はい、今日作ったのはこちら。

1時間どころか四捨五入で2時間かかってます。
お前…話と違うやんけ…

コード

MazeMaker mazeMaker;
boolean canEx = false;
boolean reStart = false;

void setup()
{
  size(800, 450);
  mazeMaker = new MazeMaker(79, 45);
}

void draw()
{
  background(0);
  mazeMaker.display();
  if (canEx)
    return;
  canEx = mazeMaker.make();
}

void mouseReleased()
{
  if (reStart)
  {
    canEx = false;
    reStart = false;
  }
  else
  {
    mazeMaker = null;
    mazeMaker = new MazeMaker(79, 45);
    canEx = true;
    reStart = true;
  }
}

MazeMakerクラス

class MazeMaker
{
  Square[][] squares;
  ArrayList<Square> isAisle = new ArrayList<Square>();
  ArrayList<Square> stackAisle = new ArrayList<Square>();

  Square operateSquare;

  MazeMaker(int xSize, int ySize)
  {
    squares = new Square[xSize][ySize];
    for (int x = 0; x < squares.length; x++)
      for (int y = 0; y < squares[x].length; y++)
      {
        boolean isWall = false;

        if (x * y == 0 || x == squares.length - 1 || y == squares[0].length - 1)
          isWall = true;
        squares[x][y] = new Square(x, y, height / squares[x].length, isWall);
        if (x % 2 == 0 && y % 2 == 0 && !isWall)
          isAisle.add(squares[x][y]);
      }

    getRandomStart();
  }

  void display()
  {
    for (int x = 0; x < squares.length; x++)
      for (int y = 0; y < squares[x].length; y++)
        squares[x][y].display();
  }

  boolean make()
  {
    ArrayList<PVector> canExpansion = new ArrayList<PVector>();
    for (int x = -2; x <= 2; x += 4)
      if (checkCanExpansion(x, 0))
        canExpansion.add(new PVector(x, 0));
    for (int y = -2; y <= 2; y += 4)
      if (checkCanExpansion(0, y))
        canExpansion.add(new PVector(0, y));

    if (canExpansion.size() == 0)
    {
      if (stackAisle.size() == 0)
      {
        return getRandomStart();
      }
      stackAisle.remove(operateSquare);
      operateSquare = stackAisle.get(stackAisle.size() - 1);
      return false;
    }

    PVector expansion = canExpansion.get(int(random(canExpansion.size())));
    Square nextSquare = squares[operateSquare.x + (int)expansion.x][operateSquare.y + (int)expansion.y];

    squares[(operateSquare.x + nextSquare.x) / 2][(operateSquare.y + nextSquare.y) / 2].isWall = true;

    if (nextSquare.isWall)
      return getRandomStart();

    nextSquare.isWall= true;
    operateSquare = nextSquare;
    stackAisle.add(operateSquare);
    isAisle.remove(operateSquare);

    return false;
  }

  boolean checkCanExpansion(int x, int y)
  {
    for (int i = 0; i < stackAisle.size(); i++)
      if (stackAisle.get(i) == squares[operateSquare.x + x][operateSquare.y + y])
        return false;

    return true;
  }

  boolean getRandomStart()
  {
    int remainderAisle = isAisle.size();

    if (remainderAisle == 0)
      return true;

    operateSquare = isAisle.get(int(random(remainderAisle)));
    operateSquare.isWall = true;
    stackAisle.clear();
    stackAisle.add(operateSquare);
    isAisle.remove(operateSquare);
    return false;
  }
}

Squareクラス

class Square
{
  int x, y;
  float size;
  boolean isWall;

  Square(int x, int y, float size, boolean isWall)
  {
    this.x = x;
    this.y = y;
    this.size = size;
    this.isWall = isWall;
  }

  void display()
  {
    if (isWall)
      fill(255);
    else
      fill(0);

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

  void toWall()
  {
    isWall = true;
  }
}

OneHourProcessing 四日目

OneHourProcessing 四日目

三日坊主で終わりませんでした!!!
うっせやろお前…(自分を信用していない人の図)
本日作った物はこちら。

作るのにかかった時間は「59分14秒」。
時間以内、やったぜ。

コード

Square[][] Squares;
Particle[] particles;

int Time;

void setup()
{
  size(800, 450);
  squareReset();
  particleReset();
}

void draw()
{
  Time++;
  if (Time % 120 == 0)
    squareReset();

  background(0);

  noFill();
  stroke(255);
  for (int x = 0; x < Squares.length; x++)
    for (int y = 0; y < Squares[x].length; y++)
      Squares[x][y].display();

  stroke(0);
  fill(200);
  for (int i = 0; i < particles.length; i++)
    particles[i].update(Squares);
  for (int i = 0; i < particles.length; i++)
    particles[i].display();
}

void squareReset()
{
  float random_x = random(10000);
  float random_y = random(10000);

  Squares = null;
  Squares = new Square[32][18];
  for (int x = 0; x < Squares.length; x++)
    for (int y = 0; y < Squares[x].length; y++)
    {
      float vector = map(noise(random_x + x * 0.1, random_y + y * 0.1), 0, 1, 0, 4 * PI);
      Squares[x][y] = new Square(new PVector(x, y), width / Squares.length, vector);
    }

  Time = 0;
}

void particleReset()
{
  particles = null;
  particles = new Particle[100];
  for (int i = 0; i < particles.length; i++)
    particles[i] = new Particle(new PVector(random(width), random(height)), 20);
}

void keyReleased()
{
  particleReset();
}

Particle(流されてく粒子)クラス

class Particle
{
  PVector position;
  PVector vector;
  PVector acceleration;
  int radius;

  Particle(PVector position, int radius)
  {
    this.position = position;
    this.radius = radius;
    vector = new PVector(0, 0);
    acceleration = new PVector(0, 0);
  }

  void update(Square[][] squares)
  {
    int x = int(map(position.x, 0, width, 0, squares.length));
    int y = int(map(position.y, 0, height, 0, squares[0].length));

    acceleration = squares[x][y].getVector();
    acceleration.div(50);
    vector.add(acceleration);
    position.add(vector);

    if (position.x < width / squares.length)
    {
      position.x = width / squares.length;
      vector.x = 0;
    }
    if (position.x > width - width / squares.length)
    {
      position.x = width - width / squares.length;
      vector.x = 0;
    }
    if (position.y < height / squares[0].length)
    {
      position.y = height / squares[0].length;
      vector.y = 0;
    }
    if (position.y > height - height / squares[0].length)
    {
      position.y = height - height / squares[0].length;
      vector.y = 0;
    }
  }

  void display()
  {
    ellipse(position.x, position.y, radius, radius);
  }
}

Square(後ろ側のマス)クラス

class Square
{
  PVector position;
  float size;
  float angle;

  Square(PVector position, float size, float angle)
  {
    this.position = position;
    this.size     = size;
    this.angle   = angle;
  }

  PVector getVector()
  {
    return new PVector(cos(angle), sin(angle));
  }

  void display()
  {
    float center_x = position.x * size + size / 2;
    float center_y = position.y * size + size / 2;
    
    rect(center_x - size / 2, center_y - size / 2, size, size);
    line(center_x + cos(angle) * size / 4, center_y + sin(angle) * size / 4, center_x - cos(angle) * size / 4,          center_y - sin(angle) * size / 4);
    line(center_x + cos(angle) * size / 4, center_y + sin(angle) * size / 4, center_x + cos(angle + PI / 6) * size / 6, center_y + sin(angle + PI / 6) * size / 6);
    line(center_x + cos(angle) * size / 4, center_y + sin(angle) * size / 4, center_x + cos(angle - PI / 6) * size / 6, center_y + sin(angle - PI / 6) * size / 6);
  }
}

OneHourProcessing 三日目

OneHourProcessing 三日目

今日作ったのはこんな感じの避けゲー

かかった時間は「1時間10分」。
またオーバーしました…

コード

ArrayList<Enemy> enemys = new ArrayList<Enemy>();

int Time;
int Score;

PVector PlayerPos = new PVector(400, 400);
int PlayerSize = 20;
int PlayerCoreRadius = 5;
int PlayerCanMove = 3;

String Phase = "GAME";

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

void draw()
{
  background(0);

  if (Phase.equals("END"))
  {
    textSize(50);
    textAlign(CENTER, CENTER);
    text("GAME OVER (SCORE : " + Score + ")", width / 2, height / 2);
    return;
  }

  Time++;
  Score = Time;

  boolean isTouch = false;

  if (Time % 5 == 0)
    enemys.add(new Enemy(new PVector(random(width), -5), new PVector(0, 5), new PVector(0, 0), 10));

  for (Enemy enemy : enemys)
  {
    enemy.update();
    enemy.display();
    if (enemy.isTouch(PlayerPos, PlayerCoreRadius))
      Phase = "END";
    if (enemy.isTouch(PlayerPos, PlayerSize))
    {
      isTouch = true;
      Score += 5;
    }
  }
  for (int i = 0; i < enemys.size(); i++)
    if (enemys.get(i).isDestroy())
    {
      enemys.remove(i);
      i--;
    }

  PlayerUpdate();
  PlayerDisplay(isTouch);
  
  textSize(20);
  textAlign(LEFT, CENTER);
  text("TIME :" + (Time / 60), 10, 10);
  text("SCORE:" + Score, 10, 30);
  text("MOVE :(ArrowKey)", 10, 50);
  text("RESET:(EnterKey)", 10, 70);
}

void keyPressed()
{
  if (keyCode == ENTER)
    reset();
}

void reset()
{
  Time = 0;
  enemys.clear();
  PlayerPos = new PVector(400, 400);
  Phase = "GAME";
}

void PlayerUpdate()
{
  if (keyPressed)
  {
    switch(keyCode)
    {
    case LEFT:
      PlayerPos.x -= PlayerCanMove;
      break;
    case RIGHT:
      PlayerPos.x += PlayerCanMove;
      break;
    case UP:
      PlayerPos.y -= PlayerCanMove;
      break;
    case DOWN:
      PlayerPos.y += PlayerCanMove;
      break;
    }
  }
  if (PlayerPos.x < 0)
    PlayerPos.x = 0;
  if (PlayerPos.x > width)
    PlayerPos.x = width;
  if (PlayerPos.y < 0)
    PlayerPos.y = 0;
  if (PlayerPos.y > height)
    PlayerPos.y = height;
}

void PlayerDisplay(boolean isTouch)
{
  float rad = PI / 180 * Time;
  float sixty = PI / 3 * 2;
  strokeWeight(1);
  stroke(255);
  ellipse(PlayerPos.x, PlayerPos.y, PlayerCoreRadius * 2, PlayerCoreRadius * 2);
  if (isTouch)
    stroke(255, 0, 0);
  pushMatrix();
  translate(PlayerPos.x + 5, PlayerPos.y);
  rotate(-PI / 2);
  triangle(cos(-rad) * PlayerSize, sin(-rad) * PlayerSize, cos(-rad + sixty) * PlayerSize, sin(-rad + sixty) * PlayerSize, cos(-rad + sixty * 2) * PlayerSize, sin(-rad + sixty * 2) * PlayerSize);
  rotate(PI / 2);
  translate(-10, 0);
  rotate(-PI / 2);
  triangle(cos(rad) * PlayerSize, sin(rad) * PlayerSize, cos(rad + sixty) * PlayerSize, sin(rad + sixty) * PlayerSize, cos(rad + sixty * 2) * PlayerSize, sin(rad + sixty * 2) * PlayerSize);
  popMatrix();
}

Enemyクラス

class Enemy
{
  PVector pos;
  PVector vec;
  PVector acc;
  int radius;

  Enemy(PVector pos, PVector vec, PVector acc, int radius)
  {
    this.pos = pos;
    this.vec = vec;
    this.acc = acc;
    this.radius = radius;
  }

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

  void display()
  {
    strokeWeight(radius / 2);
    stroke(255);
    ellipse(pos.x, pos.y, radius * 2, radius * 2);
  }
  
  boolean isDestroy()
  {
    return pos.y > height + radius;
  }

  boolean isTouch(PVector player_pos, float player_size)
  {
    return pos.dist(player_pos) <= radius + player_size;
  }
}

OneHourProcessing 二日目

二日目の作品

今日作った作品はこちら

作るのにかかった時間は「57分30秒」。
コードは以下の通りです。

Pixel[][] pixel;

void setup()
{
  size(800, 450);
  pixel = new Pixel[width][height];

  for (int x = 0; x < width; x++)
    for (int y = 0; y < height; y++)
      pixel[x][y] = new Pixel(color(255));
}

void draw()
{
  if (mousePressed && mouseButton == LEFT)
    pixel[mouseX][mouseY].now_color = color(0);

  for (int x = 0; x < width; x++)
    for (int y = 0; y < height; y++)
    {
      int left  = x - 1;
      int right = x + 1;
      int up    = y - 1;
      int down  = y + 1;

      if (left == -1)
        left++;
      if (right == width)
        right--;
      if (up == -1)
        up++;
      if (down == height)
        down--;

      pixel[x][y].create(pixel[left][y], pixel[right][y], pixel[x][up], pixel[x][down]);
    }

  loadPixels();

  for (int x = 0; x < width; x++)
    for (int y = 0; y < height; y++)
      pixels[x + y * width] = pixel[x][y].update();

  updatePixels();
}

class Pixel
{
  color now_color;
  color next_color;
  
  Pixel(color default_color)
  {
    now_color = default_color;
    next_color = default_color;
  }

  /**** 周りの色を平均して次の色を決める関数 ****/

  void create(Pixel left, Pixel right, Pixel up, Pixel down)
  {
    float red   = (  red(up.now_color) +   red(down.now_color) +   red(left.now_color) +   red(right.now_color)) / 4;
    float green = (green(up.now_color) + green(down.now_color) + green(left.now_color) + green(right.now_color)) / 4;
    float blue  = ( blue(up.now_color) +  blue(down.now_color) +  blue(left.now_color) +  blue(right.now_color)) / 4;
    
    red   = (  red +   red(now_color)) / 2;
    green = (green + green(now_color)) / 2;
    blue  = ( blue +  blue(now_color)) / 2;
    
    next_color = color(red, green, blue);
  }

  /**** 色を更新する関数 ****/

  color update()
  {
    now_color = next_color;
    return now_color;
  }
}

OneHourProcessing 一日目

OneHourProcessingとは?

OneHourProcessingとは、この僕、霜暮黒夢がなんとなく思い付きで始めたものです。
ルールは、「1時間でProcessingのプログラムを書く」というもの。
とりあえずそれだけです。
もしかしたら3日坊主どころか1日坊主になりそうな所ですが、とりあえず頑張ってみます。

一日目の作品

今日作ったのはこれ。

作るのにかかった時間は「1時間と15分」。初日から思いっきりオーバーしてます。
コードは以下に記載。制限時間を超えてる上にめっさ汚ぇ…

PVector Pos;//球の位置
PVector Vec;//球の速度
int size;//球のサイズ

float Ang;//球を発射する角度

PVector Wall;//壁の穴の始点位置と長さ

PVector Target;//的の位置

String Phase;

int Time;//経過時間
int Count;//反射回数

void setup()
{
  size(800, 450);
  reset_field();
  textSize(20);
}

void draw()
{
  background(0);
  stroke(255, 0, 0);

  if (Phase.equals("clear") || Phase.equals("failure"))
  {
    textAlign(CENTER, CENTER);
    textSize(50);
    if (Phase.equals("clear"))
      text("CLEAR! (SCORE : " + Time * (10 - Count) + " )", width / 2, height / 2 - 50);
    else
      text("FAILURE...", width / 2, height / 2 - 50);

    textSize(20);
    text("(RESTART)[SHIFT], (RESET)[ALT]", width / 2, height / 2 + 50);
    return;
  }
  textSize(20);

  if (Phase.equals("move"))
  {
    if (Pos.x <= 0 || Pos.x >= width)
      reflection(false);
    if (Pos.y <= 0 || Pos.y >= height)
      reflection(true);
    if (Pos.x <= 400 && Pos.x + Vec.x >= 400 && Pos.y >= Wall.x && Pos.y <= Wall.x + Wall.y)
      reflection(false);
    if (Pos.x >= 400 && Pos.x + Vec.x <= 400 && Pos.y >= Wall.x && Pos.y <= Wall.x + Wall.y)
      reflection(false);
    if (Pos.dist(Target) <= (size + 50) / 2)
      Phase = "clear";


    Pos.add(Vec);
  }
  if (Time < 0 || Count > 9)
    Phase = "failure";
  Time--;

  if (Phase.equals("set"))
    line(Pos.x, Pos.y, 100 + cos(Ang) * 300, 225 + sin(Ang) * 300);

  stroke(255);
  fill(255);

  line(400, Wall.x, 400, Wall.x + Wall.y);

  ellipse(Target.x, Target.y, 50, 50);

  ellipse(Pos.x, Pos.y, size, size);//球の描画

  textAlign(LEFT, CENTER);
  text("TIME:" + Time / 60, 10, 15);
  text("COUNT:" + Count, 10, 40);
  text("(MOVE)[UP & DOWN]", 10, 65);
  text("(GO)[ENTER]", 10, 90);
  text("(RESTART)[SHIFT]", 10, 115);
  text("(RESET)[ALT]", 10, 140);
}

void reflection(boolean isHeight)
{
  if (isHeight)
    Vec.y = -Vec.y;
  else
    Vec.x = -Vec.x;

  Count++;
}

void restart()
{
  Pos = new PVector(100, 225);
  Vec = new PVector(0, 0);
  Ang = 0;
  Phase = "set";
  Time = 1800;
  Count = 0;
}

void reset_field()
{
  restart();
  size = 8;
  Wall = new PVector(random(450 - 300), 300);
  Target = new PVector(725, random(25, 425));
}

void keyPressed()
{
  switch(keyCode)
  {
  case UP:
    Ang -= PI / 180;
    break;

  case DOWN:
    Ang += PI / 180;
    break;

  case ENTER:
    if (!Phase.equals("set"))
      break;
    Phase = "move";
    Vec = new PVector(cos(Ang) * 10, sin(Ang) * 10);
    break;

  case SHIFT:
    restart();
    break;

  case ALT:
    reset_field();
    break;
  }
}

何一つ進まない

申し訳ないですが、今日の進捗はありません…。明日の僕に期待しましょう…。
というのもですね、別のプロジェクトをやっていたからなんです…。
それでは、良い黒の夢が見られますように…。