Week 4: Hangman

The concept for this week’s assignment, I wanted to make a hangman word game. The code consists of helper functions such as keyPressed, startNewGame, checkGuess etc. Firstly, it creates a number of empty dashes  based on the number of letters in the chosen word. As a letter is pressed, dashes are changed to a letter if the letter is in the word, and the body of the hangman is drawn if it is not. At the end of the game, it checks if the user guessed the word correctly, and shows corresponding texts.

Further on, I would like to add randomized word generation or read from csv or text file large pool of words.

code:

let word;
let guessedWord;
let maxAttempts = 6
let attemptsLeft = maxAttempts;
let guessedLetters = [];

function setup() {
  createCanvas(400, 400);
  textAlign(CENTER, CENTER);
  startNewGame();
}

function draw() {
  background(220);

  // Display hangman
  drawHangman();

  // Display guessed word
  textSize(32);
  text(guessedWord.join(' '), width / 2, height / 2 - 20);

  // Display attempts left
  textSize(16);
  text(`Attempts left: ${attemptsLeft}`, width / 2, height - 20);

  // Display guessed letters
  text(`Guessed letters: ${guessedLetters.join(', ')}`, width / 2, height - 40);

  // Check for game over
  if (attemptsLeft === 0 || guessedWord.indexOf('_') === -1) {
    gameOver();
  }
}

function keyPressed() {
  if (keyCode === ENTER) {
    startNewGame();
  } else if (key >= 'a' && key <= 'z' && !guessedLetters.includes(key)) {
    checkGuess(key);
  }
}

function startNewGame() {
  // List of possible words
  let words = ["apple", "banana", "simulation", "problems", "reaction"];

  // Choose a random word
  word = random(words).split('');

  // Initialize guessedWord with underscores
  guessedWord = Array(word.length).fill('_');

  // Reset attempts
  attemptsLeft = maxAttempts;

  // Reset guessed letters
  guessedLetters = [];
}

function checkGuess(letter) {
  // Add guessed letter to the list
  guessedLetters.push(letter);

  // Check if the letter is in the word
  let correctGuess = false;
  for (let i = 0; i < word.length; i++) {
    if (word[i] === letter) {
      guessedWord[i] = letter;
      correctGuess = true;
    }
  }

  // Decrease attempts if the guess is incorrect
  if (!correctGuess) {
    attemptsLeft--;
  }
}

function drawHangman() {
  stroke(0);
  
  line(width / 2 - 100, height /2 - 80 ,width /2 - 50, height /2 -80)
  line(width / 2 - 75, height / 2 -80, width / 2 -75, height /2 - 180)
  line(width / 2 -75, height / 2 - 180, width / 2, height / 2 - 180)
  line(width / 2, height / 2 - 180, width / 2, height / 2- 160)
  
  // Head
  if (attemptsLeft < maxAttempts) {
    ellipse(width / 2, height / 4 - 50, 20, 20)
  }

  // Body
  if (attemptsLeft < maxAttempts - 1) {
    line(width / 2, height / 4 - 40, width / 2, height / 2 - 80);
  }

  // Left arm
  if (attemptsLeft < maxAttempts - 2) {
    line(width / 2, height / 4 -30, width / 2 - 20, height / 4-10 );
  }

  // Right arm
  if (attemptsLeft < maxAttempts - 3) {
    line(width / 2, height / 4 -30, width / 2 + 20, height / 4 -10);
  }

  // Left leg
  if (attemptsLeft < maxAttempts - 4) {
    line(width / 2, height / 2 - 80, width / 2 - 20, height / 2 -60);
  }

  // Right leg
  if (attemptsLeft < maxAttempts - 5) {
    line(width / 2, height / 2 -80, width / 2 + 20, height / 2 -60);
  }
}

function gameOver() {
  // Display game over message
  textSize(16);
  if (guessedWord.indexOf('_') === -1) {
    text("You guessed the word!", width / 2, height / 2 + 60);
  } else {
    text("Game over! The word was " + word.join(''), width / 2, height / 2 + 60);
  }

  // Display new game prompt
  textSize(16);
  text("Press ENTER for a new game", width / 2, height/ 2 + 100);
}

the game:

Leave a Reply