顯示具有 week15 標籤的文章。 顯示所有文章
顯示具有 week15 標籤的文章。 顯示所有文章

2021年1月18日 星期一

week15_鄭皓文

 

       臨近期末要開始準備作品了,所以這星期咱要返璞歸真,回到P語言圖學上,可能會對我們的期末作品有幫助,首先我們先回到矢量顯示圖片的功能上,原理和期中考前我們學過的差不多,只是結構稍微複雜了一些,程式碼如下:

 

size(775,1000);

PImage img=loadImage("img.png");

PVector user=new PVector(10,20,0);

PVector user2=new PVector(30,40,50);

image(img,0,0);

print("user's="+user.x);

println(" user2's="+user2.x);

這樣就可以展示出某部70年代經典電影的海報了

  

所以其實矢量坐標的觀念和我們在圖學中接觸過的十分相似,那通過這個方式我們也可以利用坐標軸將我們需要的圖形設定在特定位置,例如我可以讓一個半徑為5的圓產生在(100,100)的位置,程式碼如下:
PVector user;

void setup()

{

  size(600,600);

  user=new PVector(100, 100);

}

void draw()

{

  background(255);

  ellipse(user.x,user.y,50,50);

}

真的可以説是夢回圖學了

   接下來老師教了我們如何使用P寫出Angry Bird的彈弓發射程式,雖是簡化版的但基本有彈弓發射的雛形,要是加上牛頓原理會更逼真,代碼如下:

PVector user;

PVector Y,v=null;

void setup()

{

  size(600,600);

  user=new PVector(100, 300);

  Y= new PVector(100,300);

}

void draw()

{

  background(255);

  line(user.x,user.y,Y.x,Y.y);

  textSize(40);

  fill(255,0,0);text("Y",Y.x,Y.y);

  fill(255); ellipse(user.x,user.y,20,20);

  if(v!=null)

    user.add(v);

}

void mouseDragged()

{

  user.x=mouseX;

  user.y=mouseY;

}

void mouseReleased()

{

  PVector diff = PVector.sub(Y, user);

  v=diff.div(10);

}

但生成的結果綫會一直連著,所以我感覺相對彈弓這更像投石索

  

   現在我們要給游戲加上新功能,使用搖桿操作彈弓,所以理論上我們需要UNO可以回傳搖桿的坐標值給此時的Processing使用,所以我們要先使用如下的程式碼:
void setup()

{

  pinMode(2,INPUT_PULLUP);

  Serial.begin(9600);

}

void loop()

{

  int x = analogRead(A0);

  int y = analogRead(A1);

  int sw = analogRead(2);

  Serial.write(x/4);

  Serial.write(y/4);

  Serial.write(sw);

  delay(20);

}



 

然後我們需要測試一下是否可以在Processing中正常運行,使用如下的程式碼測試⚪的移動軌跡就可以了:

假如把殘影打開的話可以產生連點呈現的效果

   最後只要把這兩部合并,放在剛剛寫出的彈弓上,就是我們要的最終效果了:

import processing.serial.*;

Serial myport;

PVector user;

PVector Y,v=null;

void setup()

{

  size(600,400);

  myport = new Serial(this,"COM3",9600);

  user = new PVector(100,300);

  Y = new PVector(100,300);

}

void draw()

{

  background(255);

  if(myport.available()>=3)

  {

    int x = myport.read();

    int y = myport.read();

    int sw = myport.read();

    user.x = 100+x-128;

    user.y = 900+y-128;

  }

  textSize(40);

  fill(255,0,0); text("Y",Y.x,Y.y);

  fill(255); ellipse(user.x,user.y,20,20);

   if(v!=null)

   {

    user.add(v);

    v.y+=9.8;

   }

}

這樣就是最後的效果,不過好像⚪飛的太快了

      這就是這星期我們學到内容,接下來要進行期末作業製作,這個確實可以給人提供不少項目的靈感。

2021年1月4日 星期一

WEEK1515151515

 WEEK15

今天教的是PVector的使用

有點像憤怒鳥彈弓的概念

最後結合搖桿使搖桿可以控制球的方向

processing

(1)

PVector user;//宣告,setup()及draw()都看的到

PVector Y, v=null;//彈弓,v飛行速度

void setup()

{

  size(600,400);

  user = new PVector(100, 300);

  Y = new PVector(100, 300);

}

void draw()

{

  background(255);

  line( user.x, user.y, Y.x, Y.y);

  textSize(40);

  fill(255,0,0); text("Y", Y.x, Y.y);

  fill(255); ellipse(user.x, user.y,20,20);

  if(v!=null)user.add(v);

}

void mouseDragged()

{

  user.x=mouseX;

  user.y=mouseY;

}

void mouseReleased()

{

  PVector diff = PVector.sub(Y, user);

  v = diff.div(10);

}


(2)

import processing.serial.*;

Serial myPort;

void setup()

{

  size(256,256);

  myPort = new Serial(this, "COM5",9600);

}

int ballX=0, ballY=0;

void draw()

{

  if( myPort.available() >=3 )

  {

    int x = myPort.read();

    int y = myPort.read();

    int sw = myPort.read();

    ballX = x;

    ballY = y;

  }

  ellipse( ballX, ballY, 10,10);

}


(3)

import processing.serial.*;

Serial myPort;

PVector user;

PVector Y, v=null;

void setup()

{

  size(600,400);

  myPort = new Serial(this, "COM5",9600);

  user = new PVector(100, 300);

  Y = new PVector(100, 300);

}

void draw()

{

  background(255);

  if( myPort.available() >=3 )

  {

    int x = myPort.read();

    int y = myPort.read();

    int sw = myPort.read();

    user.x = 100+x-128;

    user.y = 300+y-128;

  }

  textSize(40);

  fill(255,0,0); text("Y", Y.x, Y.y);

  fill(255); ellipse(user.x, user.y,20,20);

  if(v!=null)

  {

    user.add(v);

    v.y+= 0.98;

  }

}


Arduino

(1)

void setup() 

{

  pinMode(2, INPUT_PULLUP);

  Serial.begin(9600);

}


void loop()

{

  int x = analogRead(A0);

  int y = analogRead(A1);

  int sw = digitalRead(2);


  Serial.write(x);

  Serial.write('+');

  Serial.write(y);

  Serial.write('+');

  Serial.write(sw);

  Serial.write(' ');

  delay(20);

}


(2)

void setup() 

{

  pinMode(2, INPUT_PULLUP);

  Serial.begin(9600);

}


void loop()

{

  int x = analogRead(A0);

  int y = analogRead(A1);

  int sw = digitalRead(2);


  Serial.write(x/4);

  Serial.write(y/4);

  Serial.write(sw);

  delay(20);

}

2020年12月21日 星期一

喵~PVector~

 Proecssing~PVector~

  • 比較:
    • 以前:(僅宣告)
      • float uX=10,uY=20,uZ=0;
      • float u2X=30,u2Y=40,u2Z=0;
    • 現在:(可利用PVector裡寫好的函式庫)
      • PVector u=new PVector(10,20,0);
      • PVector u2=new PVector(30,40,0);
    • 注意:檔名不可取到函數名(如:PVector、int、PImage......)
  • 憤怒鳥(angry brid)
    • Processing程式
      • PVector bird;
      • PVector Y,v=null;
      • void setup(){
      •   size(600,400);
      •   bird=new PVector(150,300);//只在setup() new一次
      •   Y=new PVector(150,300);
      • }
      • void draw(){
      •   background(255);
      •   textSize(40);
      •   fill(255,0,200);
      •   text("Y",Y.x-10,Y.y+30);
      •   fill(255);
      •   ellipse(bird.x,bird.y,20,20);
      •   line(bird.x,bird.y,Y.x,Y.y);
      •   if(v!=null){
      •     bird.add(v);
      •     v.y+=0.98;
      •   }
      • }
      • void mouseDragged(){//滑鼠拖曳
      •   bird.x=mouseX;
      •   bird.y=mouseY;
      •   v=null;
      • }
      • void mouseReleased(){
      •   PVector diff=PVector.sub(Y,bird);//減法
      •   v=diff.div(4);//除法
      • }
  • 搖桿(x,y,sw)結合Processing
    • 搖桿
      • GND - GND
      • +5V - 5V
      • URX - A0
      • URY - A1
      • SW - 2
    • Arduino
      • void setup() {
      •   pinMode(2,INPUT_PULLUP);
      •   Serial.begin(9600);
      • }
      • void loop() {//1000Hz(1000fps)
      •   int x=analogRead(A0);//0~1023
      •   int y=analogRead(A1);//0~1023
      •   int sw=digitalRead(2);//HIGH放開
      •   Serial.write(x/4);//第1個byte
      •   Serial.write(y/4);//第2個byte
      •   Serial.write(sw);//第3個byte
      •   delay(20);//1000ms/20ms => 50fps,變慢
      • }
    • Processing程式
      • import processing.serial.*;
      • Serial myPort;
      • void setup(){
      •   size(256, 256);
      •   myPort = new Serial(this,"COM4",9600);
      • }
      • int ballX=0,ballY=0;
      • void draw(){
      •   if(myPort.available()>=3){//延遲少一點 if 改 while
      •     int x=myPort.read();//0~255變成-128~+127
      •     int y=myPort.read();
      •     int sw=myPort.read();
      •     ballX=x;
      •     ballY=y;
      •   }
      •   ellipse(ballX,ballY,10,10);
      • }
  • ↑憤怒鳥結合搖桿↑(Arduino同上↑)
    • import processing.serial.*;
    • Serial myPort;
    • PVector bird;
    • PVector Y,v=null;
    • void setup(){
    •   size(600,400);
    •   myPort = new Serial(this,"COM4",9600);
    •   bird=new PVector(150,300);
    •   Y=new PVector(150,300);
    • }
    • void draw(){
    •   background(255);
    •   if(myPort.available()>=3){
    •     int x=myPort.read();
    •     int y=myPort.read();
    •     int sw=myPort.read();
    •     bird.x=100+x-128;
    •     bird.y=900+x-128;
    •   }
    •   textSize(40);
    •   fill(255,0,200);
    •   text("Y",Y.x-10,Y.y+30);
    •   fill(255);
    •   ellipse(bird.x,bird.y,20,20);
    •   if(v!=null){
    •     bird.add(v);
    •     v.y+=0.98;
    •   }
    • }

                                  Carousel~W15

                                   

                                  PVector user;//外面宣告,setup & draw看得到
                                  void setup(){
                                    size(600,600);
                                    user = new PVector(10, 20);//只在setup()
                                  }
                                  void draw(){
                                  background(255);
                                  ellipse( user.x, user.y,20,20);//用它的值
                                  }



                                  憤怒鳥

                                  PVector user;

                                  PVector Y, v=null;

                                  void setup(){

                                    size(600, 400);

                                    user = new PVector(100, 300);//(2)只在setu

                                    Y = new PVector(100, 300);

                                  }

                                  void draw(){

                                    background(255);

                                    line( user.x, user.y, Y.x, Y.y);

                                    textSize(40);

                                    fill(255,0,0); text("Y", Y.x, Y.y);

                                    fill(255); ellipse( user.x, user.y,20,20);

                                    if(v!=null) user.add(v);

                                  }

                                  void mouseDragged(){

                                    user.x=mouseX;

                                    user.y=mouseY;

                                  }

                                  void mouseReleased(){

                                  PVector diff = PVector. sub(Y,user);

                                  v = diff.div(10);

                                  }


                                  傳輸速率

                                  void setup(){

                                  pinMode (2, INPUT_PULLUP) ; //拉高的Input

                                  Serial.begin (9600);//開始設定USB的傳輸速度

                                  }

                                  void loop() {//1000Hz (1000 fps)

                                  int x = analogRead (A0);//0...1023

                                  int y = analogRead(A1);//0...1023

                                  int sw= digitalRead(2);//HIGH(放開)or LOW(按下去)

                                  Serial.write (x);

                                  Serial.write ('+') ;

                                  Serial.write(y) ;

                                  Serial.write ('+') ;

                                  Serial.write (sw) ;

                                  Serial.write(' ');

                                  delay (20) ;//1000ms/20ms => 50fps,變慢了

                                  }



                                  void setup()
                                  {
                                    pinMode(2, INPUT_PULLUP);//拉高的Input
                                    Serial.begin(9600);//開始設定USB的傳輸速度
                                  }
                                  void loop() {//1000Hz (1000 fps)
                                    int x = analogRead(A0);//0...1023
                                    int y = analogRead(A1);//0...1023
                                    int sw= digitalRead(2);//HIGH(放開)
                                    
                                    Serial.write (x/4);//第1個byte
                                    Serial.write(y/4);//第2個byte
                                    Serial.write (sw); //第3個byte
                                    delay(20);//1000ms/20ms => 50fps,變慢了
                                  }

                                  processing

                                  import processing.serial.*;

                                  Serial myPort;

                                  void setup(){

                                  size(256,256);

                                  myPort = new Serial(this, "COM6",9600);

                                  }

                                  int bal1X=0,bal1Y=0;

                                  void draw()

                                  {

                                   if(myPort.available()>=3)

                                   {

                                     int x = myPort.read();

                                     int y = myPort.read();

                                     int sw = myPort.read();

                                     bal1X=x;

                                     bal1Y=y;

                                   }

                                   ellipse(bal1X,bal1Y,10,10);

                                  }



                                  KIO Week15

                                   Week15

                                  複習期中考前的程式

                                  Vector向量的用法


                                  做出類似彈弓的小遊戲


                                  add,sub,div用法


                                  arduino搖桿讀取


                                  processing 搖桿應用


                                  與彈弓結合




                                  第15週


                                   *交PVector如何使用*

                                  程式碼:

                                  size(600,600);

                                  PImage img=loadImage("img.jpg");

                                  PVector pt=new PVector(10,20,0);

                                  image(img,0,0);

                                  println(pt.x);

                                  ///之前的

                                  float userX=10,userY=20,userZ=0;

                                  float user2X=30,user2Y=40,user2Z=0;

                                  //現在可以以使用物件處理

                                  PVector user=new PVector(10,20,0);

                                  PVector user2=new PVector(30,40,0);


                                  *做憤怒鳥,球會射出去*
                                  程式碼:
                                  PVector user; //(1)外面宣告,setup()及draw()都看的到
                                  PVector Y,v=null;//彈弓,v飛行速度
                                  void setup(){
                                    size(600,400);
                                    user = new PVector(100,300);//(2)只在setup() new一次
                                    Y = new PVector(100,300);
                                  }
                                  void draw(){
                                    background(255);
                                    line(user.x, user.y, Y.x, Y.y);
                                    textSize(40);
                                    fill(255,0,0);  text("Y", Y.x, Y.y);
                                    fill(255);  ellipse(user.x, user.y,20,20);//(3)用他的值
                                    if(v!=null) user.add(v);
                                  }
                                  void mouseDragged(){
                                    user.x=mouseX;
                                    user.y=mouseY;
                                  }
                                  void mouseReleased(){
                                    PVector diff = PVector.sub(Y, user);
                                    v = diff.div(10);
                                  }

                                  *控制按鈕的方向,球會跟著移動*
                                  P語言:
                                  import processing.serial.*;
                                  Serial myPort;
                                  void setup(){
                                    size(256,256);
                                    myPort = new Serial(this,"COM3",9600);
                                  }
                                  int ballX=0, ballY=0;
                                  void draw(){
                                    if(myPort.available()>=3){
                                      int x = myPort.read();
                                      int y = myPort.read();
                                      int sw = myPort.read();
                                      ballX = x;
                                      ballY = y;
                                    }
                                    ellipse(ballX,ballY,10,10);
                                  }
                                  Arduino:
                                  void setup() {
                                    pinMode(2,INPUT_PULLUP);
                                    Serial.begin(9600);
                                  }
                                  void loop() {
                                    int x = analogRead(A0);
                                    int y = analogRead(A1);
                                    int sw = digitalRead(2);

                                    Serial.write(x/4);
                                    Serial.write(y/4);
                                    Serial.write(sw);
                                    delay(20);
                                  }




                                  *清畫面版本*
                                  程式碼:
                                  import processing.serial.*;
                                  Serial myPort;
                                  void setup(){
                                    size(256,256);
                                    myPort = new Serial(this,"COM3",9600);
                                  }
                                  int ballX=0, ballY=0;
                                  void draw(){
                                    background(255);//清畫面
                                    if(myPort.available()>=3){
                                      int x = myPort.read();
                                      int y = myPort.read();
                                      int sw = myPort.read();
                                      ballX = x;
                                      ballY = y;
                                    }
                                    ellipse(ballX,ballY,10,10);
                                  }

                                  12/21

                                   使用PVector叫出圖形

                                  size(600,600);
                                  PImage img=loadImage("img.jpg");
                                  PVector pt=new PVector(10,20,0);
                                  image(img,0,0);
                                  println(pt.x);
                                  float userX=10,userY=20,userZ=0;
                                  float user2X=30,user2Y=40,user2Z=0;
                                  PVector user=new PVector(10,20,0);
                                  PVector user2=new PVector(30,40,0);



                                  PVector user;
                                  PVector Y;
                                  void setup(){
                                     size(600,400);
                                     user = new PVector(100,300);
                                     Y= new PVector(100,300);
                                  }
                                  void draw(){
                                     background(255);
                                     line(user.x,user.y,Y.x,Y.y);
                                     textSize(40);
                                     fill(255,0,0);text("Y",Y.x,Y.y);
                                     fill(255);ellipse(user.x,user.y,20,20);
                                  }
                                  void mouseDragged(){
                                    user.x=mouseX;
                                    user.y=mouseY;
                                  }
                                  void mouseReleased(){
                                      
                                  }




                                  𝔍𝔒𝔗𝔇-week15

                                   今日進度:

                                  1.學習用PVector代替定義多個座標

                                  2.學習應用PVector的座標

                                  3.學習如何應用PVector製作憤怒鳥

                                  4.makeuno回傳座標值

                                  1.程式碼:

                                  size(600,600);

                                  PImage img=loadImage("img.jpg");

                                  PVector user=new PVector(10,20,0);

                                  PVector user2=new PVector(30,40,50);

                                  image(img,0,0);

                                  print("user's="+user.x);

                                  println(" user2's="+user2.x);

                                  執行結果:


                                  2.程式碼:

                                  PVector user;

                                  void setup()

                                  {

                                    size(600,600);

                                    user=new PVector(10, 20);

                                  }

                                  void draw()

                                  {

                                    background(255);

                                    ellipse(user.x,user.y,20,20);

                                  }


                                  執行結果:


                                  3.程式碼:

                                  PVector user;

                                  PVector Y,v=null;

                                  void setup()

                                  {

                                    size(600,600);

                                    user=new PVector(100, 300);

                                    Y= new PVector(100,300);

                                  }

                                  void draw()

                                  {

                                    background(255);

                                    line(user.x,user.y,Y.x,Y.y);

                                    textSize(40);

                                    fill(255,0,0);text("Y",Y.x,Y.y);

                                    fill(255); ellipse(user.x,user.y,20,20);

                                    if(v!=null)

                                      user.add(v);

                                  }

                                  void mouseDragged()

                                  {

                                    user.x=mouseX;

                                    user.y=mouseY;

                                  }

                                  void mouseReleased()

                                  {

                                    PVector diff = PVector.sub(Y, user);

                                    v=diff.div(10);

                                  }

                                  執行結果:


                                  4.程式碼:
                                  void setup() 
                                  {
                                    // put your setup code here, to run once:
                                    pinMode(2,INPUT_PULLUP);
                                    Serial.begin(9600);
                                  }

                                  void loop()
                                  {
                                    // put your main code here, to run repeatedly:
                                    int x = analogRead(A0);
                                    int y = analogRead(A1);
                                    int sw = analogRead(2);
                                    Serial.write(x);
                                    Serial.write('+');
                                    Serial.write(y);
                                    Serial.write('+');
                                    Serial.write(sw);
                                    Serial.write('\n');
                                    delay(20);
                                  }

                                  執行結果:


                                  5.makeuno程式碼:

                                  void setup() 

                                  {

                                    // put your setup code here, to run once:

                                    pinMode(2,INPUT_PULLUP);

                                    Serial.begin(9600);

                                  }


                                  void loop()

                                  {

                                    // put your main code here, to run repeatedly:

                                    int x = analogRead(A0);

                                    int y = analogRead(A1);

                                    int sw = analogRead(2);

                                    Serial.write(x/4);

                                    Serial.write(y/4);

                                    Serial.write(sw);

                                    delay(20);

                                  }

                                  processing程式碼:

                                  import processing.serial.*;

                                  Serial myport;

                                  void setup()

                                  {

                                    size(256,256);

                                    myport = new Serial(this,"COM6",9600);

                                  }

                                  int ballX=0,ballY=0;

                                  void draw()

                                  {

                                    background(255);

                                    if(myport.available()>=3)

                                    {

                                      int x = myport.read();

                                      int y = myport.read();

                                      int sw = myport.read();

                                      ballX=x;

                                      ballY=y;

                                    }

                                    ellipse(ballX,ballY,10,10);

                                  }


                                  執行結果:



                                  6.程式碼:
                                  import processing.serial.*;
                                  Serial myport;
                                  PVector user;
                                  PVector Y,v=null;
                                  void setup()
                                  {
                                    size(600,400);
                                    myport = new Serial(this,"COM6",9600);
                                    user = new PVector(100,300);
                                    Y = new PVector(100,300);
                                  }
                                  void draw()
                                  {
                                    background(255);
                                    if(myport.available()>=3)
                                    {
                                      int x = myport.read();
                                      int y = myport.read();
                                      int sw = myport.read();
                                      user.x = 100+x-128;
                                      user.y = 900+y-128;
                                    }
                                    textSize(40);
                                    fill(255,0,0); text("Y",Y.x,Y.y);
                                    fill(255); ellipse(user.x,user.y,20,20);
                                     if(v!=null)
                                     {
                                      user.add(v);
                                      v.y+=9.8;
                                     }
                                  }
                                  執行結果:



                                  WEEK15

                                   

                                  憤怒鳥雛形
                                  Arduino 程式碼

                                  void setup() {

                                      // put your setup code here, to run once:

                                      pinMode(2, INPUT_PULLUP);

                                      Serial.begin(9600);

                                  }


                                  void loop() {

                                    // put your main code here, to run repeatedly:

                                    int x = analogRead(A0);

                                    int y = analogRead(A1);

                                    int sw = digitalRead(2);


                                    Serial.write(x/4);

                                    Serial.write(y/4);

                                    Serial.write(sw);

                                     delay(20);

                                  }

                                  Processing 程式碼

                                  import  processing.serial.*;

                                  Serial myPort;

                                  PVector user;

                                  PVector Y,v=null;

                                   void setup(){

                                     size(600,400);

                                     myPort = new Serial(this,"COM5",9600);

                                     user = new PVector(100,300);

                                     Y = new PVector(100,300);

                                   }

                                   int ballX=0,ballY=0;

                                   void draw(){

                                     background(255);

                                     if(myPort.available()>=3){

                                       int x = myPort.read();

                                       int y = myPort.read();

                                       int sw = myPort.read();

                                       user.x = 100+x-128;

                                       user.y = 300+y-128;

                                     }

                                    textSize(40);

                                    fill(255,0,0); text("Y",Y.x,Y.y);

                                    fill(255);

                                    ellipse(user.x, user.y, 20,20);

                                    if(v!=null){

                                      user.add(v);

                                      v.y+=0.98;

                                    }

                                   }


                                  丹ㄉ憤怒鳥 很怒ㄛ

                                   我們今天要來做憤怒鳥

                                  用PVector

                                  首先我們先弄出一張img

                                  然後再新建檔案
                                  弄出一顆球球
                                  再來我們弄出一個點Y

                                  利用mouse release 我們可以把球球射出去了

                                  然後我們開啟arduino
                                  讓他可以感應到我們的搖桿

                                  (移動搖桿後)

                                  上下左右測試成功後 再來我們就可以用搖桿在processing畫圖了



                                  week 15

                                   憤怒鳥

                                  Processing

                                  PVector user;

                                  PVector Y, v=null;

                                  void setup(){

                                    size(600, 400);

                                    user = new PVector(100, 300);//(2)只在setu

                                    Y = new PVector(100, 300);

                                  }

                                  void draw(){

                                    background(255);

                                    line( user.x, user.y, Y.x, Y.y);

                                    textSize(40);

                                    fill(255,0,0); text("Y", Y.x, Y.y);

                                    fill(255); ellipse( user.x, user.y,20,20);

                                    if(v!=null) user.add(v);

                                  }

                                  void mouseDragged(){

                                    user.x=mouseX;

                                    user.y=mouseY;

                                  }

                                  void mouseReleased(){

                                  PVector diff = PVector. sub(Y,user);

                                  v = diff.div(10);

                                  }

                                  arduino

                                  void setup() {

                                    // put your setup code here, to run once:

                                    pinMode(2,INPUT_PULLUP);

                                    Serial.begin(9600);

                                  }


                                  void loop() {

                                    // put your main code here, to run repeatedly:

                                    int x=analogRead(A0);

                                    int y=analogRead(A1);

                                    int sw=digitalRead(2);


                                    Serial.write(x/4);

                                    Serial.write(y/4);

                                    Serial.write(sw);

                                    delay(20);  

                                  }

                                  Yang_Week15

                                   Week15

                                   PVector:

                                  PVector pt;

                                  PVector pt;

                                  PImage img;

                                  void setup(){

                                      size(600,600);

                                      img=loadImage("img.jpg");

                                      pt=new PVector(10,20,0);

                                  }

                                  void draw(){

                                        image(img,0,0);

                                        println(pt.x);

                                  }











                                  拉彈弓:

                                  PVector pt,Y;

                                  PImage img;

                                  void setup(){

                                    size(600,600);

                                    Y=new PVector(100,300);

                                    pt=new PVector(100,300);

                                  }

                                  void draw(){

                                    background(255);

                                    line(pt.x,pt.y,Y.x,Y.y);

                                    textSize(40);

                                    fill(255,100,0);text("Y",Y.x,Y.y);

                                    fill(255);ellipse(pt.x,pt.y,30,30);

                                  }

                                  void mouseDragged(){

                                    pt.x=mouseX;

                                    pt.y=mouseY;

                                  }











                                  拉弓並彈出:

                                  PVector pt,Y,v;

                                  PImage img;

                                  void setup(){

                                    size(600,600);

                                    Y=new PVector(100,300);

                                    pt=new PVector(100,300);

                                  }

                                  void draw(){

                                    background(255);

                                    line(pt.x,pt.y,Y.x,Y.y);

                                    textSize(40);

                                    fill(255,100,0);text("Y",Y.x,Y.y);

                                    fill(255);ellipse(pt.x,pt.y,30,30);

                                    if(v!=null)pt.add(v);

                                  }

                                  void mouseDragged(){

                                    pt.x=mouseX;

                                    pt.y=mouseY;

                                  }

                                  void mouseReleased(){

                                    PVector diff=PVector.sub(Y,pt);

                                    v=diff.div(10);

                                  }











                                  Joystick控制移動

                                  Arduino:

                                  void setup() {

                                    // put your setup code here, to run once:

                                    pinMode(2,INPUT_PULLUP);

                                    Serial.begin(9600);

                                  }


                                  void loop() {

                                    // put your main code here, to run repeatedly:

                                    int x=analogRead(A0);

                                    int y=analogRead(A1);

                                    int sw= digitalRead(2);


                                    Serial.write(x/4);

                                    Serial.write(y/4);

                                    Serial.write(sw);

                                    delay(20);

                                  }

                                  Processing:

                                  import processing.serial.*;

                                  Serial myPort;

                                  void setup(){

                                    size(256,256);

                                    myPort = new Serial(this,"COM4",9600);

                                  }

                                  int ballX=0,ballY=0;

                                  void draw(){

                                    background(255);

                                     if(myPort.available()>=3){

                                       int x=myPort.read();

                                       int y=myPort.read();

                                       int sw=myPort.read();

                                       ballX=x;

                                       ballY=y;

                                     }

                                     fill(255,0,0);

                                     ellipse(ballX,ballY,10,10);

                                  }