2020年12月21日 星期一

🗿week15


 

圖片

//int a=10

size(600,600);

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

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

image(img ,0 ,0);

println(pt.x);5

//以前要用的時候

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(10,20,0);

_______________________________________________________________________

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

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 mouseReleased(){

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

  v = diff.div(10);

}___________________________________________________



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);

}


Arduino程式碼

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,變慢了

}


喵~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;
    •   }
    • }

                                  week15

                                  1. 使用PVector叫出圖形


                                  2.

                                  PVector user;                                    1.宣告PVector,讓setup()跟draw()用的到

                                  void setup()
                                  {
                                    
                                    size(600,600);

                                    user = new PVector(10,20);            2.只在setup()做一次

                                  }

                                  void draw()
                                  {
                                    
                                      background(255);
                                   
                                      ellipse(user.x,user.y,20,20);            3.使用PVector的值

                                  }




                                  3.可以拉出線

                                  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-17,Y.y+20);
                                    fill(255);ellipse(user.x,user.y,20,20);
                                  }
                                  void mouseDragged()
                                  {
                                    user.x=mouseX;
                                    user.y=mouseY;
                                  }
                                  void mouseReleased()
                                  {

                                  }



                                  4.射出彈珠

                                  PVector user;
                                  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-17,Y.y+20);
                                    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);
                                  }



                                  5.射出的彈珠加上重力加速度

                                  PVector user;
                                  PVector Y , v = null;     //V是速率
                                  void setup()
                                  {
                                    size(600,400);
                                    user = new PVector(100,300);
                                    Y=new PVector(100,300);
                                  }
                                  void draw()
                                  {
                                    background(255);
                                    if(mousePressed)line(user.x,user.y,Y.x,Y.y);
                                    textSize(40);
                                    fill(255,0,0); text("Y",Y.x-17,Y.y+20);
                                    fill(255);ellipse(user.x,user.y,20,20);
                                    if(v!=null) 
                                      {
                                        user.add(v);
                                        v.y+=0.98;
                                      }
                                  }
                                  void mouseDragged()
                                  {
                                    user.x=mouseX;
                                    user.y=mouseY;
                                    v=null;
                                  }
                                  void mouseReleased()
                                  {
                                    PVector diff =PVector.sub(Y,user);
                                    v=diff.div(4);
                                  }



                                  6.監視器回傳搖桿值




                                  7.P語言畫出搖桿位置

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

                                                                                              processing:

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





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

                                  wendy_week15

                                   互動技術概論

                                  week15


                                  Processing憤怒鳥小遊戲

                                  PVector user1,Y,y,v=null;

                                  PImage sky,brid;

                                  float time=0;

                                  int n=0;

                                  void setup(){

                                    size(500,500);

                                    user1 = new PVector(168,345);

                                    Y = new PVector(168,345);

                                    y = new PVector(160,365);

                                    sky = loadImage("01.png");

                                    brid = loadImage("brid2.png");

                                  }

                                  void draw(){

                                     background(sky);

                                    textSize(50);

                                    fill(0);

                                    text("Y",y.x,y.y);

                                    fill(#537972);

                                    imageMode(CENTER);

                                    image(brid,user1.x,user1.y,40,40);

                                    if(mousePressed)line(user1.x,user1.y,Y.x,Y.y);

                                    if(v!=null){

                                      user1.add(v);

                                      v.y+=0.98;//重力加速度

                                    }

                                    if(n==1)time++;

                                    if(time>200){ 

                                      user1 = new PVector(168,345);

                                      v=null;

                                      time=0;

                                      n=0;

                                    }

                                    println(time);

                                  }

                                  void mouseDragged(){

                                    user1.x=mouseX;

                                    user1.y=mouseY;

                                    v=null;

                                  }

                                  void mouseReleased(){

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

                                    v = diff.div(4);//除法

                                    time++;

                                    n=1;  

                                  }



                                  Joystrick控制球球

                                  Arduino程式碼

                                  import processing.serial.*;

                                  Serial myPort;


                                  void setup(){

                                    size(500,500);

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

                                  }

                                  int ballX=0, ballY=0;

                                  void draw(){

                                    background(255);

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

                                      int x = myPort.read();

                                      int y = myPort.read();

                                      int sw = myPort.read();

                                      ballX = x;

                                      ballY = y;

                                    }

                                    fill(#2F5A52);

                                    ellipse(ballX,ballY,40,40);

                                  }

                                  Processing程式碼

                                  PVector user1,Y,y,v=null;

                                  PImage sky,brid;

                                  float time=0;

                                  int n=0;

                                  void setup(){

                                    size(500,500);

                                    user1 = new PVector(168,345);

                                    Y = new PVector(168,345);

                                    y = new PVector(160,365);

                                    sky = loadImage("01.png");

                                    brid = loadImage("brid2.png");


                                  }

                                  void draw(){

                                    

                                    background(sky);

                                    textSize(50);

                                    fill(0);

                                    text("Y",y.x,y.y);

                                    fill(#537972);

                                    imageMode(CENTER);

                                    image(brid,user1.x,user1.y,40,40);

                                    if(v!=null){

                                      user1.add(v);

                                      v.y+=0.98;//重力加速度

                                    }

                                    if(n==1)time++;

                                    if(time>200){ 

                                      user1 = new PVector(168,345);

                                      v=null;

                                      time=0;

                                      n=0;

                                    }

                                    println(time);

                                    

                                  }

                                  void mouseDragged(){

                                    user1.x=mouseX;

                                    user1.y=mouseY;

                                    v=null;

                                  }

                                  void mouseReleased(){

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

                                    v = diff.div(4);//除法

                                    time++;

                                    n=1;  

                                  }