Problem with my network pong

edited February 2017 in Library Questions

Hello everyone,

I want to make a pong in network but I can't understand why it doesn't work ? My architecture is : I will send the position of the bot paddle and the ball by the server to the client like this the client would display everything. And the client have to send the position of the top paddle like that the server could display everything. Sorry if my english is bad, i'm not an english native speaker. My program works if i make few modifications to play with two players on the same keyboard.

import processing.net.*;

Server server; 
Client client;
String input;
int data[];

boolean gameover= false, right = false, left = false;
int topscore=0;
int bottomscore=0;
int time;
float changespeed=0;
Paddle bot;
Ball pongball;
Paddle top;

void setup()
{
  server = new Server(this, 12012); // Start a simple server on a port
  frameRate(100);
  noStroke();
  pongball= new Ball();
  bot=new Paddle();
  top=new Paddle();
  top.y=0;
  size(1000, 1000);
}

void keyPressed()
{
  if (keyCode == LEFT)  left = true;
  if (keyCode == RIGHT) right = true;
}

void keyReleased()
{
  if (keyCode == LEFT)     left = false;
  if (keyCode == RIGHT)    right = false;
}

void draw()
{
  drawMyPong();
}

void drawMyPong(){
  if (gameover==false)
  {
    background(0);
    bot.showThePaddle();
    top.showThePaddle();

    if (left==true)  bot.moveLeft();
    if (right==true) bot.moveRight();

    pongball.ballSettingInMotion();
    pongball.bounce();
    pongball.showTheBall();

    if (pongball.positionY<-8)
    {
      gameover=true;
      bottomscore++;
    }
    if (pongball.positionY>1008)
    {
      gameover=true;
      topscore++;
    }
    //send the position of the bot paddle and the position of the ball
    server.write(bot.x + " " + pongball.positionX + " " + pongball.positionY + "\n");

    // Receive data from client, position of the top paddle
    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
      //Draw the ball and the bot paddle
      top.x = data[0];

    }
  }
}

class Paddle
{
  int x, y;

  Paddle()
  {
    x=500;
    y=996;
  }

  void showThePaddle()
  {
    fill(46, 178, 253);
    rect(x, y, 120, 4);
  }

  void moveLeft()
  {
    if (x>=0)  x-=5;
  }
  void moveRight()
  {
    if (x<=880)  x+=5;
  }
}

class Ball
{
  int positionX, positionY;
  boolean up, right;

  Ball()
  {
    positionX=16;
    positionY=484;
    up=true;
    right=true;
  }

  void ballSettingInMotion()
  {
    if (up==true)     positionY=int(positionY-2-changespeed/2);
    else              positionY=int(positionY+2+changespeed/2); //up==false
    if (right==true)  positionX=int(positionX+1+changespeed);
    else              positionX=int(positionX-1-changespeed); //right==false
  }
  void bounce()
  {
    if (get(int(positionX)-8, int(positionY))!=color(0))          right=true;
    if (get(int(positionX)+8, int(positionY))!=color(0))          right=false;
    if (get(int(positionX), int(positionY)-8)==color(46, 178, 253))  up=false;
    if (get(int(positionX), int(positionY)+8)==color(46, 178, 253))
    {
      up=true;
      changespeed+=0.5;
    }
  }

  void showTheBall()
  {
    fill(255, 255, 255);
    ellipse(positionX, positionY, 22, 22);
  }
}

Here is my Client Program :

import processing.net.*;

Server server; 
Client client;
String input;
int data[];

boolean gameover= false, right = false, left = false;
int topscore=0;
int bottomscore=0;
int time;
float changespeed=0;
Paddle bot;
Ball pongball;
Paddle top;

void setup()
{
  client = new Client(this, "localhost", 12012); // Start a simple server on a port
  frameRate(100);
  noStroke();
  pongball= new Ball();
  bot=new Paddle();
  top=new Paddle();
  top.y=0;
  size(1000, 1000);
}

void keyPressed()
{
  if (keyCode == LEFT)  left = true;
  if (keyCode == RIGHT) right = true;
}

void keyReleased()
{
  if (keyCode == LEFT)     left = false;
  if (keyCode == RIGHT)    right = false;
}

void draw()
{
  drawMyPong();
}

void drawMyPong(){
  if (gameover==false)
  {
    background(0);
    bot.showThePaddle();
    top.showThePaddle();

    if (left==true)  top.moveLeft();
    if (right==true) top.moveRight();

    pongball.ballSettingInMotion();
    pongball.bounce();
    pongball.showTheBall();

    if (pongball.positionY<-8)
    {
      gameover=true;
      bottomscore++;
    }
    if (pongball.positionY>1008)
    {
      gameover=true;
      topscore++;
    }
    //WITHOUT THIS PART, it goes correctly
    //send the position of the bot paddle and the position of the ball
    client.write(top.x + "\n");

    // Receive data from client, position of the top paddle
    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
      //Draw the ball and the bot paddle
      bot.x = data[0];
      pongball.positionX = data[1];
      pongball.positionY = data[2];

    }
  }
}

class Paddle
{
  int x, y;

  Paddle()
  {
    x=500;
    y=996;
  }

  void showThePaddle()
  {
    fill(46, 178, 253);
    rect(x, y, 120, 4);
  }

  void moveLeft()
  {
    if (x>=0)  x-=5;
  }
  void moveRight()
  {
    if (x<=880)  x+=5;
  }
}

class Ball
{
  int positionX, positionY;
  boolean up, right;

  Ball()
  {
    positionX=16;
    positionY=484;
    up=true;
    right=true;
  }

  void ballSettingInMotion()
  {
    if (up==true)     positionY=int(positionY-2-changespeed/2);
    else              positionY=int(positionY+2+changespeed/2); //up==false
    if (right==true)  positionX=int(positionX+1+changespeed);
    else              positionX=int(positionX-1-changespeed); //right==false
  }
  void bounce()
  {
    if (get(int(positionX)-8, int(positionY))!=color(0))          right=true;
    if (get(int(positionX)+8, int(positionY))!=color(0))          right=false;
    if (get(int(positionX), int(positionY)-8)==color(46, 178, 253))  up=false;
    if (get(int(positionX), int(positionY)+8)==color(46, 178, 253))
    {
      up=true;
      changespeed+=0.5;
    }
  }

  void showTheBall()
  {
    fill(255, 255, 255);
    ellipse(positionX, positionY, 22, 22);
  }
}
Tagged:
Sign In or Register to comment.