Skip to content

Commit

Permalink
Merge pull request #2151 from Aditi22Bansal/master
Browse files Browse the repository at this point in the history
Solved #2090- Hoplite
  • Loading branch information
Sulagna-Dutta-Roy authored Jul 5, 2024
2 parents 26ed84f + 2368d8f commit 9b72e20
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Hoplite/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hoplite Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Hoplite Game</h1>
<div id="gameBoard"></div>
</div>
<script src="script.js"></script>
</body>
</html>
27 changes: 27 additions & 0 deletions Hoplite/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "Hoplite Game",
"short_name": "Hoplite",
"description": "A simple web-based strategy game inspired by Hoplite.",
"version": "1.0.0",
"manifest_version": 2,
"start_url": "index.html",
"display": "standalone",
"background_color": "#f5f5f5",
"theme_color": "#ffffff",
"orientation": "portrait",
"icons": [
{
"src": "icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"permissions": [
"storage"
]
}
61 changes: 61 additions & 0 deletions Hoplite/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
document.addEventListener('DOMContentLoaded', () => {
const gameBoard = document.getElementById('gameBoard');
const boardSize = 5;
const heroPosition = { x: 2, y: 2 };
const enemies = [{ x: 1, y: 1 }, { x: 3, y: 3 }];

function createBoard() {
for (let y = 0; y < boardSize; y++) {
for (let x = 0; x < boardSize; x++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.x = x;
cell.dataset.y = y;
gameBoard.appendChild(cell);
}
}
renderBoard();
}

function renderBoard() {
document.querySelectorAll('.cell').forEach(cell => {
cell.classList.remove('hero', 'enemy');
});

const heroCell = document.querySelector(`.cell[data-x='${heroPosition.x}'][data-y='${heroPosition.y}']`);
heroCell.classList.add('hero');

enemies.forEach(enemy => {
const enemyCell = document.querySelector(`.cell[data-x='${enemy.x}'][data-y='${enemy.y}']`);
enemyCell.classList.add('enemy');
});
}

function moveHero(x, y) {
if (x >= 0 && x < boardSize && y >= 0 && y < boardSize) {
heroPosition.x = x;
heroPosition.y = y;
renderBoard();
}
}

document.addEventListener('keydown', (event) => {
const { x, y } = heroPosition;
switch (event.key) {
case 'ArrowUp':
moveHero(x, y - 1);
break;
case 'ArrowDown':
moveHero(x, y + 1);
break;
case 'ArrowLeft':
moveHero(x - 1, y);
break;
case 'ArrowRight':
moveHero(x + 1, y);
break;
}
});

createBoard();
});
43 changes: 43 additions & 0 deletions Hoplite/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f5f5f5;
margin: 0;
}

.container {
text-align: center;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

#gameBoard {
display: grid;
grid-template-columns: repeat(5, 50px);
gap: 5px;
margin: 20px 0;
}

.cell {
width: 50px;
height: 50px;
background-color: #ddd;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid #ccc;
cursor: pointer;
}

.cell.hero {
background-color: #ffa;
}

.cell.enemy {
background-color: #f88;
}

0 comments on commit 9b72e20

Please sign in to comment.