Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
100 lines (97 sloc) 2.62 KB
//global variables go here!
// I need to import the arduino
import processing.serial.*;
Serial myPort; // this is the serial port?
// the following variables are imported.
int pot1, pot2;
float xPos, xSpeed = 5, ySpeed = 7;
// these are the point counters
int point1, point2;
// these are the X-Position for the paddles
int paddle1X = 150, paddle2X = 850;
int paddleH = 100; // height of the paddle
// rules for the canvas
int canvasStart = 100, roof = 100; // where the ball will bounce
int floor = 900, canvasEnd = 900; // where the ball will bounce
// rules for the ball
int ballXPos = 500, ballYPos = 500; // initial position of the ball
int ballXSpeed = 1, ballYSpeed = 1;
void setup() {
size(1000, 1000);
//lists all serial ports
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
}
void draw() {
background(200);
scoreKeeper();
gameReset();
pongTable();
// draw a ball at x position
// color of the ball
while (myPort.available() > 1) {
// if so, read one byte of data at a time, saving to two separate variables
pot1 = myPort.read();
pot2 = myPort.read();
// print out the value just to see their values
// println(pot1, pot2);
}
// draw an ellipse with the size relative to
// the analog reading from the arduino
fill(255);
float l = map(pot1, 0, 255, 100, 800);
float r = map(pot2, 0, 255, 100, 800);
int y1 =int(r);
int y2= int(l);
stroke(255);
line(paddle1X, y1, paddle1X, y1+paddleH);
//paddle 2
line(paddle2X, y2, paddle2X, y2+paddleH);
fill(255, 255, 0);
ballRules();
circle(ballXPos, ballYPos, 40);
ballBounceRoofandFloor();
// move the ball
ballXPos = ballXPos + ballXSpeed;
ballYPos = ballYPos + ballYSpeed;
//another rule for Y position
//
if ((ballXPos-20==paddle1X)&&((ballYPos<=y1+100)&&(ballYPos>=y1))) {
ballXSpeed = ballXSpeed * -1; // reverse!
}
if ((ballXPos+20==paddle2X)&&((ballYPos<=y2+100)&&(ballYPos>=y2))) {
ballXSpeed = ballXSpeed * -1; // reverse!
}
}
void ballRules() {
ballXPos = ballXPos + ballXSpeed;
ballYPos = ballYPos + ballYSpeed;
}
void ballBounceRoofandFloor() {
if (ballYPos >= floor-20 || ballYPos <= roof+20) {
ballYSpeed = ballYSpeed * -1; // reverse!
}
}
void pongTable() {
fill(0);
rect(100, 100, 800, 800);
}
void gameReset() {
//rules for resetting the board.
if ((ballXPos <=100)||(ballXPos>=900)) {
if (ballXPos <= 100) {
point2++;
}
if (ballXPos >= 900) {
point1++;
}
ballXPos = 500;
ballYPos = 500;
}
}
void scoreKeeper() {
fill(255);
textSize(50);
text("P1 points:"+point1, 100, 80);
text("P2 points:"+point2, 625, 80);
}
You can’t perform that action at this time.