<!DOCTYPE html> <html> <head> <title>Pong Game</title> <style> canvas { background-color: black; display: block; margin: 0 auto; } </style> </head> <body> <canvas id="pong" width="800" height="400"></canvas> <script> const canvas = document.getElementById("pong"); const ctx = canvas.getContext("2d"); // Load the Teachable Machine model async function loadModel() { const model = await tf.loadLayersModel('path/to/your/exported/model.json'); return model; } // Initialize paddle position and model let playerY = (canvas.height - 100) / 2; let model; // Preprocess an image for input to the model function preprocessImage(imageData) { // Process the imageData here (e.g., resize and normalize) // Return a tensor that matches the model's input shape return tf.tensor3d(/* {"tfjsVersion":"1.3.1","tmVersion":"2.4.7","packageVersion":"0.8.4-alpha2","packageName":"@teachablemachine/image","timeStamp":"2023-10-02T00:09:05.377Z","userMetadata":{},"modelName":"tm-my-image-model","labels":["Up","Down","Stay put"],"imageSize":224} */); } // Make predictions with the loaded model function predictWithModel(model, inputData) { const predictions = model.predict(inputData); return predictions; } // Interpret the predictions and decide paddle movement function interpretPredictions(predictions) { // Process the predictions to determine paddle movement if (/* Point Up */) { // Move the paddle up playerY -= 10; } if (/* Point down */) { // Move the paddle down playerY += 10; } else if { // Paddle Stay Put } } // Game loop that continuously captures frames and updates the paddle function gameLoop() { // Capture frames or images from your game canvas // You can implement this part using HTML5 canvas methods // Preprocess the captured data const preprocessedData = preprocessImage(/* capturedImageData */); // Make predictions with the model const predictions = predictWithModel(model, preprocessedData); // Interpret predictions and update paddle position interpretPredictions(predictions); // Clear the canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw paddles ctx.fillStyle = "white"; ctx.fillRect(10, playerY, 10, 100); // Continue the game loop requestAnimationFrame(gameLoop); } // Start the game loop after loading the model loadModel().then((loadedModel) => { model = loadedModel; gameLoop(); }); // Paddle settings const paddleWidth = 10; const paddleHeight = 100; let playerY = (canvas.height - paddleHeight) / 2; let computerY = (canvas.height - paddleHeight) / 2; const paddleSpeed = 5; // Ball settings const ballSize = 10; let ballX = canvas.width / 2; let ballY = canvas.height / 2; let ballSpeedX = 5; let ballSpeedY = 5; // Score let playerScore = 0; let computerScore = 0; // Paddle movement using arrow keys document.addEventListener("predictWithModel", function(event) { if (event.key === "Up" && playerY > 0) { playerY -= paddleSpeed; } if (event.key === "Down" && playerY < canvas.height - paddleHeight) { playerY += paddleSpeed; } else if (event.key === "Stay Put" && playerY = 0) { playerY = paddleSpeed; } }); // Update function function update() { ballX += ballSpeedX; ballY += ballSpeedY; // Ball collision with top and bottom walls if (ballY < 0 || ballY > canvas.height) { ballSpeedY = -ballSpeedY; } // Ball collision with paddles if ( ballX < paddleWidth && ballY > playerY && ballY < playerY + paddleHeight ) { ballSpeedX = -ballSpeedX; } else if (ballX > canvas.width - paddleWidth) { if ( ballY > computerY && ballY < computerY + paddleHeight ) { ballSpeedX = -ballSpeedX; } else { // Ball out of bounds (player side) if (ballX < 0) { computerScore++; resetBall(); } // Ball out of bounds (computer side) if (ballX > canvas.width) { playerScore++; resetBall(); } } } } // Reset ball to the center function resetBall() { ballX = canvas.width / 2; ballY = canvas.height / 2; ballSpeedX = -ballSpeedX; } // Draw everything function draw() { // Clear the canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Draw paddles ctx.fillStyle = "white"; ctx.fillRect(0, playerY, paddleWidth, paddleHeight); ctx.fillRect(canvas.width - paddleWidth, computerY, paddleWidth, paddleHeight); // Draw the ball ctx.beginPath(); ctx.arc(ballX, ballY, ballSize, 0, Math.PI * 2); ctx.fill(); // Display scores ctx.fillText(`Player: ${playerScore}`, 100, 50); ctx.fillText(`Computer: ${computerScore}`, canvas.width - 200, 50); } // Game loop function gameLoop() { update(); draw(); requestAnimationFrame(gameLoop); } gameLoop(); </script> </body> </html>
Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML
. You can also specify the stylesheet information in styles.css
tab and scripts information in scripts.js
tab and start coding.
HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.
<!DOCTYPE html>
<html>
and ends with </html>
<h1>
to <h6>
where <h1>
is the highest important heading and <h6>
is the least important sub-heading.<p>..</p>
tag.<a>
tag.
<a href="https://onecompiler.com/html">HTML online compiler</a>
<img>
tag, where src
attribute consists of image name.<button>..</button>
tag<ul>
for unordered/bullet list and <ol>
for ordered/number list, and the list items are defined in <li>
.<a href="https://onecompiler.com/html">HTML online compiler</a>
CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.
Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.
body{
padding: 25px;
}
.title {
color: #228B22;
font-family: Candara;
}
<table>
tag.<tr>
tag<th>
tag<td>
tag<caption>
tag<script>
is the tag used to write scripts in HTML<script src="script.js"></script>