2024年12月2日 星期一

WEEK13-翁逸豪

week13-0

//計時
void setup(){
  size(300,300);
}
int m;
int click=0;
void draw() {
  m = millis()/1000;
  if(click>=1){
    print(" "+(m-s));
  }
}
int s;
void mousePressed(){
  if(click==0){
    s=m;
    click+=1;
  }
}

week13-1

//week13_1_animation_background_array_I_frameCount
PImage [] img = new PImage[3];
void setup(){
  size(550,370);
  img[0] = loadImage("snail01.png");
  img[1] = loadImage("snail02.png");
  img[2] = loadImage("snail03.png");
}
int I = 2;
void draw(){
  background(img[I]);
  if(frameCount%20==0) I = (I+1)/3;
}
用連續圖片做動畫

week13-2

//week13_2_animation_background_frameCount_x
void setup() {
  size(500, 300);
}
void draw() {
  background(#FFFFF2);
  float x = (frameCount*5%1000);
  if (x>500) x = 1000-x;
  for (int y=30; y<300; y+=50) {
    ellipse(x, y, 30, 30);
  }
}
用物體的重複移動做動畫

week13-3

//week13_3_animation_background_frog_pushMatrix_translate_rotate_popMatrix
PImage img;
void setup() {
  size(1000, 600);
  img = loadImage("img.png");
  imageMode(CENTER);
}
void frog(int x, int y) {
  pushMatrix();
    translate(x, y);
    rotate(radians(frameCount));
    image(img, 0, 0);
  popMatrix();
}
void draw() {
  background(255);
  frog(mouseX, mouseY);
  frog(200, 200);
  frog(800, 200);
  frog(200, 400);
  frog(800, 400);
}
用push跟pop Matrix做動畫

week13-4

//week13_4_PShape_gundam_loadShape_shape_obj_mtl_jpg
PShape gundam;
void setup(){
  size(500,500,P3D);
  gundam = loadShape("Gundam.obj");
}
void draw(){
  shape(gundam,250,0,250,500);
}
匯入3D模型

week13-5

//week13_5_PShape_gundam_loadShape_shape_pushMatrix_scale_translate_rotate_popMatrix
PShape gundam;
void setup() {
  size(500, 500, P3D);
  gundam = loadShape("Gundam.obj");
}
void draw() {
  background(#BBFF81);
  pushMatrix();
    translate(mouseX, mouseY);
    rotateY(radians(frameCount));
    rotate(radians(180));
    scale(5,5,5);//增加厚度
    shape(gundam, 0, 0);
  popMatrix();
}
旋轉3D模型

week13-6

//week13_6_PShape_gundam_for_loop
PShape gundam;
void setup() {
  size(500, 500, P3D);
  gundam = loadShape("Gundam.obj");
}
void draw() {
  background(#BBFF81);
  for(int x=0;x<=500;x+=500/4){
    drawGundam(x,300);
  }
}
void drawGundam(int x,int y){
  pushMatrix();
    translate(x, y);
    rotateY(radians(frameCount));
    rotate(radians(180));
    scale(10,10,10);
    shape(gundam, 0, 0);
  popMatrix();
}
用函數呼叫畫鋼彈

week13-7

//week13_7_terris_falling
void setup(){
  size(300,600);
}
float x = 50,y = 50;
void draw(){
  
  background(0);
  fill(153,0,204);
  rect(x,y-25,25,25);
  rect(x+25,y,25,25);
  rect(x,y,25,25);
  rect(x,y+25,25,25);
  if(frameCount%50==0) y+=50;
}
void keyPressed(){
  if(keyCode==RIGHT) x+=25;
  if(keyCode==LEFT) x-=25;
}
方塊掉落

week13-8

//week13_8_terris_grid_2d_array_fill_rect
void setup(){
  size(240,440);
}
void draw(){
  for(int i=0;i<22;i++){
    for(int j=0;j<12;j++){
      if(grid[i][j]==1)fill(119,119,119);
      if(grid[i][j]==0)fill(0);
      if(grid[i][j]==2)fill(153,0,204);
      rect(j*20,i*20,20,20);
    }
  }
}
int [][]grid = {
  {1,1,1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,2,0,0,0,0,0,0,1},
  {1,0,0,0,2,2,0,0,0,0,0,1},
  {1,0,0,0,2,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},//week13_8_terris_grid_2d_array_fill_rect
void setup(){
  size(240,440);
}
void draw(){
  for(int i=0;i<22;i++){
    for(int j=0;j<12;j++){
      if(grid[i][j]==1)fill(119,119,119);
      if(grid[i][j]==0)fill(0);
      if(grid[i][j]==2)fill(153,0,204);
      rect(j*20,i*20,20,20);
    }
  }
}
int [][]grid = {
  {1,1,1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,2,0,0,0,0,0,0,1},
  {1,0,0,0,2,2,0,0,0,0,0,1},
  {1,0,0,0,2,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,1,1,1,1,1,1,1,1,1,1,1},
};
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,1,1,1,1,1,1,1,1,1,1,1},
};
俄羅斯方塊

week13-9

//week13_9_tetris_color_array_c_now
color [] c= {#000000,#777777,#9900CC,#CCCC00,#00B500,#CC0000};
           //黑色    灰色    紫色    黃色    綠色    紅色
void setup(){
  size(240,440);
}
void draw(){
  for(int i=0;i<22;i++){
    for(int j=0;j<12;j++){
      int now = grid[i][j];
      fill(c[now]);
      rect(j*20,i*20,20,20);
    }
  }
}
int [][]grid = {
  {1,1,1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,2,0,0,0,0,0,0,1},
  {1,0,0,0,2,2,0,0,0,0,0,1},
  {1,0,0,0,2,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,2,0,0,0,0,0,0,1},
  {1,0,0,0,2,2,0,0,0,0,0,1},
  {1,0,0,0,2,0,0,0,0,0,0,1},
  {1,1,1,1,1,1,1,1,1,1,1,1},
};
用陣列來設定顏色

week13-10

//week13_10_tetris_falling_if_frameCount_for_for_if
color [] c= {#000000,#777777,#9900CC,#CCCC00,#00B500,#CC0000};
           //黑色    灰色    紫色    黃色    綠色    紅色
void setup(){
  size(240,440);
}
void draw(){
  for(int i=0;i<22;i++){
    for(int j=0;j<12;j++){
      int now = grid[i][j];
      fill(c[now]);
      rect(j*20,i*20,20,20);
    }
  }
  if(frameCount%50==0){
    int bad=0;
    for(int i=20;i>=1;i--){
      for(int j=1;j<12-1;j++){
        if(grid[i][j]==2){
          if(grid[i+1][j]!=0 && grid[i+1][j]!=2){
            bad=1;
          }
        }
      }
    }
    if(bad==0){
        for(int i=20;i>=1;i--){
          for(int j=1;j<12-1;j++){  
            if(grid[i][j]==2){
              grid[i+1][j]=2;
              grid[i][j]=0;
            }
          }
        }
      }
  }
}
...
往下掉

week13-11

//week13_11_tetris_falling_alive_9_freeze_dead_T
color [] c= {#000000, #777777, #9900CC, #CCCC00, #00B500, #CC0000, #000DCC, #CC9900, #0000CC, #FFFFFF};
//黑色    灰色    紫色    黃色    綠色    紅色
int T=2;
void setup() {
  size(240, 440);
}
void draw() {
  for (int i=0; i<22; i++) {
    for (int j=0; j<12; j++) {
      int now = grid[i][j];
      fill(c[now]);
      rect(j*20, i*20, 20, 20);
    }
  }
  if (frameCount%50==0) {
    int bad=0;
    for (int i=20; i>=1; i--) {
      for (int j=1; j<12-1; j++) {
        if (grid[i][j]==9) {
          if (grid[i+1][j]!=0 && grid[i+1][j]!=9)
            bad=1;
        }
      }
    }
    if (bad==0) {
      for (int i=20; i>=1; i--) {
        for (int j=1; j<12-1; j++) {
          if (grid[i][j]==9) {
            grid[i+1][j]=9;
            grid[i][j]=0;
          }
        }
      }
    } else {
      for (int i=20; i>=1; i--) {
        for (int j=1; j<12-1; j++) {
          if (grid[i][j]==9) {
            grid[i][j]=T;
          }
        }
      }
    }
  }
}
...
初始為白色,碰到東西後變成其他顏色



沒有留言:

張貼留言