Calculator Tools Calculator Tools
Create

Tennis Pong Game

Oct 2, 2023

About this app

A fun pong game in the theme of tennis.

Tennis Pong Game

Related apps

Put this on your site

Source

                    <html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Tennis Pong Game</title>
<meta name="description" content="A fun pong game in the theme of tennis.">
<meta name="keywords" content="Game, Tennis, Pong, Web Game, Interactive">

<style>
/* App CSS Goes Here */
body {
background-color: #2ecc71;
font-family: 'Comic Sans MS', cursive, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#pongBoard {
background-color: #27ae60;
width: 800px;
height: 400px;
position: relative;
display: flex;
justify-content: space-between;
padding: 20px 0;
border: 5px solid #2c3e50;
}
.paddle, .ball {
background-color: #ecf0f1;
position: absolute;
}
.paddle {
width: 20px;
height: 100px;
}
.ball {
width: 20px;
height: 20px;
border-radius: 50%;
top: 50%;
left: 50%;
}
</style>
</head>
<body>
<div id="pongBoard">
<div id="leftPaddle" class="paddle"></div>
<div id="rightPaddle" class="paddle"></div>
<div id="ball" class="ball"></div>
</div>

<script type="text/javascript">
try {
// App Javascript Goes Here

var leftPaddle = document.getElementById('leftPaddle');
var rightPaddle = document.getElementById('rightPaddle');
var ball = document.getElementById('ball');

var boardHeight = parseInt(window.getComputedStyle(document.getElementById('pongBoard')).height);
var paddleHeight = parseInt(window.getComputedStyle(leftPaddle).height);
var ballSize = parseInt(window.getComputedStyle(ball).width);

var leftPaddleY = rightPaddleY = (boardHeight - paddleHeight) / 2;
var ballX = ballY = (boardHeight - ballSize) / 2;
var ballSpeedX = ballSpeedY = 2;

// This will run when the DOM is ready.
document.addEventListener("DOMContentLoaded", function() {
setInterval(moveBall, 10);
document.addEventListener('keydown', movePaddle);
});

function moveBall() {
ballX += ballSpeedX;
ballY += ballSpeedY;

if (ballY <= 0 || ballY >= boardHeight - ballSize) {
ballSpeedY *= -1;
}

if (ballX <= 20) {
if (ballY > leftPaddleY && ballY < leftPaddleY + paddleHeight) {
ballSpeedX *= -1;
} else {
resetBall();
}
}

if (ballX >= 760) {
if (ballY > rightPaddleY && ballY < rightPaddleY + paddleHeight) {
ballSpeedX *= -1;
} else {
resetBall();
}
}

ball.style.left = ballX + 'px';
ball.style.top = ballY + 'px';
}

function movePaddle(event) {
switch (event.key) {
case 'w':
if (leftPaddleY > 0) {
leftPaddleY -= 10;
}
break;
case 's':
if (leftPaddleY < boardHeight - paddleHeight) {
leftPaddleY += 10;
}
break;
case 'ArrowUp':
if (rightPaddleY > 0) {
rightPaddleY -= 10;
}
break;
case 'ArrowDown':
if (rightPaddleY < boardHeight - paddleHeight) {
rightPaddleY += 10;
}
break;
}

leftPaddle.style.top = leftPaddleY + 'px';
rightPaddle.style.top = rightPaddleY + 'px';
}

function resetBall() {
ballX = ballY = (boardHeight - ballSize) / 2;
}

} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>
</body>
</html>