trex: added highscore and setting (for highscore reset)

pull/751/head
Johannes Schneider 2021-05-29 13:04:11 +02:00
parent d49a22a990
commit 6de833db3b
5 changed files with 51 additions and 5 deletions

View File

@ -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",

View File

@ -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)

19
apps/trex/settings.js Normal file
View File

@ -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);
});

View File

@ -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();
}

2
core

@ -1 +1 @@
Subproject commit 0ba4c2bd5503264279222b04a41505471c6622ff
Subproject commit 3f2ff467f22b746da94160e59ff89b621601b261