Git Product home page Git Product logo

lab-canvas-hangman's Introduction

Ironhack Logo

JS | Canvas Hangman

Introduction

In this exercise, we are going to create the classic game Hangman. In this game, we have to figure out a secret word by guessing one letter at a time, with each error, the man will get one step closer to death! The goal is to successfully guess the word before the man gets hanged.

As we have learned, we can't confront the whole game without splitting it up into smaller steps. We will split it up into three different parts: structure, logic, and game layout. This technique is known as incremental build.

We will separate the game logic from the iteration with the canvas. We should be able to play from the console first and then add the graphics.

Requirements

  • Fork this repo
  • Clone this repo

Submission

Upon completion, run the following commands:

$ git add .
$ git commit -m "done"
$ git push origin master

Create Pull Request so your TAs can check up your work.

1. First iteration: Game Logic

:::info In order to do the game logic, we add some Jasmine tests to help you. Navigate to:

starter_code
  |___ jasmine

Open the SpecRunner.html to check them. :::

Hangman Constructor

First, of at all let's create our Hangman constructor. It should have the following properties:

  • words. An array where we will store all the words that the player need to guess. We will take one of them randomly.
  • secretWord. Here we will store the word chosen for each game.
  • letters. An array to store the letters the user already clicked, so we do not repeat them.
  • guessedLetter. A string to store the letters the user clicked and guessed. We will use this to know when the user wins.
  • errorsLeft. It should start at 10, and decrease every time a user clicks on a letter that is not in the word.

Prototypes properties

  • getWord(). Returns a random word from our array words.
  • checkIfLetter. This function should check if the key the user has typed is a letter.
  • checkClickedLetters. Checks if the pressed letter has already been pressed and returns true if it was not or false in the opposite case.
  • checkGameOver. Returns a boolean value, true if the users lose, and false in any other case.
  • checkWinner. Check if the users win and return a boolean value.
  • addCorrectLetter. Adds to the guessedLetter variable the letter that was pressed. Also, it should check if the user wins.
  • addWrongLetter. Should subtract one from the variable errorsLeft and check if the game is over.

2. Second Iteration: Draw in Canvas

Now we need to start drawing the hangman in order to finish our game! As we said, we will do it in a different file separating it from the logic.

Let´s create a constructor HangmanCanvas and store in a variable our canvas. After storing it, we should get the context. Remember getContext('2d'). We also want to store in a variable the secret word.

Creating the board

First, we need to draw the board. We should clear the canvas, so every time we start the game we have a clean one. We can also set up here the width we want to set to our lines.

Draw the Secret Word Lines

We know the secret word so we should put one line for each of the letters the user has to guess.

Write the Guessed Letters and the Wrong Ones

Every time a user clicks the keyboard, after checking if the letter was not already clicked, we should write it on our board. If the secret word includes the letter we should write it in the position it corresponds, and if is not included in the secret word, we will write it on the top right so the user knows which ones he already clicked.

Draw the Hangman

The last task, we need to draw THE HANGMAN. You can notice the drawing is composed of lines and one circle. Go ahead and think how can we do it 😉

Bonus Iteration

Your game is finished! Anybody can play it, but we need to show them something when they win or lose, so go ahead and create two functions gameOver and winner, to display the images available on the starter_code > images folder.

Happy coding! ❤️

lab-canvas-hangman's People

Contributors

papuarza avatar sandrabosk avatar

Watchers

James Cloos avatar Felipe Parra avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.