1
0
Fork 0
BangleApps/apps/snake/snake.js

155 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-04-14 15:43:12 +00:00
Bangle.setLCDMode("120x120");
2020-04-14 07:08:26 +00:00
const H = g.getWidth();
const W = g.getHeight();
let running = true;
let score = 0;
let d;
2020-04-14 15:43:12 +00:00
const gridSize = 20;
2020-04-14 07:08:26 +00:00
const tileSize = 6;
let nextX = 0;
let nextY = 0;
const defaultTailSize = 3;
let tailSize = defaultTailSize;
const snakeTrail = [];
2020-04-14 15:43:12 +00:00
const snake = { x: 10, y: 10 };
const apple = { x: Math.floor(Math.random() * gridSize), y: Math.floor(Math.random() * gridSize) };
function drawBackground(){
g.setColor("#000000");
g.fillRect(0, 0, H, W);
}
function drawApple(){
g.setColor("#FF0000");
g.fillCircle((apple.x * tileSize) + tileSize/2, (apple.y * tileSize) + tileSize/2, tileSize/2);
}
2020-04-14 07:08:26 +00:00
2020-04-14 15:43:12 +00:00
function drawSnake(){
g.setColor("#008000");
for (let i = 0; i < snakeTrail.length; i++) {
g.fillRect(snakeTrail[i].x * tileSize, snakeTrail[i].y * tileSize, snakeTrail[i].x * tileSize + tileSize, snakeTrail[i].y * tileSize + tileSize);
//snake bites it's tail
if (snakeTrail[i].x === snake.x && snakeTrail[i].y === snake.y && tailSize > defaultTailSize) {
Bangle.buzz(1000);
gameOver();
}
}
}
function drawScore(){
g.setColor("#FFFFFF");
g.setFont("6x8");
g.setFontAlign(0, 0);
g.drawString("Score:" + score, W / 2, 10);
}
2020-04-14 07:08:26 +00:00
function gameStart() {
running = true;
score = 0;
}
2020-04-14 15:43:12 +00:00
function gameOver() {
2020-04-14 07:08:26 +00:00
g.clear();
g.setColor("#FFFFFF");
2020-04-14 15:43:12 +00:00
g.setFont("6x8");
g.drawString("GAME OVER!", W / 2, H / 2 - 10);
g.drawString("Tap to Restart", W / 2, H / 2 + 10);
2020-04-14 07:08:26 +00:00
running = false;
tailSize = defaultTailSize;
}
function draw() {
if (!running) {
return;
}
2020-04-14 15:43:12 +00:00
g.clear();
2020-04-14 07:08:26 +00:00
// move snake in next pos
2020-04-14 15:43:12 +00:00
snake.x += nextX;
snake.y += nextY;
2020-04-14 07:08:26 +00:00
2020-04-14 15:43:12 +00:00
// snake over game world
if (snake.x < 0) {
snake.x = gridSize - 1;
2020-04-14 07:08:26 +00:00
}
2020-04-14 15:43:12 +00:00
if (snake.x > gridSize - 1) {
snake.x = 0;
2020-04-14 07:08:26 +00:00
}
2020-04-14 15:43:12 +00:00
if (snake.y < 0) {
snake.y = gridSize - 1;
2020-04-14 07:08:26 +00:00
}
2020-04-14 15:43:12 +00:00
if (snake.y > gridSize - 1) {
snake.y = 0;
2020-04-14 07:08:26 +00:00
}
2020-04-14 15:43:12 +00:00
//snake bite apple
if (snake.x === apple.x && snake.y === apple.y) {
Bangle.beep(20);
2020-04-14 07:08:26 +00:00
tailSize++;
score++;
2020-04-14 15:43:12 +00:00
apple.x = Math.floor(Math.random() * gridSize);
apple.y = Math.floor(Math.random() * gridSize);
drawApple();
2020-04-14 07:08:26 +00:00
}
2020-04-14 15:43:12 +00:00
drawBackground();
drawApple();
drawSnake();
drawScore();
2020-04-14 07:08:26 +00:00
//set snake trail
2020-04-14 15:43:12 +00:00
snakeTrail.push({ x: snake.x, y: snake.y });
2020-04-14 07:08:26 +00:00
while (snakeTrail.length > tailSize) {
snakeTrail.shift();
}
2020-04-14 15:43:12 +00:00
g.flip();
2020-04-14 07:08:26 +00:00
}
// input
setWatch(() => {// Up
if (d !== 'd') {
nextX = 0;
nextY = -1;
d = 'u';
}
}, BTN1, { repeat: true });
setWatch(() => {// Down
if (d !== 'u') {
nextX = 0;
nextY = 1;
d = 'd';
}
}, BTN3, { repeat: true });
setWatch(() => {// Left
if (d !== 'r') {
nextX = -1;
nextY = 0;
d = 'l';
}
}, BTN4, { repeat: true });
setWatch(() => {// Right
if (d !== 'l') {
nextX = 1;
nextY = 0;
d = 'r';
}
}, BTN5, { repeat: true });
2020-04-14 15:43:12 +00:00
setWatch(() => {// Pause
running = !running;
}, BTN2, { repeat: true });
2020-04-14 07:08:26 +00:00
Bangle.on('touch', button => {
if (!running) {
gameStart();
}
});
// render X times per second
2020-04-14 15:43:12 +00:00
const x = 5;
2020-04-14 07:08:26 +00:00
setInterval(draw, 1000 / x);