xxxxxxxxxx
165
const askForPort = true; // true first time to pick port, then change to false
const serial = new p5.WebSerial();
let portButton;
let inData;
let outData;
// let digitalread;
var s;
var scl = 20;
var food;
function Snake() {
this.x = 0;
this.y = 0;
this.xspeed = 2;
this.yspeed = 0;
this.total = 0;
this.tail = [];
this.dir = function(x, y) {
this.xspeed = x;
this.yspeed = y;
}
this.eat = function(pos) {
var d = dist(this.x, this.y, pos.x, pos.y);
if (d < 20) {
this.total ++;
return true;
} else {
return false;
}
}
this.update = function() {
if (this.total === this.tail.length) {
for (var i=0; i < this.tail.length-1; i++) {
this.tail[i] = this.tail[i+1];
}
}
this.tail[this.total-1] = createVector(this.x, this.y);
this.x = this.x + this.xspeed * 5; // Move the snake in steps of 10 pixels
this.y = this.y + this.yspeed * 5;
// Wrap snake position when it goes off the canvas
this.x = (this.x + this.xspeed*scl);
this.y = (this.y + this.yspeed*scl);
this.x = constrain(this.x, 0, width-scl);
this.y = constrain(this.y, 0, height-scl);
}
this.death = function() {
for (var i=0; i<this.tail.length; i++){
var pos = this.tail[i];
var d = dist(this.x, this.y, pos.x, pos.y);
if (d<1) {
console.log("Starting Over!")
fill(255, 0, 0);
textSize(150);
text("GAME\nOVER!", 100, 300);
this.total = 0;
this.tail = [];
}
}
}
this.show = function() {
fill(255);
for (var i=0; i < this.tail.length; i++) {
rect(this.tail[i].x, this.tail[i].y, scl, scl)
}
fill(255);
for (var i=0; i<this.total; i++) {
rect(this.tail[i].x, this.tail[i].y, scl, scl)
}
rect(this.x, this.y, scl, scl);
}
}
function setup() {
createCanvas(600, 600);
s = new Snake();
frameRate(5); // Set the frame rate to make the game run smoother
pickLocation();
if (!navigator.serial) {
alert("WebSerial is not supported. Try Chrome.");
}
// First time you connect, create button to identify port
if (askForPort) {
makePortButton();
} else {
serial.getPorts(); // Skip the button, use port from last time
}
serial.on("portavailable", openPort);
serial.on("data", serialEvent);
}
function pickLocation() {
var cols = floor(width/scl);
var rows = floor(height/scl);
food = createVector(floor(random(cols)), floor(random(rows)));
food.mult(scl);
}
function draw() {
if (inData) {
let x = inData[0];
// console.log(x);
if (x == 0) {
s.dir(0, -1);
} else if (x == 1) {
s.dir(0, 1);
} else if (x == 3) {
s.dir(-1, 0);
} else if (x == 2) {
s.dir(1, 0);
}
background(0);
s.death();
s.update();
s.show();
fill(255, 0, 0);
rect(food.x, food.y, scl, scl);
if (s.eat(food)) {
pickLocation();
}
}
}
function serialEvent() {
// Read a string from the serial port until you get carriage return and newline:
let inString = serial.readStringUntil("\r\n");
// console.log(inString);
// Check to see that there's actually a string there:
if (inString) {
inData = split(inString, ",");
}
}
function openPort() {
serial.open();
if (portButton) portButton.hide();
}
function makePortButton() {
// Create and position a port chooser button:
portButton = createButton("Choose Port");
portButton.position(10, 10);
// Give the port button a mousepressed handler:
portButton.mousePressed(() => serial.requestPort());
}