Outils pour utilisateurs

Outils du site


wiki:ateliers:processing:4

Pour cet atelier nous nous attaquons au réseau.

On repart d'un « Pong » mais en vertical \o/.

pongClient.pde
import processing.net.*;
 
Client c;
 
int lBar, hBar, ballSize;
 
int x1, y1, x2, y2, x3, y3;
 
void setup() {
  size(400, 600);
  ballSize = 15;
  lBar = 100;
  hBar = 20;
 
  c = new Client(this, "127.0.0.1", 4321);
}
 
void draw() {
  background(0);
 
  sendPosition();
  receivePosition();
  drawBar();
  drawBall();
  x2 = mouseX;
}
 
void drawBar() {
  rect(min(max(x1 - (lBar/2), 0), (width - lBar)), height - (hBar + 10), lBar, hBar);
  rect(min(max(x2 - (lBar/2), 0), (width - lBar)), 10, lBar, hBar);
}
 
void drawBall() {
  ellipse(x3, y3, ballSize, ballSize);
}
 
void sendPosition() {
  c.write(x2+"\n");
}
void receivePosition() {
  if (c.available() > 0) {
    String input = c.readString();
    input = input.substring(0, input.indexOf("\n"));
    int[] data = int(splitTokens(input, ","));
    x1 = data[0];
    x3 = data[1];
    y3 = data[2];
  }
}
pongServeur.pde
import processing.net.*;
 
Server s;
Client c;
 
int lBar, hBar, ballSize;
 
int x1, y1, x2, y2, x3, y3;
int h, v;
 
void setup() {
  size(400, 600);
  ballSize = 15;
  lBar = 100;
  hBar = 20;
  x3 = width/2;
  y3 = height/2;
  s = new Server(this, 4321);
  h = 3;
  v = 2;
}
 
void draw() {
  background(0);
 
  h = (x3 > width - ballSize/2) || (x3 < ballSize/2)?-h:h;
  if (v > 0) {
    v = (y3 > height-(hBar+10+ballSize/2)) && (x3 > x1-lBar/2) && (x3< x1+lBar/2)?-v:v;
  }
  else if (v < 0) {
    v = ((y3 < (ballSize/2+hBar+10))&&(x3 < x2+lBar/2)&&(x3 > x2-lBar/2) )?-v:v;
  }
 
  y3 = (y3 > height - ballSize/2) || (y3 < ballSize/2)?(height/2):y3;
 
  x3 += h;
  y3 += v;
  sendPosition();
  receivePosition();
  drawBar();
  drawBall();
  x1 = mouseX;
}
 
void drawBar() {
  rect(min(max(x1 - (lBar/2), 0), (width - lBar)), height - (hBar + 10), lBar, hBar);
  rect(min(max(x2 - (lBar/2), 0), (width - lBar)), 10, lBar, hBar);
}
 
void drawBall() {
  ellipse(x3, y3, ballSize, ballSize);
}
 
void sendPosition() {
  s.write(x1+","+x3+","+y3+"\n");
}
void receivePosition() {
  c = s.available();
  if (c != null) {
    String input = c.readString();
    input = input.substring(0, input.indexOf("\n"));
    int data = Integer.parseInt(input);
    x2 = data;
  }
}
wiki/ateliers/processing/4.txt · Dernière modification: 2016/09/11 13:14 (modification externe)