Pong game multi-players with network
in
Core Library Questions
•
1 year ago
Hi I have made Processing pong game for 2 players with net library,
I want the game to be played on networking. The server part receive data
and then relay data to the clients, the clients data to server which is comprise of
ball coordinate, ball velocity, ball size, ball speed, left and right paddle coordinate,
paddle size and their speed. So, Now I can send and receive data through client and
server (see from println() ) but fail to visualise and control the paddle on draw() function
with unknown reason, so please help me to figure the project out.
Here, I have the code for server and client part.
Thank you, I really appreciated your help.
import processing.net.*;
Server server;
String incomingMessage = "";
PFont f;
void setup() {
size(200, 200);
server = new Server(this, 5204);
f = createFont("GIllSansStd", 16, true);
}
void draw() {
background(255);
textFont(f);
fill(0);
textAlign(CENTER);
text(incomingMessage, width/2, height/2);
Client client = server.available();
if(client != null) {
incomingMessage = client.readStringUntil('*');
println("Client says: " + incomingMessage);
// Write the message back out (note this goes to all clients)
server.write(incomingMessage);
}
}
// The server event is called whenever the new client is connect
void serverEvent(Server server, Client client) {
incomingMessage = "A new client has connected: " + client.ip();
println(incomingMessage);
}
//-------------------------------------------------------------------------------------------
// Client part
import processing.net.*;
Client client;
//Ball
float ballx, bally;
float xspeed, yspeed;
float radius;
// Player 1
float rpaddleSize;
float rpaddlex, rpaddley;
// Player 2
float lpaddlex, lpaddley;
float lpaddleSize;
float lspeed;
void setup(){
size(250, 400);
smooth();
ballx = width - 10.0;
bally = height/2;
client = new Client(this, "127.0.0.1", 5204);
}
void draw() {
background(255);
if(client.available() > 0) {
String in = client.readStringUntil('*');
println("Receiving data: " + in);
float[] vals = float(splitTokens(in,",*"));
// 1) Ball: ballx, bally, radius
fill(175);
ellipseMode(CENTER);
ellipse(vals[0], vals[1], vals[11], vals[11]);
vals[0] += vals[2];
vals[1] += vals[3];
// 2) Player paddle: paddleX, paddleY, paddleSize
fill(0, 0, 0);
rect(vals[4], vals[5] - vals[6]/2, 5, vals[6]);
vals[5] = mouseY;
if (vals[5] < vals[6]/2) {
vals[5] = vals[6]/2;
}
if (vals[5] > height - vals[6]/2) {
vals[5] = height - vals[6]/2;
}
// 3) Computer paddle: compaddlex, compaddley, compaddleSize
fill(0, 0, 0);
rect(vals[7], vals[8] - vals[9]/2, 5, vals[9]);
vals[8] = vals[8] + constrain(vals[1] - vals[8], -vals[10], vals[10]);
if (vals[8] < vals[9]/2){
vals[8] = vals[9]/2;
}
if (vals[8] > height - vals[9]/2){
vals[8] = height - vals[9]/2;
}
//1) Collision player
if((abs(vals[0] - vals[4]) < vals[11]/2) && (abs(vals[1]-vals[5]) < vals[6]/2)) {
vals[2] *= -1;
vals[3] = (vals[1] - vals[5])/vals[6]*2;
}
// 2) Collision Computer
if((abs(vals[0] - vals[7]) < vals[11]/2) && (abs(vals[1]-vals[8]) < vals[9]/2)) {
vals[2] *= -1;
vals[3] = (vals[1] - vals[8])/vals[9]*2;
}
// If ball higher than hight or less than 0
if(vals[1] > height || vals[1] < 0) {
vals[1] *= -1;
}
}
pong();
}
void pong() {
ballx = width/2;
bally = height/2;
xspeed = -2;
yspeed = 2;
radius = 10;
// Player 1
rpaddleSize = 30.0;
rpaddlex = 10.0;
rpaddley = height/2;
// Player 2
lpaddlex = 235.0;
lpaddley = height/2;
lpaddleSize = 30.0;
lspeed = 3.3;
String out = ballx + "," + bally + "," + xspeed + "," + yspeed + "," + rpaddlex + "," +
rpaddley + "," + rpaddleSize + "," + lpaddlex + "," + lpaddley + "," + lpaddleSize
+ "," + lspeed + "," + radius + "*";
client.write(out);
println("Sending; " + out);
}
1