2020年12月21日 星期一

𝔍𝔒𝔗𝔇-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;
   }
}
執行結果:



沒有留言:

張貼留言