這周要做打字的小遊戲。
先把遊戲的視窗、文字的顏色、大小和位置排好。
程式碼:
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;
}
-----------------------------------------------------------------------------------為了解決亂碼 ( 切換輸入法時 ) 的問題。加上一層保護,只讓大小寫的a~z出現在視窗中。程式碼:void setup()
{
size(500, 500);
textSize(64);
}
String line = "Input: ";
void draw()
{
text(line, 100, 100);
}
void keyPressed()
{
if(key>='A' && key<='Z') line += key;
if(key>='a' && key<='z') line += key;
}
-----------------------------------------------------------------------------------製作做倒退鍵 (打錯字的時候)。加上下面的backspace,避免刪除字的時候連Input的時候也刪掉。
程式碼:
void setup()
{
size(500, 500);
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.*;
SoundFile file;
void setup()
{
size(300, 300);
file = new SoundFile(this, "music.mp3");
file.play();
}
void draw()
{
}
-----------------------------------------------------------------------------------再來是按下按鍵,出現對應的聲音。程式碼:
import processing.sound.*;
SoundFile dadada, good, over;
void setup()
{
size(300, 300);
dadada = new SoundFile(this, "dadada.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();
}
-----------------------------------------------------------------------------------加上輸入文字時會有的音效。
程式碼:
import processing.sound.*;
SoundFile ouch, da, dingdong;
void setup()
{
size(500, 500);
textSize(64);
da = new SoundFile(this, "da.mp3");
ouch = new SoundFile(this, "ouch.mp3");
dingdong = new SoundFile(this, "dingdong.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;
}
-----------------------------------------------------------------------------------當我們按按鍵時,會有射線射向文字(問題Q)。
程式碼:
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, 500, 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, 500, 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]; } 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, 500, 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, 500, x, y); } } void keyPressed() { int i = A.length(); if( key == Q.charAt(i) ) { A += key; if( Q.equals(A) ) { randomQ(); A=""; } } }----------------------------------------------------------------------------------- 讓線對準單字的中間。
程式碼: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, 500, x+textWidth(A), y); } } void keyPressed() { int i = A.length(); if( key == Q.charAt(i) ) { A += key; if( Q.equals(A) ) { randomQ(); A=""; } } }




沒有留言:
張貼留言