2024年11月4日 星期一

week09 期中考週

 1.第一節上課

精靈文下載



將下載的精靈文丟入processing
安裝文字
//week09_1_createFont_loadFont_textFont
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);

//week09_2_PFont_outside_void_setup_createFont_void_draw_text
PFont font0,font,font2;
void setup(){  
  size(500,500);//大視窗
  font0 = createFont("Ariel",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);
}
改個方法,利用之前寫的方式但是會很吃記憶體

到library
找到音樂的函式
到範例核心函式庫找到sound的simpleplayback

//week09_3_import_processing_sound 聲音相關 使用音樂相關的library函式庫
//Sketch-Library-Manger Libraries 找sound 可找到sound 函式庫(Processing基金會做的),安裝!
import processing.sound.*;
SoundFile sound;

void setup(){
  size(500,500);
  sound = new SoundFile(this,"In Game Music.mp3");
  sound.play();
}
void draw(){
  
}


2.第二節上課
//week09_4_multiple_sound_files_keyPressed_stop_play
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();//mouse按下時,揮劍的聲音
}
void keyPressed(){
  if(key=='1'){
    sound2.stop();
    sound1.play();
  }else{
    sound1.stop();
    sound2.play();
  }
}
加入三個音效,讓滑鼠點擊時和鍵盤點即時都會撥放音效
//week09_5_keyboard_ninja01_background_image_appleX_appleY_appleVX_appleVY_g
PImage board;
void setup(){
  size(600,400);
  board = loadImage("board.png");//
}
float appleX = 100,appleY = 500, appleVX = 3, appleVY = -3;
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;
  }
}
重現keyboard ninja 遊戲 創建一個會移動的蘋果
//week09_6_keyboard_ninja03_void_randomApple
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;
  appleVX = random(-8,8);
  appleVY = random(-30,20);
}
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();//當蘋果出借時,重設新的蘋果位置、速度
  }
}
利用random函式讓他以四種方式來移動

3.第三節上課



//week09_7_keyboard_ninja04_void_keyPressed_appleKey
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(8,0);
  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();//當蘋果出借時,重設新的蘋果位置、速度
  }
}
增加了文字讓他對其蘋果正中央,並且按到剛好的字會加扣分

//week09_8_keyboard_ninja05_start_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(8,0);
  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 -=20;//答錯扣很多分
    }
  }
}
int state=0;//0;wait,1;3,2,1,GO,2.Game
int countdown = 60*4;
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();//當蘋果出借時,重設新的蘋果位置、速度
    }
  }
}
增加開頭動畫倒數321go

//week09_9_keyboard_ninja06_add_music
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");
  sound3 = new SoundFile(this,"Gong.mp3");
  sound3 = 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(8,0);
  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 -=20;//答錯扣很多分
    }
  }
}
int state=0;//0;wait,1;3,2,1,GO,2.Game
int countdown = 60*4;
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();//當蘋果出借時,重設新的蘋果位置、速度
    }
  }
}

4.上傳github


沒有留言:

張貼留言