xxxxxxxxxx
181
/*
This P5 sketch is a template for getting started with Serial Communication.
The SerialEvent callback is where incoming data is received
By Arielle Hein, adapted from ITP Phys Comp Serial Labs
Edited March 12 2019
*/
var serial; //variable to hold an instance of the serial port library
var portName = '/dev/ttyACM0'; //fill in with YOUR port
var slideValue = 0;
var potValue = 0;
var valArr;
var x = 0;
var y = 0;
var moveSpeed = 1;
var didHit = 0;
function setup() {
createCanvas(800, 300);
serial = new p5.SerialPort(); //a new instance of serial port library
//set up events for serial communication
serial.on('connected', serverConnected);
serial.on('open', portOpen);
serial.on('data', serialEvent);
serial.on('error', serialError);
serial.on('close', portClose);
//open our serial port
serial.open(portName);
//let's figure out what port we're on - useful for determining your port
// serial.on('list', printList); //set a callback function for the serialport list event
//serial.list(); //list the serial ports
}
function draw() {
background('dodgerblue');
y = Number(slideValue);
drawGame();
drawShip(x,slideValue);
// console.log(x);
moveShip();
didHit = detectCollision()
if(didHit==1){
console.log("You Lose!")
gameOverLED()
x = -80
}
if(x>width){
if(didHit == 0){
console.log("You win!")
}
x = -50;
}
}
//all my callback functions are down here:
//these are useful for giving feedback
function serverConnected(){
console.log('connected to the server');
}
function portOpen(){
console.log('the serial port opened!');
}
//THIS IS WHERE WE RECEIVE DATA!!!!!!
//make sure you're reading data based on how you're sending from arduino
function serialEvent(){
//receive serial data here
inData = serial.readLine();
if(inData==""){
return;
}
valArr = parseString(inData);
}
function serialError(err){
console.log('something went wrong with the port. ' + err);
}
function portClose(){
console.log('the port was closed');
}
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
print(i + " " + portList[i]);
}
}
/*******************************/
//BLAKE'S CODE
function parseString(str){
var ostr = str.split(",");
slideValue = ostr[0];
potValue = ostr[1];
}
function drawGame(){
fill(255,255,255,255)
rect(80,0,40,100)
rect(250,height-100,40,100)
ellipse(450,70,70,70)
rect(600,0,40,120)
rect(600,height-60,40,60)
}
function drawShip(x,y){
fill(0, 0, 0, 255);
x = Number(x);
y = Number(y);
//body
rect(x,y,50,50);
//cockpit
rect(x+50,y+10,10,30);
rect(x+25,y-30,5,30)
}
function moveShip(){
moveSpeed = Math.round(map(Number(potValue),0,254,0,9))
//console.log(Number(x)+" "+Number(moveSpeed))
x = x + moveSpeed
}
function detectCollision(){
x = x+25;
y = y+25;
if(x>80&&x<130){
if(y<100&&y>0){
return 1;
console.log("Hit bar 1 at "+x+" "+y)}
}
else if(x>250&&x<290){
if(y>height-100&&y<height){
return 1;
console.log("Hit bar 2 at "+x+" "+y)}}
else if(x>390&&x<520){
if(y>70&&y<140){
return 1;
console.log("Hit circle at "+x+" "+y)}}
else if(x>600&&x<640){
if(y<100&&y>0){
return 1;
console.log("Hit wall at "+x+" "+y)}
else if(y>height-60&&y<height){
return 1;
console.log("Hit wall at "+x+" "+y)}}
x = x-25;
y = y-25;
return 0;
}
function gameOverLED(){
var flash = [0,20,0,20,40,20,100,0,200,10,250,255,130,140,15];
for (var i = 0; i < 20; i++) {
serial.write(flash[i]);
}
serial.write(0);
}