/* Gilad Lotan - Etch & Sketch-"ish" Networked game for two players. Data from potentiometers is stored in a text file on the itp server. It is parsed and translated into the visuals of the game. Each colored square must try to get as many points as possible by touching the same colored ball. If the other colored ball touches, then points are deducted. */ Rectangle player1,player2,ball1,ball2; int xPos1,OldxPos1,xPos2,OldxPos2; // horiz position of the nub int yPos1,OldyPos1,yPos2,OldyPos2; // vert pos of the nub //set up some global variables int ball1Xspeed,ball2Xspeed; int ball1Yspeed,ball2Yspeed; int ball1X,ball2X; int ball1Y,ball2Y; int ballR; int Count1,Count2; String sentence = ""; int[] data; //our setup function void setup() { size(560,560); //define size background(255); // initial position of player1 xPos1 = 0; yPos1 = height/2; // initial position of player2 xPos2 = width; yPos2 = height/2; // allocate space for new Rectangle objects player1 = new Rectangle(xPos1,yPos1,50,50); player2 = new Rectangle(xPos2,yPos2,50,50); ball1 = new Rectangle(width/2, height/2,40,40); ball2 = new Rectangle(width/2, height/2,40,40); ball1Xspeed = 4; //(int) random(-5,5); //if we want random (note "random" gives us a float ball1Yspeed = 2; //(int) random(-5,5); //so we have to "cast" it back into an "int") ball1X = width/2; //initialize our x,y,r variables ball1Y = height/2; ballR = 30; ball2Xspeed = 4; //(int) random(-5,5); //if we want random (note "random" gives us a float ball2Yspeed = 1; //(int) random(-5,5); //so we have to "cast" it back into an "int") ball2X = width/4; //initialize our x,y,r variables ball2Y = height/4; ballR = 30; Count1 = 0; Count2 = 0; load(); } void draw() { background(50); //first we draw the background // Draw Player1 stroke(255,0,0); fill(50+Count1,50,50); player1.setLocation(xPos1,yPos1); if(player1.intersects(ball1)){ Count1++; } if(player1.intersects(ball2)){ if( Count1>0 ) Count1--; } rect(player1.x,player1.y,player1.width,player1.height); // Draw Player2 stroke(0,255,0); fill(50,50+Count2,50); player2.setLocation(xPos2,yPos2); // add points when touching ball 2 if(player2.intersects(ball2)){ Count2++; } // deduct points when touching ball 1 if(player2.intersects(ball1)){ if( Count2>0 ) Count2--; } rect(player2.x,player2.y,player2.width,player2.height); // don't touch the other player if(player1.intersects(player2)){ if( Count1>0 ) Count1--; if( Count2>0 ) Count2--; } bounceBalls(); load(); println("***SCORE*** player1 : "+Count1+" player2 : "+Count2); } void load() { String editText; // if doesn't get a loadStrings use the old one... // get updated reading from the data1.txt file String url1 = "http://itp.nyu.edu/~gl637/data1.txt"; String[] lines1 = loadStrings(url1); String url2 = "http://itp.nyu.edu/~gl637/data2.txt"; String[] lines2 = loadStrings(url2); // parse player1 int token1 = lines1[0].indexOf(":"); String tmpStr=lines1[0].substring(token1+1); int token2 = tmpStr.indexOf(":"); // parse the two sides of the ':' and turn into integers String String1 = tmpStr.substring(0,token2); String String2 = tmpStr.substring(token2+1,tmpStr.length()); xPos1= int(String1); xPos1/=2; yPos1= int(String2); yPos1/=2; println("1: " + xPos1 + ", " + yPos1); // parse player2 token1 = lines2[0].indexOf(":"); tmpStr=lines2[0].substring(token1+1); token2 = tmpStr.indexOf(":"); // parse the two sides of the ':' and turn into integers String String3 = tmpStr.substring(0,token2); String String4 = tmpStr.substring(token2+1,tmpStr.length()); xPos2= int(String3); xPos2/=2; yPos2= int(String4); yPos2/=2; println("2: " + xPos2 + ", " + yPos2); return; } void bounceBalls() { noStroke(); // draw ball 1 fill(200,0,0); //set ellipse color ellipse(ball1X,ball1Y,ballR,ballR); //draw ellipse //adjust x,y based on speed ball1X = ball1X + ball1Xspeed; ball1Y = ball1Y + ball1Yspeed; ball1.setLocation(ball1X,ball1Y); //acount for bouncing off edges if ((ball1.x > width) || (ball1.x < 0)) { ball1Xspeed = ball1Xspeed * -1; } if ((ball1.y > height) || (ball1.y < 0)) { ball1Yspeed = ball1Yspeed * -1; } // draw ball 2 fill(0,200,0); //set ellipse color ellipse(ball2X,ball2Y,ballR,ballR); //draw ellipse //adjust x,y based on speed ball2X = ball2X + ball2Xspeed; ball2Y = ball2Y + ball2Yspeed; ball2.setLocation(ball2X,ball2Y); //acount for bouncing off edges if ((ball2.x > width) || (ball2.x < 0)) { ball2Xspeed = ball2Xspeed * -1; } if ((ball2.y > height) || (ball2.y < 0)) { ball2Yspeed = ball2Yspeed * -1; } }