Skip to content

Commit

Permalink
Merge pull request #2322 from Aditi22Bansal/master
Browse files Browse the repository at this point in the history
Solved #2287- Hexquest game extension
  • Loading branch information
Sulagna-Dutta-Roy authored Jul 11, 2024
2 parents f53ba2a + ddfdba2 commit da6a4e7
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Hexquest game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HexQuest</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>HexQuest</h1>
<div id="game-board"></div>
<div id="info">
<p id="message"></p>
<button id="restart">Restart Game</button>
</div>
<script src="script.js"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions Hexquest game/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"manifest_version": 3,
"name": "HexQuest",
"version": "1.0",
"description": "A hexagonal grid-based adventure game.",
"permissions": [],
"background": {
"service_worker": "script.js"
},
"action": {
"default_popup": "index.html",
"default_icon": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
},
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
}
}
55 changes: 55 additions & 0 deletions Hexquest game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
document.addEventListener("DOMContentLoaded", () => {
const gameBoard = document.getElementById("game-board");
const message = document.getElementById("message");
const restartButton = document.getElementById("restart");
const rows = 10;
const cols = 10;
let characterPosition = 0;
let goalPosition = 99;

// Initialize game board
function initBoard() {
gameBoard.innerHTML = "";
characterPosition = 0;
goalPosition = 99;
for (let i = 0; i < rows * cols; i++) {
const hex = document.createElement("div");
hex.classList.add("hex");
if (i === characterPosition) hex.classList.add("character");
if (i === goalPosition) hex.classList.add("goal");
if (Math.random() < 0.1 && i !== characterPosition && i !== goalPosition) hex.classList.add("obstacle");
hex.addEventListener("click", () => moveCharacter(i));
gameBoard.appendChild(hex);
}
updateMessage("Find the goal!");
}

// Move character
function moveCharacter(position) {
const distance = Math.abs(position - characterPosition);
if (distance === 1 || distance === cols) {
const hexes = document.querySelectorAll(".hex");
hexes[characterPosition].classList.remove("character");
characterPosition = position;
hexes[characterPosition].classList.add("character");

if (characterPosition === goalPosition) {
updateMessage("You found the goal! Click Restart to play again.");
} else if (hexes[characterPosition].classList.contains("obstacle")) {
updateMessage("You hit an obstacle! Click Restart to try again.");
} else {
updateMessage("Keep going!");
}
}
}

// Update message
function updateMessage(text) {
message.textContent = text;
}

// Restart game
restartButton.addEventListener("click", initBoard);

initBoard();
});
57 changes: 57 additions & 0 deletions Hexquest game/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}

h1 {
text-align: center;
margin-bottom: 20px;
}

#game-board {
display: grid;
grid-template-columns: repeat(10, 50px);
gap: 5px;
justify-content: center;
}

.hex {
width: 50px;
height: 50px;
background-color: #fff;
border: 1px solid #ccc;
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
display: flex;
justify-content: center;
align-items: center;
font-size: 14px;
cursor: pointer;
}

.hex.character {
background-color: #4caf50;
}

.hex.obstacle {
background-color: #f44336;
}

.hex.goal {
background-color: #ffeb3b;
}

#info {
margin-top: 20px;
text-align: center;
}

button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}

0 comments on commit da6a4e7

Please sign in to comment.