xxxxxxxxxx
96
var px = 0;
var py = 0;
var gs = 20;
var tc = 20;
var ax = 15;
var ay = 15;
var xv = 1;
var yv = 0;
var trail = [];
var tail = 1;
var dead = false;
var score = 0;
function setup() {
createCanvas(400, 400);
frameRate(15);
}
function draw() {
background(0);
px += xv;
py += yv;
if (px < 0) {
textAlign(CENTER);
textSize(50);
text("Score: " + str(score), width / 2, height / 2);
noLoop();
}
if (px > tc - 1) {
textAlign(CENTER);
textSize(50);
text("Score: " + str(score), width / 2, height / 2);
noLoop();
}
if (py < 0) {
textAlign(CENTER);
textSize(50);
text("Score: " + str(score), width / 2, height / 2);
noLoop();
}
if (py > tc - 1) {
textAlign(CENTER);
textSize(50);
text("Score: " + str(score), width / 2, height / 2);
noLoop();
}
if (ax == px && ay == py) {
tail++;
ax = floor(random() * tc);
ay = floor(random() * tc);
score++;
}
fill(255, 0, 0);
rect(ax * gs, ay * gs, gs - 2, gs - 2);
fill(0, 255, 0);
for (var i = 0; i < trail.length; i++) {
rect(trail[i].x * gs, trail[i].y * gs, gs - 2, gs - 2);
if (trail[i].x == px && trail[i].y == py) {
fill(255);
textAlign(CENTER);
textSize(50);
text("Score: " + str(score), width / 2, height / 2);
noLoop();
}
}
trail.push({
x: px,
y: py
});
while (trail.length > tail) {
trail.shift();
}
}
function keyPressed() {
switch (keyCode) {
case 37:
xv = -1;
yv = 0;
break;
case 38:
xv = 0;
yv = -1;
break;
case 39:
xv = 1;
yv = 0;
break;
case 40:
xv = 0;
yv = 1;
break;
}
}