2023年10月19日 星期四

Hank-week06

 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(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;
}



4 . 可以刪除字
void setup()
{
  size(800, 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);
  }
}



5 . 新增音樂
import processing.sound.*;

SoundFile file;
void setup()
{
   size(300, 300);
   file = new SoundFile(this, "a.mp3");
   file.play();
}
void draw()
{
    
}



6 . 新增更多的音效
import processing.sound.*;
SoundFile dadadadada, good, over;
SoundFile file;
void setup()
{
   size(300, 300);
   dadadadada = new SoundFile(this, "dadadadada.mp3");
   good = new SoundFile(this, "dadadadada.mp3");
   over = new SoundFile(this, "dadadadada.mp3");
}
void draw()
{
    if(key=='1') dadadadada.play();
    if(key=='2') good.play();
    if(key=='3') over.play();
}



7 . 打字時有音效
import processing.sound.*;
SoundFile da, ouch, dingdong ;
void setup()
{
   size(800, 400);
   textSize(64);
   da = new SoundFile(this, "dadadadada.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;da.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;
  
  
}



9 . 完整整個遊戲
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[]={"float", "setup", "void", "fill", "string"};
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, 255, 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="";
     }  
   }
   
}




躲避障礙物




沒有留言:

張貼留言