Merge branch 'espruino:master' into master

pull/808/head
Smooklu 2021-06-08 21:00:15 -05:00
commit 19d3e0401f
6 changed files with 62 additions and 18 deletions

View File

@ -4,7 +4,7 @@
"tags": "tool,system",
"type":"bootloader",
"icon": "bootloader.png",
"version":"0.25",
"version":"0.26",
"description": "This is needed by Bangle.js to automatically load the clock, menu, widgets and settings",
"storage": [
{"name":".boot0","url":"boot0.js"},
@ -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

@ -24,3 +24,4 @@
0.23: Move to a precalculated .boot0 file which should speed up load time
0.24: Add Bangle.setUI polyfill
0.25: Fix error in 'no clock app' message
0.26: Remove buzz in setUI polyfill (#750)

View File

@ -97,25 +97,22 @@ if (Bangle.touchandler) {
Bangle.removeListener("touch", Bangle.touchHandler);
delete Bangle.touchHandler;
}
function b() {
try{Bangle.buzz(20);}catch(e){}
}
if (!mode) return;
else if (mode=="updown") {
Bangle.btnWatches = [
setWatch(function() { b();cb(-1); }, BTN1, {repeat:1}),
setWatch(function() { b();cb(1); }, BTN3, {repeat:1}),
setWatch(function() { b();cb(); }, BTN2, {repeat:1})
setWatch(function() { cb(-1); }, BTN1, {repeat:1}),
setWatch(function() { cb(1); }, BTN3, {repeat:1}),
setWatch(function() { cb(); }, BTN2, {repeat:1})
];
} else if (mode=="leftright") {
Bangle.btnWatches = [
setWatch(function() { b();cb(-1); }, BTN1, {repeat:1}),
setWatch(function() { b();cb(1); }, BTN3, {repeat:1}),
setWatch(function() { b();cb(); }, BTN2, {repeat:1})
setWatch(function() { cb(-1); }, BTN1, {repeat:1}),
setWatch(function() { cb(1); }, BTN3, {repeat:1}),
setWatch(function() { cb(); }, BTN2, {repeat:1})
];
Bangle.swipeHandler = d => {b();cb(d);};
Bangle.swipeHandler = d => {cb(d);};
Bangle.on("swipe", Bangle.swipeHandler);
Bangle.touchHandler = d => {b();cb();};
Bangle.touchHandler = d => {cb();};
Bangle.on("touch", Bangle.touchHandler);
} else
throw new Error("Unknown UI mode");

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});
@ -9,8 +19,8 @@ g.flip = function() {
},0,(240-128)/2,{scale:2});
};
var W = g.getWidth();
var BTNL = BTN4;
var BTNR = BTN5;
var BTNL = BTN2;
var BTNR = BTN3;
var BTNU = BTN1;
// Images can be added like this in Espruino v2.00
@ -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();
}