xxxxxxxxxx
186
var s;
//pode mudar essa variavel para 10, 20, 40, 50, 100...
var scl = 20;
var f;
function preload() {
ponto = loadSound("ponto.wav");
derrota = loadSound("derrota.wav");
virada = loadSound("virada.wav");
}
function setup() {
createCanvas(600, 500);
s = new Snake();
s.body.push([0, 0]);
f = new Food();
frameRate(10);
food = createVector(random(width), random(height));
}
function draw() {
background(0);
drawGrid();
s.update();
s.death();
s.show();
//verifica se está na mesma posição de f
s.eat(f);
f.show();
}
function keyPressed() {
//VERIFICA SE NÃO ESTÁ TENTANDO ANDAR PRA TRÁS. PORÉM, SE FOR TAMANHO INICIAL, PERMITE ANDAR PRA TRÁS.
if (keyCode === UP_ARROW && (s.yspeed != 1 || s.body.length == 1)) {
s.direction(0, -1);
virada.play();
} else if (keyCode === DOWN_ARROW && (s.yspeed != -1 || s.body.length == 1)) {
s.direction(0, 1);
virada.play();
} else if (keyCode === LEFT_ARROW && (s.xspeed != 1 || s.body.length == 1)) {
s.direction(-1, 0);
virada.play();
} else if (
(keyCode === RIGHT_ARROW && s.xspeed != -1) ||
s.body.length == 1
) {
s.direction(1, 0);
virada.play();
}
}
function drawGrid() {
var passoX = floor(width / scl); // 400/20 = 20
var passoY = floor(height / scl);
var passoXX = width / passoX;
var passoYY = height / passoY;
//desenha linhas horizontais
for (i = 0; i <= height; i = i + passoXX) {
stroke(50);
strokeWeight(0.5);
line(0, i, width, i);
}
//desenha linhas verticais
for (i = 0; i <= width; i = i + passoYY) {
stroke(50);
strokeWeight(0.5);
line(i, 0, i, height);
}
}
function Snake() {
this.x = 0;
this.y = 0;
this.xspeed = 1;
this.yspeed = 0;
this.body = [];
this.update = function () {
this.x = this.x + this.xspeed * scl;
this.y = this.y + this.yspeed * scl;
//Verifica as extremidadas, fazendo mudar a direção
if (this.x > width - scl) {
this.x = 0;
}
if (this.x < 0) {
this.x = width;
}
if (this.y > height - scl) {
this.y = 0;
}
if (this.y < 0) {
this.y = height;
}
this.x = constrain(this.x, 0, width - scl);
this.y = constrain(this.y, 0, height - scl);
// Adiciona o novo ponto no array
this.body.push([this.x, this.y]);
// Retira o primeiro ponto do array
this.body.shift();
};
this.show = function () {
fill(255);
for (var i = 0; i < this.body.length; i++) {
rect(this.body[i][0], this.body[i][1], scl, scl);
}
};
this.direction = function (x, y) {
this.xspeed = x;
this.yspeed = y;
};
this.eat = function (food) {
if (this.x == food.x && this.y == food.y) {
this.body.push([this.x, this.y]);
food.newFood(s);
ponto.play();
}
};
this.death = function () {
//maior elemento do array body
var head = this.body.length - 1;
//iterar através do body exceto o último elemento
for (var i = 0; i < this.body.length - 1; i++) {
if (
this.body[i][0] == this.body[head][0] &&
this.body[i][1] == this.body[head][1]
) {
//morreu
console.log("morreu");
this.x = 0;
this.y = 0;
this.body = [];
this.body.push([0, 0]);
background(255, 0, 100);
derrota.play();
}
}
};
}
function Food() {
this.RandomLocationX = function () {
var cols = width / scl;
return floor(random() * cols) * scl;
};
this.RandomLocationY = function () {
var rows = height / scl;
return floor(random() * rows) * scl;
};
this.x = this.RandomLocationX();
this.y = this.RandomLocationY();
this.newFood = function (s) {
this.x = this.RandomLocationX();
this.y = this.RandomLocationY();
for (var i = 0; i < s.body.length; i++) {
if (s.body[i][0] == this.x && s.body[i][1] == this.y) {
this.newFood(s);
}
}
this.show();
};
this.show = function () {
fill(255, 0, 100);
rect(this.x, this.y, scl, scl);
};
}