Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Hexagon Game</title>
- <style>
- #welcomeScreen {
- text-align: center;
- }
- #startButton {
- margin-top: 20px;
- }
- #instructions {
- float: left;
- width: 50%;
- margin-top: 50px;
- }
- </style>
- </head>
- <body>
- <div id="welcomeScreen">
- <h2>Welcome to Hexagon Game</h2>
- <label for="player1Name">Player 1 Name:</label>
- <input type="text" id="player1Name" required><br><br>
- <label for="player2Name">Player 2 Name (or Bot):</label>
- <input type="text" id="player2Name"><br><br>
- <label for="level">Choose Level:</label>
- <select id="level">
- <option value="easy">Easy</option>
- <option value="medium">Medium</option>
- <option value="hard">Hard</option>
- </select><br><br>
- <!-- Disable start button initially -->
- <button id="startButton" disabled>Start Game</button>
- </div>
- <canvas id="canvas" width="800" height="700" style="display: none;"></canvas>
- <h2 id="name1" style="display: none;"><span id="player1Info"> </span>: <span id="player1Total">0</span></h2>
- <h2 id="name2" style="display: none;"><span id="player2Info"> </span>: <span id="player2Total">0</span></h2>
- <script>
- const welcomeScreen = document.getElementById('welcomeScreen');
- const startButton = document.getElementById('startButton');
- const canvas = document.getElementById('canvas');
- const name1 = document.getElementById('name1');
- const name2 = document.getElementById('name2');
- const ctx = canvas.getContext('2d');
- const levels = {
- easy: { disabledHexagons: 4 },
- medium: { disabledHexagons: 6 },
- hard: { disabledHexagons: 8 }
- };
- startButton.addEventListener('click', startGame);
- function startGame() {
- // Hide welcome screen
- welcomeScreen.style.display = 'none';
- // Show canvas
- canvas.style.display = 'block';
- name1.style.display = 'block';
- name2.style.display = 'block';
- // // Start game logic
- init();
- }
- // Validation logic
- const player1NameInput = document.getElementById('player1Name');
- const levelSelect = document.getElementById('level');
- player1NameInput.addEventListener('input', validateInput);
- levelSelect.addEventListener('change', validateInput);
- function validateInput() {
- const player1Name = player1NameInput.value.trim();
- const selectedLevel = levelSelect.value;
- startButton.disabled = !(player1Name && selectedLevel);
- }
- // Game initialization
- const a = 2 * Math.PI / 6;
- const r = 40; // Decreased radius to fit more hexagons
- let numRows = 8; // Number of rows
- let numCols = 10; // Number of columns
- const yOffset = r * Math.sin(a); // Y offset between rows
- let hexagons = [];
- let player1Total = 0;
- let player2Total = 0;
- let currentPlayer = 1; // Player 1 starts first
- const player1TotalElement = document.getElementById('player1Total');
- const player2TotalElement = document.getElementById('player2Total');
- document.getElementById("startButton").addEventListener("click", function() {
- // Get input values
- const player1Name = document.getElementById("player1Name").value;
- const player2Name = document.getElementById("player2Name").value;
- // Show input values in h2 elements
- document.getElementById("player1Info").innerText = player1Name;
- document.getElementById("player2Info").innerText = player2Name || "Bot";
- });
- // Initialize game
- function init() {
- // Calculate canvas width and height based on number of rows and columns
- const canvasWidth = numCols * r * Math.sqrt(3) + r * Math.sqrt(3) / 2;
- const canvasHeight = numRows * r * 1.5 + r * 0.5; // Added extra height for bottom margin
- canvas.width = canvasWidth;
- canvas.height = canvasHeight;
- drawGrid(canvasWidth, canvasHeight);
- canvas.addEventListener('click', handleClick);
- }
- // Draw hexagonal grid
- function drawGrid(width, height) {
- hexagons = []; // Reset hexagons array
- // Calculate initial position to center the hexagons
- const startX = (width - (numCols * r * Math.sqrt(3) + r * Math.sqrt(3) / 2)) / 2;
- const startY = (height - (numRows * r * 1 + r * 1)) / 2;
- for (let row = 0; row < numRows; row++) {
- for (let col = 0; col < numCols; col++) {
- const x = startX + col * r * Math.sqrt(3) + (row % 2 === 1 ? r * Math.sqrt(3) / 2 : 0);
- const y = startY + row * r * 1.5;
- drawHexagon(x, y);
- hexagons.push({ x, y, active: true, clickedBy: 0, number: 0 }); // Set default status active, clicked by player, and number
- }
- }
- // Disable hexagons based on selected level
- const level = levelSelect.value;
- const disabledHexagons = levels[level].disabledHexagons;
- for (let i = 0; i < disabledHexagons; i++) {
- const randomIndex = Math.floor(Math.random() * hexagons.length);
- hexagons[randomIndex].active = false;
- drawDisabledHexagon(hexagons[randomIndex].x, hexagons[randomIndex].y);
- }
- }
- // Draw a single hexagon
- function drawHexagon(x, y, rotationAngle = Math.PI / 2) {
- ctx.beginPath();
- for (let i = 0; i < 6; i++) {
- const angle = a * i + rotationAngle; // Start angle from the top side
- ctx.lineTo(x + r * Math.cos(angle), y + r * Math.sin(angle));
- }
- ctx.closePath();
- ctx.stroke();
- }
- // Draw a disabled hexagon
- function drawDisabledHexagon(x, y, rotationAngle = Math.PI / 2) {
- ctx.beginPath();
- ctx.fillStyle = "#c0c0c0"; // Gray color for disabled hexagons
- for (let i = 0; i < 6; i++) {
- const angle = a * i + rotationAngle; // Start angle from the top side
- ctx.lineTo(x + r * Math.cos(angle), y + r * Math.sin(angle));
- }
- ctx.closePath();
- ctx.fill();
- ctx.stroke();
- }
- // Handle click event on hexagons
- function handleClick(event) {
- const rect = canvas.getBoundingClientRect();
- const mouseX = event.clientX - rect.left;
- const mouseY = event.clientY - rect.top;
- hexagons.forEach(hexagon => {
- if (hexagon.active && isPointInHexagon(mouseX, mouseY, hexagon.x, hexagon.y)) {
- // Check if the clicked hexagon is empty
- if (hexagon.clickedBy === 0) {
- // Place a random number in the hexagon
- hexagon.number = Math.floor(Math.random() * 20) + 1;
- // Determine the color based on the current player
- ctx.fillStyle = currentPlayer === 1 ? "red" : "blue";
- // Fill the hexagon with the player's color
- ctx.beginPath();
- for (let i = 0; i < 6; i++) {
- const angle = a * i + Math.PI / 2; // Start angle from the top side
- ctx.lineTo(hexagon.x + r * Math.cos(angle), hexagon.y + r * Math.sin(angle));
- }
- ctx.closePath();
- ctx.fill();
- ctx.stroke();
- // Draw the number inside the hexagon
- ctx.fillStyle = "white";
- ctx.font = "16px Arial";
- ctx.fillText(hexagon.number, hexagon.x - 10, hexagon.y + 5);
- // Update total score for the current player
- if (currentPlayer === 1) {
- player1Total += hexagon.number;
- player1TotalElement.textContent = player1Total;
- } else {
- player2Total += hexagon.number;
- player2TotalElement.textContent = player2Total;
- }
- // Switch to the next player
- currentPlayer = currentPlayer === 1 ? 2 : 1;
- }
- }
- });
- }
- // Function to check if a point is inside a hexagon
- function isPointInHexagon(x, y, hexX, hexY) {
- // Algorithm to determine if a point is inside a hexagon
- const dx = x - hexX;
- const dy = y - hexY;
- return (Math.abs(dx) < r && Math.abs(dy) < r * Math.sin(a) && Math.abs(dy) < r * Math.sin(Math.PI / 3) - Math.sin(a) * dx / 2);
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement