2016年3月17日 星期四

Week04 0317

射擊









Server


//Server's code computer"0"
import de.voidplus.leapmotion.*;
import processing.net.*;
PVector []userPosition;
int UDP[][] = new int[10][5];
int data[];
String input;
Server server;
Client client;
void setup() {
  size(1280, 740);
  server = new Server(this, 0124);
  for(int i=0;i<10;i++)for(int j=1;j<5;j++)UDP[i][j]=0;
}
void draw() {
  myInformation();
  clientInformation();
  for(int i=0;i<10;i++)line(UDP[i][1], UDP[i][2], UDP[i][3], UDP[i][4]);
}



void myInformation() {
  if (mousePressed == true) {
    line(mouseX, mouseY, pmouseX, pmouseY);
    server.write("0" + "," + mouseX + "," + mouseY + "," + pmouseX + "," + pmouseY +"\n");
  }
}



void clientInformation() {
  client = server.available();
  if (client != null) {
    input = client.readString();
    input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
    data = int(split(input, ','));  // Split values into an array
    for(int i=1;i<5;i++)UDP[data[0]][i]=data[i];
    // Draw line using received coords
    stroke(0);
   
  }
}







Client

//Client's code computer"1"
import de.voidplus.leapmotion.*;
import processing.net.*;
PVector []userPosition;
int UDP[][] = new int[10][5];
int data[];
String input;
Client client;
void setup() {
  size(1280, 740);
  for(int i=0;i<10;i++)for(int j=1;j<5;j++)UDP[i][j]=0;
  client = new Client(this,"127.0.0.1",0124);
}
void draw() {
  myInformation();
  clientInformation();
  for(int i=0;i<10;i++)line(UDP[i][1], UDP[i][2], UDP[i][3], UDP[i][4]);
}

void myInformation() {
  if (mousePressed == true) {
    line(mouseX, mouseY, pmouseX, pmouseY);
    client.write("1" + "," + mouseX + "," + mouseY + "," + pmouseX + "," + pmouseY +"\n");
  }
}

void clientInformation() {
  if (client.available()>0) {
    input = client.readString(); 
    input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
    data = int(split(input, ','));  // Split values into an array
    for(int i=1;i<5;i++)UDP[data[0]][i]=data[i];
    // Draw line using received coords
    stroke(0);
    
  }
}

沒有留言:

張貼留言