Skip to content

Commit

Permalink
Space Invaders added, logic to fill every box after 3 seconds added a…
Browse files Browse the repository at this point in the history
…s well!
  • Loading branch information
Bhardwaj-Himanshu committed Mar 16, 2024
1 parent c3f23b2 commit c51c868
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ <h2>Vanilla JS Projects</h2>
<a href="./imageSearchApp/imageSearchApp.html" target="_blank">
<button>Image Search App</button>
</a>
<a href="./jsSpaceInvaders/SpaceInvaders.html" target="_blank">
<button>Space Invaders</button>
</a>
</div>

<div class="buttons React">
Expand Down
22 changes: 22 additions & 0 deletions jsSpaceInvaders/SpaceInvaders.css
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;
}
14 changes: 14 additions & 0 deletions jsSpaceInvaders/SpaceInvaders.html
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>
37 changes: 37 additions & 0 deletions jsSpaceInvaders/Spaceinvaders.js
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);

0 comments on commit c51c868

Please sign in to comment.