2020年12月21日 星期一

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

}






























沒有留言:

張貼留言