import java.io.*; import java.net.*; //connectXport String host0, host1; int port0, port1; Socket s[]; DataInputStream is[]; DataOutputStream os[]; int data[]; //create ball and paddles Rectangle pad1, pad2, ball; Point ballPoint, ballSpeed; int ballWidth = 12; int padWidth = 10; int padHeight = 70; PFont font; int p0points = 0; int p1points = 0; void setup(){ size(400,255); background(0); framerate(30); host0 = "xport3.faludi.com"; port0 = 10001; host1 = "128.122.151.110"; port1 = 10001; s = new Socket[2]; is = new DataInputStream[2]; os = new DataOutputStream[2]; data = new int[2]; for (int i=0; i<2; i++) { data[i] = 0; } font = loadFont("Arial-Black-16.vlw"); connectXport(host0, port0, 0); connectXport(host1, port1, 1); pad1 = new Rectangle(10,(height/2)+(padHeight/2),padWidth,padHeight); pad2 = new Rectangle(width-10-padWidth ,(height/2)+(padHeight/2),padWidth,padHeight); resetBall(); ball = new Rectangle((int)ballPoint.x, (int)ballPoint.y,ballWidth,ballWidth); } void draw(){ background(0); //check connections checkConnection(host0, port0, 0); checkConnection(host1, port1, 1); //draw paddle1 with network updated locations pad1.setLocation(10,readByte(0)); //pad1.setLocation(10,mouseY); noStroke(); fill(255,0,0); rect(pad1.x,pad1.y,pad2.width,pad2.height); //draw paddle2 with network updated locations pad2.setLocation(width-10-padWidth,readByte(1)); //pad2.setLocation(width-10-padWidth ,mouseY); noStroke(); fill(0,255,0); rect(pad2.x,pad2.y,pad2.width,pad2.height); checkBall(); moveBall(); textFont(font, 16); fill(255); text(str(p0points), width/2-60, 25); text(str(p1points), width/2+60, 25); } ///////CONNECT XPORT\\\\\\\\ void connectXport(String host_, int port_, int connection){ println("trying to connect to: " + host_ + " at port: " + port_); try{ s[connection] = new Socket(host_,port_); println("connected!"); } catch(Exception e){ e.printStackTrace(); println("unable to connect to: " + host_+ " at port: " + port_); } } ////////CHECK CONNECTION\\\\\\\\ void checkConnection(String host_, int port_, int connection ) { if(s[connection] == null) { connectXport(host_, port_, connection); } else if(s[connection].isConnected() == false) { connectXport(host_, port_, connection); } } ////////READ BYTE\\\\\\\\\\ int readByte(int connection){ if ( is[connection] == null) { try { is[connection] = new DataInputStream(s[connection].getInputStream()); println("opening input stream"); } catch (Exception e) { e.printStackTrace(); println("no input stream"); } } try{ //DataInputStream is = new DataInputStream(s[connection].getInputStream()); // ONLY READ THE BYTE IF THERE IS A BYTE TO READ OTHERWISE DON'T BLOCK! if (is[connection].available()>0) { data[connection] = int(is[connection].readByte()); } } catch(Exception e){ e.printStackTrace(); println("no data"); } return data[connection]; } ////////WRITE BYTE\\\\\\\\\\ void writeByte(int connection, char event){ if (os[connection] == null) { try { os[connection] = new DataOutputStream(s[connection].getOutputStream()); } catch (Exception e) { e.printStackTrace(); println("no output stream"); } } try{ os[connection].writeByte(event); } catch(Exception e){ e.printStackTrace(); println("event send failed"); } } ///////CHECK BALL\\\\\\\\ void checkBall(){ //check if ball has intersected with a paddle if(ball.intersects(pad1)){ ballSpeed.x *= -1.25; writeByte(0,'H'); // send a Hit message } if(ball.intersects(pad2)){ ballSpeed.x *= -1.25; writeByte(1,'H'); // send a Hit message } //check if ball is off the right side of screen if(ball.getX() > width){ points(1); writeByte(1,'M'); // send a Miss message resetBall(); } //check to see if ball is off the left side of screen if(ball.getX() < 0-ballWidth){ points(0); writeByte(0,'M'); // send a Miss message resetBall(); } //check to see if ball has hit top or bottom if(ball.getY() > height-ballWidth || ball.getY() < 0){ ballSpeed.y *= -1.25; } } ///////MOVE BALL\\\\\\\\ void moveBall(){ ballPoint.x += ballSpeed.x; ballPoint.y += ballSpeed.y; ball.setLocation((int)ballPoint.x,(int)ballPoint.y); noStroke(); fill(255); ellipseMode(CENTER); rect(ballPoint.x,ballPoint.y,ballWidth,ballWidth); } ///////RESET BALL\\\\\\\ void resetBall(){ int xDir, yDir; ballPoint = new Point(width/2, height/2); xDir = int(random(0,3))-1; while(xDir == 0){ xDir = int(random(0,3))-1; } yDir = int(random(0,3))-1; while(yDir == 0){ yDir = int(random(0,3))-1; } ballSpeed = new Point(xDir*4,yDir*2); } void points(int player){ if(player == 0){ p0points++; println("player1 has " + p0points + " points"); if( p0points == 10){ println("PLAYER1 WINS!!!!!"); p0points = 0; p1points = 0; } } if(player == 1){ p1points++; println("player2 has " + p1points + " points"); if( p1points == 10){ println("PLAYER2 WINS!!!!!"); p0points = 0; p1points = 0; } } } public void stop() { try { for(int i=0; i<2; i++) { is[i].close(); os[i].close(); s[i].close(); } } catch (Exception e) { e.printStackTrace(); println("couldn't close connection"); } }