2023年10月19日 星期四

91 week06

 印出字體Hello world,添加顏色:

size(500,500);
textSize(64);
text("Hello world",100,100);

fill(255,0,0);
text("Red",100,200);
可以打字,宣告字串:
void setup(){
  size(500,500);
  textSize(64);
}
String line="Input:";
void draw(){
  text(line,100,100);
}
void keyPressed(){
  line+=key;
}
只能輸入英文大小寫,自行更改自行大小、視窗背影顏色:
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;
  line+=key;
}
增加功能鍵backspace,line.length:
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);
  }
}
讀取播放聲音:
import processing.sound.*;
//https://processing.org/reference/libraries/sound/SoundFile.html
SoundFile file;
void setup(){
  size(300,300);
  file=new SoundFile(this,"a.mp3");
  file.play();
}
void draw(){
  

}

增加鍵盤打擊聲音,按123:
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();
}
打字enter backspace添加聲音:
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();
  }
}
射擊小遊戲,鍵盤畫射線:
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={"void","setup","float","string","background","fill"};
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;
    if(Q.equals(A)){
      randomQ();
      A="";
    }
  }
}

期中期末作品(暫定):   遊戲理念:以失敗死亡來獲得最後的成功通關
                                               角色:心臟人 
                                               風格:類似馬力歐
                                       遊戲相關(暫定):心臟隨時間變色、強心劑可以恢復點健康等等
                                               題目:未定
                                               組員:陳彥霖、江軒緯

沒有留言:

張貼留言