2020年12月21日 星期一

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;

  }

 }


沒有留言:

張貼留言