xxxxxxxxxx
205
const CELLS_PER_DIMENSION = 11;
const CELLS_RIGHT_OF_CENTER = (CELLS_PER_DIMENSION - 1) / 2;
const STARTING_NUM_SEGMENTS = 3;
const MS_PER_MOVE = 1000;
const SPEEDUP_FACTOR = 3;
let serial;
let snake;
let rez = 20;
let speed = 10;
let food=[];
let w;
let h;
let xPos;
let yPos;
function setup() {
createCanvas(400, 400);
w = floor(width / rez);
h = floor(height / rez);
frameRate(5);
snake = new Snake();
setUpSerial();
// for (let i = 0; i < 3; i++) {
// food.push(
// new Food(
// randomX(),
// randomY(),
// 10,
// 10,
// canvas.ratio,
// '#FF0000'
// )
// );
foodLocation();
foodLocation();
}
function foodLocation() {
let x = floor(random(w));
let y = floor(random(h));
food = createVector(x, y);
// food2= createVector(x, y);
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
snake.setDir(-1, 0);
} else if (keyCode === RIGHT_ARROW) {
snake.setDir(1, 0);
} else if (keyCode === DOWN_ARROW) {
snake.setDir(0, 1);
} else if (keyCode === UP_ARROW) {
snake.setDir(0, -1);
} else if (key == ' ') {
snake.grow();
} else if (key == 's') {
// important to have in order to start the serial connection!!
setUpSerial(SELECT_PORT);
}
}
function draw() {
scale(rez);
background(0,204,0);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
if (snake.eat(food)) {
foodLocation();
}
if (frameCount % speed == 0) {
snake.update(xPos,yPos);
}
if (snake.endGame()) {
print("END GAME");
background(255, 0, 0);
noLoop();
}
noStroke();
fill(255, 0, 0);
rect(food.x, food.y, 1, 1);
fill(255, 144, 144)
rect(food.x, food.y,1,1);
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 2) {
// only store values here
// do everything with those values in the main draw loop
xPos = fromArduino[0];
yPos = fromArduino[1];
print(xPos+ " " + yPos )
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "\n";
writeSerial(sendToArduino);
}
}
class Snake {
constructor() {
this.body = [];
this.body[0] = createVector(floor(w/2), floor(h/2));
this.xdir = 0;
this.ydir = 0;
this.len = 0;
}
setDir(x, y) {
this.xdir = x;
this.ydir = y;
}
update(x,y) {
let head = this.body[this.body.length-1].copy();
this.body.shift();
head.x += this.xdir;
head.y += this.ydir;
this.body.push(head);
if (x>120 && y<120 ) {
snake.setDir(1, 0)
} else if (x<120 && y<120) {
snake.setDir(-1, 0)
} else if (x>120 && y<120) {
snake.setDir(0, 1);
} else if (x>120 && y>120) {
snake.setDir(0, -1);
}
}
grow() {
let head = this.body[this.body.length-1].copy();
this.len++;
this.body.push(head);
}
endGame() {
let x = this.body[this.body.length-1].x;
let y = this.body[this.body.length-1].y;
if(x > w-1 || x < 0 || y > h-1 || y < 0) {
return true;
}
for(let i = 0; i < this.body.length-1; i++) {
let part = this.body[i];
if(part.x == x && part.y == y) {
return true;
}
}
return false;
}
eat(pos) {
let x = this.body[this.body.length-1].x;
let y = this.body[this.body.length-1].y;
if(x == pos.x && y == pos.y) {
this.grow();
return true;
}
return false;
}
show() {
for(let i = 0; i < this.body.length; i++) {
fill(0);
noStroke();
rect(this.body[i].x, this.body[i].y, 1, 1)
}
}
}