2024年12月16日 星期一

Week15 還剩三周放假

WEEK15
15-1(生成球)

void setup(){
  size(640,360);
}
Ball ball = new Ball(100,200,20);
void draw(){
  ball.display();
}
class Ball{
  float x,y,r;
  Ball(int x0,int y0,int r0){
    x = x0;
    y = y0;
    r = r0;
  }
   void display(){
     ellipse(x,y,r+r,r+r);
   }
}

15-2(生成球)

///main
//week15_02_circle_class_update
void setup(){
  size(640,360);
}
Ball ball = new Ball(100,200,20);
void draw(){
  ball.display();
  ball.update();
}
///Ball
class Ball{
  float x,y,r;
  float vx,vy;
  Ball(int x0,int y0,int r0){
    x = x0;
    y = y0;
    r = r0;
    vx = random(-10,+10);
    vy = random(-10,+10);
  }
  void update(){
    if(x+vx>640 || x+vx<0) vx = -vx;
    if(y+vy>360 || y+vy<0) vy = -vy;
    x += vx;
    y += vy;
  }
   void display(){
     ellipse(x,y,r+r,r+r);
   }
}



15-3(2球)

///main
//week15_03_two_ball 
void setup(){
  size(640,360);
}
Ball ball = new Ball(100,200,60);
Ball ball2 =new Ball(300,200,60);
void draw(){
  background(51);
  ball.display();
  ball.update();
  ball2.display();
  ball2.update();
}
///Ball
class Ball{
  float x,y,r;
  float vx,vy;
  Ball(int x0,int y0,int r0){
    x = x0;
    y = y0;
    r = r0;
    vx = random(-10,+10);
    vy = random(-10,+10);
  }
  void update(){
    if(x+vx>640 || x+vx<0) vx = -vx;
    if(y+vy>360 || y+vy<0) vy = -vy;
    x += vx;
    y += vy;
  }
   void display(){
     ellipse(x,y,r+r,r+r);
   }
}

15-4(2球碰撞)







15-5(生成迷宮)

//week15_05_maze_2D
int [][] maze = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 1, 1, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 1, 0, 0, 0, 0, 1},
  {1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
  {1, 0, 0, 0, 1, 1, 1, 0, 0, 1},
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

void setup() {
  size(400, 400);
}
void draw() {
  for (int i=0; i<maze.length; i++) {
    for (int j=0; j<maze.length; j++) {
      if (maze[i][j]==1) fill(0);
      else fill(255);
      rect(j*40, i*40, 40, 40);
    }
  }

}

15-6(走迷宮)

//week15_06_maze_visited
int [][] maze = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 1, 1, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 1, 0, 0, 0, 0, 1},
  {1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
  {1, 0, 0, 0, 1, 1, 1, 0, 0, 1},
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
int [][] visited = new int[10][10];
int startI =1, startJ = 1, I=1, J=1, step = 1;
void setup() {
  size(400, 400);
  visited[I][J] = step;
}
void draw() {
  for (int i=0; i<maze.length; i++) {
    for (int j=0; j<maze.length; j++) {
      if (maze[i][j]==1) fill(0);
      else fill(255);
      rect(j*40, i*40, 40, 40);
    }
  }
  fill(255, 0, 0);
  rect(startJ*40, startI*40, 40, 40);
}

15-7(右下走迷宮)

//week15_07_maze_visited_moved
int [][] maze = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 1, 1, 1, 1, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 0, 1, 1, 0, 1, 1},
  {1, 0, 1, 0, 1, 0, 0, 0, 0, 1},
  {1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
  {1, 0, 0, 0, 1, 1, 1, 0, 0, 1},
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
int [][] visited = new int[10][10];
int startI =1, startJ = 1, I=1, J=1, step = 1;
void setup() {
  size(400, 400);
  //visited[I][J] = step;
}
void draw() {
  for (int i=0; i<maze.length; i++) {
    for (int j=0; j<maze.length; j++) {
      if (maze[i][j]==1) fill(0);
      else fill(255);
      rect(j*40, i*40, 40, 40);
    }
  }
  fill(255, 0, 0);
  rect(startJ*40, startI*40, 40, 40);
  for (int i=0; i<maze.length; i++) {
    for (int j=0; j<maze.length; j++) {
      if (visited[i][j]>0) {
      fill(0,150,150);
      text(""+visited[i][j],j*40+20,i*40+20);
      }
    }
  }
}

void mousePressed(){
  if(maze[I+1][J]==0){
    I=I+1;
    visited[I][J] = ++step;
  }else if(maze[I][J+1]==0){
    J=J+1;
    visited[I][J] = ++step;
  }
}

15-8(一次走完迷宮)

//week15_08_maze_gotoend
int [][] maze = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
  {1, 0, 1, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 0, 1, 1},
  {1, 0, 0, 0, 0, 0, 1, 0, 1, 1},
  {1, 0, 1, 0, 1, 0, 1, 0, 0, 1},
  {1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
  {1, 0, 0, 0, 1, 1, 1, 0, 0, 1},
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
int [][] visited = new int[10][10];
int startI =1, startJ = 1, I=1, J=1, step = 1;
void setup() {
  size(400, 400);
  //visited[I][J] = step;
}
void draw() {
  for (int i=0; i<maze.length; i++) {
    for (int j=0; j<maze.length; j++) {
      if (maze[i][j]==1) fill(0);
      else fill(255);
      rect(j*40, i*40, 40, 40);
    }
  }
  fill(255, 0, 0);
  rect(startJ*40, startI*40, 40, 40);
  for (int i=0; i<maze.length; i++) {
    for (int j=0; j<maze.length; j++) {
      if (visited[i][j]>0) {
      fill(0,150,150);
      text(""+visited[i][j],j*40+20,i*40+20);
      }
    }
  }
}
boolean DFS(int i,int j,int s){
  if(i== 8&& j==8) return true;
  if(visited[i][j] > 0)return false;
  if(maze [i][j]==1)return false;
  visited[i][j] = s;
  if( DFS(i+1,j,s+1) )return true;
  if( DFS(i-1,j,s+1) )return true;
  if( DFS(i,j+1,s+1) )return true;
  if( DFS(i,j-1,s+1) )return true;
  visited[i][j] = 0;
  return false;
}
void mousePressed(){
  visited[1][1] = 0;
  DFS(1,1,1);
}




沒有留言:

張貼留言