Created
October 19, 2016 09:44
-
-
Save stephkoltun/1f42e1a02d2a597ff5105ecbd2bae866 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// paddle is constantly falling | |
// adjusting the light level of the photo resistor | |
// counteracts the "gravity" | |
var serial; // variable to hold an instance of the serialport library | |
var sensorValue; | |
var paddle; | |
var ball; | |
function Ball() { | |
this.x = width/2; | |
this.y = height/2; | |
this.diameter = 6; | |
this.xSpeed = 1; | |
this.ySpeed = 2; | |
this.inBounds = true; | |
this.display = function() { | |
fill(255); | |
noStroke(); | |
rect(this.x, this.y, this.diameter, this.diameter); | |
}; | |
this.move = function() { | |
// detect if ball is in same xPos as paddle | |
if (this.x-this.diameter/2 <= paddle.x+paddle.width/2) { | |
// if the paddle is at the same vertical location as ball | |
if (this.y >= paddle.y-paddle.height/2 && this.y <= paddle.y+paddle.height/2) { | |
this.inBounds = true; | |
this.xSpeed = -this.xSpeed; | |
} else { | |
this.inBounds = false; | |
} | |
} | |
if (this.x >= width-this.diameter/2) { | |
this.xSpeed = -this.xSpeed; | |
} | |
if (this.y >= height-this.diameter/2 || this.y <= this.diameter/2) { | |
this.ySpeed = -this.ySpeed; | |
} | |
if (this.inBounds) { | |
this.x = this.x + this.xSpeed; | |
this.y = this.y + this.ySpeed; | |
} else { | |
// player missed ball | |
this.x = -15; | |
this.y = -15; | |
this.xSpeed = 0; | |
this.ySpeed = 0; | |
background(255,0,0); | |
} | |
this.reset = function() { | |
this.x = width/2; | |
this.y = height/2; | |
this.xSpeed = 1; | |
this.ySpeed = 2; | |
this.inBounds = true; | |
}; | |
}; | |
} | |
function Paddle() { | |
this.x = 15; | |
this.y = 100; | |
this.width = 5; | |
this.height = 35; | |
this.speed = 1; | |
this.resistance = -1; // if negative, paddle will fall | |
// if positive, paddle with rise | |
this.display = function() { | |
fill(255); | |
noStroke(); | |
rect(this.x, this.y, this.width, this.height); | |
}; | |
this.move = function() { | |
// detect if mouse is outside vertical canvas dims | |
if (this.y > height-this.height/2) { | |
this.y = height-this.height/2; | |
} else if (this.y < this.height/2) { | |
this.y = this.height/2; | |
} else { | |
this.y = this.y + this.speed + this.resistance; | |
} | |
}; | |
} | |
// get the list of ports: | |
function printList(portList) { | |
for (var i = 0; i < portList.length; i++) { | |
// Display the list the console: | |
println(i + " " + portList[i]); | |
} | |
} | |
function setup() { | |
createCanvas(300, 300); | |
//initialize serial | |
serial = new p5.SerialPort(); // make a new instance of serialport library | |
serial.on('list', printList); // callback function for serialport list event | |
serial.on('data', serialEvent); // callback for new data coming in | |
serial.list(); // list the serial ports | |
serial.open("/dev/cu.usbmodem1411"); // open a port | |
serial.write("X"); | |
rectMode(CENTER); | |
ellipseMode(CENTER); | |
// create paddle and ball | |
paddle = new Paddle(); | |
ball = new Ball(); | |
} | |
function draw() { | |
background(0); | |
fill(255); | |
if (paddle.resistance >= 0) { | |
triangle(20, 15, 30, 15, 25, 25); | |
} else { | |
triangle(20, 25, 30, 25, 25, 15); | |
} | |
text("resistance level: " + floor(paddle.resistance), 40, 25); | |
paddle.display(); | |
paddle.move(); | |
ball.display(); | |
ball.move(); | |
} | |
function mousePressed() { | |
ball.reset(); | |
} | |
function serialEvent() { | |
var inString = serial.readLine(); | |
if (inString.length > 0) { | |
inString = inString.trim(); | |
sensorValue = Number(inString); | |
sensorValue = map(sensorValue, 400, 825, -5, 5); | |
println(sensorValue); | |
// high value = falling (resistance = positive) | |
// low value = floating (resistance = negative) | |
paddle.resistance = sensorValue; | |
serial.write("X"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment