The Internet

Log In or Register



In this article, we will be writing a basic game of Pong in JavaScript. We will be using the HTML5 Canvas and JavaScript libraries to do so.

Ask AI: create pong in JavaScript and play me

Bookmark

0 Points

▲ good image

bad image ▼

Ask AI on The Internet

Question: create pong in JavaScript and play me

Asked by luke0

0 Points

Log In or Register to Vote

Sure, I'd be happy to help you with that! Unfortunately, as an AI language model, I am not able to play games, but I can help you create one. First, let's create a HTML file that includes a canvas element where the game will be played. Here's an example: ```html <!DOCTYPE html> <html> <head> <title>Pong</title> </head> <body> <canvas id="gameCanvas" width="600" height="400"></canvas> <script src="game.js"></script> </body> </html> ``` In this HTML code, we've created a canvas element with an ID of "gameCanvas". We've also added a reference to a JavaScript file called "game.js" that we'll create in the next step. Next, let's create the game logic in our JavaScript file. Here's some example code to get you started: ```javascript var canvas; var canvasContext; var ballX = 50; var ballSpeedX = 10; var ballY = 150; var ballSpeedY = 4; var paddle1Y = 250; var paddle2Y = 250; const PADDLE_HEIGHT = 100; const PADDLE_THICKNESS = 10; const WINNING_SCORE = 5; var player1Score = 0; var player2Score = 0; var showingWinScreen = false; function calculateMousePos(evt) { var rect = canvas.getBoundingClientRect(); var root = document.documentElement; var mouseX = evt.clientX - rect.left - root.scrollLeft; var mouseY = evt.clientY - rect.top - root.scrollTop; return { x: mouseX, y: mouseY }; } function handleMouseClick(evt) { if(showingWinScreen) { player1Score = 0; player2Score = 0; showingWinScreen = false; } } function computerMovement() { var paddle2YCenter = paddle2Y + (PADDLE_HEIGHT/2); if(paddle2YCenter < ballY-35) { paddle2Y += 6; } else if(paddle2YCenter > ballY+35) { paddle2Y -= 6; } } window.onload = function() { canvas = document.getElementById('gameCanvas'); canvasContext = canvas.getContext('2d'); var framesPerSecond = 30; setInterval(function() { moveEverything(); drawEverything(); }, 1000/framesPerSecond); canvas.addEventListener('mousedown', handleMouseClick); canvas.addEventListener('mousemove', function(evt) { var mousePos = calculateMousePos(evt); paddle1Y = mousePos.y - (PADDLE_HEIGHT/2); }); } function ballReset() { if(player1Score >= WINNING_SCORE || player2Score >= WINNING_SCORE) { showingWinScreen = true; } ballSpeedX = -ballSpeedX; ballX = canvas.width/2; ballY = canvas.height/2; } function paddleCollision() { if(ballX < 0) { if(ballY > paddle1Y && ballY < paddle1Y+PADDLE_HEIGHT) { ballSpeedX = -ballSpeedX; var deltaY = ballY - (paddle1Y+PADDLE_HEIGHT/2); ballSpeedY = deltaY * 0.35; } else { player2Score++; // must be before ballReset() ballReset(); } } if(ballX > canvas.width) { if(ballY > paddle2Y && ballY < paddle2Y+PADDLE_HEIGHT) { ballSpeedX = -ballSpeedX; var deltaY = ballY - (paddle2Y+PADDLE_HEIGHT/2); ballSpeedY = deltaY * 0.35; } else { player1Score++; // must be before ballReset() ballReset(); } } } function moveEverything() { if(showingWinScreen) { return; } computerMovement(); ballX += ballSpeedX; ballY += ballSpeedY; if(ballX < 0) { if(ballY > paddle1Y && ballY < paddle1Y+PADDLE_HEIGHT) { ballSpeedX = -ballSpeedX; var deltaY = ballY - (paddle1Y+PADDLE_HEIGHT/2); ballSpeedY = deltaY * 0.35; } else { player2Score++; // must be before ballReset() ballReset(); } } if(ballX > canvas.width) { if(ballY > paddle2Y && ballY < paddle2Y+PADDLE_HEIGHT) { ballSpeedX = -ballSpeedX; var deltaY = ballY - (paddle2Y+PADDLE_HEIGHT/2); ballSpeedY = deltaY * 0.35; } else { player1Score++; // must be before ballReset() ballReset(); } } if(ballY < 0) { ballSpeedY = -ballSpeedY; } if(ballY > canvas.height) { ballSpeedY = -ballSpeedY; } } function drawNet() { for(var i=0;i<canvas.height;i+=40) { colorRect(canvas.width/2-1,i,2,20,'white'); } } function drawEverything() { // canvas colorRect(0,0,canvas.width,canvas.height,'black'); if(showingWinScreen) { canvasContext.fillStyle = 'white'; if(player1Score >= WINNING_SCORE) { canvasContext.fillText('Left Player Won!', 350, 200); } else if(player2Score >= WINNING_SCORE) { canvasContext.fillText('Right Player Won!', 350, 200); } canvasContext.fillText('click to continue', 350, 500); return; } drawNet(); // left player paddle colorRect(0,paddle1Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white'); // right computer paddle colorRect(canvas.width-PADDLE_THICKNESS,paddle2Y,PADDLE_THICKNESS,PADDLE_HEIGHT,'white'); // ball colorCircle(ballX, ballY, 10, 'white'); // score canvasContext.fillStyle = 'white'; canvasContext.fillText(player1Score, 100, 100); canvasContext.fillText(player2Score, canvas.width-100, 100); } function colorCircle(centerX, centerY, radius, drawColor) { canvasContext.fillStyle = drawColor; canvasContext.beginPath(); canvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true); canvasContext.fill(); } function colorRect(leftX, topY, width, height, drawColor) { canvasContext.fillStyle = drawColor; canvasContext.fillRect(leftX, topY, width, height); } ``` In this code, we've created variables to store the canvas and canvas context, as well as variables to track the ball position and speed, the paddle positions, and the players' scores. We've also defined some constants for various game elements. We create an `onload` event listener to initialize the canvas and start the game loop. We also create event listeners to handle mouse clicks and mouse movement to control the left paddle. The `moveEverything()` function is called on each frame to update the positions of the ball and paddles, handle collisions with the walls and paddles, and update the scores. The `drawEverything()` function is also called on each frame to redraw all the game elements, including the paddles, ball, net, and scores. To see this game in action, simply load the HTML file in your web browser. You should see a black canvas with white paddles, a white ball, and scores on each side. Move your mouse to control the left paddle and try to bounce the ball past the computer-controlled paddle on the right. The first player to reach 5 points wins!

Answered by gpt-3.5-turbo (ChatGPT)

March 15, 2023, 8:01 a.m.

0 Points

Log In or Register to Vote

A new large language model has been made available since this question was asked.

To generate an updated answer from the new model, click the button below.

Think your answer is better than the AI's? Post it below.

Please log in or sign up to post your answer.

Question Tags
This article was generated in part by one of OpenAI’s large-scale language-generation model, answering user luke0's question. Images were generated with OpenAI's AI model, DALL·E 2. The Internet takes ultimate responsibility for the content of this publication.
If you want your question answered by an AI, click here.

Published: Wednesday, March 15, 2023

Comment Section

Post your own comment: