第一節課
week06-1
老師說有別系老師問有關拼字的程式,所以第一個目標寫出一個可以拼字的程式。
1.第一個字太小且不清楚。
第二個加上字串,可以用鍵盤打字。
1.目前有些符號判斷不出來,會寫特殊符號。void setup(){ size(500,500); textSize(64); } String line="Input: "; void draw(){ text(line,100,100); } void keyPressed(){ line+=key; }
把字的限制在大小寫A到Z,其他不會打上去。
week06-4
加上if判斷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); } }
week06-5
用Processing匯入音檔。
import processing.sound.*; SoundFile file; void setup(){ size(300,300); file=new SoundFile(this,"a.mp3"); file.play(); } void draw(){ }
小葉老師錄製三個mp3檔,把它匯到程式裡,按鍵盤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(); }
把week06-4跟week06-6結合,在打字時發出指定的聲音。
week06-8
介紹一個小遊戲,目標會持續靠近玩家,試著寫看看。
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; }
把打字遊戲弄完整,設定random讓他取亂數,最後當判定輸入的字一樣,目標消失。
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","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=""; } } }
沒有留言:
張貼留言