├── .github └── FUNDING.yml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── css └── main.css ├── favicon.png ├── index.html ├── js ├── Genetics.js ├── Invader.js ├── Player.js └── main.js ├── manifest.appcache ├── manifest.json ├── screenshot.png └── sw.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | patreon: victorqribeiro 4 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at victorqribeiro@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Victor Ribeiro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # InvaderZ 2 | 3 | InvaderZ is like the game Space Invaders, except InvaderZ uses the [genetic algorithm](https://en.wikipedia.org/wiki/Genetic_algorithm) to mutate the invaders as you play. 4 | 5 | ![InvaderZ](screenshot.png) 6 | 7 | If you want to see a live version of it, it's hosted [here](https://victorribeiro.com/invaderz). 8 | 9 | [Alternative link](https://victorqribeiro.github.io/invaderz). 10 | 11 | You can also add it as an app to your phone (menu / add to home screen), if you wish so. 12 | 13 | ## How to Play 14 | 15 | Left arrow or A keys - move the cannon to the left 16 | 17 | Right arrow or D keys - move the cannon to the right 18 | 19 | Space bar - Shoots; 20 | 21 | IF you are on your phone or tablet, use the buttons 22 | 23 | ## About 24 | 25 | You are the last hope of the human kind as the defense against the InvaderZ. They are deployed by their mothership with the sole purpose of entering earth's atmosphere. Although they are not a danger for you as an individual, if 5 of them gets through, everything is doomed. You can only shoot one projectile at the time, so be mindful, as you are not getting another shot until the last fired projectile disperse into the space or hits a target. Each InvaderZ has a shape that influence how they move. When a InvaderZ dies, they upload their progress to the mothership, so it can generate more InvaderZ like the ones who did well before. After each 7 generations of InvaderZ, the mothership generates a complete new wave of InvaderZ keeping only the very best of the last past 7 generations. 26 | 27 | ## Genetic Algorithm 28 | 29 | A random population of InvaderZ are generated at the beginning of the game. 30 | 31 | The fit score of the InvaderZ is how far it made through the earth's atmosphere. 32 | 33 | The way they move are directly related to their body shape. 34 | 35 | After they die, a new wave of InvaderZ are created by crossing over the InvaderZ with the higher fit score. 36 | 37 | The cross over could happen of 6 different forms: 38 | 39 | * First horizontal half of the first parent with the second horizontal half of the second parent. 40 | * First horizontal half of the second parent with the second horizontal half of the first parent. 41 | * First vertical half of the first parent with the second vertical half of the second parent. 42 | * First vertical half of the second parent with the second vertical half of the first parent. 43 | * Odd genes from the first parent, even genes from the second parent. 44 | * Even genes from the first parent, odd genes from the second parent. 45 | 46 | There's a 10% chance of mutation after each cross over, altering their body shape and so the way they move. 47 | 48 | To keep things interesting, after 7 generations, a new wave is created from scratch and only the best of the 7 past generations is keep. 49 | 50 | [![Donations](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=victorqribeiro%40gmail%2ecom&lc=BR&item_name=Victor%20Ribeiro&item_number=donation¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted) 51 | -------------------------------------------------------------------------------- /css/main.css: -------------------------------------------------------------------------------- 1 | html, body{ 2 | margin: 0; 3 | padding: 0; 4 | text-align: center; 5 | -moz-user-select: -moz-none; 6 | -khtml-user-select: none; 7 | -webkit-user-select: none; 8 | -ms-user-select: none; 9 | user-select: none; 10 | } 11 | canvas{ 12 | display: block; 13 | margin: 10px auto; 14 | image-rendering: auto; 15 | } 16 | div{ 17 | margin: auto auto; 18 | display: flex; 19 | align-items: center; 20 | justify-content: space-between; 21 | width: 240px; 22 | } 23 | button{ 24 | font-family: Arial; 25 | font-size: 2em; 26 | min-width: 2em; 27 | cursor: pointer; 28 | border: none; 29 | background-color: #008CBA; 30 | color: white; 31 | padding: 20px; 32 | text-align: center; 33 | text-decoration: none; 34 | display: inline-block; 35 | margin: 4px 2px; 36 | box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19); 37 | } 38 | -------------------------------------------------------------------------------- /favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorqribeiro/invaderz/a3ac9574f7a1904fb74d7fdff22b9b2e7c94a310/favicon.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | InvaderZ - Space invaders that evolve 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /js/Genetics.js: -------------------------------------------------------------------------------- 1 | class Genetics { 2 | 3 | constructor(){ 4 | this.population = []; 5 | this.populationTmp = []; 6 | this.populationSize = 6; 7 | this.populationFeaturesSize = 16; 8 | this.bestOfGeneration; 9 | } 10 | 11 | selectParent(){ 12 | let total = 0; 13 | for(let i = 0; i < this.populationTmp.length; i++){ 14 | total += this.populationTmp[i].fit; 15 | } 16 | let prob = Math.random() * total; 17 | for(let i = 0; i < this.populationTmp.length; i++){ 18 | if( prob < this.populationTmp[i].fit ){ 19 | return this.populationTmp.splice(i,1)[0]; 20 | } 21 | prob -= this.populationTmp[i].fit 22 | } 23 | } 24 | 25 | createPopulation(){ 26 | this.population = []; 27 | for(let i = 0; i < this.populationSize; i++){ 28 | let shape = []; 29 | for(let j = 0; j < this.populationFeaturesSize; j++){ 30 | shape.push( Math.random() < 0.5 ? 1 : 0 ); 31 | } 32 | this.population.push( new Invader( w/4/2 ,Math.random()*-20, shape) ); 33 | } 34 | } 35 | 36 | crossOver(_a,_b){ 37 | let a,b,x; 38 | if( Math.random() < 0.5 ){ 39 | a = _a; 40 | b = _b; 41 | x = w/4/2; 42 | }else{ 43 | a = _b; 44 | b = _a; 45 | x = a.x 46 | } 47 | let child = new Invader(x,Math.random()*-20,a.shape.slice()); 48 | let rand = Math.random(); 49 | if( rand < 0.33 ){ 50 | for(let i = 0; i < a.shape.length; i+=4){ 51 | child.shape[i] = b.shape[i]; 52 | child.shape[i+1] = b.shape[i+1]; 53 | } 54 | }else if( rand < 0.66){ 55 | for(let i = 0; i < a.shape.length/2; i++){ 56 | child.shape[i] = b.shape[i]; 57 | } 58 | }else{ 59 | for(let i = 0; i < a.shape.length; i++){ 60 | let value; 61 | if( i % 2 ){ 62 | value = a.shape[i]; 63 | }else{ 64 | value = b.shape[i]; 65 | } 66 | child.shape[i] = value; 67 | } 68 | } 69 | return child; 70 | } 71 | 72 | mutate(child){ 73 | let spot = Math.floor(Math.random() * child.shape.length); 74 | if( child.shape[spot] ){ 75 | child.shape[spot] = 0; 76 | }else{ 77 | child.shape[spot] = 1; 78 | } 79 | return child; 80 | } 81 | 82 | evolve(){ 83 | let newPopulation = []; 84 | for(let x = 0; x < this.populationSize; x++){ 85 | this.populationTmp = this.population.slice(); 86 | let a = this.selectParent(); 87 | let b = this.selectParent(); 88 | let child = this.crossOver(a,b); 89 | if( Math.random() < 0.1 ){ 90 | child = this.mutate(child); 91 | } 92 | newPopulation.push( child ); 93 | } 94 | this.population = newPopulation; 95 | } 96 | 97 | elitism(){ 98 | this.createPopulation(); 99 | let rand = Math.floor(Math.random() * this.population.length); 100 | let invader = new Invader(w/4/2 ,Math.random()*-20, this.bestOfGeneration.shape.slice()); 101 | this.population[rand] = invader; 102 | } 103 | 104 | } 105 | -------------------------------------------------------------------------------- /js/Invader.js: -------------------------------------------------------------------------------- 1 | class Invader { 2 | 3 | constructor(x,y,shape,color,speed){ 4 | this.x = x || 0; 5 | this.y = y || 0; 6 | this.xDir = 1; 7 | this.s = 4; 8 | this.i = 0; 9 | this.speed = speed != undefined ? speed : 0.008; 10 | this.frame = 0; 11 | this.dir = ( Math.random () < 0.5 ? -1 : 1 ); 12 | this.maxFrame = Math.floor( Math.random() * 32 ) + 16; 13 | this.color = color || 'black'; 14 | this.shape = shape || [0,1,1,0,0,1,1,0,1,1,1,1,0,1,1,0]; 15 | this.isAlive = true; 16 | this.fit = 0; 17 | } 18 | 19 | update(){ 20 | if( this.y >= h>>2 ){ 21 | lives++; 22 | this.isAlive = false; 23 | return; 24 | } 25 | 26 | if( !this.shape[this.i] ){ 27 | 28 | let value = this.dir * this.speed * dt 29 | if( this.x + value > 0 && (this.x + value) * this.s < w - this.s * this.s ){ 30 | this.x += value; 31 | } 32 | 33 | } 34 | 35 | this.y += this.speed * dt; 36 | 37 | if( this.frame == this.maxFrame ){ 38 | this.dir = -this.dir; 39 | this.frame = 0; 40 | this.maxFrame = Math.floor( Math.random() * 32 ) + 16; 41 | this.i = ++this.i % this.shape.length; 42 | } 43 | 44 | this.frame++; 45 | this.fit = Math.round( this.y ); 46 | 47 | if( Math.sqrt( (player.bullet.y - this.y)**2 + (player.bullet.x - (this.x+2))**2) < 2.5 ){ 48 | let x = player.bullet.x; 49 | let y = player.bullet.y; 50 | let area = c.getImageData((x * this.s), y * this.s, player.bullet.s+1, player.bullet.s); 51 | for(let i = 0; i < area.data.length; i++){ 52 | if( area.data[i] ){ 53 | this.isAlive = false; 54 | player.bullet = {}; 55 | player.isShooting = false; 56 | break; 57 | } 58 | } 59 | } 60 | 61 | } 62 | 63 | show(){ 64 | if(this.isAlive){ 65 | c.fillStyle = this.color; 66 | for(let i = 0; i < this.shape.length; i++){ 67 | if(this.shape[i]){ 68 | c.fillRect( (this.x+i%4)*this.s, (this.y+(i>>2))*this.s, this.s, this.s); 69 | } 70 | } 71 | this.update(); 72 | } 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /js/Player.js: -------------------------------------------------------------------------------- 1 | class Player { 2 | 3 | constructor(x,y,shape,color){ 4 | this.x = x || 0; 5 | this.y = y || 0; 6 | this.xDir = 1; 7 | this.s = 4; 8 | this.color = color || 'black'; 9 | this.shape = shape || [0,0,0,0,0,1,1,0,0,1,1,0,1,1,1,1]; 10 | this.speed = 0.05; 11 | this.isMovingLeft = false; 12 | this.isMovingRight = false; 13 | this.isShooting = false; 14 | this.bullet = {x: this.x, y: this.y, s: 3}; 15 | } 16 | 17 | shoot(){ 18 | if( !this.isShooting ){ 19 | this.bullet = {x: this.x+this.s/2, y: this.y, s: 3}; 20 | this.isShooting = true; 21 | } 22 | } 23 | 24 | update(){ 25 | 26 | if( this.x > 0 && this.isMovingLeft ){ 27 | this.x -= this.speed * dt; 28 | } 29 | if( this.x < w/4-this.s && this.isMovingRight ){ 30 | this.x += this.speed * dt; 31 | } 32 | 33 | if(this.isShooting){ 34 | this.bullet.y -= 0.1 * dt; 35 | if( this.bullet.y < 0 ){ 36 | this.isShooting = false; 37 | this.bullet = {}; 38 | } 39 | } 40 | 41 | } 42 | 43 | show(){ 44 | c.fillStyle = this.color; 45 | for(let i = 0; i < this.shape.length; i++){ 46 | if(this.shape[i]){ 47 | c.fillRect( (this.x+i%4)*this.s, (this.y+(i>>2))*this.s, this.s, this.s); 48 | } 49 | } 50 | if( this.isShooting ){ 51 | c.fillRect( this.bullet.x*this.s, this.bullet.y*this.s, this.bullet.s, this.bullet.s); 52 | } 53 | this.update(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /js/main.js: -------------------------------------------------------------------------------- 1 | let canvas, 2 | c, 3 | invaders, 4 | w, 5 | h, 6 | dt, 7 | player, 8 | lives, 9 | lastUpdate, 10 | leftBtn, 11 | rightBtn, 12 | fireBtn, 13 | div, 14 | generation; 15 | 16 | canvas = document.createElement('canvas'); 17 | canvas.width = w = 240; 18 | canvas.height = h = 480; 19 | c = canvas.getContext('2d',{alpha: false}); 20 | if (window.devicePixelRatio > 1) { 21 | c.canvas.width = c.canvas.width * window.devicePixelRatio; 22 | c.canvas.height = c.canvas.height * window.devicePixelRatio; 23 | c.canvas.style.width = w+'px'; 24 | c.canvas.style.height = h+'px'; 25 | c.scale(window.devicePixelRatio, window.devicePixelRatio); 26 | } 27 | leftBtn = document.createElement('button'); 28 | leftBtn.innerText = "<"; 29 | rightBtn = document.createElement('button'); 30 | rightBtn.innerText = ">"; 31 | fireBtn = document.createElement('button'); 32 | fireBtn.innerText = "*"; 33 | div = document.createElement('div'); 34 | div.appendChild(leftBtn); 35 | div.appendChild(fireBtn) 36 | div.appendChild(rightBtn); 37 | 38 | function init(){ 39 | lives = 0; 40 | generation = 1; 41 | dt = 0; 42 | lastUpdate = Date.now(); 43 | canvas.style.border = "solid"; 44 | document.body.appendChild( canvas ); 45 | document.body.appendChild( div ); 46 | invaders = new Genetics(); 47 | invaders.createPopulation(); 48 | player = new Player( w/4/2, h/4-4 ); 49 | update(); 50 | } 51 | 52 | function deltaTime(){ 53 | let now = Date.now(); 54 | dt = now - lastUpdate; 55 | lastUpdate = now; 56 | } 57 | 58 | function getBestOfGeneration(){ 59 | let index = 0, best = 0; 60 | for(let i = 0; i < invaders.population.length; i++){ 61 | if( invaders.population[i].fit > best ){ 62 | best = invaders.population[i].fit; 63 | index = i; 64 | } 65 | } 66 | if( !invaders.bestOfGeneration || invaders.population[index].fit > invaders.bestOfGeneration.fit ){ 67 | invaders.bestOfGeneration = invaders.population[index]; 68 | } 69 | } 70 | 71 | function gameOver(){ 72 | c.fillStyle = "white"; 73 | c.fillRect(0,0,w,h); 74 | c.fillStyle = "black"; 75 | c.font = "10px Arial"; 76 | c.fillText("Generation: "+generation, 5, 10); 77 | c.fillText("Invaders: "+lives, 5, 20); 78 | let txt = "Game Over!"; 79 | c.font = "30px Arial"; 80 | c.fillText(txt, (w-c.measureText(txt).width)/2, h/2); 81 | } 82 | 83 | function update(){ 84 | c.fillStyle = "white"; 85 | c.fillRect(0,0,w,h); 86 | c.fillStyle = "black"; 87 | c.font = "10px Arial"; 88 | c.fillText("Generation: "+generation, 5, 10); 89 | c.fillText("Invaders: "+lives, 5, 20); 90 | for(let i = 0; i < invaders.population.length; i++){ 91 | invaders.population[i].show(); 92 | } 93 | player.show(); 94 | let allDead = true; 95 | for(let i = 0; i < invaders.population.length; i++){ 96 | if( invaders.population[i].isAlive ){ 97 | allDead = false; 98 | break; 99 | } 100 | } 101 | if(allDead){ 102 | getBestOfGeneration(); 103 | if(generation%7){ 104 | invaders.evolve(); 105 | }else{ 106 | invaders.elitism(); 107 | } 108 | generation++; 109 | } 110 | if(lives > 4){ 111 | gameOver(); 112 | return; 113 | } 114 | deltaTime(); 115 | requestAnimationFrame(update); 116 | } 117 | 118 | function addEvents(){ 119 | document.addEventListener("keydown",function(e){ 120 | switch(e.keyCode){ 121 | case 13 : 122 | init(); 123 | break; 124 | case 32 : 125 | player.shoot(); 126 | break; 127 | case 37 : 128 | case 65 : 129 | player.isMovingLeft = true; 130 | break; 131 | case 39 : 132 | case 68 : 133 | player.isMovingRight = true; 134 | break; 135 | } 136 | }); 137 | 138 | document.addEventListener("keyup",function(e){ 139 | switch(e.keyCode){ 140 | case 37 : 141 | case 65 : 142 | player.isMovingLeft = false; 143 | break; 144 | case 39 : 145 | case 68 : 146 | player.isMovingRight = false; 147 | break; 148 | } 149 | }); 150 | 151 | window.addEventListener("focus",function(){ 152 | lastUpdate = Date.now(); 153 | }); 154 | 155 | fireBtn.addEventListener('touchstart', function(){ 156 | if( lives > 4){ 157 | init(); 158 | }else{ 159 | player.shoot(); 160 | } 161 | }); 162 | 163 | fireBtn.addEventListener('mousedown', function(){ 164 | if( lives > 4){ 165 | init(); 166 | }else{ 167 | player.shoot(); 168 | } 169 | }); 170 | 171 | leftBtn.addEventListener('touchstart', function(){ 172 | player.isMovingLeft = true; 173 | }); 174 | 175 | leftBtn.addEventListener('touchend', function(){ 176 | player.isMovingLeft = false; 177 | }); 178 | 179 | leftBtn.addEventListener('mousedown', function(){ 180 | player.isMovingLeft = true; 181 | }); 182 | 183 | leftBtn.addEventListener('mouseup', function(){ 184 | player.isMovingLeft = false; 185 | }); 186 | 187 | rightBtn.addEventListener('touchstart', function(){ 188 | player.isMovingRight = true; 189 | }); 190 | 191 | rightBtn.addEventListener('touchend', function(){ 192 | player.isMovingRight = false; 193 | }); 194 | 195 | rightBtn.addEventListener('mousedown', function(){ 196 | player.isMovingRight = true; 197 | }); 198 | 199 | rightBtn.addEventListener('mouseup', function(){ 200 | player.isMovingRight = false; 201 | }); 202 | 203 | window.addEventListener('load', function(e) { 204 | 205 | window.applicationCache.addEventListener('updateready', function(e) { 206 | if (window.applicationCache.status == window.applicationCache.UPDATEREADY) { 207 | window.applicationCache.swapCache(); 208 | window.location.reload(); 209 | } 210 | }, false); 211 | 212 | }, false); 213 | 214 | 215 | if('serviceWorker' in navigator) { 216 | navigator.serviceWorker 217 | .register('/invaderz/sw.js') 218 | .then(function() { }); 219 | } 220 | 221 | let deferredPrompt; 222 | const addBtn = document.createElement('button'); 223 | 224 | window.addEventListener('beforeinstallprompt', (e) => { 225 | e.preventDefault(); 226 | deferredPrompt = e; 227 | addBtn.addEventListener('click', (e) => { 228 | addBtn.style.display = 'none'; 229 | deferredPrompt.prompt(); 230 | deferredPrompt.userChoice.then((choiceResult) => { 231 | deferredPrompt = null; 232 | }); 233 | }); 234 | }); 235 | 236 | } 237 | 238 | addEvents(); 239 | init(); 240 | 241 | -------------------------------------------------------------------------------- /manifest.appcache: -------------------------------------------------------------------------------- 1 | CACHE MANIFEST 2 | # 2018-10-30:v1.01 3 | 4 | CACHE: 5 | manifest.json 6 | favicon.png 7 | index.html 8 | css/main.css 9 | js/main.js 10 | js/Genetics.js 11 | js/Invader.js 12 | js/Player.js 13 | sw.js 14 | -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "InvaderZ", 3 | "short_name": "InvaderZ", 4 | "description": "Space invaders, but the invaders evolve with genetic algorithm", 5 | "start_url": "index.html", 6 | "display": "standalone", 7 | "orientation": "portrait", 8 | "background_color": "#FFF", 9 | "icons": [ 10 | { 11 | "src": "favicon.png", 12 | "sizes": "256x256", 13 | "type": "image/png", 14 | "density": 1 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victorqribeiro/invaderz/a3ac9574f7a1904fb74d7fdff22b9b2e7c94a310/screenshot.png -------------------------------------------------------------------------------- /sw.js: -------------------------------------------------------------------------------- 1 | self.addEventListener('fetch', function(e) { 2 | e.respondWith( 3 | caches.match(e.request).then(function(response) { 4 | return response || fetch(e.request); 5 | }) 6 | ); 7 | }); 8 | --------------------------------------------------------------------------------