Then we need to attach 2 more elements:

  1. An element where we are going to display the text that the players have to type:
    . We are going to add the text that needs to be typed using a simple javascript piece of code:
            var text2type = \\'Some text that needs to be typed...\\';            function init(){                $(\\'#txt\\').text( text2type );            }
  1. An element that needs to have the focus, where we can add an event listener to when the player press a key. We don't really need to display the element, we just need to have it in tha page and it must be visible, otherwise it can not receive the focus. However, if we don't want to show it, we will set it's width and height to 0.

Now we need to ensure, the text input element has always the focus. First we ad an event in boda=y tag that will set the focus to the textinput element when the click on the body, which practically means anywhere in the page:

We also need to set the focus when the page is loaded and ready.

$( document ).ready(function() {    $(\\'#textinput\\').focus();});

Now we need to code the most important part, the code which handles the typing part. The code is quite easy, it has 3 main methods:

\\'use strict\\';class TypingGame {    constructor( text, div ) {      this.text = text;      this.history = \\\"\\\";      this.misspelled = false;      this.stats = [];    }    add(c) {      if ( c == this.text.substring( this.history.length, this.history.length   1 ) ){        this.history  = c;        this.misspelled = false;      }      else{        this.misspelled = true;      }      this.render();    }    render(){        let cursorstyle = \\'cursor\\'   ( this.misspelled ? \\'-misstyped\\' : \\'\\' );      $(\\'#txt\\').html(         \\'\\'   this.history   \\'\\'          \\'\\'   this.text.substring( this.history.length, this.history.length   1 )   \\'\\'          \\'\\'   this.text.substring( this.history.length   1 )   \\'\\'      );    }  }

For the next part, we need to update the typer object, when a new letter is inputed in the textinput element(remember we have a listner there ).

            function updateText(){                let c = $(\\'#textinput\\').val();                if ( c.length > 0 ) {                    typer.add( c.slice(-1) );                }                $(\\'#textinput\\').val(\\'\\');                showCurrent()            }

Now we have the first prototype with the game mechanics of a working typing game. In the next section we are going to add some more elements to measure the typing speed in words per minutes and characters per minute(wpm and cpm) and to display the performance on a nice dialog.

","image":"http://www.luping.net/uploads/20241106/1730856247672ac537d7b5f.jpg","datePublished":"2024-11-08T12:33:57+08:00","dateModified":"2024-11-08T12:33:57+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Create a typing game in javascript to measure wpm speed

Create a typing game in javascript to measure wpm speed

Published on 2024-11-08
Browse:380

Create a typing game in javascript to measure wpm speed

Create a speed typing game in javascript

In this tutorial we are going to create a simple typing game that will measure our typing speed in wpm - words per minute, chars per minute, misspellings and will allow to improve it. We are going to use only javascript and jQuery(it's not really needed, but it will make our typing game less verbose so we can focus on the rest of the application.

We are starting by creating a simple html page:


    
        Typing Test WPM: How fast can you type? ⌨️?

Then we need to attach 2 more elements:

  1. An element where we are going to display the text that the players have to type:
    . We are going to add the text that needs to be typed using a simple javascript piece of code:
            var text2type = 'Some text that needs to be typed...';

            function init(){
                $('#txt').text( text2type );
            }

  1. An element that needs to have the focus, where we can add an event listener to when the player press a key. We don't really need to display the element, we just need to have it in tha page and it must be visible, otherwise it can not receive the focus. However, if we don't want to show it, we will set it's width and height to 0.

Now we need to ensure, the text input element has always the focus. First we ad an event in boda=y tag that will set the focus to the textinput element when the click on the body, which practically means anywhere in the page:

We also need to set the focus when the page is loaded and ready.

$( document ).ready(function() {
    $('#textinput').focus();
});

Now we need to code the most important part, the code which handles the typing part. The code is quite easy, it has 3 main methods:

  • the constructor which sets the text that needs to be typed and initializes some class variables.
  • the add method, which adds the new character that was typed, which can result in an error or not.
  • the render method is rendering the text with the players can a visual feedback of the progress, and they can see when then misspelled someting.
'use strict';

class TypingGame {

    constructor( text, div ) {
      this.text = text;
      this.history = "";

      this.misspelled = false;
      this.stats = [];
    }

    add(c) {

      if ( c == this.text.substring( this.history.length, this.history.length   1 ) ){
        this.history  = c;
        this.misspelled = false;
      }
      else{
        this.misspelled = true;
      }

      this.render();
    }


    render(){

        let cursorstyle = 'cursor'   ( this.misspelled ? '-misstyped' : '' );

      $('#txt').html( 
        ''   this.history   ''  
        ''   this.text.substring( this.history.length, this.history.length   1 )   ''
          ''   this.text.substring( this.history.length   1 )   ''
      );

    }


  }

For the next part, we need to update the typer object, when a new letter is inputed in the textinput element(remember we have a listner there ).

            function updateText(){

                let c = $('#textinput').val();
                if ( c.length > 0 ) {
                    typer.add( c.slice(-1) );
                }
                $('#textinput').val('');
                showCurrent()
            }

Now we have the first prototype with the game mechanics of a working typing game. In the next section we are going to add some more elements to measure the typing speed in words per minutes and characters per minute(wpm and cpm) and to display the performance on a nice dialog.

Release Statement This article is reproduced at: https://dev.to/websilvercraft/create-a-speed-typing-game-in-javascript-4lpc?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3