Improvements of snake game
parent
86c2d02e73
commit
24cfc840e0
|
|
@ -1219,7 +1219,7 @@
|
||||||
"name": "Snake",
|
"name": "Snake",
|
||||||
"shortName":"Snake",
|
"shortName":"Snake",
|
||||||
"icon": "snake.png",
|
"icon": "snake.png",
|
||||||
"version":"0.01",
|
"version":"0.02",
|
||||||
"description": "The classic snake game. Eat apples and don't bite your tail:",
|
"description": "The classic snake game. Eat apples and don't bite your tail:",
|
||||||
"tags": "game,fun",
|
"tags": "game,fun",
|
||||||
"readme": "README.md",
|
"readme": "README.md",
|
||||||
|
|
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
0.01: New App!
|
0.01: New App!
|
||||||
|
0.02: Performance and graphic improvements, game pause, beep and buzz
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# Snake
|
# Snake
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
The legentary classic game is now available on Bangle.js!
|
The legentary classic game is now available on Bangle.js!
|
||||||
Eat apples and don't bite your tail.
|
Eat apples and don't bite your tail.
|
||||||
|
|
@ -11,3 +11,4 @@ Eat apples and don't bite your tail.
|
||||||
- DOWN: BTN3
|
- DOWN: BTN3
|
||||||
- LEFT: BTN4
|
- LEFT: BTN4
|
||||||
- RIGHT: BTN5
|
- RIGHT: BTN5
|
||||||
|
- PAUSE: BTN2
|
||||||
|
|
|
||||||
|
|
@ -1,37 +1,61 @@
|
||||||
|
Bangle.setLCDMode("120x120");
|
||||||
|
|
||||||
const H = g.getWidth();
|
const H = g.getWidth();
|
||||||
const W = g.getHeight();
|
const W = g.getHeight();
|
||||||
let running = true;
|
let running = true;
|
||||||
let score = 0;
|
let score = 0;
|
||||||
let d;
|
let d;
|
||||||
|
const gridSize = 20;
|
||||||
// game world
|
|
||||||
const gridSize = 40;
|
|
||||||
const tileSize = 6;
|
const tileSize = 6;
|
||||||
let nextX = 0;
|
let nextX = 0;
|
||||||
let nextY = 0;
|
let nextY = 0;
|
||||||
|
|
||||||
// snake
|
|
||||||
const defaultTailSize = 3;
|
const defaultTailSize = 3;
|
||||||
let tailSize = defaultTailSize;
|
let tailSize = defaultTailSize;
|
||||||
const snakeTrail = [];
|
const snakeTrail = [];
|
||||||
let snakeX = 10;
|
const snake = { x: 10, y: 10 };
|
||||||
let snakeY = 10;
|
const apple = { x: Math.floor(Math.random() * gridSize), y: Math.floor(Math.random() * gridSize) };
|
||||||
|
|
||||||
// apple
|
function drawBackground(){
|
||||||
let appleX = Math.floor(Math.random() * gridSize);
|
g.setColor("#000000");
|
||||||
let appleY = Math.floor(Math.random() * gridSize);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
function gameStart() {
|
function gameStart() {
|
||||||
running = true;
|
running = true;
|
||||||
score = 0;
|
score = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function gameStop() {
|
function gameOver() {
|
||||||
g.clear();
|
g.clear();
|
||||||
g.setColor("#FFFFFF");
|
g.setColor("#FFFFFF");
|
||||||
g.setFont("6x8", 2);
|
g.setFont("6x8");
|
||||||
g.drawString("GAME OVER!", W / 2, H / 2 - 20);
|
g.drawString("GAME OVER!", W / 2, H / 2 - 10);
|
||||||
g.drawString("Tap to Restart", W / 2, H / 2 + 20);
|
g.drawString("Tap to Restart", W / 2, H / 2 + 10);
|
||||||
running = false;
|
running = false;
|
||||||
tailSize = defaultTailSize;
|
tailSize = defaultTailSize;
|
||||||
}
|
}
|
||||||
|
|
@ -41,66 +65,50 @@ function draw() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.clear();
|
||||||
|
|
||||||
// move snake in next pos
|
// move snake in next pos
|
||||||
snakeX += nextX;
|
snake.x += nextX;
|
||||||
snakeY += nextY;
|
snake.y += nextY;
|
||||||
|
|
||||||
// snake over game world?
|
// snake over game world
|
||||||
if (snakeX < 0) {
|
if (snake.x < 0) {
|
||||||
snakeX = gridSize - 1;
|
snake.x = gridSize - 1;
|
||||||
|
}
|
||||||
|
if (snake.x > gridSize - 1) {
|
||||||
|
snake.x = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (snakeX > gridSize - 1) {
|
if (snake.y < 0) {
|
||||||
snakeX = 0;
|
snake.y = gridSize - 1;
|
||||||
|
}
|
||||||
|
if (snake.y > gridSize - 1) {
|
||||||
|
snake.y = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (snakeY < 0) {
|
//snake bite apple
|
||||||
snakeY = gridSize - 1;
|
if (snake.x === apple.x && snake.y === apple.y) {
|
||||||
}
|
Bangle.beep(20);
|
||||||
if (snakeY > gridSize - 1) {
|
|
||||||
snakeY = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//snake bite apple?
|
|
||||||
if (snakeX === appleX && snakeY === appleY) {
|
|
||||||
tailSize++;
|
tailSize++;
|
||||||
score++;
|
score++;
|
||||||
|
|
||||||
appleX = Math.floor(Math.random() * gridSize);
|
apple.x = Math.floor(Math.random() * gridSize);
|
||||||
appleY = Math.floor(Math.random() * gridSize);
|
apple.y = Math.floor(Math.random() * gridSize);
|
||||||
|
drawApple();
|
||||||
}
|
}
|
||||||
|
|
||||||
//paint background
|
drawBackground();
|
||||||
g.setColor("#000000");
|
drawApple();
|
||||||
g.fillRect(0, 0, H, W);
|
drawSnake();
|
||||||
|
drawScore();
|
||||||
// paint snake
|
|
||||||
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 === snakeX && snakeTrail[i].y === snakeY && tailSize > defaultTailSize) {
|
|
||||||
gameStop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// paint apple
|
|
||||||
g.setColor("#FF0000");
|
|
||||||
g.fillRect(appleX * tileSize, appleY * tileSize, appleX * tileSize + tileSize, appleY * tileSize + tileSize);
|
|
||||||
|
|
||||||
// paint score
|
|
||||||
g.setColor("#FFFFFF");
|
|
||||||
g.setFont("6x8");
|
|
||||||
g.setFontAlign(0, 0);
|
|
||||||
g.drawString("Score:" + score, W / 2, 10);
|
|
||||||
|
|
||||||
//set snake trail
|
//set snake trail
|
||||||
snakeTrail.push({ x: snakeX, y: snakeY });
|
snakeTrail.push({ x: snake.x, y: snake.y });
|
||||||
while (snakeTrail.length > tailSize) {
|
while (snakeTrail.length > tailSize) {
|
||||||
snakeTrail.shift();
|
snakeTrail.shift();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g.flip();
|
||||||
}
|
}
|
||||||
|
|
||||||
// input
|
// input
|
||||||
|
|
@ -132,6 +140,9 @@ setWatch(() => {// Right
|
||||||
d = 'r';
|
d = 'r';
|
||||||
}
|
}
|
||||||
}, BTN5, { repeat: true });
|
}, BTN5, { repeat: true });
|
||||||
|
setWatch(() => {// Pause
|
||||||
|
running = !running;
|
||||||
|
}, BTN2, { repeat: true });
|
||||||
|
|
||||||
Bangle.on('touch', button => {
|
Bangle.on('touch', button => {
|
||||||
if (!running) {
|
if (!running) {
|
||||||
|
|
@ -140,5 +151,5 @@ Bangle.on('touch', button => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// render X times per second
|
// render X times per second
|
||||||
var x = 5;
|
const x = 5;
|
||||||
setInterval(draw, 1000 / x);
|
setInterval(draw, 1000 / x);
|
||||||
Loading…
Reference in New Issue