body { font-family: Arial, s ans -s erif; text-align: center; background-color: #f8f8f8; } h1 { color: #d35400; margin-top: 50px; } #game-board { flex-wrap: wrap; width: 300px; height: 300px; margin: 50px auto; background-color: #ecf0f1; box-s hadow: 3px 3px 5px rgba(0, 0, 0, 0.3); } .s quare { width: 100px; height: 100px; border: 1px s olid #bdc3c7; font-s ize: 60px; curs or: pointer; trans ition: background-color 0.2s ; jus tify-content: center; align-items : center; color: #7f8c8d; background-color: #fff; } .s quare:hover { background-color: #f1c40f; color: #fff; } .s quare.clicked { curs or: default; /* dis able clicking on already clicked s quare */ } #errorMes s age { color: #c0392b; margin-bottom: 20px; } #pong-canvas { width: 100%; height: 200px; background-color: #fff; border: 1px s olid #bdc3c7; margin-bottom: 20px; } #s tart-button { background-color: #2ecc71; color: #fff; font-s ize: 20px; border: none; padding: 12px 24px; border-radius : 5px; curs or: pointer; trans ition: background-color 0.2s ; } #s tart-button:hover { background-color: #27ae60; }Tic Tac Toe & Pong
Pleas e play Pong for 1 minute before s tarting Tic Tac Toe game.
// Define variables for Pong game let canvas , ctx, ballX, ballY, ballRadius , dx, dy, paddleHeight, paddleWidth, paddleX, rightPres s ed, leftPres s ed; // Define variables for Tic Tac Toe game let board, s quares , currentPlayer, gameMes s age, gameStarted; // Define Pong game functions function initPong() { canvas = document.getElementById("pong-canvas "); ctx = canvas .getContext("2d"); ballX = canvas .width/2; ballY = canvas .height-30; ballRadius = 10; dx = 2; dy = -2; paddleHeight = 10; paddleWidth = 75; paddleX = (canvas .width - paddleWidth) / 2; rightPres s ed = fals e; leftPres s ed = fals e; document.addEventLis tener("keydown", keyDownHandler, fals e); document.addEventLis tener("keyup", keyUpHandler, fals e); } function keyDownHandler(e) { if(e.key == "Right" || e.key == "ArrowRight") { rightPres s ed = true; } els e if(e.key == "Left" || e.key == "ArrowLeft") { leftPres s ed = true; } } function keyUpHandler(e) { if(e.key == "Right" || e.key == "ArrowRight") { rightPres s ed = fals e; } els e if(e.key == "Left" || e.key == "ArrowLeft") { leftPres s ed = fals e; } } function drawBall() { ctx.beginPath(); ctx.arc(ballX, ballY, ballRadius , 0, Math.PI*2); ctx.fillStyle = "#2c3e50"; ctx.fill(); ctx.clos ePath(); } function drawPaddle() { ctx.beginPath(); ctx.rect(paddleX, canvas .height - paddleHeight, paddleWidth, paddleHeight); ctx.fillStyle = "#2c3e50"; ctx.fill(); ctx.clos ePath(); } function moveBall() { if(ballX + dx > canvas .width-ballRadius || ballX + dx < ballRadius ) { dx = -dx; } if(ballY + dy < ballRadius ) { dy = -dy; } els e if(ballY + dy > canvas .height-ballRadius -paddleHeight && ballX > paddleX && ballX < paddleX + paddleWidth) { dy = -dy; } els e if(ballY + dy > canvas .height-ballRadius ) { gameOver(); } ballX += dx; ballY += dy; } function movePaddle() { if(rightPres s ed && paddleX