How to create a Hangman game with Javascript

 

Image for post

This the part of Brad Traversy’s course.

Features:

Github code

HTML

Image for post

SVG drawing: the rode is a four-line shape.

x1,x2 are the axis of x,y1,y2 are the axis of y, which two-point makes a line

head is a circle with cx, cy(the center of the circle),r(the radius of the circle)

body ,arms and legs are all the lines

Image for post

where displaying the wrong letters

Image for post

success message box

Image for post

same typing message box

CSS

Image for post

the hangman fill and stroke set up

hide the hangman lines first

Image for post

hide the popup container first and change it in the js

Image for post

position the notification box outside of the page and change the y axis to show up

JS

Image for post

all the DOMs needed

Image for post

the data is the array with several words or can be fetched from API

each time the selected word should be a random one

the correctLetters and wrongLetters to strore the qualidied array

Image for post

loop the selected word and if the letter is included in the correctLetter, show the letter

if the displaying word is the same as the selected word, the success final message box shows up.

Image for post

when key pressing the letters from a to z, checking the condition that whether the pressing letter included in the selected word. If the letter is already in the correct array, show the notification, if not, then add to the correct array. The same as the wrong array.

Image for post

When want to display the wrong letters, it’s the same way.

map the wrong array and display in the dom

loop each of the hangman lines, if the index smaller than the length of the wrong array, showing the line.

When the line length is the same as the wrong array length, the message box shows up.

Image for post

show a notification if needed and hide after 2 second

Image for post

When clicking the play again button or key press the enter, reset all the arrays, and run the functions again, also hide the popup box.

Image for post

do not forget to call the displayWord() when loading the page

Popular posts from this blog

Fancyapps — easily build overlay windows with carousel

Thoughts about Progressive Web App

Meet Mantine: A TS-Based Open-Source React Components Library