Basketball Shooting Game: Play Now! - Free Online HTML Game
<!DOCTYPE html>
<html>
<head>
<title>Basketball Shooting Game</title>
<style type='text/css'>
#canvas {
border: 2px solid #000;
}
</style>
</head>
<body>
<h1>Basketball Shooting Game</h1>
<canvas id='canvas' width='400' height='400'></canvas>
<script>
// Initialize the canvas and context variables
var c = document.getElementById('canvas');
var ctx = c.getContext('2d');
<pre><code>// Define the ball properties
var x = c.width / 2;
var y = c.height - 30;
var dx = 2;
var dy = -2;
var ballRadius = 10;
// Define the basket properties
var basketWidth = 80;
var basketHeight = 10;
var basketX = (c.width - basketWidth) / 2;
// Draw the ball
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = '#0095DD';
ctx.fill();
ctx.closePath();
}
// Draw the basket
function drawBasket() {
ctx.beginPath();
ctx.rect(basketX, c.height - basketHeight, basketWidth, basketHeight);
ctx.fillStyle = '#0095DD';
ctx.fill();
ctx.closePath();
}
// Draw the game
function draw() {
ctx.clearRect(0, 0, c.width, c.height);
drawBall();
drawBasket();
// Check for collisions
if (x + dx > c.width - ballRadius || x + dx < ballRadius) {
dx = -dx;
}
if (y + dy < ballRadius) {
dy = -dy;
} else if (y + dy > c.height - ballRadius) {
if (x > basketX && x < basketX + basketWidth) {
dy = -dy;
} else {
alert('Game Over');
document.location.reload();
}
}
x += dx;
y += dy;
}
setInterval(draw, 10);
</code></pre>
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmcc 著作权归作者所有。请勿转载和采集!