1
0
Fork 0

stacker updated to 0.2: optimizations

master
novadawn999 2023-07-31 10:42:10 -05:00
parent d8c6dbe03b
commit fbb38ebe55
3 changed files with 46 additions and 50 deletions

View File

@ -1 +1,2 @@
0.01: New App!
0.01: New App!
0.02: Optimizations

View File

@ -1,25 +1,21 @@
const TICKRATE = 80;
const HARDWARE_VERSION = process.env.HWVERSION;
const BUTTON = HARDWARE_VERSION === 2 ? BTN : BTN2;
const TICKRATE = 69;
const BLOCK_SIZE = 16;
const BLOCK_COLOR_PRIMARY = "#ff0000";
const BLOCK_COLOR_SECONDARY = "#ffff00";
const GAMEBOARD_X = 16;
const GAMEBOARD_WIDTH = g.getWidth() - 16 - BLOCK_SIZE;
const START_X = GAMEBOARD_X;
const START_Y = g.getHeight() - BLOCK_SIZE - 1;
const START_LENGTH = 5;
var startTime = 0;
var length = 5;
const START_LENGTH = 4;
var length;
var updateTimeout;
var rows = [];
var playing = true;
var gameState = ""; //win, lose, play
class Block {
constructor(x, y, match) {
this.x = x;
this.y = y;
this.match = match;
this.show = true;
}
function Block (x, y, match) {
this.x = x;
this.y = y;
this.match = match;
this.show = true;
}
class Row {
@ -59,49 +55,47 @@ class Row {
}
}
function init() {
Bangle.setLCDPower(1);
g.setTheme({bg:"#000", fg:"#fff", dark:true}).clear();
setInterval(update, TICKRATE);
setWatch(input_pressed, BTN);
var test = new Row(START_X, START_Y, length, 1, true);
rows.push(test);
setWatch(handleInput, BUTTON, {repeat:true});
changeState("play");
}
function update() {
"ram"
if (playing) {
if (gameState === "play") {
g.clear(reset);
rows[rows.length - 1].update();
rows.forEach(row => row.draw());
g.flip();
}
}
function resetGame() {
playing = true;
rows = [];
length = START_LENGTH;
var test = new Row(START_X, START_Y, length, 1, true);
rows.push(test);
update();
}
function lose() {
print("lose");
function changeState(gs) {
gameState = gs;
g.clear(reset);
E.showMessage("YOU LOSE!");
playing = false;
setWatch(resetGame, BTN, {repeat:0,debounce:50,edge:"rising"});
}
function win() {
playing = false;
setWatch(resetGame, BTN, {repeat:0,debounce:50,edge:"rising"});
g.clear(reset);
E.showMessage("YOU WIN!");
switch(gameState) {
case "win":
E.showMessage("YOU WIN!");
break;
case "lose":
E.showMessage("YOU LOSE!");
break;
case "play":
rows = [];
length = START_LENGTH;
var first = new Row(GAMEBOARD_X, START_Y, length, 1, true);
rows.push(first);
break;
}
}
function collapse() {
"ram"
for (var i = 0; i < rows[rows.length - 1].blocks.length; i++) {
for (var j = 0; j < rows[rows.length -2].blocks.length; j++) {
if (rows[rows.length - 1].blocks[i].x === rows[rows.length - 2].blocks[j].x) {
@ -114,21 +108,22 @@ function collapse() {
if (rows[rows.length - 1].blocks[y].match === false) {
length -= 1;
if (length < 1) {
lose();
playing = false;
changeState("lose");
}
rows[rows.length - 1].blocks[y].show = false;
}
}
}
function input_pressed() {
setWatch(input_pressed, BTN);
if (playing) {
function handleInput() {
if (gameState === "win" || gameState === "lose") {
changeState("play");
}
else {
if (rows.length > 1) {
collapse();
if (rows[rows.length - 1].y === -1) {
win();
if (rows[rows.length - 1].y <= -1) {
changeState("win");
}
}
var r = new Row(GAMEBOARD_X + Math.round(length/2) * BLOCK_SIZE, rows[rows.length - 1].y - BLOCK_SIZE, length, 1, false);

View File

@ -1,11 +1,11 @@
{ "id": "stacker",
"name": "Stacker",
"shortName":"Stacker",
"version":"0.01",
"version":"0.02",
"description": "Game of Stacking",
"icon": "app.png",
"tags": "game",
"supports" : ["BANGLEJS2"],
"supports" : ["BANGLEJS", "BANGLEJS2"],
"readme": "README.md",
"storage": [
{"name":"stacker.app.js","url":"app.js"},