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

}

沒有留言:

張貼留言