Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Ball can't be seen on the client side

$
0
0

Has been struggling a while now, Trying to make pong using a client/server connection . The coördinates of both rectangles seem to be exchanged fine, the coördinates of the ball however won't.

I can't find the problem, but the weird thing is, I am doing exactly the same with the rectangle of the server send to the client, but when the server sends the coörds of the ball it just won't work.

server

import processing.net.*;

int size = 15; // Ball diameter
float xpos, ypos; // Position of the ball
float xspeed = random(5,6); // Speed of the ball in the x-direction
float yspeed = random(0,4); // Velocity of the ball in y-direction
int error1=0;
int error2=0;
int playerpos1;


Server s;
Client c;
String input;
int data[];

void setup() {
  s = new Server(this, 12345);  // Start a simple server on a port

  size(640, 400);
  noStroke();
  frameRate(30);
  smooth();
  playerpos1 = height/2;
  xpos = 45;
  ypos = playerpos1;
  frameRate(30);
}
void draw() {
  textSize(20);
  textAlign(CENTER);
  text ("Score: " + str(error2)+ ":" +str(error1) , width/2, 20);
    background(204);


  // Receive data from client
  c = s.available();
  if (c != null) {
    input = c.readString();
    input = input.substring(0, input.indexOf("\n"));  // Only up to the newline
    data = int(split(input, ' '));  // Split values into an array
    // Draw rect using received coords
    rect(data[0], data[1]-50, data[2], data[3]);
  }

 // speed of the ball
 xspeed *= 1.001;
 xpos += xspeed;
 ypos += yspeed;

// bounce the ball (up, down)
 if (ypos > height-size/2 || ypos < 0+size/2) {
 yspeed *= -1;
 }

// Boundary left and error
 if ( xpos <= 30+size/2) {
 if (ypos > playerpos1-50 && ypos < playerpos1+50){
 xspeed *= -1;
 yspeed += (ypos-playerpos1)/5;
 }
 }

 //Error 1
 if (xpos <= 0){
 error1 += 1;
 if (error1 <= 4){
 xpos = 50;
 ypos = playerpos1;
 xspeed = random(5,6);
 //xspeed *= -1;
 yspeed = 0;
println("Score: "+ str(error2)+":"+str(error1) );
 }
 if (error1 ==5) {
 println("Player 2 has won:"+ str(error2));
   text ("Player 2 has won " + str(error2), width/2, 20);
 }
 }

 // Right limit and error
 if ( xpos >= width-30-size/2) {
 if (ypos > data[1]-50 && ypos < data[1]+50){
 xspeed *= -1;
 yspeed += (ypos-data[1])/5;
 }
 }

 //Error 2
 if (xpos >= width){
 error2 +=1 ;
 if (error2 <= 4){
 xpos = width-50;
 ypos = data[1];
 xspeed = -random(5,6);
 //xspeed *= -1;
 yspeed = 0;
println("Score: "+ error2+":"+ error1 );
 }

 if (error2 ==5) {
 println("Player 1 has won:"+ str(error1));
   text ("Player 1 has won:" + str(error1), width/2, 20);
 }
 }

 // Draw the ball and the rectangles

 ellipse(xpos, ypos, size, size);
 rect (10, playerpos1-50,20,100);
 s.write(10 + " " + playerpos1 + " " + 20 + " " + 100 + " " + xpos + " " + ypos + " " + "\n");


// Move the rectangles
 if (keyPressed) {
   if (key == 'w') {
   if (playerpos1 >=50)
   playerpos1 = playerpos1 - 8;
   }
   if (key == 's') {
   if (playerpos1 <=height-50)
   playerpos1 = playerpos1 + 8;
   }
 }

}

client

import processing.net.*;


int playerpos2;


Client c;
String input;
int data[];
int size = 15;

void setup() {
  // Connect to the server’s IP address and port­
  c = new Client(this, "000.000.0.00", 12345); // Replace with your server’s IP and port

  size(640, 400);
  noStroke();
  frameRate(30);
  smooth();
  playerpos2 =height/2;
  frameRate(30);
}

void draw() {
    background(204);

    // Draw rect
     fill(255);
     rect(width-30, playerpos2-50,20,100);
    // Send mouse coords to other person
    c.write((width-30) + " " + playerpos2 + " " + 20 + " " + 100 + "\n");


  // Receive data from server
   if (c.available() > 0) {
    input = c.readString();
    input = input.substring(0,input.indexOf("\n"));  // Only up to the newline
    data = int(split(input, ' '));  // Split values into an array
    // Draw line using received coords
    fill(255);
    rect(data[0], data[1], data[2], data[3]);
    ellipse(data[4], data[5], size, size);
  }



   if (keyPressed) {
   if (key == 'i')
    {
     if (playerpos2 >=50)
     playerpos2 = playerpos2 - 8;
     }
     if (key == 'k') {
     if (playerpos2 <=height-50)
     playerpos2 = playerpos2 + 8;
   }
 }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles