Bangle 2 support and misc game tweaks

pull/794/head
Gordon Williams 2021-08-26 16:53:43 +01:00
parent 2e8d6db613
commit 466fef2baf
7 changed files with 127 additions and 75 deletions

View File

@ -402,9 +402,9 @@
{ "id": "trex",
"name": "T-Rex",
"icon": "trex.png",
"version":"0.03",
"version":"0.04",
"description": "T-Rex game in the style of Chrome's offline game",
"tags": "game",
"tags": "game,b2",
"allow_emulator":true,
"storage": [
{"name":"trex.app.js","url":"trex.js"},
@ -418,9 +418,9 @@
{ "id": "astroid",
"name": "Asteroids!",
"icon": "asteroids.png",
"version":"0.02",
"version":"0.03",
"description": "Retro asteroids game",
"tags": "game",
"tags": "game,b2",
"allow_emulator":true,
"storage": [
{"name":"astroid.app.js","url":"asteroids.js"},
@ -1096,9 +1096,9 @@
{ "id": "flappy",
"name": "Flappy Bird",
"icon": "app.png",
"version":"0.04",
"version":"0.05",
"description": "A Flappy Bird game clone",
"tags": "game",
"tags": "game,b2",
"allow_emulator":true,
"storage": [
{"name":"flappy.app.js","url":"app.js"},

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: Bangle 2 support

View File

@ -1,12 +1,24 @@
var BTNL, BTNR, BTNU, BTNA;
if (process.env.HWVERSION==2) {
var tap = {};
// use tapping on screen for left,right,accel
Bangle.on('drag',e=>tap=e);
BTNL = { read : _=>tap.b && tap.x < 58};
BTNR = { read : _=>tap.b && tap.x > 117};
BTNU = { read : _=>tap.b && tap.x > 58 && tap.x < 117};
// use button for fire
BTNA = BTN1;
} else {
// use hard buttons
BTNL = BTN4;
BTNR = BTN5;
BTNU = BTN1;
BTNA = BTN2;
Bangle.setLCDMode("doublebuffered");
}
var W = g.getWidth();
var H = g.getHeight();
g.setFontAlign(0,-1);
var BTNL = BTN4;
var BTNR = BTN5;
var BTNU = BTN1;
var BTNA = BTN2;
g.clear().setFontAlign(0,-1);
function newAst(x,y) {
var a = {
@ -92,8 +104,7 @@ function onFrame() {
}
g.clear();
g.drawString(score,120,0);
g.drawString(score,W-20,0);
var rs = Math.PI*0.8;
g.drawPoly([
ship.x+Math.cos(ship.r)*4, ship.y+Math.sin(ship.r)*4,

View File

@ -1,3 +1,4 @@
0.02: Tweaks to make flappy bird run with less RAM available
0.03: A few tweaks to improve rendering speed
0.04: Add "ram" keyword to allow 2v06 Espruino builds to cache function that needs to be fast
0.05: Don't use Bangle.setLCDMode, just use offscreen buffer (allows widgets)

View File

@ -1,3 +1,21 @@
b = Graphics.createArrayBuffer(120,120,8);
var gimg = {
width:120,
height:104,
bpp:8,
buffer:b.buffer
};
if (process.env.HWVERSION==2) {
b.flip = function() {
g.drawImage(gimg,28,50);
};
} else {
b.flip = function() {
g.drawImage(gimg,0,24,{scale:2});
};
}
var BIRDIMG = E.toArrayBuffer(atob("EQyI/v7+/v7+/gAAAAAAAP7+/v7+/v7+/gYG0tLS0gDXAP7+/v7+/v4A0tLS0tIA19fXAP7+/v4AAAAA0tLS0gDX1wDXAP7+ANfX19cA0tLSANfXANcA/v4A19fX19cA0tLSANfX1wD+/gDS19fX0gDS0tLSAAAAAAD+/gDS0tIA0tLS0gDAwMDAwAD+/gAAAM3Nzc0AwAAAAAAA/v7+/v4Azc3Nzc0AwMDAwAD+/v7+/v4AAM3Nzc0AAAAAAP7+/v7+/v7+AAAAAP7+/v7+/g=="))
var FLOORIMG = require("heatshrink").decompress(atob("iEKxH+kklABuLAAlgAAwNFB34OLmAAO0YAO5wAOA"));
@ -33,26 +51,26 @@ function gameStop() {
function draw() {
"ram"
var H = g.getHeight()-24;
g.setColor("#71c6cf");
g.fillRect(0,0,g.getWidth(),H-1);
var H = b.getHeight()-24;
b.setColor("#71c6cf");
b.fillRect(0,0,b.getWidth(),H-1);
floorpos++;
for (var x=-(floorpos&15);x<g.getWidth();x+=16)
g.drawImage(FLOORIMG,x,H);
for (var x=-(floorpos&15);x<b.getWidth();x+=16)
b.drawImage(FLOORIMG,x,H);
if (!running) {
var x = g.getWidth()/2;
g.setColor("#000000");
g.setFontAlign(0,0);
g.setFont("4x6",2);
g.drawString("GAME OVER!",x,20);
g.setFont("6x8",1);
g.drawString("Score",x,40);
g.drawString(score,x,56);
g.drawString("Tap screen to",x,76);
g.drawString("restart and flap",x,84);
g.flip();
var x = b.getWidth()/2;
b.setColor("#000000");
b.setFontAlign(0,0);
b.setFont("4x6",2);
b.drawString("GAME OVER!",x,20);
b.setFont("6x8",1);
b.drawString("Score",x,40);
b.drawString(score,x,56);
b.drawString("Tap screen to",x,76);
b.drawString("restart and flap",x,84);
b.flip();
return;
}
@ -63,30 +81,30 @@ function draw() {
if (birdy > H)
gameStop();
// draw bird
g.drawImage(BIRDIMG, 6,birdy, {rotate:Math.atan2(birdvy,15)});
b.drawImage(BIRDIMG, 6,birdy, {rotate:Math.atan2(birdvy,15)});
// draw barriers
barriers.forEach(function(b) {
b.x1--;
b.x2--;
var btop = b.y-b.gap;
var bbot = b.y+b.gap;
g.setColor("#73bf2f"); // middle
g.fillRect(b.x1+4, 0, b.x2-4, btop-1);
g.fillRect(b.x1+4, bbot, b.x2-4, H-1);
g.setColor("#c0f181"); // left
g.fillRect(b.x1+1, 0, b.x1+3, btop-1);
g.fillRect(b.x1+1, bbot, b.x1+3, H-1);
g.setColor("#538917"); // right
g.fillRect(b.x2-3, 0, b.x2-1, btop-1);
g.fillRect(b.x2-3, bbot, b.x2-1, H-1);
g.setColor("#808080"); // outlines
g.drawRect(b.x1, btop-5, b.x2, btop); // top
g.drawLine(b.x1+1, 0, b.x1+1, btop-6);
g.drawLine(b.x2-2, 0, b.x2-2, btop-6);
g.drawRect(b.x1, bbot, b.x2, bbot+5); // bottom
g.drawLine(b.x1+1, bbot+6, b.x1+1, H-1);
g.drawLine(b.x2-1, bbot+6, b.x2-1, H-1);
if (b.x1<6 && (birdy-3<btop || birdy+3>bbot))
barriers.forEach(function(r) {
r.x1--;
r.x2--;
var btop = r.y-r.gap;
var bbot = r.y+r.gap;
b.setColor("#73bf2f"); // middle
b.fillRect(r.x1+4, 0, r.x2-4, btop-1);
b.fillRect(r.x1+4, bbot, r.x2-4, H-1);
b.setColor("#c0f181"); // left
b.fillRect(r.x1+1, 0, r.x1+3, btop-1);
b.fillRect(r.x1+1, bbot, r.x1+3, H-1);
b.setColor("#538917"); // right
b.fillRect(r.x2-3, 0, r.x2-1, btop-1);
b.fillRect(r.x2-3, bbot, r.x2-1, H-1);
b.setColor("#808080"); // outlines
b.drawRect(r.x1, btop-5, r.x2, btop); // top
b.drawLine(r.x1+1, 0, r.x1+1, btop-6);
b.drawLine(r.x2-2, 0, r.x2-2, btop-6);
b.drawRect(r.x1, bbot, r.x2, bbot+5); // bottom
b.drawLine(r.x1+1, bbot+6, r.x1+1, H-1);
b.drawLine(r.x2-1, bbot+6, r.x2-1, H-1);
if (r.x1<6 && (birdy-3<btop || birdy+3>bbot))
gameStop();
});
while (barriers.length && barriers[0].x2<=0) {
@ -94,7 +112,7 @@ function draw() {
newBarrier(g.getWidth());
}
g.flip();
b.flip();
}
Bangle.on('touch', function(button) {
@ -105,11 +123,9 @@ Bangle.on('touch', function(button) {
}
});
// Finally, start everything going
setTimeout(()=>{
Bangle.setLCDMode("120x120");
g.setBgColor("#e3db9d");
Bangle.loadWidgets();
g.clear();
Bangle.drawWidgets();
b.setBgColor("#e3db9d");
gameStart();
setInterval(draw, 100);
},10);

View File

@ -1,2 +1,3 @@
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)
0.04: Bangle.js 2 support

View File

@ -8,8 +8,32 @@ function saveHighScore(score) {
f.write(score + "\n");
}
greal = g;
g.clear();
var BTNL, BTNR, BTNU;
if (process.env.HWVERSION==2) {
var tap = {};
// use tapping on screen for left and right
Bangle.on('drag',e=>tap=e);
BTNL = { read : _=>tap.b && tap.x < 88};
BTNR = { read : _=>tap.b && tap.x > 88};
// use button for jump
BTNU = BTN1;
greal = g;
g = Graphics.createArrayBuffer(88,64,1,{msb:true});
g.flip = function() {
greal.drawImage({
width:88,
height:64,
buffer:g.buffer
},0,(176-128)/2,{scale:2});
};
} else {
// use hard buttons
BTNL = BTN2;
BTNR = BTN3;
BTNU = BTN1;
greal = g;
g = Graphics.createArrayBuffer(120,64,1,{msb:true});
g.flip = function() {
greal.drawImage({
@ -18,10 +42,8 @@ g.flip = function() {
buffer:g.buffer
},0,(240-128)/2,{scale:2});
};
}
var W = g.getWidth();
var BTNL = BTN2;
var BTNR = BTN3;
var BTNU = BTN1;
// Images can be added like this in Espruino v2.00
var IMG = {