2024年11月4日 星期一

周家契約9號












修改字體

記得先安裝字體

再把字體拖到程式裡 

//week09_01_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("ㄇㄉㄈㄎ", 20 ,250);
















比較不吃記憶體
//week09_02_PFont_outside_void_setup_createFont_void_draw_text
PFont font0, font, font2;
void setup(){
  size(500,500);
  font0 = createFont("Arial", 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("ㄇㄉㄈㄎ", 20 ,250);
}

















將音樂拉進素描本
配合按鍵來播放聲音
//week09_04_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();
}
void keyPressed(){
  if(key=='1'){
    sound2.stop();
    sound1.play();
  }
  else{
    sound1.stop();
    sound2.play();
  }
}















做一個忍者切水果的遊戲
畫一個圓再用拋物線讓他動
0.98是重力加速度
記得要先匯入背景檔案

//week09_05_keyboard_ninja_background_image_appleX_appleY_appleVX_appleVY
PImage board; void setup() {   size(600, 400);   board = loadImage("board.png"); } float appleX = 100, appleY = 500, appleVX = 5, appleVY = -30; 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;   } }
















加入random()來讓蘋果的拋物線實現隨機化
以及隨機出現
//week09_06_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;
  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;
  appleY += appleVY;
  appleVY += 0.98;
  if (appleX>600 || appleX<0 || appleY>550) {
    randomApple();
  }
}




















加入keyPressed()
加入如果keycode==按下的那就加100
如果按錯就-200
右上角顯示分數
//week09_07_keyboard_ninja04_void_keyPressed_appleKey
PImage board;
void setup() {
  size(600, 400);
  board = loadImage("board.png");
  randomApple();
}
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;
  appleY += appleVY;
  appleVY += 0.98;
  if (appleX>600 || appleX<0 || appleY>550) {
    randomApple();
  }
}




















加入倒數計時
countdown代表計時器
按下任何鍵後開始到數計時
countdown一直--
減到>0顯示go
就能開始玩了
//week09_08_keyboard_ninja04_start3_2_1_go
PImage board;
void setup() {
  size(600, 400);
  board = loadImage("board.png");
  randomApple();
}
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 -= 20;
    }
  }
}
int state = 0;
int countdown = 60*4;
void draw() {
  background(board);
  textSize(50);
  textAlign(CENTER, CENTER);
  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;
    appleY += appleVY;
    appleVY += 0.98;
    if (appleX>600 || appleX<0 || appleY>550) {
      randomApple();
    }
  }
}



















加入前幾個程式的音樂
首先是打開程式先撥音樂
再來是倒數3秒時播放音效
所以countdown%60==0代表的是在讀秒剛好在整數時播音效
再來就是進遊戲後取消sound2改成播放sound3
//week09_09_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");
  sound2 = 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(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 -= 20;
    }
  }
}
int state = 0;
int countdown = 60*4;
void draw() {
  background(board);
  textSize(50);
  textAlign(CENTER, CENTER);
  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;
    appleY += appleVY;
    appleVY += 0.98;
    if (appleX>600 || appleX<0 || appleY>550) {
      randomApple();
    }
  }
}

沒有留言:

張貼留言