Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Space invaders #212

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Space-invaders/.DS_Store
Binary file not shown.
Binary file added Space-invaders/Images/.DS_Store
Binary file not shown.
Binary file added Space-invaders/Images/alien.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Space-invaders/Images/ship.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions Space-invaders/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Space Invaders Game:
Welcome to the Space Invaders game! This classic arcade game has been brought to life using HTML, CSS, and JavaScript. Defend the Earth from the invading alien forces by maneuvering your spaceship and shooting down the relentless invaders.

Table of Contents:
1)Getting Started
2)Gameplay
3)Controls
4)Scoring
5)Game Over
6)Development

Getting Started:
To play the game, simply open the index.html file in your web browser. The game will load, and you can start playing immediately.

Gameplay:
Defend Earth by controlling your spaceship and shooting down the invading aliens. The game features multiple levels, and the difficulty increases as you progress. Can you survive the relentless alien onslaught?

Controls:
1)Left Arrow- Move the spaceship to the left.
2)Right Arrow- Move the spaceship to the right.
3)Spacebar- Shoot bullets to destroy aliens.

Scoring:
1)Score points by successfully shooting down aliens.
2)The score is displayed at the top-left corner of the game board.

Game Over:
The game ends under the following conditions-
1)An alien reaches the same level as your spaceship.
2)You successfully shoot down all the invading aliens.
3)When the game ends, you can restart by refreshing the page.

Development:
The game was developed using HTML, CSS, and JavaScript. The code is well-structured and easy to understand. Feel free to explore the script.js file for insights into the game mechanics.
14 changes: 14 additions & 0 deletions Space-invaders/index.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 invader</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h1>Space invaders</h1>
<canvas id="board"></canvas>
</body>
</html>
182 changes: 182 additions & 0 deletions Space-invaders/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
let tileSize = 32;
let rows = 16;
let columns = 16;

let board;
let boardWidth = tileSize * columns;
let boardHeight = tileSize * rows;
let context;

let shipWidth = tileSize*2;
let shipHeight = tileSize;
let shipX = tileSize * columns/2 - tileSize;
let shipY = tileSize * rows - tileSize*2;

let ship = {
x : shipX,
y : shipY,
width : shipWidth,
height : shipHeight
}

let shipImg;
let shipVelocityX = tileSize;

let alienArray = [];
let alienWidth = tileSize*2;
let alienHeight = tileSize;
let alienX = tileSize;
let alienY = tileSize;
let alienImg;

let alienRows = 2;
let alienColumns = 3;
let alienCount = 0;
let alienVelocityX = 1;

let bulletArray = [];
let bulletVelocityY = -10;

let score = 0;
let gameOver = false;

window.onload = function(){
board = document.getElementById("board");
board.width = boardWidth;
board.height = boardHeight;
context = board.getContext("2d");

shipImg = new Image();
shipImg.src = "./Images/ship.png";
shipImg.onload = function(){
context.drawImage(shipImg, ship.x, ship.y, ship.width, ship.height);
}

alienImg = new Image();
alienImg.src = "./Images/alien.png"

createAliens()

requestAnimationFrame(update);
document.addEventListener("keydown", moveShip);

document.addEventListener("keyup", shoot);
}

function update(){
requestAnimationFrame(update);

if(gameOver){
return;
}

context.clearRect(0, 0, board.width, board.height)
context.drawImage(shipImg, ship.x, ship.y, ship.width, ship.height)

for(let i=0; i < alienArray.length; i++){
let alien = alienArray[i];
if(alien.alive){
alien.x += alienVelocityX
if(alien.x + alien.width >= board.width || alien.x <= 0){
alienVelocityX *= -1;
alien.x += alienVelocityX*2;

for(let j=0; j<alienArray.length; j++)
{
alienArray[j].y += alienHeight;
}
}
context.drawImage(alienImg, alien.x, alien.y, alien.width, alien.height);

if(alien.y >= ship.y){
gameOver = true;
}
}
}
for(let i=0; i<bulletArray.length; i++){
let bullet = bulletArray[i];
bullet.y += bulletVelocityY;
context.fillStyle = "white";
context.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);

for(let j=0; j < alienArray.length; j++){
let alien = alienArray[j];
if(!bullet.used && alien.alive && detectCollision(bullet, alien)){
bullet.used = true;
alien.alive = false;
alienCount--;
score += 100;
}
}
}

while(bulletArray.length>0 && bulletArray[0].used || bulletArray[0].y < 0){
bulletArray.shift();
}

if(alienCount == 0){
alienColumns = Math.min(alienColumns + 1, columns/2 -2);
alienRows = Math.min(alienRows + 1, rows-4);
alienVelocityX += 0.2;
alienArray = [];
bulletArray = [];
createAliens();
}
context.fillStyle = "white";
context.font = "16px courier";
context.fillText(score, 5, 20);
}

function moveShip(e){
if(gameOver){
return;
}

if(e.code == "ArrowLeft" && ship.x - shipVelocityX >=0){
ship.x -= shipVelocityX;
}
else if(e.code == "ArrowRight" && ship.x + shipVelocityX + ship.width <= board.width){
ship.x += shipVelocityX;
}
}

function createAliens(){
for(let c = 0; c < alienColumns; c++){
for(let r = 0; r < alienRows; r++){
let alien = {
img : alienImg,
x : alienX + c*alienWidth,
y : alienY + r*alienHeight,
width : alienWidth,
height : alienHeight,
alive : true
}
alienArray.push(alien);
}
}
alienCount = alienArray.length;
}

function shoot(e){
if(gameOver){
return;
}

if(e.code == "Space"){
let bullet = {
x : ship.x + shipWidth*15/32,
y : shipY,
width : tileSize/8,
height : tileSize/2,
used : false
}
bulletArray.push(bullet);
}
}

function detectCollision(a, b){
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}
8 changes: 8 additions & 0 deletions Space-invaders/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body{
font-family: 'Courier New', Courier, monospace;
text-align: center;
}

#board{
background-color: black;
}
7 changes: 6 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,12 @@ const projects = [
discription: "Simple minesweeper game made using HTML, CSS and JS.",
link: "Minesweeper/index.html",
image: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBIREhISEhEYGBISGhgaGBgYGBgYGBESGRwZHBgYGBgdIS4mHR4rIRgYJjgmKy8xNTY1HCU7QDtAPy40NTEBDAwMEA8QGBIRGDEdGCE0MTE0MTE0MTQxNDQxMTE0NDE0MTExNDQ0NDQxMTQ0MT8xNDExNDExMT80NDQ0NDE/Mf/AABEIAM8A9AMBIgACEQEDEQH/xAAcAAEAAQUBAQAAAAAAAAAAAAAAAQIDBQYHBAj/xABAEAACAQIEBAMFBgUCBAcAAAABAgADEQQSITEFBkFRE2FxByIygZEUQlJicqEjM4KSwbGyU9Hw8RU0Q5OiwtL/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQL/xAAaEQEBAQADAQAAAAAAAAAAAAAAARExQVEC/9oADAMBAAIRAxEAPwDssREBERAREQEREBERAREQEREBERASDJkQEiIgJSYJkEyoGQZBMgmAMoMkmUkyiDKTJMgmUQZBgmRARIvEDJxETCkREBERAREQERLVGqrjMrBluRcEEXBsRcdQQRAuxEQERIgYTivHkw+JwtBgLVy2Y9aY2Q+hYEfKZucd5w4ga2Jq1E1yEZAdiKZ93X8LWv8A1GdI5Y4suKw6OGuQFBJ3ZWVWRjpuUZSbaBsw6S4jNSImA5l42MMmRD/Gce71yKdMxH1sD28oVnFqA3sQcpsbEGzaGx7GxH1k3mv8o+7RdSbsWDm+pu6qSSetyCfmZniYQJkEwTIJgCZSTBMpJlAmUkwTIJlAmUkwTIJgJSYMQESIgZWIiYUiIgIiICIkQJnM+I4+rw/H18jEI75yu6tnAckr11YjodDYidLnO/aNhrVqVS38xCvzptf9xUH9ssSts4Nx+niQo0Vz0vdW/Q3XcaGx8p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8NNBbO7flGg8zYDUzYeP06i1qlCihas9V6VJFtdmzMB5AAAkk6ADWdM5G5Up8Mw+T4sRVsa1T8bdFBOuRbm3zPWatRpnEPZh9mRDh+I10RnValwLKXGUPlQr9/IDfYG9/d15tzBhK2Gr1KFSs7lDuS3vA7GxY2n0/XorUR6bC6OCrA9VYWI+hnDfarw1kelVbVxek7fjZQGRz0uysG02uR0kGrcn8F+347D4Y38N2zVCNCKSDM+vS4GW/dhPoLg/KHD8GQ1DCU1cbOQXqD0d7sPkZoPsO4Rpicaw3IoofIWeoR5ElB/SZ1yRSa5xfllKovTAUjULsoYbFD907aWKnXQElpscQPmXmjlavw9yHUtSJ918uWxOyuoJCt21Kn7pOtu5ezjH/AGjheDfqieG1970iUufUKD85neIcPpYmm1OtTV0YEEEA6HeYnlHl3/w1K1BHzUGqF6YN81NWChkJ6gFbg/mMDYYiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiBgeG8u06OKr4tverVWbIbfyabEFgvmx1J7ADob56IgJontX4T42Aq1FW70grG2pshJB+hdf6vKb3KWUEEEXB3B1BEDD8ocJ+w4HDYe3vogL+dVrs5/uYzNREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBEiLwJiReLwJiReLwJiReLwJiReLwJiReLwJiReLwJiReLwJiReLwJiREBERA//9k="
}
},
{ title: "Space Invaders",
discription: "Welcome to the Space Invaders game! This classic arcade game has been brought to life using HTML, CSS, and JavaScript. Defend the Earth from the invading alien forces by maneuvering your spaceship and shooting down the relentless invaders.",
link: "Space-invaders/index.html",
image: "https://gamerhub.co.uk/wp-content/uploads/2020/11/what.jpg"
},
];

const cards = document.getElementsByClassName('cards');
Expand Down