2024年11月4日 星期一

BK Week09 ninja

 Processing 程式碼

用 createFont 顯示 標楷體 和 精靈文
size(500, 500); //大視窗
background(0); //黑色背景
textSize(50);
text("Hello", 50, 50);

PFont font = createFont("標楷體", 50);
textFont(font);
text("中文看到了", 50, 150);

PFont font2 = createFont("elffont-rock.otf", 50);
textFont(font2);
text("ㄇㄉㄈㄎ", 50, 250);


從外面宣告 再顯示一次
PFont font0, font, font2; //3種字型:英文、中文、精靈文(外面宣告, 兩個函式都看的到)
void setup(){
   size(500, 500); //大視窗
   font0 = createFont("Times New Roman", 50);
   font = createFont("標楷體", 50);
   font2 = createFont("elffont-rock.otf", 50); //字型檔, 也要拉進來
}
void draw(){
   background(0); //黑色背景
   textSize(50);
   textFont(font0);
   text("Hello", 50, 50);
   
   textFont(font);
   text("中文看到了", 50, 150);
   
   textFont(font2);
   text("ㄇㄉㄈㄎ", 50, 250);
   
}


匯入 Sound
// Sketch-Library-Manager Libraries 找 sound 可找到 Sound 函式庫(Processing 基金會製作), 安裝!
// File-Example, 選 Libraries核心函式庫, 剛剛裝的 Sound 的 Soundfile 的 SimplePlayback 或 JumbleSound
// 我們自己寫「最精簡的版本」
import processing.sound.*;
SoundFile sound;

void setup(){
   size(500, 500);
   sound = new SoundFile(this, "In Game Music.mp3");
   //sound.play(); //播放1次
   sound.loop(); //一直迴圈播放
}

播放開場音樂 mousePressed()揮劍聲 keyPressed()切換音樂
import processing.sound.*;
SoundFile sound1, sound2, hit;
void setup(){
   size(500, 500);
   sound1 = new SoundFile(this, "In Game Music.mp3");
   sound2 = new SoundFile(this, "Intro Song_Final.mp3");
   hit = new SoundFile(this, "sword slash.mp3");
   sound2.play(); //開始時, 先播放開場音樂
}
void draw(){
    background(255);
}
void mousePressed(){
   hit.play(); 
}
void keyPressed(){
   if(key=='1'){
      sound2.stop();
      sound1.play();
   }else{
      sound1.stop();
      sound2.play();
   }
}

讓蘋果往上拋再落下 運用「重力」的概念
PImage board; //外面宣告變數
void setup(){
   size(600, 400);
   board = loadImage("board.png"); //裡面修改變數
}
float appleX = 100, appleY = 500, appleVX = 3, appleVY = -30;
void draw(){
   background(board); //裡面使用變數
   fill(255, 0, 0);
   ellipse(appleX, appleY, 80, 80); //紅色的蘋果
   appleX += appleVX; //照著運動的速度, X往右移
   appleY += appleVY; //照著運動的速度, Y往右移
   appleVY += 0.98; //0.98 gravity 重力加速度(移動的速度, 會受到「加速度」影響)
   if(appleX > 600){
      appleX = 100; 
      appleY = 500; 
      appleVX = 5; 
      appleVY = -30; 
   } //當蘋果太右邊時, 重設新的蘋果的位置、速度
}


讓蘋果可以 隨機 上拋的 位置 速度
PImage board; //外面宣告變數
void setup() {
  size(600, 400);
  board = loadImage("board.png"); //裡面修改變數
}
float appleX = 100, appleY = 500, appleVX = 5, appleVY = -30;
void randomApple() {
  appleX = random(100, 500);
  appleY = 500;
  if (appleX<300) appleVX = random(0, 8);
  else appleVX = random(-8, 0);
  appleVY = random(-30, -25);
}
void draw() {
  background(board); //裡面使用變數
  fill(255, 0, 0);
  ellipse(appleX, appleY, 80, 80); //紅色的蘋果
  appleX += appleVX; //照著運動的速度, X往右移
  appleY += appleVY; //照著運動的速度, Y往右移
  appleVY += 0.98; //0.98 gravity 重力加速度(移動的速度, 會受到「加速度」影響)
  if (appleX > 600 || appleX < 0 || appleY > 550) {
    randomApple(); //當蘋果出界時, 重設新的蘋果的位置、速度
  }
}



蘋果中間出現英文字母 keyPressed() 會加分 或 扣分
PImage board; //外面宣告變數
void setup() {
  size(600, 400);
  board = loadImage("board.png"); //裡面修改變數
}
float appleX = 100, appleY = 500, appleVX = 5, appleVY = -30;
char appleKey; //蘋果上面, 要秀的字母
void randomApple() {
  appleX = random(100, 500);
  appleY = 500;
  if (appleX<300) appleVX = random(0, 8);
  else appleVX = random(-8, 0);
  appleVY = random(-30, -25);
  appleKey = (char)('a' + int(random(26)));
}
int score = 0;
void keyPressed() {
  if (key==appleKey) {
    score += 100; //答對加分
  } else {
    score -= 200; //答錯扣很多分
  }
}
void draw() {
  background(board); //裡面使用變數
  text("Score:"+score, 400, 50);
  fill(255, 0, 0);
  ellipse(appleX, appleY, 80, 80); //紅色的蘋果
  fill(255, 255, 0);
  textSize(50);
  textAlign(CENTER, CENTER);
  text(appleKey, appleX, appleY);
  appleX += appleVX; //照著運動的速度, X往右移
  appleY += appleVY; //照著運動的速度, Y往右移
  appleVY += 0.98; //0.98 gravity 重力加速度(移動的速度, 會受到「加速度」影響)
  if (appleX > 600 || appleX < 0 || appleY > 550) {
    randomApple(); //當蘋果出界時, 重設新的蘋果的位置、速度
  }
}


加上開頭的 3 2 1 GO 的開始動畫
PImage board; //外面宣告變數
void setup() {
  size(600, 400);
  board = loadImage("board.png"); //裡面修改變數
}
float appleX = 100, appleY = 500, appleVX = 5, appleVY = -30;
char appleKey; //蘋果上面, 要秀的字母
void randomApple() {
  appleX = random(100, 500);
  appleY = 500;
  if (appleX<300) appleVX = random(0, 8);
  else appleVX = random(-8, 0);
  appleVY = random(-30, -25);
  appleKey = (char)('a' + int(random(26)));
}
int score = 0;
void keyPressed() {
  if (state==0) state = 1;
  if (state==2) {
    if (key==appleKey) {
      score += 100; //答對加分
    } else {
      score -= 200; //答錯扣很多分
    }
  }
}
int state = 0; //0: wait, 1: 3, 2, 1, GO, 2: Game
int countdown = 60*4; // 倒數計時3秒+GO1秒
void draw() {
  background(board); //裡面使用變數
  textSize(50);
  textAlign(CENTER, CENTER);
  fill(255, 255, 0);
  if (state==0) text("Press Any Key to Start", 300, 200);
  else if (state==1) {
    textSize(100);
    countdown--;
    if (countdown>60*3) text("3", 300, 200);
    else if (countdown>60*2) text("2", 300, 200);
    else if (countdown>60*1) text("1", 300, 200);
    else if (countdown>0) text("GO", 300, 200);
    else state = 2; //時間用完, 就跳入新畫面
  } else {
    text("Score:"+score, 400, 50);
    fill(255, 0, 0);
    ellipse(appleX, appleY, 80, 80); //紅色的蘋果
    fill(255, 255, 0);
    textSize(50);
    textAlign(CENTER, CENTER);
    text(appleKey, appleX, appleY);
    appleX += appleVX; //照著運動的速度, X往右移
    appleY += appleVY; //照著運動的速度, Y往右移
    appleVY += 0.98;
  } //0.98 gravity 重力加速度(移動的速度, 會受到「加速度」影響)
  if (appleX > 600 || appleX < 0 || appleY > 550) {
    randomApple(); //當蘋果出界時, 重設新的蘋果的位置、速度
  }
}


加上音樂
import processing.sound.*;
SoundFile sound1, sound2, sound3;
PImage board; //外面宣告變數
void setup() {
  size(600, 400);
  board = loadImage("board.png"); //裡面修改變數
  randomApple();
  sound1 = new SoundFile(this, "Intro Song_Final.mp3");
  sound2 = new SoundFile(this, "Gong.mp3");
  sound2 = new SoundFile(this, "In Game Music.mp3");
  sound1.play();
}
float appleX = 100, appleY = 500, appleVX = 5, appleVY = -30;
char appleKey; //蘋果上面, 要秀的字母
void randomApple() {
  appleX = random(100, 500);
  appleY = 500;
  if (appleX<300) appleVX = random(0, 8);
  else appleVX = random(-8, 0);
  appleVY = random(-30, -25);
  appleKey = (char)('a' + int(random(26)));
}
int score = 0;
void keyPressed() {
  if (state==0) state = 1;
  if (state==2) {
    if (key==appleKey) {
      score += 100; //答對加分
    } else {
      score -= 200; //答錯扣很多分
    }
  }
}
int state = 0; //0: wait, 1: 3, 2, 1, GO, 2: Game
int countdown = 60*4; // 倒數計時3秒+GO1秒
void draw() {
  background(board); //裡面使用變數
  textSize(50);
  textAlign(CENTER, CENTER);
  fill(255, 255, 0);
  if (state==0) text("Press Any Key to Start", 300, 200);
  else if (state==1) {
    textSize(100);
    countdown--;
    if (countdown%60 == 0) {
      sound2.stop();
      sound2.play();
    }
    if (countdown>60*3) text("3", 300, 200);
    else if (countdown>60*2) text("2", 300, 200);
    else if (countdown>60*1) text("1", 300, 200);
    else if (countdown>0) text("GO", 300, 200);
    else {
      state = 2; //時間用完, 就跳入新畫面
      sound2.stop();
      sound3.loop();
    }
  } else {
    text("Score:"+score, 400, 50);
    fill(255, 0, 0);
    ellipse(appleX, appleY, 80, 80); //紅色的蘋果
    fill(255, 255, 0);
    textSize(50);
    textAlign(CENTER, CENTER);
    text(appleKey, appleX, appleY);
    appleX += appleVX; //照著運動的速度, X往右移
    appleY += appleVY; //照著運動的速度, Y往右移
    appleVY += 0.98;
  } //0.98 gravity 重力加速度(移動的速度, 會受到「加速度」影響)
  if (appleX > 600 || appleX < 0 || appleY > 550) {
    randomApple(); //當蘋果出界時, 重設新的蘋果的位置、速度
  }
}

沒有留言:

張貼留言