From 6de833db3bca32b0e3e1d9d609477c6d8fe5eae3 Mon Sep 17 00:00:00 2001 From: Johannes Schneider Date: Sat, 29 May 2021 13:04:11 +0200 Subject: [PATCH] trex: added highscore and setting (for highscore reset) --- apps.json | 8 ++++++-- apps/trex/ChangeLog | 1 + apps/trex/settings.js | 19 +++++++++++++++++++ apps/trex/trex.js | 26 ++++++++++++++++++++++++-- core | 2 +- 5 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 apps/trex/settings.js diff --git a/apps.json b/apps.json index 89be44fcf..cdfc14d24 100644 --- a/apps.json +++ b/apps.json @@ -362,13 +362,17 @@ { "id": "trex", "name": "T-Rex", "icon": "trex.png", - "version":"0.02", + "version":"0.03", "description": "T-Rex game in the style of Chrome's offline game", "tags": "game", "allow_emulator":true, "storage": [ {"name":"trex.app.js","url":"trex.js"}, - {"name":"trex.img","url":"trex-icon.js","evaluate":true} + {"name":"trex.img","url":"trex-icon.js","evaluate":true}, + {"name":"trex.settings.js","url":"settings.js"} + ], + "data": [ + {"name":"trex.score", "storageFile": true} ] }, { "id": "astroid", diff --git a/apps/trex/ChangeLog b/apps/trex/ChangeLog index 42c1df403..587dec05b 100644 --- a/apps/trex/ChangeLog +++ b/apps/trex/ChangeLog @@ -1 +1,2 @@ 0.02: Add "ram" keyword to allow 2v06 Espruino builds to cache function that needs to be fast +0.03: Enabled BTN2 and BTN3, added highscore (score is saved to storage and can be reset in app settings menu) diff --git a/apps/trex/settings.js b/apps/trex/settings.js new file mode 100644 index 000000000..67aa9a518 --- /dev/null +++ b/apps/trex/settings.js @@ -0,0 +1,19 @@ +(function (back) { + const menu = { + '': { 'title': 'T-Rex' }, + '< Back': back, + 'Reset Highscore': () => { + E.showPrompt('Reset Highscore?').then((v) => { + let delay = 50; + if (v) { + delay = 500; + E.showMessage('Resetting'); + var f = require('Storage').open('trex.score', 'w'); + f.write('0\n'); + } + setTimeout(() => E.showMenu(menu), delay); + }); + } + }; + E.showMenu(menu); +}); diff --git a/apps/trex/trex.js b/apps/trex/trex.js index 642c10054..0e36ec59a 100644 --- a/apps/trex/trex.js +++ b/apps/trex/trex.js @@ -1,3 +1,13 @@ +function loadHighScore() { + var f = require("Storage").open("trex.score", "r"); + return f.readLine() || 0; +} + +function saveHighScore(score) { + var f = require("Storage").open("trex.score", "w"); + f.write(score + "\n"); +} + greal = g; g.clear(); g = Graphics.createArrayBuffer(120,64,1,{msb:true}); @@ -134,6 +144,8 @@ var IMG = { IMG.rex.forEach(i=>i.transparent=0); IMG.cacti.forEach(i=>i.transparent=0); var cacti, rex, frame; +// displayedHighScore is not updated before restart +var highScore = loadHighScore(), displayedHighScore; function gameStart() { rex = { @@ -152,6 +164,7 @@ function gameStart() { } IMG.ground = { width: 128, height: 3, bpp : 1, buffer : random.buffer }; frame = 0; + displayedHighScore = highScore; setInterval(onFrame, 50); } function gameStop() { @@ -159,8 +172,13 @@ function gameStop() { rex.img = 2; // dead clearInterval(); setTimeout(function() { + // putting saveHighScore here to not delay the frame drawing + if (rex.score > highScore) { + highScore = rex.score; + saveHighScore(highScore); + } setWatch(gameStart, BTNU, {repeat:0,debounce:50,edge:"rising"}); - }, 1000); + }, 800); setTimeout(onFrame, 10); } @@ -190,6 +208,9 @@ function onFrame() { while (cacti.length && cacti[0].x<0) cacti.shift(); } else { g.drawString("Game Over!",(W-g.stringWidth("Game Over!"))/2,20); + if (rex.score > highScore) { + g.drawString("New Record!",(W-g.stringWidth("New Record!"))/2,28); + } } g.drawLine(0,60,239,60); cacti.forEach(c=>g.drawImage(IMG.cacti[c.img],c.x,60-IMG.cacti[c.img].height)); @@ -213,7 +234,8 @@ function onFrame() { var groundOffset = frame&127; g.drawImage(IMG.ground, -groundOffset, 61); g.drawImage(IMG.ground, 128-groundOffset, 61); - g.drawString(rex.score,(W-1)-g.stringWidth(rex.score)); + g.drawString(displayedHighScore,(W-1)-g.stringWidth(displayedHighScore), 0); + g.drawString(rex.score,(W-1)-g.stringWidth(rex.score), 8); g.flip(); } diff --git a/core b/core index 0ba4c2bd5..3f2ff467f 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 0ba4c2bd5503264279222b04a41505471c6622ff +Subproject commit 3f2ff467f22b746da94160e59ff89b621601b261