SIXTH
今天的主題是打字遊戲(像是Ztype的遊戲)
1.先試著秀出字,再將字填充一個顏色
size(500,500); textSize(64); text("Hello World", 100,100); fill(255,0,0); text("Red", 100,200);
2.能夠輸入字跟跳行,但特殊符號目前還沒解決
void setup(){ size(500,500); textSize(64); } String line = "Input: "; void draw(){ text(line,100,100); } void keyPressed(){ line += key; }
3.解決不能打大寫的問題
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; }
4.能夠使用Backspace鍵來刪除打錯的字
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); } }
5.利用程式素描本裡的函式庫sound來播放音樂,要畫出視窗才能播音效
import processing.sound.*; SoundFile file; void setup(){ size(300,300); file = new SoundFile(this, "a.mp3"); file.play(); } void draw(){ }
6.利用按鍵1、2、3來播放音效
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(){ textSize(32); text("press 1, 2, 3", 100,100); } void keyPressed(){ if(key=='1') dadada.play(); if(key=='2') good.play(); if(key=='3') over.play(); }
7.打字的時候會發出音效
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; dingdong.play();} //小心大括號 if(key==BACKSPACE && line.length()>0){ line = line.substring(0, line.length()-1); ouch.play(); } if(key==ENTER){ dingdong.play(); } }
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; if(keyPressed){ stroke(255, 0, 0); line(200, 550, x, y); } }
9.將打到的字母填充上顏色,來確認有沒有打對
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; } }
10.字會在上方隨機落下,並且打完字之後會再換成別的字能夠繼續打
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+textWidth(A), y); } } void keyPressed(){ int i = A.length(); if(key == Q.charAt(i) ){//非常好, 相同 A += key; if(Q.equals(A)){ randomQ(); A = ""; } } }============================================================================
我想做的是俄羅斯方塊(暫定),目前跟陳姿婷一組
沒有留言:
張貼留言