2023年10月19日 星期四

LS._. Week06

打字遊戲-Text TextSize Fill

  • 先寫出Hello World

  • 再做出紅色的字

  • 程式碼
size(500,500);
textSize(64);
text("Hello World",100,100);
fill(255,0,0);
text("Red",100,200);

打字遊戲-KeyPressed String

  • 另存剛剛的程式碼
  • 更改程式碼為打字遊戲(鍵盤按字母就可以印出字母)
  • 按Enter可換行
  • 程式碼
void setup(){
  size(500,500);
  textSize(64);
}
String line="Input: ";
void draw(){
  text(line,100,100);
}
void keyPressed(){
  line+=key;
}

打字遊戲-if alphabet

  • 另存剛剛的程式碼
  • 更改程式碼,使其可顯示出大小寫
  • 將視窗變成長方形
  • 程式碼
void setup(){
  size(800,400);
  textSize(64);
}
String line="Input: ";
void draw(){
  text(line,50,100);
}
void keyPressed(){
  if(key>='A' && key<='Z')line+=key;
  if(key>='a' && key<='z')line+=key;
}

打字遊戲-Backspace string

  • 另存剛剛的程式碼
  • 更改程式碼,讓執行後可以倒退
  • 更改程式碼,讓執行後可以正常倒退
  • 正常倒退如下
  • 程式碼
void setup(){
  size(800,400);
  textSize(64);
}
String line="";
void draw(){
  background(0);
  text("Input: "+line,50,100);
}
void keyPressed(){
  if(key>='A' && key<='Z')line+=key;
  if(key>='a' && key<='z')line+=key;
  if(key==BACKSPACE&& line.length()>0){
    line=line.substring(0,line.length()-1);
  }
}

音樂

  • 開啟Contribution Manager
  • 輸入sound,安裝它
  • 開新檔案,使用函式庫的Sound
  • 點開參考文獻
  • 點進Libraries
  • 點選Sound
  • 找到SoundFile
  • 複製貼上範例程式碼
  • 去下載音樂,轉檔
  • 將音樂丟到資料夾
  • 執行出音樂
  • 程式碼
import processing.sound.*;
SoundFile file;
void setup() {
  size(300,300);
  background(255);
  file = new SoundFile(this, "a.mp3");
  file.play();
}      
void draw() {
}

打字遊戲-game dadada good over

  • 打上程式碼
  • 將老師的錄音檔拉進去程式裡
  • 改一下程式碼,讓程式成功執行
  • 程式碼
import processing.sound.*;
SoundFile dadada,good,over;
void setup() {
  size(300,300);
  dadada = new SoundFile(this, "dadadadada.mp3");
  good = new SoundFile(this, "good.mp3");
  over = new SoundFile(this, "over.mp3");
}      
void draw() {
  text("press 1,2,3",100,100);
}
void keyPressed(){
  if(key=='1')dadada.play();
  if(key=='2')good.play();
  if(key=='3')over.play();
}

打字遊戲

  • 另存6-4程式碼,將6-6與6-4的程式碼更改合併
  • 將老師的錄音檔拉進去程式裡
  • 改一下程式碼,讓程式成功執行
  • 程式碼
import processing.sound.*;
SoundFile da,dingdong,ouch;
void setup(){
  size(800,400);
  textSize(64);
  da = new SoundFile(this, "da.mp3");
  dingdong = new SoundFile(this, "dingdong.mp3");
  ouch = new SoundFile(this, "ouch.mp3");
}
String line="";
void draw(){
  background(0);
  text("Input: "+line,50,100);
}
void keyPressed(){
  if(key>='A' && key<='Z'){line+=key;da.play();}
  if(key>='a' && key<='z'){line+=key;da.play();}
  if(key==BACKSPACE&& line.length()>0){
    line=line.substring(0,line.length()-1);
    ouch.play();
  }
  if(key==ENTER){
    dingdong.play();
  }
}

打字遊戲-shoot word

  • 開新檔案,打上程式碼
  • sqrt為開根號
  • 做出紅色射線
  • 程式碼
void setup(){
  size(400,600);
  textSize(20);
}
String Q="hello",A="";
float x=100,y=100;
void draw(){
  background(0);
  text(Q,x,y);
  ellipse(200,550,20,20);
  float dx=(200-x),dy=(550-y),d=sqrt(dx*dx+dy*dy)*3;
  x+=dx/d;
  y+=dy/d;
  if(keyPressed){
    stroke(255,0,0);
    line(200,550,x,y);
  }
}

打字遊戲

  • 另存剛剛的程式碼
  • 更改程式碼,執行後輸到正確的字母,可變紅色
  • 程式碼
void setup(){
  size(400,600);
  textSize(20);
}
String Q="hello",A="";
float x=100,y=100;
void draw(){
  background(0);
  fill(#FFFFFF);text(Q,x,y);
  fill(#FF0000);text(A,x,y);
  ellipse(200,550,20,20);
  float dx=(200-x),dy=(550-y),d=sqrt(dx*dx+dy*dy)*3;
  x+=dx/d;
  y+=dy/d;
  if(keyPressed){
    stroke(255,0,0);
    line(200,550,x,y);
  }
}
void keyPressed(){
  int i=A.length();
  if(key==Q.charAt(i)){
    A+=key;
  }
}

打字遊戲

  • 另存剛剛的程式碼
  • 更改程式碼,讓它變成遊戲
  • 執行後可以隨機出題,打對後會跳另一題
  • 改一點程式碼
  • 程式碼

void setup(){

  size(400,600);

  textSize(20);

  randomQ();

}

void randomQ(){

  int i=int(random(QQ.length));

  Q=QQ[i];

  x=random(0,300);

  y=random(0,100);

}

String[] QQ={"hello","setup","float","string","fill","background"};

String Q="hello",A="";

float x=100,y=100;

void draw(){

  background(0);

  fill(#FFFFFF);text(Q,x,y);

  fill(#FF0000);text(A,x,y);

  ellipse(200,550,20,20);

  float dx=(200-x),dy=(550-y),d=sqrt(dx*dx+dy*dy)*3;

  x+=dx/d;

  y+=dy/d;

  if(keyPressed){

    stroke(255,0,0);

    line(200,550,x+textWidth(A),y);

  }

}

void keyPressed(){

  int i=A.length();

  if(key==Q.charAt(i)){

    A+=key;

    if(Q.equals(A)){

      randomQ();

      A="";

    }

  }

}

上傳檔案

  • 上傳至GitHub.com

期末作業

  • 已找好組員,題目待定

沒有留言:

張貼留言