• Develop a Ping-Pong game using HTML5, CSS3 and JavaScript

    Published on November 15, 2011
    With 3 comments

    Creating the game loop

    What do we have to set for the game loop?

    Firstly, we need to call the functions to move the ball and the right and left paddles (moveBall() and movePaddle(). Then, based on the criteria of achieving the maximum score value, wee need to stop the game loop and to re-initialize some values and make the “Start game” button clickable and to make the “Start/Resume game” button reappear. Also the code for pausing the game (somehow trivial) is added. The pause game action can be polished using the clearInterval() function to stop/resume the game.. setInterval function is not implemented in the same way for all major browser. Also, an evaluation is necessary for the alert() JavaScript function. The jQuery Timer plugin it is more desirable to use than the native setInterval() and setTimeout JavaScript functions. Briefly, when using Timers, the code execution is broken when an event is triggered. But this is not true for Mozilla Firefox (at least for this project). On the other hand, alert() function is set to break the code execution when it is called. But, what is happening if it is called in a repetitive function call? Mozilla Firefox break the code execution, the other browsers break the execution of the code but only for the current instance of the function. So, the pause game effect is called differently for Mozilla Firefox on one hand, and for the rest of the major browser, on the other hand. It is not the best approach but is reliable.

    Moving the paddles

    Let’s take a look first at the CSS statements for the divs which represent the paddles:

    Note the statement position:absolute which means that the element is positioned relative to its first positioned (not static) ancestor element. Practically, the top values for the positioned elements (paddles) will move tha paddles in the desired directions.

    The paddles are moved based on the pressed key sets (W and S for the left player and Arrow UP and Arrow DOWN for the right player). Both paddles are set to stop moving on up or down direction if their half-height overflows the playground top and bottom limit. Setting this code our paddles can move.

    Moving the ball

    The ball is moving on X and Y axes based on its position on playground and based on paddles position. The has an absolute position, which means that is positioned relative to the first parent element that has a position other than static (playground is positioned relatively to his parent):

    First, we need to initialize some variables:

    Initial check is, when ball is moving up on Y axis, if ball position exceeds playground height, and then change the moving orientation on Y axis:

    3 comments to "Develop a Ping-Pong game using HTML5, CSS3 and JavaScript"

    Share your thoughts on this article.

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">