Processing:
import processing.serial.*;
/****************************************************
* ezPong_processing
*
* copyleft 2006 | Elie Zananiri
* prisonerjohn@gmail.com
*
* based on the Arduino meets Processing tutorials
*****************************************************/
// serial connection variables
Serial port;
String portName = "/dev/cu.usbserial-3B1";
int baudRate = 19200;
int value = 0;
String buffer="";
int potValue = 0;
// visuals variables
Ball ball;
int ballSize = 10;
Paddle paddle;
int paddleWidth = 80;
int paddleTop = 25;
int paddlePos;
void setup() {
size(600, 400);
framerate(30);
smooth();
// establish serial port connection
port = new Serial(this, portName, baudRate);
// create ball
ball = new Ball(random(ballSize, width-ballSize), random(ballSize, height-ballSize), ballSize, color(200, 0, 0));
// create paddle
paddle = new Paddle(random(paddleWidth, width-paddleWidth), height-paddleTop, paddleWidth, ballSize, color(200, 200, 200));
}
void serialEvent(int serial){
if (serial == 10) {
// if serial is line break store and clear the buffer
potValue = 1023-int(buffer);
buffer = "";
} else if (serial == 60) {
// if serial event is beep signal clear the buffer
buffer = "";
} else {
// add an event to the buffer
buffer += char(serial);
}
// convert potValue to the correct range for the paddle position
paddlePos = (potValue*(width-ballSize-paddleWidth))/1023+ballSize/2;
// move the paddle
paddle.move(paddlePos);
}
void draw() {
// listen to serial port and trigger serial event
while (port.available() > 0) {
value = port.read();
serialEvent(value);
}
// draw graphics
// erase bg using a semi-transparent square to have a motion blur effect
drawBG();
// move and draw the ball
ball.step();
// draw the paddle
paddle.draw();
}
void drawBG() {
// draw a semi-transparent rect over the entire canvas to create a motion blur effect
noStroke();
fill(200, 200, 100, 150);
rect(0, 0, width, height);
}
/**
* class Ball
*/
class Ball {
// position variables
float x;
float y;
// speed variables
float dX;
float dY;
int frame;
// graphics variables
int size;
color col;
// constructor
public Ball(float startX, float startY, int theSize, color theColor) {
x = startX;
y = startY;
size = theSize;
col = theColor;
// randomly set a starting speed
dX = ceil(random(0, 5));
dY = ceil(random(0, 5));
frame = 0;
}
public void move() {
x += dX;
y += dY;
}
public void accelerate() {
dX *= 1.01;
dY *= 1.01;
}
public void draw() {
stroke(0);
fill(col);
ellipse(x, y, size, size);
}
public void checkCollisions() {
// check for right wall collision
if ((x+size/2) > width) {
x = width-size-1;
dX *= -1;
// check for left wall collision
} else if ((x-size/2) < 0) {
x = size+1;
dX *= -1;
}
// check for paddle collision
if ((y+(size/2) > paddle.y) && (x > paddle.x) && (x < (paddle.x+paddle.w))) {
y = paddle.y-size-1;
dY *= -1;
// send signal to serial
//port.write(60);
// check for bottom wall collision
} else if (y+(size/2) > height) {
y = height-size-1;
dY *= -1;
// check for top wall collision
} else if (y-(size/2) < 0) {
y = size+1;
dY *= -1;
}
}
public void step() {
move();
// accelerate ball speed every 30 frames i.e. every second
if (++frame%30 == 0) {
accelerate();
}
checkCollisions();
draw();
}
}
/**
* class Paddle
*/
class Paddle {
// position variables
float x;
float y;
// graphics variables
int w;
int h;
color col;
// constructor
public Paddle(float startX, float startY, int theWidth, int theHeight, color theColor) {
x = startX;
y = startY;
w = theWidth;
h = theHeight;
col = theColor;
}
public void move(float newX) {
// assign the received x position
x = newX;
}
public void draw() {
// set the fill color
noStroke();
fill(col);
// draw the left rounded edge
ellipse(x, y, h, h);
// draw the center
rect(x, y-h/2, w, h);
// draw the right rounded edge
ellipse(x+w, y, h, h);
// set the stroke color
stroke(0);
// add the outline around the paddle shape
line(x, y-h/2, x+w, y-h/2);
line(x, y+h/2, x+w, y+h/2);
arc(x, y, h, h, PI/2, -PI/2);
arc(x+w, y, h, h, -PI/2, PI/2);
}
}