-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomponent---src-pages-games-react-nuzaq-js-dbc945abfef93280cb0e.js
More file actions
2 lines (2 loc) · 2.02 KB
/
component---src-pages-games-react-nuzaq-js-dbc945abfef93280cb0e.js
File metadata and controls
2 lines (2 loc) · 2.02 KB
1
2
"use strict";(self.webpackChunkdocunext=self.webpackChunkdocunext||[]).push([[55],{9624:function(t,e,n){n.r(e),n.d(e,{default:function(){return u}});var r=n(1721),a=n(7294),o=n(1207);var c=function(){"undefined"==typeof document||function(){var t,e,n=!1,r=document.getElementById("myCanvas").getContext("2d"),a=13,o=9,c=0,i=0,u="ArrowRight",l=3,f=[],h=[],d="#00ff",s="#ddd",m="#000",v=150;function p(t,e,n){r.beginPath(),r.rect(35*t+20,35*e+20,25,25),r.fillStyle=n,r.fill(),r.closePath()}function E(){t=Math.floor(Math.random()*a),e=Math.floor(Math.random()*o),f.forEach((function(n){n[0]==t&&n[1]==e&&E()})),p(t,e,d)}function w(){for(var t=0;t<o;t++)for(var e=0;e<a;e++)p(e,t,s)}function g(){w(),E()}function k(){C(),b()&&!M()&&(p(c,i,m),f.length>l&&p((h=f.shift())[0],h[1],s),c==t&&i==e&&(l++,E()))}g();var y={ArrowUp:[0,-1],ArrowDown:[0,1],ArrowLeft:[-1,0],ArrowRight:[1,0]};function A(t){t.code.startsWith("Arrow")&&(u=t.code)}function C(){f.push([c,i]),c+=y[u][0],i+=y[u][1]}function b(){return!(c<0||c>a-1||i<0||i>o-1)||(S("The Snake went off the board!"),!1)}function M(){return f.forEach((function(t){if(t[0]==c&&t[1]==i)return S("The Snake bit itself!"),!0})),!1}function S(t){!0!==n&&(clearInterval(G),n=!0,alert("GAME OVER: "+t))}var G=setInterval(k,v);document.addEventListener("keyup",A)}()},i=function(t){function e(e){return t.call(this,e)||this}(0,r.Z)(e,t);var n=e.prototype;return n.componentDidMount=function(){c()},n.restartGame=function(){c()},n.render=function(){return a.createElement("div",null,a.createElement("h1",null,"Snake Game"),a.createElement("canvas",{id:"myCanvas",width:"480",height:"360"}),a.createElement("button",{onClick:this.restartGame},"play again"),a.createElement("p",null,"This is the classic game snake written in JavaScript, and implemented via the CANVAS tag."),a.createElement("p",null,"Its placed here via React / Gatsby."))},e}(a.Component),u=function(t){t.data;return a.createElement(o.Z,null,a.createElement(i,null))}}}]);
//# sourceMappingURL=component---src-pages-games-react-nuzaq-js-dbc945abfef93280cb0e.js.map