2024年10月28日 星期一

SY-BlingBangBang week08 遊戲製作點播

   [TODO]

上課內容:

遊戲頁面切換

// week08_1_int_page_0_1
PImage img;
void setup(){
  size(640,480);
  img = loadImage("640x480.png");
}
int page = 0; //0:開場畫面 1:第一關 2:第二關
void draw(){
  if(page==0){
    background(0);
    textSize(70);
    textAlign(CENTER,CENTER);
    text("Opening",320,240);
  }
  else if(page==1){
    background(img); //小心,圖檔大小要與size()相同,才能變背景
  }
}
void mousePressed(){
  if(page==0) page=1;
}


遊戲RPG
// week08_2_RPG_background_640_480_rect_32_32
PImage img;
void setup(){
  size(640,480); //15格=480,480/15=32小圖
  img = loadImage("640x480.png");
}
void draw(){
  background(img);
  for(int i=0; i<15; i++){ //高度 480
    for(int j=0; j<20; j++){ //寬度 640
      noFill();
      rect(j*32, i*32, 32, 32); //小格子的大小是 32x32
    }
  }
}



遊戲RPG
// week08_3_RPG_PImage_createImage_copy_save
PImage img;
void setup(){
  size(640,480); //15格=480,480/15=32小圖
  img = loadImage("640x480.png");
}
void draw(){
  background(img);
  for(int i=0; i<15; i++){ //高度 480
    for(int j=0; j<20; j++){ //寬度 640
      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(){ // void Moved() 移動時,要修改I, J
  I = mouseY/32; //小心Y
  J = mouseX/32; //小心X
}
void mousePressed(){
  //PImage now = createImage(640, 480, RGB);
  //now.copy(img, 0, 0, 640, 480, 0, 0, 640, 480);
  PImage now = createImage(32, 32, RGB);
  now.copy(img, J*32, I*32, 32, 32, 0, 0, 32, 32);
  now.save("1.png");
}

*按Ctrl+K 開啟檔案目錄,看圖片變化

遊戲RPG
// week08_4_RPG_2d_array_floor_map
int [][] floor = {
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,1,19,3,22,3,22,9,2,2,2,9,1,16,5}, //有20個數字
  {1,1,1,1,1,1,1,2,3,18,3,9,9,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,2,3,9,9,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,7,8,9,10,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,18,3,11,10,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,18,3,9,7,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,18,3,9,5,9,6,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,18,3,9,8,10,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,7,8,9,10,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,18,3,11,10,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,2,3,9,9,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,18,3,11,10,9,2,2,2,9,1,5},
  {1,1,1,1,1,1,1,2,3,18,3,11,10,9,2,2,2,9,1,5},
};
PImage img;
PImage [] fimg = new PImage[40];
void setup(){
  size(640,480); //15格=480,480/15=32小圖
  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<floor.length; i++){ //高度 480
    for(int j=0; j<20; j++){ //寬度 640
      int now = floor[i][j];
      image(fimg[now], j*32, i*32);
    }
  }
}



遊戲RPG
// week08_5_RPG_user_move_userJ_userI_keyPressed
int [][] floor = {
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1}, //有20個數字
  {1,1,1,1,1,1,4,2,3,18,3,9,9,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,11,3,2,3,9,9,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,10,3,7,8,9,10,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,2,6,18,3,11,10,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,21,6,18,3,9,7,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,22,7,18,3,9,11,9,6,2,2,4,1,1},
  {1,1,1,1,1,1,4,9,11,18,3,9,8,10,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,2,10,7,8,9,10,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,14,20,18,3,11,10,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
PImage img,user;
PImage [] fimg = new PImage[40];
void setup(){
  size(640,480); //15格=480,480/15=32小圖
  img = loadImage("640x480.png");
  user = loadImage("5.png"); //勇者/使用者
  for(int i=1; i<=22; i++){
    fimg[i] = loadImage(i+".png");
  }
}
int userI = 11, userJ = 15;
void draw(){
  //background(img);
  for(int i=0; i<floor.length; i++){ //高度 480
    for(int j=0; j<20; j++){ //寬度 640
      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++;
}


遊戲RPG
// week08_6_RPG_wall_detection
int [][] floor = {
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1}, //有20個數字
  {1,1,1,1,1,1,4,2,3,18,3,9,9,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,11,3,2,3,9,9,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,10,3,7,8,7,10,9,6,2,2,4,1,1},
  {1,1,1,1,1,1,4,2,6,18,3,11,10,9,2,7,2,4,1,1},
  {1,1,1,1,1,1,4,21,6,18,3,9,7,9,2,2,8,4,1,1},
  {1,1,1,1,1,1,4,22,7,18,3,9,11,9,6,2,2,4,1,1},
  {1,1,1,1,1,1,4,9,11,18,3,9,8,10,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,2,10,7,8,9,10,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,14,20,18,3,11,10,9,2,2,2,4,1,1},
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
PImage img,user;
PImage [] fimg = new PImage[40];
void setup(){
  size(640,480); //15格=480,480/15=32小圖
  img = loadImage("640x480.png");
  user = loadImage("5.png"); //勇者/使用者
  for(int i=1; i<=22; i++){
    fimg[i] = loadImage(i+".png");
  }
}
int userI = 11, userJ = 15;
void draw(){
  //background(img);
  for(int i=0; i<floor.length; i++){ //高度 480
    for(int j=0; j<20; j++){ //寬度 640
      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]!=1 && floor[newI][newJ]!=3 && floor[newI][newJ]!=4 && floor[newI][newJ]!=9){ //不是石頭牆,也不是木頭牆,才能走過去
    userI = newI;
    userJ = newJ;
  }
}


遊戲RPG
// week08_7_RPG_eat_good
int [][] floor = {
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1}, //有20個數字
  {1,1,1,1,1,1,4,20,3,18,3,9,9,9,11,10,8,4,1,1},
  {1,1,1,1,1,1,4,11,15,2,3,9,9,9,13,12,7,4,1,1},
  {1,1,1,1,1,1,4,10,3,7,8,7,10,9,6,10,8,4,1,1},
  {1,1,1,1,1,1,4,13,6,20,3,11,10,9,11,7,6,4,1,1},
  {1,1,1,1,1,1,4,21,6,18,3,9,7,9,12,20,8,4,1,1},
  {1,1,1,1,1,1,4,22,7,3,3,9,11,9,6,13,22,4,1,1},
  {1,1,1,1,1,1,4,9,11,10,3,9,8,10,22,16,17,4,1,1},
  {1,1,1,1,1,1,4,12,10,7,8,9,10,9,21,17,22,4,1,1},
  {1,1,1,1,1,1,4,14,20,17,3,11,10,9,13,16,16,4,1,1},
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
PImage img,user;
PImage [] fimg = new PImage[40];
void setup(){
  size(640,480); //15格=480,480/15=32小圖
  img = loadImage("640x480.png");
  user = loadImage("5.png"); //勇者/使用者
  for(int i=1; i<=22; i++){
    fimg[i] = loadImage(i+".png");
  }
}
int userI = 11, userJ = 15;
void draw(){
  //background(img);
  for(int i=0; i<floor.length; i++){ //高度 480
    for(int j=0; j<20; j++){ //寬度 640
      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]!=1 && floor[newI][newJ]!=3 && floor[newI][newJ]!=4 && floor[newI][newJ]!=9){ //不是石頭牆,也不是木頭牆,才能走過去
    userI = newI;
    userJ = newJ;
    if(floor[userI][userJ]==14) gameOver = true; //走到樓上,結束囉
    floor[userI][userJ] = 19;
  }
}


遊戲RPG
// week08_8_RPG_show_info
int [][] floor = {
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1}, //有20個數字
  {1,1,1,1,1,1,4,20,3,18,3,9,9,9,11,10,8,4,1,1},
  {1,1,1,1,1,1,4,11,15,2,3,9,9,9,13,12,7,4,1,1},
  {1,1,1,1,1,1,4,10,3,7,8,7,10,9,6,10,8,4,1,1},
  {1,1,1,1,1,1,4,13,6,20,3,11,10,9,11,7,6,4,1,1},
  {1,1,1,1,1,1,4,21,6,18,3,9,7,9,12,20,8,4,1,1},
  {1,1,1,1,1,1,4,22,7,3,3,9,11,9,6,13,22,4,1,1},
  {1,1,1,1,1,1,4,9,11,10,3,9,8,10,22,16,17,4,1,1},
  {1,1,1,1,1,1,4,12,10,7,8,9,10,9,21,17,22,4,1,1},
  {1,1,1,1,1,1,4,14,20,17,3,11,10,9,13,16,16,4,1,1},
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
PImage img,user;
PImage [] fimg = new PImage[40];
void setup(){
  size(640,480); //15格=480,480/15=32小圖
  img = loadImage("640x480.png");
  user = loadImage("5.png"); //勇者/使用者
  for(int i=1; i<=22; i++){
    fimg[i] = loadImage(i+".png");
  }
}
int userI = 11, userJ = 15;
void draw(){
  //background(img);
  for(int i=0; i<floor.length; i++){ //高度 480
    for(int j=0; j<20; j++){ //寬度 640
      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("Live: " + live, 16, 140);
  text("Attack: " + attack, 16, 170);
  text("Defense: " + defense, 16, 200);
  text("Magic: " + magic, 16, 230);
  text("Exp: " + exp, 16, 260);
  text("Coins: " + coin, 16, 290);
  image(user, userJ*32, userI*32);
  if(gameOver) background(0, 255, 0);
}
int level = 1, live = 2693, attack = 14, defense = 16, magic = 10, exp = 72, coin = 72;
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]!=1 && floor[newI][newJ]!=3 && floor[newI][newJ]!=4 && floor[newI][newJ]!=9){ //不是石頭牆,也不是木頭牆,才能走過去
    userI = newI;
    userJ = newJ;
    if(floor[userI][userJ]==14) gameOver = true; //走到樓上,結束囉
    floor[userI][userJ] = 19;
  }
}


遊戲RPG
// week08_9_RPG_attack_amge
int [][] floor = {
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //有20個數字
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1}, //有20個數字
  {1,1,1,1,1,1,4,20,3,18,3,9,9,9,11,10,8,4,1,1},
  {1,1,1,1,1,1,4,11,15,2,3,9,9,9,13,12,7,4,1,1},
  {1,1,1,1,1,1,4,10,3,7,8,7,10,9,6,10,8,4,1,1},
  {1,1,1,1,1,1,4,13,6,20,3,11,10,9,11,7,6,4,1,1},
  {1,1,1,1,1,1,4,21,6,18,3,9,7,9,12,20,8,4,1,1},
  {1,1,1,1,1,1,4,22,7,3,3,9,11,9,6,13,22,4,1,1},
  {1,1,1,1,1,1,4,9,11,10,3,9,8,10,22,16,17,4,1,1},
  {1,1,1,1,1,1,4,12,10,7,8,9,10,9,21,17,22,4,1,1},
  {1,1,1,1,1,1,4,14,20,17,3,11,10,9,13,16,16,4,1,1},
  {1,1,1,1,1,1,4,4,4,4,4,4,4,4,4,4,4,4,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
PImage img,user;
PImage [] fimg = new PImage[40];
void setup(){
  size(640,480); //15格=480,480/15=32小圖
  img = loadImage("640x480.png");
  user = loadImage("5.png"); //勇者/使用者
  for(int i=1; i<=22; i++){
    fimg[i] = loadImage(i+".png");
  }
}
int userI = 11, userJ = 15;
void draw(){
  //background(img);
  for(int i=0; i<floor.length; i++){ //高度 480
    for(int j=0; j<20; j++){ //寬度 640
      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("Magic: " + magic, 16, 230);
  text("Exp: " + exp, 16, 260);
  text("Coins: " + coin, 16, 290);
  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, magic = 10, exp = 72, coin = 72;
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]==11){ //遇到魔法師
    if(hp-10<=0) gameOver = 2;
    else{
      hp -= 10; //扣你的血
      coin += 10; //加錢
      exp += 1; //加經驗值
      userI = newI;
      userJ = newJ;
      floor[userI][userJ] = 2;
    }
  }
  else if(floor[newI][newJ]!=1 && floor[newI][newJ]!=3 && floor[newI][newJ]!=4 && floor[newI][newJ]!=9){ //不是石頭牆,也不是木頭牆,才能走過去
    userI = newI;
    userJ = newJ;
    if(floor[userI][userJ]==14) gameOver = 1; //走到樓上,結束囉
    floor[userI][userJ] = 19;
  }
}



Week08當不了人辣,JOJO。享受期中考/作業的折磨

 Week08

切換圖片
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);
  }else if(page==1){
      background(img);
  }
}
void mousePressed(){
   if(page==0) page = 1; 
}


PImage img;
void setup(){
   size(640,480);//15格=480,480/15=32
   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);
      }
   }
}

擷取一塊圖片
PImage img;
void setup(){
   size(640,480);//15格=480,480/15=32
   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);
      }
   }
   stroke(255,0,0);
   rect(J*32, I*32, 32, 32);
}
int I = -1, J = -1;
void mouseMoved(){
   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++;
}


int [][] floor = {
  {3, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3}
};
PImage img;
PImage [] fimg = new  PImage[40];
void setup(){
   size(640,480);//15格=480,480/15=32
   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<floor.length; i++){//高
      for(int j=0; j<20; j++){//寬
         int now = floor[i][j];
         image(fimg[now],j*32, i*32);
      }
   }
}

int [][] floor = {
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 5, 6, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 7, 8, 9, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 10, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 18, 19, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3}
};
PImage img, user;
PImage [] fimg = new  PImage[40];
void setup(){
   size(640,480);//15格=480,480/15=32
   img = loadImage("640x480.png");
   user = loadImage("20.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++;
}

int [][] floor = {
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 5, 6, 3, 3, 3, 14, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 7, 8, 9, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 10, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 18, 19, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3}
};
PImage img, user;
PImage [] fimg = new  PImage[40];
void setup(){
   size(640,480);//15格=480,480/15=32
   img = loadImage("640x480.png");
   user = loadImage("20.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]!=2 && floor[newI][newJ]!=4){     
      userI = newI;
      userJ = newJ;
      if(floor[userI][userJ]==14)gameOver = true;
      floor[userI][userJ] = 3;
   }
}



int [][] floor = {
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 5, 6, 3, 3, 3, 14, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 7, 8, 9, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 10, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 18, 19, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3},
  {3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3}
};
PImage img, user;
PImage [] fimg = new  PImage[40];
void setup() {
  size(640, 480);//15格=480,480/15=32
  img = loadImage("640x480.png");
  user = loadImage("20.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("ATK:   "+ATK, 16, 170);
  text("Defense:   "+defense, 16, 200);
  text("Magic:   "+ magic, 16, 260);
  text("Coins:  "+ coin, 16, 290);

  image(user, userJ*32, userI*32);
  if (gameOver==1)background(0, 255, 0);
  if (gameOver==2)background(0, 255, 0);
}
int level = 1, hp = 2693, ATK = 14, defense = 16, magic=10, exp =73, coin = 0;
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]==14) {
    if (hp-10<=0)gameOver = 2;
    else {
      hp -= 10;
      userI = newI;
      userJ = newJ;
      floor[userI][userJ] = 2;
    }
  } else if (floor[newI][newJ]!=2 && floor[newI][newJ]!=4) {
    userI = newI;
    userJ = newJ;
    if (floor[userI][userJ]==14)gameOver = 1;
    floor[userI][userJ] = 3;
  }
}

Week08

 RPG

08_1

//week_08_01_int_page

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

  } else if (page==1) {

    background(img);

  }

}

void mousePressed() {

  if (page==0) page=1;

}


08-2

// week08_02_RPG_backgroung
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);
    }
  }
}

08-3

//week08_03_RPG_2d_floor_map
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);
    }
  }
  stroke(255,0,0);
  rect(J*32, I*32, 32, 32);
}
int I = -1,J = -1;
void mouseMoved(){
  I = mouseY/32;
  J = mouseX/32;
}
void mousePressed(){
  PImage now = createImage(32,32, RGB);
  now.copy(img,J*32,I*32,32,32,0,0,32,32);
  now.save("1.png");
}

08-4

//week08_04_RPG_2d_floor_map
int [][] floor = {
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 19, 3, 22, 3, 22, 9, 2, 2, 2, 9, 2, 16, 5},
  {4, 1, 1, 1, 5, 4, 1, 19, 3, 22, 3, 22, 9, 2, 2, 2, 9, 2, 16, 5},
};
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] = (i+".png");
}
void draw()
{
  for (int i=0; i<3; i++) {
    for (int j=0; j<20; j++) {
      int now = floor[i][j];
      image(fimg[now],j*32,i*32);
    }
  }
}

Week08-唐門外系弟子-林活修行互動技術秘笈

 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);
  } else if (page==1) {
    background(img);
  }
}
void mousePressed() {
  if (page==0) page = 1;
}

week08_02_RPG_backgournd_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);
    }
  }
}
week08_03_RPG_PImage_createImage_copy_save
現在利用一個紅框的方式去選取地圖的一格方格,選取到的圖片就存取下來。
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);
    }
  }
  stroke(255,0,0);
  rect(J*32, I*32, 32, 32);
  
}
int I = -1, J = -1;
void mouseMoved(){
  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_04_RPG_2d_array_floor_map
利用上回的功能將所有需要的素材,我們要開始繪製地圖。
int [][] floor ={ 
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1,19, 3,22, 3,22, 9, 2, 2, 2, 9,16, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 2, 3,18, 3, 9, 9, 9, 2, 2, 2, 9, 1, 5},
};
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<floor.length;i++){
    for(int j=0;j<20;j++){
      int now = floor[i][j];
      image(fimg[now], j*32, i*32);
    }
  }
}
week08_05_RPG_user_move_userJ_userI_keyPressed
繪製完成後,要讓玩家的角色出現並讓角色可以用key去移動,但是會穿牆。
int [][] floor ={ 
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1,19, 3,22, 3,22, 9, 2, 2, 2, 9,16, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 2, 3,18, 3, 9, 9, 9, 2, 2, 2, 9, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
};
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_06_RPG_wall_detection
要設置牆壁地形,讓玩家不可以穿透牆壁房門等障礙,
切記要確認障礙物的圖檔名稱。
int [][] floor ={ 
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1,19, 3,22, 3,22, 9, 2, 2, 2, 9,16, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 2, 3,18, 3, 9, 9, 9, 2, 2, 2, 9, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
};
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]!=4 && floor[newI][newJ]!=5){
    userI = newI;
    userJ = newJ;
  }
}
week08_07_RPG_eat_good
我們設置功能讓玩家可以吃掉物件,然後碰到某物件會gameover。
int [][] floor ={ 
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1,19, 3,22, 3,22, 9, 2, 2, 2, 9,16, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 2, 3,18, 3, 9, 9, 9, 2, 2, 2, 9, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 1, 1, 1, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 21, 2, 2, 2, 2, 2, 2, 5},
};
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]!=4 && floor[newI][newJ]!=5){
    userI = newI;
    userJ = newJ;
    if(floor[userI][userJ]==21) gameOver = true;  
    floor[userI][userJ] = 2;
  }
}

week08_08_RPG_show_info
在左側顯示角色的能力值。
int [][] floor ={ 
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1,19, 3,22, 3,22, 9, 2, 2, 2, 9,16, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 2, 3,18, 3, 9, 9, 9, 2, 2, 2, 9, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4,4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 4, 4, 4, 5, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 4, 4, 4, 5, 4, 2, 2, 2, 2, 2, 2, 21, 2, 2, 2, 2, 2, 2, 5},
};
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("Magic Defense: "+magic, 16, 230);
  text("Exp:  "+exp,  16,260);
  text("Coins:  "+coin,  16,290);
  image(user, userJ*32, userI*32);
  if(gameOver) background(0, 255, 0);
}
int level = 1, hp= 2693, attack = 14, defense = 16,magic=10, exp=72,coin = 72;

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]!=4 && floor[newI][newJ]!=5){
    userI = newI;
    userJ = newJ;
    if(floor[userI][userJ]==21) gameOver = true;  
    floor[userI][userJ] = 2;
  }
}
week08_09_RPG_attack_magician
設定魔法師的素質設定,讓魔法師可以跟玩家對打。打贏魔法師,
有失去也有獲得。
int [][] floor ={
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 19, 3, 22, 3, 22, 9, 2, 2, 2, 9, 16, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 2, 3, 18, 3, 9, 9, 9, 2, 2, 2, 9, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5},
  {4, 4, 4, 4, 5, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5},
  {4, 4, 4, 4, 5, 4, 2, 2, 2, 2, 2, 2, 21, 2, 2, 2, 2, 2, 2, 5},
  {4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5},
};
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("Magic Defense: "+magic, 16, 230);
  text("Exp:  "+exp, 16, 260);
  text("Coins:  "+coin, 16, 290);
  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, magic=10, exp=72, coin = 72;
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 -= 10;
      coin +=10;
      exp += 1;
      userI = newI;
      userJ = newJ;
      floor[userI][userJ]=2;
    }
  } else if (floor[newI][newJ]!=4 && floor[newI][newJ]!=5) {
    userI = newI;
    userJ = newJ;
    if(floor[newI][newJ]==21) gameOver = 1;
    floor[userI][userJ] = 2;
  }
}