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;//按鍵盤一個鍵 看的見的小寫
}
step 01-4 增加backspace的功能 當字串長度<0時 原本的 Input: 才不會被刪掉


void setup()
{
size(500,500);
textSize(64);//字的大小
}
String line = "";//字串
void draw()
{
background(0);//背景黑色
text("Input: "+line,100,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);//input會被刪掉
}
}
新增聲音
step 02-1 把聲音mp3檔加到程式碼裡面
import processing.sound.*;//使用java的聲音
SoundFile file;
void setup()
{
size(300,300);
file = new SoundFile(this,"a.mp3.mp3");
file.play();
}
void draw()
{
}
step 02-2 把三個音檔傳到程式碼裡面 按數字鍵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()
{
text("press 1,2,3",100,100);
}
void keyPressed()
{
if(key=='1') dadada.play();
if(key=='2') good.play();
if(key=='3') over.play();
}
step 02-3 當打字母時 會有da的音效 按ENTER會有DINGDONG的音效 字串<0 會有OUCH的音效
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,100,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);//input會被刪掉
ouch.play();
}
if( key == ENTER )
{
dingdong.play();
}
}
step 03-1 製作一個打字遊戲
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;
}
step 03-2 當鍵盤按鍵按下 會有一條紅色線打到字
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);
}
}
step03-3 打英文字 顏色會變紅色
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();//i的某位數字
if ( key == Q.charAt(i) )
{
A += key;
}
}
step03-4 字打完會消失 然後顯示其他的字
void setup()
{
size(400,600);
textSize(20);
}
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();//i的某位數字
if ( key == Q.charAt(i) )//非常好 相同
{
A += key;
if ( Q.equals(A) )//打完字 會消失換其他的字
{
randomQ();
A = " ";
}
}
}
題目:踩地雷
和10160163一組
沒有留言:
張貼留言