-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Space Invaders added, logic to fill every box after 3 seconds added a…
…s well!
- Loading branch information
1 parent
c3f23b2
commit c51c868
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
body { | ||
background-color: pink; | ||
display: flex; | ||
flex-direction: column; | ||
row-gap: 7rem; | ||
align-items: center; | ||
justify-content: end; | ||
} | ||
|
||
.grid { | ||
display: flex; | ||
flex-wrap: wrap; | ||
background-color: white; | ||
border: 3px solid black; | ||
width: 300px; | ||
height: 300px; | ||
} | ||
|
||
.grid div { | ||
width: 20px; | ||
height: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Space Invaders</title> | ||
<link rel="stylesheet" href="SpaceInvaders.css" /> | ||
</head> | ||
<body> | ||
<h1 class="result">0</h1> | ||
<div class="grid"></div> | ||
<script src="Spaceinvaders.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const grid = document.querySelector('.grid'); | ||
const resultDisplay = document.querySelector('.result'); | ||
const width = 15; | ||
// A grid_array that contains all the created squares numbered 0-224 | ||
let grid_array = []; | ||
|
||
// An alien Invaders array, which contains random numbers, but they get incremeneted | ||
const alienInvaders = [ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 18, 19, 20, 21, 22, 28, 29, 30, 31, | ||
32, 33, 34, 35, | ||
]; | ||
|
||
// So we are essentially looking for a 15x15 matrix or grid | ||
for (let i = 0; i < width * width; i++) { | ||
// creating a new sqaure div everytime | ||
const square = document.createElement('div'); | ||
// setting it's inner text to be no. of square | ||
square.textContent = i; | ||
// appending it to the DOM | ||
grid.appendChild(square); | ||
// appending it to the grid_array | ||
grid_array.push(square); | ||
} | ||
|
||
setInterval(() => { | ||
for (let i = 0; i < alienInvaders.length; i++) { | ||
// We get the element number we need to change | ||
const alienIndex = alienInvaders[i]; | ||
// Then we get the <div> cell from grid_array using element as element index for grid_array | ||
const gridCell = grid_array[alienIndex]; | ||
// change the style to red | ||
gridCell.style.backgroundColor = 'red'; | ||
// increment the alienInvaders | ||
alienInvaders[i]++; | ||
} | ||
console.log(alienInvaders); | ||
}, 3000); |