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_SIZE = 16;
const BLOCK_COLOR_PRIMARY = "#ff0000";
const BLOCK_COLOR_SECONDARY = "#ffff00";
const GAMEBOARD_X = 16; const GAMEBOARD_X = 16;
const GAMEBOARD_WIDTH = g.getWidth() - 16 - BLOCK_SIZE; const GAMEBOARD_WIDTH = g.getWidth() - 16 - BLOCK_SIZE;
const START_X = GAMEBOARD_X;
const START_Y = g.getHeight() - BLOCK_SIZE - 1; const START_Y = g.getHeight() - BLOCK_SIZE - 1;
const START_LENGTH = 5; const START_LENGTH = 4;
var startTime = 0; var length;
var length = 5;
var updateTimeout; var updateTimeout;
var rows = []; var rows = [];
var playing = true; var gameState = ""; //win, lose, play
class Block { function Block (x, y, match) {
constructor(x, y, match) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.match = match; this.match = match;
this.show = true; this.show = true;
}
} }
class Row { class Row {
@ -59,49 +55,47 @@ class Row {
} }
} }
function init() { function init() {
Bangle.setLCDPower(1); Bangle.setLCDPower(1);
g.setTheme({bg:"#000", fg:"#fff", dark:true}).clear(); g.setTheme({bg:"#000", fg:"#fff", dark:true}).clear();
setInterval(update, TICKRATE); setInterval(update, TICKRATE);
setWatch(input_pressed, BTN); setWatch(handleInput, BUTTON, {repeat:true});
var test = new Row(START_X, START_Y, length, 1, true); changeState("play");
rows.push(test);
} }
function update() { function update() {
"ram" "ram"
if (playing) { if (gameState === "play") {
g.clear(reset); g.clear(reset);
rows[rows.length - 1].update(); rows[rows.length - 1].update();
rows.forEach(row => row.draw()); rows.forEach(row => row.draw());
g.flip();
} }
} }
function resetGame() { function changeState(gs) {
playing = true; gameState = gs;
g.clear(reset);
switch(gameState) {
case "win":
E.showMessage("YOU WIN!");
break;
case "lose":
E.showMessage("YOU LOSE!");
break;
case "play":
rows = []; rows = [];
length = START_LENGTH; length = START_LENGTH;
var test = new Row(START_X, START_Y, length, 1, true); var first = new Row(GAMEBOARD_X, START_Y, length, 1, true);
rows.push(test); rows.push(first);
update(); break;
} }
function lose() {
print("lose");
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!");
} }
function collapse() { function collapse() {
"ram"
for (var i = 0; i < rows[rows.length - 1].blocks.length; i++) { for (var i = 0; i < rows[rows.length - 1].blocks.length; i++) {
for (var j = 0; j < rows[rows.length -2].blocks.length; j++) { 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) { 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) { if (rows[rows.length - 1].blocks[y].match === false) {
length -= 1; length -= 1;
if (length < 1) { if (length < 1) {
lose(); changeState("lose");
playing = false;
} }
rows[rows.length - 1].blocks[y].show = false; rows[rows.length - 1].blocks[y].show = false;
} }
} }
} }
function input_pressed() { function handleInput() {
setWatch(input_pressed, BTN); if (gameState === "win" || gameState === "lose") {
if (playing) { changeState("play");
}
else {
if (rows.length > 1) { if (rows.length > 1) {
collapse(); collapse();
if (rows[rows.length - 1].y === -1) { if (rows[rows.length - 1].y <= -1) {
win(); changeState("win");
} }
} }
var r = new Row(GAMEBOARD_X + Math.round(length/2) * BLOCK_SIZE, rows[rows.length - 1].y - BLOCK_SIZE, length, 1, false); 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", { "id": "stacker",
"name": "Stacker", "name": "Stacker",
"shortName":"Stacker", "shortName":"Stacker",
"version":"0.01", "version":"0.02",
"description": "Game of Stacking", "description": "Game of Stacking",
"icon": "app.png", "icon": "app.png",
"tags": "game", "tags": "game",
"supports" : ["BANGLEJS2"], "supports" : ["BANGLEJS", "BANGLEJS2"],
"readme": "README.md", "readme": "README.md",
"storage": [ "storage": [
{"name":"stacker.app.js","url":"app.js"}, {"name":"stacker.app.js","url":"app.js"},