xxxxxxxxxx
141
// players positions
let [xV, yV] = [100, 100];
let [xp, yp] = [350, 350];
// players directions
let vilainVel = 2;
let playerVel = 4;
// background loading
let img;
// gameOn
let gameOn = false;
function preload() {
img = loadImage("grass.jpg");
}
function setup() {
createCanvas(600, 600);
frameRate(30);
xV = random(0, 500);
yV = random(0, 500);
xp = random(0, 500);
yp = random(0, 500);
}
function draw() {
image(img, 0, 0);
background("rgba(255,255,255, 0.50)");
if (keyIsDown(87)) {
yp = yp - playerVel;
}
if (keyIsDown(83)) {
yp = yp + playerVel;
}
if (keyIsDown(68)) {
xp = xp + playerVel;
}
if (keyIsDown(65)) {
xp = xp - playerVel;
}
drawCreeper(xV, yV);
drawPlayer(xp, yp);
movimentos = {
cima: new movimento(xV, yV - 1),
baixo: new movimento(xV, yV + 1),
direita: new movimento(xV + 1, yV),
esquerda: new movimento(xV - 1, yV),
baixoEsquerda: new movimento(xV - 1, yV + 1),
cimaEsquerda: new movimento(xV - 1, yV - 1),
baixoDireita: new movimento(xV + 1, yV + 1),
cimaDireita: new movimento(xV - 1, yV - 1),
};
results = [];
for (var move in movimentos) {
var distancia = dist(movimentos[move].x, movimentos[move].y, xp - 120, yp -50);
//console.log("distancia:",distancia)
results.push({
key: move,
value: distancia,
});
}
let r = escolheMovimento(results);
let m = r[0];
if (gameOn) {
if (m == "cima") {
yV = yV - vilainVel;
}
if (m == "baixo") {
yV = yV + vilainVel;
}
if (m == "esquerda") {
xV = xV - vilainVel;
}
if (m == "direita") {
xV = xV + vilainVel;
}
if (m == "baixoEsquerda") {
yV = yV + vilainVel;
xV = xV - vilainVel;
}
if (m == "cimaEsquerda") {
yV = yV - vilainVel;
xV = xV - vilainVel;
}
if (m == "baixoDireita") {
yV = yV + vilainVel;
xV = xV + vilainVel;
}
if (m == "cimaDireita") {
yV = yV - vilainVel;
xV = xV + vilainVel;
}
}
if (r[1] >= 0 && r[1] <= 20) {
gameOn = false;
xV = random(0, 500);
yV = random(0, 500);
xp = random(0, 500);
yp = random(0, 500);
}
if (gameOn == false) {
background(51);
fill("white");
textSize(31);
text("Clique para começar", 150, 300);
}
}
function movimento(x, y) {
this.x = x;
this.y = y;
}
function escolheMovimento(results) {
let menor = 800;
let movimento = "";
for (var i = 0; i < results.length; i++) {
if (results[i].value < menor) {
menor = results[i].value;
movimento = results[i].key;
}
}
return [movimento, menor];
}
function mouseClicked() {
gameOn = true;
}