PBL Unit 1 / Week 0

logic2

logic

Game Initialization: Create a list of words Select a random word from the list Set max attempts (e.g., 6) Initialize guessed letters as an empty list

Display Hangman: Create ASCII art for each part of the hangman (head, body, left arm, right arm, left leg, right leg) Create a variable to keep track of the current state of the hangman (e.g., starting with an empty hangman)

Gameplay Loop: While max attempts > 0:

Display the current state of the hangman Display the word with underscores and filled-in letters based on guessed letters Display the number of attempts left

Guess a Letter:

Ask the player for a letter guess Validate the input (single character, not guessed before)

Check in Word:

If the guessed letter is in the word: Update the displayed word to reveal the guessed letter at the correct positions

Incorrect Guess Handling:

If the guessed letter is not in the word: Decrement max attempts Add the corresponding part to the hangman

Win/Lose Condition:

If the displayed word matches the target word: Display a victory message Break out of the loop (game over) If max attempts reach 0: Display a defeat message Display the correct word Break out of the loop (game over)

Game Termination: Ask the player if they want to play again

const obstacles = document.querySelectorAll('.obstacle');
            obstacles.forEach((obstacle) => {
                let position = parseInt(obstacle.style.top);
                if (position < containerHeight) {
                    const playerLeft = player.getBoundingClientRect().left;
                    const obstacleLeft = obstacle.getBoundingClientRect().left;
                    const speed = 5; // Adjust the speed as needed
                    if (obstacleLeft < playerLeft) {
                        obstacle.style.left = obstacleLeft + speed + 'px';
                    } else if (obstacleLeft > playerLeft) {
                        obstacle.style.left = obstacleLeft - speed + 'px';
                    }
                    obstacle.style.top = position + 5 + 'px';
                } else {
                    // Reset position at the top and randomize horizontal position
                    obstacle.style.top = '0px';
                    obstacle.style.left = getRandomLeft() + 'px';
                }
            });
const playerRect = player.getBoundingClientRect();
            obstacles.forEach((obstacle) => {
                const obstacleRect = obstacle.getBoundingClientRect();
                if (
                    playerRect.left < obstacleRect.left + obstacleRect.width &&
                    playerRect.left + playerRect.width > obstacleRect.left &&
                    playerRect.top < obstacleRect.top + obstacleRect.height &&
                    playerRect.top + playerRect.height > obstacleRect.top
                ) {
                    clearInterval(gameInterval);
                    alert('Game Over! Your score: 0');