
#Pling pong game update#
The reset game function will reset all the scores and update all states of objects to the default position, essentially resetting the game.ĩ. It resets the ball to the center and updates the score displayed on the page. The function which sets the score checks if all the conditions are met to update the score. While resetting the ball I’m changing directions of the ball because the one who got a point gets the next hit. Time for some utility functions to reset the ball positions after each player gets a score, one to set the score of the players and display them on the page, and one to reset the game once it is over. Here we are updating the positions of the players based on the keypress and boundaries of the canvas.Ĩ. Now we have to update the positions of the paddles when the respective keys are pressed. It also disables the game with an overlay when the screen size is below 560px.ħ. The function resize handler is called each time we resize the screen and also at the start of the program to organize the canvas and object positions. When we resize a screen canvas needs to be resized and the objects in the canvas have to be repositioned to match the new canvas size. We have to handle resizing the window and updating the values on resizing. A function is defined to clear the canvas and draw all of the objects to the screen.Ħ. The players and ball is drawn based on the properties of the object. Now we have to write functions to draw the ball and players to the canvas*.* Event listeners are set up to update the states of keys on key down and key up.ĥ. We have a state for keys to record which keys are being pressed at a time. It also contains the score needed to win, the hit count to trigger an increase in the speed of the ball, etc. We’ll track each player’s score in the game object. Now we have to create a game state and also handle key presses and key states. The ball, the left player ( left paddle), and the right player ( right paddle ) are initialized with necessary properties like height, width, x & y positions, color, player name, speed, etc. We have created the canvas and we are storing the winner in session storage, and as of now, the winner is set to none. We have to initialize the canvas, ball, and two players. Now it’s time to build the game logic using JavaScript. Now we also have a neon effect to our game using the box shadows.ģ. The styles are straightforward, we set the canvas to the center, and scores are shown on both sides of the board. The look and feel of a page are also an important part. Now we have to add some styles to our page to make it look nice. The CSS file style.css and JavaScript file script.js are linked to this HTML file.Ģ. The block with class small-device is to disable the game and display the message on small screens because there won’t be keyboard controls. Here we have a canvas to render the game and two paragraph tags to display the scores.

Make Ping Pong Game Using HTML, CSS & Javascriptįirst, we need to create an HTML page with a canvas to run the game.
#Pling pong game code#
Let’s go through the code to understand the logic better and see how we can build ping pong. The game is continued until a player wins.It is checked whether the player has reached the score that is needed to win and if it is reached, then the player wins and the game ends. Once a player misses the ball, the other player’s score is incremented by one.The up and down boundaries of the canvas will reflect the ball when the ball hits.The direction of the ball is changed and speed is increased a little when the ball touches a paddle.UP & DOWN arrow keys for the right player to move up and down respectively. W & S for the left player to move up and down respectively.


We move the paddles up and down using the keys on the keyboard.We’ll deploy a ball with an initial velocity and a direction at the center.We are using HTML5 canvas and JavaScript to implement this game.
The first one who reaches the point needed to win will win the game. If one misses the ball, the other player gets the point. The paddles are moved up and down by the players to make sure the ball hits it and sends it back in the direction of the other player. It includes a moving ball and two paddles. Pong is a two-player table tennis theme game. LinkedIn logo for sharing a link Twitter logo for sharing a link Reddit logo for sharing a link
