2024年11月4日 星期一

Week09 十連出九金精靈文石板

 WEEK09 字體,音樂,keyboard ninja

09-1(讀取字體)

//week09_01_creatFont_loadFont
size(500,500);
background(0);
textSize(50);
text("Hellow",50,50);

PFont font = createFont("標楷體",50);
textFont(font);
text("你好",50,150);

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


09-2(在Draw外面宣告,否則會占用記憶體當機)

//week09_02_PFont_out
//在外面宣告,否則會占用記憶體當機
PFont font0, font1, font2;
void setup() {
  size(500, 500);
  font0 = createFont("Arial", 50);
  font1 = createFont("標楷體", 50);
  font2 = createFont("elffont-rock.otf", 50);
}

void draw() {
  background(0);
  textSize(50);
  
  textFont(font0);
  text("Hello", 50, 50);


  textFont(font1);
  text("你好", 50, 150);


  textFont(font2);
  text("ㄎㄌㄆㄉ", 50, 250);
}


09-3(放音樂)

//week09_03_processing_sound
// Sketch -> Library -> Manager 搜尋sound 下載processing foundation
// File -> Examples, Library核心函式 -> sound -> soundfile ->SimplePlay
//selfmade 最精簡版本
import processing.sound.*;
SoundFile sound;

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


09-4(在Draw外面宣告,否則會占用記憶體當機)

//week09_04_soundplay_skip
import processing.sound.*;
SoundFile sound1,sound2,slash;
void setup(){
  size(500,500);
  sound1 = new SoundFile(this,"In Game Music.mp3");
  sound2 = new SoundFile(this,"Intro Song_Final.mp3");
  slash = new SoundFile(this,"sword .mp3");
  sound2.play();
}
void draw(){
  background(255);
}
void mousePressed(){
  slash.play();
}
void keyPressed(){
  if(key=='1'){
    sound2.stop();
    sound1.play();
  }
  
  if(key=='2'){
    sound1.stop();
    sound2.play();
  }
}

09-5(載入圖片, 丟出球)

//week09_05_keyboard_ninja_img
PImage board;
void setup() {
  size(600, 400);
  board = loadImage("board.png");
}
float appleX = 100, appleY = 500, appleVX = 3, appleVY = -10;
void draw() {
  background(board);
  fill(255, 0, 0);
  ellipse(appleX, appleY, 80, 80);
  appleX += appleVX;
  appleY += appleVY;
  appleVY += 0.98;
  if (appleX >600) {
    appleX = 100;
    appleY = 500;
    appleVX = 5;
    appleVY = -30;
  }
}

09-6(隨機丟球)

//week09_06_keyboard_ninja_random

PImage board;
void setup() {
  size(600, 400);
  board = loadImage("board.png");
}
float appleX = 100, appleY = 500, appleVX = 3, appleVY = -10;
void randomApple(){
  appleX = random(100,500);
  appleY = 500;
  if(appleX<300) appleVX = random(0,8);
  else appleVX = random(-8,0);
  appleVY = random(-35,-25);
}
void draw() {
  background(board);
  fill(255, 0, 0);
  ellipse(appleX, appleY, 80, 80);
  appleX += appleVX;
  appleY += appleVY;
  appleVY += 0.98;
  if (appleX >600 || appleX<0 || appleY>550) {
    randomApple();
  }
}

09-7(按鍵判定)

//week09_07_keyboard_ninja_keypressed
PImage board;
void setup() {
  size(600, 400);
  board = loadImage("board.png");
  randomApple();
}
float appleX = 100, appleY = 500, appleVX = 3, appleVY = -10;
char appleKey;
void randomApple(){
  appleX = random(100,500);
  appleY = 500;
  if(appleX<300) appleVX = random(0,8);
  else appleVX = random(-8,0);
  appleVY = random(-35,-25);
  appleKey = (char)('a'+int(random(26)));
}
void draw() {
  background(board);
  fill(255, 0, 0);
  ellipse(appleX, appleY, 80, 80);
  fill(255,255,0);
  textSize(50);
  textAlign(CENTER,CENTER);
  text(appleKey,appleX,appleY);
  appleX += appleVX;
  appleY += appleVY;
  appleVY += 0.98;
  if (appleX >600 || appleX<0 || appleY>550) {
    randomApple();
  }
}


09-8(記分板)

//week09_08_keyboard_ninja_scoreboard
PImage board;
void setup() {
  size(600, 400);
  board = loadImage("board.png");
  randomApple();
}
float appleX = 100, appleY = 500, appleVX = 3, appleVY = -10;
char appleKey;
void randomApple(){
  appleX = random(100,500);
  appleY = 500;
  if(appleX<300) appleVX = random(0,8);
  else appleVX = random(-8,0);
  appleVY = random(-35,-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;
  appleY += appleVY;
  appleVY += 0.98;
  if (appleX >600 || appleX<0 || appleY>550) {
    randomApple();
  }
}

09-9(換階段)

//week09_09_keyboard_ninja_startstate
PImage board;
void setup() {
  size(600, 400);
  board = loadImage("board.png");
  randomApple();
}
float appleX = 100, appleY = 500, appleVX = 3, appleVY = -10;
char appleKey;
void randomApple() {
  appleX = random(100, 500);
  appleY = 500;
  if (appleX<300) appleVX = random(0, 8);
  else appleVX = random(-8, 0);
  appleVY = random(-35, -25);
  appleKey = (char)('a'+int(random(26)));
}
int score=0;
int state = 0;
int countdown = 60*4;
void keyPressed() {
  if (state == 0) state = 1;
  if (state == 2) {
    if (key==appleKey) {
      score+=100;
    } else {
      score-=30;
    }
  }
}
void draw() {
  background(board);
  text("Score:"+score, 400, 50);
  fill(255, 0, 0);
  textSize(50);
  textAlign(CENTER, CENTER);
  if (state==0) text("Pressed 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 {
    ellipse(appleX, appleY, 80, 80);
    fill(255, 255, 0);
    textSize(50);

    text(appleKey, appleX, appleY);
    appleX += appleVX;
    appleY += appleVY;
    appleVY += 0.98;
    if (appleX >600 || appleX<0 || appleY>550) {
      randomApple();
    }
  }
}


09-10(加音效)

//week09_10_keyboard_ninja_music
import processing.sound.*;
SoundFile sound1, sound2, sound3;
PImage board;
void setup() {
  size(600, 400);
  board = loadImage("board.png");
  sound1 = new SoundFile(this, "Intro Song_Final.mp3");
  sound2 = new SoundFile(this, "Gong.mp3");
  sound3 = new SoundFile(this, "In Game Music.mp3");
  sound1.loop();
  randomApple();
}
float appleX = 100, appleY = 500, appleVX = 3, appleVY = -10;
char appleKey;
void randomApple() {
  appleX = random(100, 500);
  appleY = 500;
  if (appleX<300) appleVX = random(0, 8);
  else appleVX = random(-8, 0);
  appleVY = random(-35, -25);
  appleKey = (char)('a'+int(random(26)));
}
int score=0;
int state = 0;
int countdown = 60*4;
void keyPressed() {
  if (state == 0) state = 1;
  if (state == 2) {
    if (key==appleKey) {
      sound2.stop();
      score+=100;
      sound2.play();
    } else {
      sound2.stop();
      score-=30;
      sound2.play();
    }
  }
}
void draw() {
  background(board);
  text("Score:"+score, 400, 50);
  fill(255, 0, 0);
  textSize(50);
  textAlign(CENTER, CENTER);
  if (state==0) {
    text("Pressed Any Key to Start", 300, 200);
    sound2.play();
  } 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;
      sound1.stop();
      sound3.play();
    }
  } else {
    ellipse(appleX, appleY, 80, 80);
    fill(255, 255, 0);
    textSize(50);

    text(appleKey, appleX, appleY);
    appleX += appleVX;
    appleY += appleVY;
    appleVY += 0.98;
    if (appleX >600 || appleX<0 || appleY>550) {
      randomApple();
    }
  }
}







沒有留言:

張貼留言