2024年10月28日 星期一

week08

 week08


week08_1_int_page_if_page_0_1





1.用if來 切換page


程式碼


///week08_1_int_page_if_page_0_1
PImage img;
void setup(){
  size(640,480);
  img = loadImage("640x480.png");
}
int page = 0;
void draw(){
  if(page==0) {
    background(0);
    textSize(70);
    textAlign(CENTER,CENTER);
    text("Opening",320,240);//小心,圖檔大小要和size()相同
  } else if(page==1) {
      background(img);
  }
}
void mousePressed(){
  if(page==0) page = 1;
}


week08_2_background_640_480_rect_32_32




1.切格子在背景上


程式碼


///week08_2_background_640_480_rect_32_32


PImage img;

void setup(){

  size(640,480);

  img = loadImage("640x480.png");

}


void draw(){

  background(img);

  for(int i=0;i<15;i++) {

    for(int j=0;j<20;j++)  {

      noFill();

      rect(j*32,i*32,32,32);//小格子的大小事32x32

    }

  }

}



week08_3_RPG_PImage_copy



1.做出紅框框,I和J移動時會修改

2.點選就存檔

程式碼


///week08_3_RPG_PImage_copy


PImage img;

void setup(){

  size(640,480);

  img = loadImage("640x480.png");

}


void draw(){

  background(img);

  for(int i=0;i<15;i++) {

    for(int j=0;j<20;j++)  {

      noFill();

      stroke(0);

      rect(j*32,i*32,32,32);//小格子的大小事32x32

    }

  }

  stroke(255,0,0);

  rect(J*32,I*32,32,32);

}

int I = -1,J = -1;

void mouseMoved(){ ///mouseMove()移動時,要修改I,J

  I = mouseY/32;

  J = mouseX/32;

}

int N = 1;

void mousePressed(){

  PImage now = createImage(32,32,RGB);

  now.copy(img,J*32,I*32,32,32,0,0,32,32);

  now.save(N+".png");

  N++;

}




綠色字


week08_3_RGB_2d_array_floor_map




1.上一個做好的檔案,存到data裡面

2.做出RGB地圖


程式碼


///week08_3_RGB_2d_array_floor_map

int [][] floor = {

  {4,1,1,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1},//20數字

  {4,1,1,1,5,1,1,1,1,1,1,1,1,3,1,1,1,1,2,1},//20數字

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},//20數字

  {4,1,1,1,5,1,3,1,1,1,1,1,13,12,1,1,1,1,2,1}//20數字

};

PImage img;

PImage [] fimg = new PImage[40];

void setup(){

  size(640,480);

  img = loadImage("640x480.png");

  for(int i=1;i<=22;i++) fimg[i] = loadImage(i+".png");

}


void draw(){

  //background(img);

  for(int i=0;i<4;i++) {

    for(int j=0;j<20;j++)  {

      int now = floor[i][j];

      image(fimg[now],j*32,i*32);

    }

  }

}


week08_5_PGB_2d_user_move_wall




1.主角移動


程式碼


///week08_5_PGB_2d_user_move_wall

int [][] floor = {

  {4,1,1,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1},//20數字

  {4,1,1,1,5,1,1,1,1,1,1,1,1,3,1,1,1,1,2,1},//20數字

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},//20數字

  {4,1,1,1,5,1,3,1,1,1,1,1,13,12,1,1,1,1,2,1},//20數字

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1}

};

PImage img,user;

PImage [] fimg = new PImage[40];

void setup(){

  size(640,480);

  img = loadImage("640x480.png");

  user = loadImage("15.png");

  for(int i=1;i<=22;i++) fimg[i] = loadImage(i+".png");

}


int userI = 12,userJ = 15;

void draw(){

  //background(img);

  for(int i=0;i<floor.length;i++) {

    for(int j=0;j<20;j++)  {

      int now = floor[i][j];

      image(fimg[now],j*32,i*32);

    }

  }

  image(user,userJ*32,userI*32);

}

void keyPressed(){

  if(keyCode==RIGHT) userJ++;

  if(keyCode==LEFT) userJ--;

  if(keyCode==UP) userI--;

  if(keyCode==DOWN) userI++;

}


week08_6_PGB_wall_detection





1.限制作出牆壁限制


程式碼


///week08_6_PGB_wall_detection

int [][] floor = {

  {4,1,11,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1},//20數字

  {4,1,11,1,5,1,1,1,1,1,1,1,1,3,1,1,1,1,2,1},//20數字

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},//20數字

  {4,1,11,1,5,1,3,1,1,1,1,1,13,12,1,1,1,1,2,1},//20數字

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,1,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,1,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,1,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1}

};

PImage img,user;

PImage [] fimg = new PImage[40];

void setup(){

  size(640,480);

  img = loadImage("640x480.png");

  user = loadImage("15.png");

  for(int i=1;i<=22;i++) fimg[i] = loadImage(i+".png");

}


int userI = 12,userJ = 15;

void draw(){

  //background(img);

  for(int i=0;i<floor.length;i++) {

    for(int j=0;j<20;j++)  {

      int now = floor[i][j];

      image(fimg[now],j*32,i*32);

    }

  }

  image(user,userJ*32,userI*32);

}

void keyPressed(){

  int newI = userI,newJ = userJ;

  if(keyCode==RIGHT) newJ++;

  if(keyCode==LEFT) newJ--;

  if(keyCode==UP) newI--;

  if(keyCode==DOWN) newI++;

  if(floor[newI][newJ]!=11 && floor[newI][newJ]!=2) {

    userI = newI;

    userJ = newJ;

  }

}


week08_7_PGB_eat_jewel




1.吃到gameover


程式碼


///week08_7_PGB_eat_jewel

int [][] floor = {

  {4,1,11,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1},//20數字

  {4,1,11,1,5,1,1,1,1,1,1,1,1,3,1,1,1,1,2,1},//20數字

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},//20數字

  {4,1,11,1,5,1,3,1,1,1,1,1,13,12,1,1,1,1,2,1},//20數字

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,4,1,1,1,1,1,1,1,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,1,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,1,1},

  {4,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {4,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1}

};

PImage img,user;

PImage [] fimg = new PImage[40];

void setup(){

  size(640,480);

  img = loadImage("640x480.png");

  user = loadImage("15.png");

  for(int i=1;i<=22;i++) fimg[i] = loadImage(i+".png");

}


int userI = 12,userJ = 15;

void draw(){

  //background(img);

  for(int i=0;i<floor.length;i++) {

    for(int j=0;j<20;j++)  {

      int now = floor[i][j];

      image(fimg[now],j*32,i*32);

    }

  }

  image(user,userJ*32,userI*32);

  if(gameOver) background(0,255,0);

}

boolean gameOver = false;

void keyPressed(){

  int newI = userI,newJ = userJ;

  if(keyCode==RIGHT) newJ++;

  if(keyCode==LEFT) newJ--;

  if(keyCode==UP) newI--;

  if(keyCode==DOWN) newI++;

  if(floor[newI][newJ]!=11 && floor[newI][newJ]!=2) {

    userI = newI;

    userJ = newJ;

    if(floor[userI][userJ]==4) gameOver = true;

    floor[userI][userJ] = 2;

  }

}



week08_8_show_info




1.RGB遊戲info


程式碼


///week08_8_show_info

int [][] floor = {

  {1,1,11,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1},//20數字

  {1,1,11,1,5,1,1,1,1,1,1,1,1,3,1,1,1,1,2,1},//20數字

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},//20數字

  {1,1,11,1,5,1,3,1,1,1,1,1,13,12,1,1,1,1,2,1},//20數字

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {1,1,11,1,5,1,1,1,3,1,1,4,1,1,1,1,1,1,1,1},

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,1,1},

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,1,1},

  {1,1,11,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1},

  {1,1,1,1,5,1,1,1,3,1,1,14,1,1,1,1,1,1,2,1}

};

PImage img,user;

PImage [] fimg = new PImage[40];

void setup(){

  size(640,480);

  img = loadImage("640x480.png");

  user = loadImage("15.png");

  for(int i=1;i<=22;i++) fimg[i] = loadImage(i+".png");

}


int userI = 12,userJ = 15;

void draw(){

  //background(img);

  for(int i=0;i<floor.length;i++) {

    for(int j=0;j<20;j++)  {

      int now = floor[i][j];

      image(fimg[now],j*32,i*32);

    }

  }

  image(user,16,16);

  textSize(30);

  text("Level: "+level,16,90);

  textSize(20);

  text("Level: "+level,16,140);

  text("Attack: "+attack,16,170);

  text("Defense: "+defense,16,200);

  text("Mp: "+mp,16,230);

  image(user,userJ*32,userI*32);

  if(gameOver) background(0,255,0);

}

int level = 1,live = 2693,attack = 14,defense = 16,mp = 200;

boolean gameOver = false;

void keyPressed(){

  int newI = userI,newJ = userJ;

  if(keyCode==RIGHT) newJ++;

  if(keyCode==LEFT) newJ--;

  if(keyCode==UP) newI--;

  if(keyCode==DOWN) newI++;

  if(floor[newI][newJ]!=11 && floor[newI][newJ]!=2) {

    userI = newI;

    userJ = newJ;

    if(floor[userI][userJ]==4) gameOver = true;

    floor[userI][userJ] = 2;

  }

}


week08_9_show_debeat_enemy





1.設定互動關係


程式碼


///week08_9_show_debeat_enemy

int [][] floor = {

  {1, 1, 11, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1}, //20數字

  {1, 1, 11, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 2, 1}, //20數字

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1}, //20數字

  {1, 1, 11, 1, 5, 1, 3, 1, 1, 1, 1, 1, 13, 12, 1, 1, 1, 1, 2, 1}, //20數字

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 1, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 1, 1},

  {1, 1, 11, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1},

  {1, 1, 1, 1, 5, 1, 1, 1, 3, 1, 1, 14, 1, 1, 1, 1, 1, 1, 2, 1}

};

PImage img, user;

PImage [] fimg = new PImage[40];

void setup() {

  size(640, 480);

  img = loadImage("640x480.png");

  user = loadImage("15.png");

  for (int i=1; i<=22; i++) fimg[i] = loadImage(i+".png");

}


int userI = 12, userJ = 15;

void draw() {

  //background(img);

  for (int i=0; i<floor.length; i++) {

    for (int j=0; j<20; j++) {

      int now = floor[i][j];

      image(fimg[now], j*32, i*32);

    }

  }

  image(user, 16, 16);

  textSize(30);

  text("Level: "+level, 16, 90);

  textSize(20);

  text("HP: "+hp, 16, 140);

  text("Attack: "+attack, 16, 170);

  text("Defense: "+defense, 16, 200);

  text("Mp: "+mp, 16, 230);

  image(user, userJ*32, userI*32);

  if (gameOver==1) background(0, 255, 0);

  if (gameOver==2) background(255, 0, 0);

}

int level = 1, hp = 2693, attack = 14, defense = 16, mp = 200;

int gameOver = 0;

void keyPressed() {

  int newI = userI, newJ = userJ;

  if (keyCode==RIGHT) newJ++;

  if (keyCode==LEFT) newJ--;

  if (keyCode==UP) newI--;

  if (keyCode==DOWN) newI++;

  if (floor[newI][newJ]==9) {

    if (hp-10<=0) gameOver = 2;

    else {

      hp-=100;

      attack+=50;

      userI = newI;

      userJ = newJ;

      floor[userI][userJ]==2;

    }

  } else if (floor[newI][newJ]!=11 && floor[newI][newJ]!=2) {

    userI = newI;

    userJ = newJ;

    if (floor[userI][userJ]==4) gameOver = true;

    floor[userI][userJ] = 2;

  }

}




沒有留言:

張貼留言