From 8f2bf34585dff32839bf48f9e2bde65617ab7c0a Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Thu, 7 Nov 2019 09:50:03 +0000 Subject: [PATCH] asteroids --- apps.json | 11 +++ apps/asteroids-icon.js | 1 + apps/asteroids.js | 171 +++++++++++++++++++++++++++++++++++++++++ apps/asteroids.json | 5 ++ apps/asteroids.png | Bin 0 -> 1689 bytes favicon.ico | Bin 0 -> 1150 bytes 6 files changed, 188 insertions(+) create mode 100644 apps/asteroids-icon.js create mode 100644 apps/asteroids.js create mode 100644 apps/asteroids.json create mode 100644 apps/asteroids.png create mode 100644 favicon.ico diff --git a/apps.json b/apps.json index aa1f751a0..976c3c8ad 100644 --- a/apps.json +++ b/apps.json @@ -33,6 +33,17 @@ {"name":"*trex","url":"trex-icon.js","evaluate":true} ] }, + { "id": "astroid", + "name": "Asteroids!", + "icon": "asteroids.png", + "description": "Retro asteroids game", + "tags": "game", + "storage": [ + {"name":"+astroid","url":"asteroids.json"}, + {"name":"-astroid","url":"asteroids.js"}, + {"name":"*astroid","url":"asteroids-icon.js","evaluate":true} + ] + }, { "id": "compass", "name": "Compass", "icon": "compass.png", diff --git a/apps/asteroids-icon.js b/apps/asteroids-icon.js new file mode 100644 index 000000000..7e6afcfdb --- /dev/null +++ b/apps/asteroids-icon.js @@ -0,0 +1 @@ +require("heatshrink").decompress(atob("mEwghC/ADkN6APN7oDGC64AWDRw9DIIgXVLh/eAYQtEFxsN7oqCCQQDBC5oOBC4IDHFxgmBAY4uvGJQuMC5IuLhpdWMBQuMSBQuMF5IubhqHDFyIfGX5giFkWQGYa/KEQcCkQACmA6KFwwWDkUgNJJVGFwgABFwoXGBYIuGC4jrL8AuBmczJAgLBRhAXCFwQXGRhIXGkUjI4i/CapReHC4KEEC6a/KC48jmQXCfQoXNX44XJawx3CX5gXHXwQuJcYYXHFxaaEhIXEFxiyFGIeQFxqEIFxy0HhwuOFAgAFCxowDAAguODA4WRAH4ADA==")) diff --git a/apps/asteroids.js b/apps/asteroids.js new file mode 100644 index 000000000..cb44db904 --- /dev/null +++ b/apps/asteroids.js @@ -0,0 +1,171 @@ +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; + +function newAst(x,y) { + var a = { + x:x,y:y, + vx:Math.random()-0.5, + vy:Math.random()-0.5, + rad:3+Math.random()*5 + }; + return a; +} + +var running = true; +var ship = {}; +var ammo = []; +var ast = []; +var score = 0; +var level = 4; +var timeSinceFired = 0; +var lastFrame; + +function gameStop() { + running = false; + g.clear(); + g.drawString("Game Over!",120,(H-6)/2); + g.flip(); +} + +function addAsteroids() { + for (var i=0;i=W) ship.x-=W; + if (ship.y>=H) ship.y-=H; + timeSinceFired+=d; + if (BTNA.read() && timeSinceFired>4) { // fire! + Bangle.beep(10); + timeSinceFired = 0; + ammo.push({ + x:ship.x+Math.cos(ship.r)*4, + y:ship.y+Math.sin(ship.r)*4, + vx:Math.cos(ship.r)*3, + vy:Math.sin(ship.r)*3, + }); + } + + g.clear(); + + g.drawString(score,120,0); + var rs = Math.PI*0.8; + g.drawPoly([ + ship.x+Math.cos(ship.r)*4, ship.y+Math.sin(ship.r)*4, + ship.x+Math.cos(ship.r+rs)*3, ship.y+Math.sin(ship.r+rs)*3, + ship.x+Math.cos(ship.r-rs)*3, ship.y+Math.sin(ship.r-rs)*3, + ],true); + var na = []; + ammo.forEach(function(a) { + a.x += a.vx*d; + a.y += a.vy*d; + g.fillRect(a.x-1, a.y, a.x+1, a.y); + g.fillRect(a.x, a.y-1, a.x, a.y+1); + var hit = false; + ast.forEach(function(b) { + var dx = a.x-b.x; + var dy = a.y-b.y; + var d = Math.sqrt(dx*dx+dy*dy); + if (d=0 && a.y>=0 && a.x=W) a.x-=W; + if (a.y>=H) a.y-=H; + if (!a.hit) { + na.push(a); + } else if (a.rad>4) { + Bangle.buzz(100); + a.hit = false; + var vx = 1*(Math.random()-0.5); + var vy = 1*(Math.random()-0.5); + a.rad/=2; + na.push({ + x:a.x, + y:a.y, + vx:a.vx-vx, + vy:a.vy-vy, + rad:a.rad, + }); + a.vx += vx; + a.vy += vy; + na.push(a); + } + + var dx = a.x-ship.x; + var dy = a.y-ship.y; + var d = Math.sqrt(dx*dx+dy*dy); + if (d < a.rad) crashed = true; + }); + ast=na; + if (!ast.length) { + level++; + addAsteroids(); + } + g.flip(); + if (crashed) { + Bangle.buzz(500); + gameStop(); + } +} + +gameStart(); +setInterval(onFrame, 50); diff --git a/apps/asteroids.json b/apps/asteroids.json new file mode 100644 index 000000000..87dedc86d --- /dev/null +++ b/apps/asteroids.json @@ -0,0 +1,5 @@ +{ + "name":"Asteroids!", + "icon":"*astroid", + "src":"-astroid" +} diff --git a/apps/asteroids.png b/apps/asteroids.png new file mode 100644 index 0000000000000000000000000000000000000000..fa678a5ea29edd379733ddc54ec7c2ebe7b31700 GIT binary patch literal 1689 zcmV;K24?w*P)M=zGTx^XB!< z%o}E=%)kfvf0@i(&i$Qx&pr3u3ukp!{~u|9Cat2;S>`#DgQrsufM5dZEXv6YUJ7y< zJgs^_7I1Thsj{zRE0fg_%2wDomE92PVc2MpAs7smMKqrFu+d7cP(>2f2gyHa{~$xO zialCIhX%PZ*?1WZAq_Ad51&&-wff_2>v9I8e4{}gcI_XouLFi7{D8qv$;Ql>16s{E zgw4SDNc;+DkRf>423(3#8tb4gXE0vsflCp-#9*>4;_qbRCS2;Y;H$U^_{f4^O$!f+ zAJeX1!5|D9eh0mnCUZG@s_+?cz=Hn}=ncF|EA!&uP3loD13tya)J(y{DTa_%avku# z1;54xz6g_#E%=>;(x3TcskX;yRD)cLvIXd8!Ox9@H>d+f0$*Wrt31Mybl`!csklcW z?D`cHA=Cp`0$-EEB$-EhTJVunYjU^S zSrV(1LrAL`gUME)zXgA96#Qxm2~h`J1?;AaNwS!ODZ?j8wTYC{3dF$IB!I641^}B7 zO45Q4a51J;4^64ULkd|)tGLDizmZmEJK+I1Mm}CjWFc*F{gc7J!bJ#mD3@6?KRs3W zqz-5$qrrj~-lTC|fiMu*4f3pKt!3MGljl*(x2ye>HBvOFP?u2|yjfG$;JhvWBsuvJtrCWQG+!erA2~)VT8K zZ1pucl*Ga(3md26L+Bv)S@3)5qSOgrQM&yhQQ46U9zbA9McMXAF7UM$e6t(8)j8<} zKcc8jtU-Jz6_%05ovE;5R^v+0xgM`@Zf_r*{~Cmj8=Oq4JPOp>1tC@eR}+r;5}D`z z)tSyUGAe{L$P|>1fbS@0v`BasmX`$*eF93%Qbn;7cJ7(g!GERg- zMbEumau7hP*blVC9&(Ug*fATbN6q z!o0-RmA2fjzQ@5zna~n@$k|FGY@CWc2gX~*wnPfdqG1&9%v1=x4TI0u$^(l%7NdH z(Xyw5qwS%D#!b`vu5D#8zsA8U>oD1-RovbacsB<6HeL3RVf_Xg0JiZKBWALwo>qCdO9uLJJL1U$rtP_y+rF;d{K z8P~3`ITd63emp|kZ~xf5V#tg3pA64@M6nKh0Dv&VStuf}rEOXD9e+*n_mG{Lk0{!K zTs8wofMN|Y!ER&}ypQk+@O{dL#ru2=d`4$JBDW{Nr%?RBE($1?QXF|wrDE3h`5-R? zy%N~mgR=7J4aLu88{XZ4Rh&;3-=Yjf2ue8%V%t=dHav#Nlh)tfz!5=f#Pq)IuNmJ2 zqNfAi)d8@skAR2y$wyfNJOT7g!RBuW%jm}{=Zjsol={O~cymufxeZ|qN)h1~x;B(Q5q1Kb jD4-#o{c=`kb%ymHMgH@ap&W=&00000NkvXXu0mjf*dq+W literal 0 HcmV?d00001 diff --git a/favicon.ico b/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..24ae659663f56ad85ce80aa656cd909b5977d7ed GIT binary patch literal 1150 zcma)*%_>Dv6vvnE?_35VM2V9za;9G1g1qIrGeS!S`qSFTHCILZ4b; z7KTAmOQrvelE`cL7hJ-noGqY=L|+%J%O&N&2Q@RUt6^esmVU m!