2016 教學軟體設計 week9
2016-11-10
1.LEAP Motion
2.檔案讀取、寫檔
寫檔實作1:
void setup()
{
size(600,600);
}
void draw()
{
if(mousePressed)
line(mouseX,mouseY,pmouseX,pmouseY);
}
void mousePressed(){
save("now.png");
}
寫檔實作1執行結果:
****查看工作資料夾
寫檔實作2:
void setup()
{
size(600,600);
}
void draw()
{
if(mousePressed) line(mouseX,mouseY,pmouseX,pmouseY);
}
int number=0;
void mousePressed(){
save("now"+number+".png");
number++;
}
寫檔實作2 執行結果:
查詢 save 相關函數
寫檔實作3:
void setup()
{
size(600,600);
}
void draw()
{
if(mousePressed)
line(mouseX,mouseY,pmouseX,pmouseY);
}
int number=0;
void mousePressed(){
saveFrame();
}
寫檔實作3執行結果:
儲存文字檔
String []
names={"AAA","BBB","CCC"};
saveStrings("myClassmateName.txt",names);
讀檔實作1:
String lines[] =
loadStrings("myClassmateName.txt");
println("there are " +
lines.length + " lines");
for (int i = 0 ; i < lines.length; i++)
{
println(lines[i]);
}
讀檔實作1 執行結果:
接coin實作1:滑鼠與coin距離<30,加100分,並顯示分數。
接coin實作2 執行結果:
接coin實作3:滑鼠or hand 與coin距離<30,加100分,漏接扣命一條,並顯示分數及生命數。
當生命數=0,則遊戲結束,滑鼠一點則顯示 allScores.txt內容。
import
de.voidplus.leapmotion.*;
LeapMotion
leap;
float
handX=0,handY=0;
PVector
[] coins=new PVector[10];
void
setup(){
size(500,500);
leap = new LeapMotion(this);
for(int i=0;i<10;i++) coins[i]=new
PVector( random(500), -random(100));
}
int
score=0, life=10, speed=2;
int
state=0;//state 0:playing, 1:gameOver, 2:showScoreBoard
String
[] allScore;
void
draw(){
for (Hand hand : leap.getHands ()) {
handX=hand.getPosition().x;
handY=hand.getPosition().y;
}
if(state==2){
background(255,255,0);
for(int i=0;i<allScore.length; i++){
fill(255,0,0); textSize(30);text(
allScore[i], 100, 200+i*50);
}
return ;
}else if(state==1){
background(128); speed=0;
allScore =
loadStrings("allScores.txt");
if(mousePressed) state=2;
}else background(255);
for(int i=0;i<10;i++){
fill(255,255,0); ellipse(coins[i].x,
coins[i].y, 30,30);
coins[i].y+=speed;//!!!!!!
if(coins[i].y>530){
coins[i].y=-random(100);
life--;
if(life<=0) state=1;
}
if(dist(handX, handY, coins[i].x,
coins[i].y)<30){
score+=100; coins[i].y=-random(100);
}
if(dist(mouseX, mouseY, coins[i].x,
coins[i].y)<30){
score+=100; coins[i].y=-random(100);
}
}
fill(255); ellipse(handX, handY, 30,30);
fill(255,0,0); textSize(30);
text("Score:"+score +"Life:"+life, 200,200);
}
接coin實作3 執行結果:
沒有留言:
張貼留言