0% found this document useful (0 votes)
66 views2 pages

Notes 240819 202558

This document contains the HTML and JavaScript code for a simple soccer game implemented on a canvas. It features two players who can move up and down to hit a ball, with a scoring system displayed on the screen. The game updates continuously, handling ball movement, collisions, and player controls through keyboard events.

Uploaded by

Divine Chiyangwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views2 pages

Notes 240819 202558

This document contains the HTML and JavaScript code for a simple soccer game implemented on a canvas. It features two players who can move up and down to hit a ball, with a scoring system displayed on the screen. The game updates continuously, handling ball movement, collisions, and player controls through keyboard events.

Uploaded by

Divine Chiyangwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

<!

DOCTYPE html>
<html>
<head>
<title>Soccer Game</title>
<style>
#gameCanvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="score">Player 1: 0 - Player 2: 0</div>
<script>
// Get the canvas element
var canvas = [Link]('gameCanvas');
var ctx = [Link]('2d');

// Define the ball properties


var ball = {
x: [Link] / 2,
y: [Link] / 2,
radius: 10,
speedX: 2,
speedY: 2
};

// Define the player properties


var player1 = {
x: 50,
y: [Link] / 2,
width: 10,
height: 100,
score: 0
};

var player2 = {
x: [Link] - 60,
y: [Link] / 2,
width: 10,
height: 100,
score: 0
};

// Draw the game elements


function draw() {
[Link](0, 0, [Link], [Link]);
[Link]();
[Link](ball.x, ball.y, [Link], 0, [Link] * 2);
[Link] = 'white';
[Link]();
[Link]();

[Link](player1.x, player1.y, [Link],


[Link]);
[Link](player2.x, player2.y, [Link],
[Link]);

[Link]('score').innerHTML = 'Player 1: ' +


[Link] + ' - Player 2: ' + [Link];
}

// Update the game state


function update() {
ball.x += [Link];
ball.y += [Link];

// Collision with walls


if (ball.x + [Link] > [Link]) {
[Link]++;
ball.x = [Link] / 2;
ball.y = [Link] / 2;
} else if (ball.x - [Link] < 0) {
[Link]++;
ball.x = [Link] / 2;
ball.y = [Link] / 2;
}
if (ball.y + [Link] > [Link] || ball.y - [Link]
< 0) {
[Link] = -[Link];
}

// Collision with players


if (ball.x - [Link] < player1.x + [Link] && ball.y >
player1.y && ball.y < player1.y + [Link]) {
[Link] = -[Link];
}
if (ball.x + [Link] > player2.x && ball.y > player2.y &&
ball.y < player2.y + [Link]) {
[Link] = -[Link];
}
}

// Main game loop


setInterval(function() {
update();
draw();
}, 16);

// Add event listeners for player movement


[Link]('keydown', function(event) {
if ([Link] === 'w') {
player1.y -= 10;
} else if ([Link] === 's') {
player1.y += 10;
} else if ([Link] === 'ArrowUp') {
player2.y -= 10;
} else if ([Link] === 'ArrowDown') {
player2.y += 10;
}
});
</script>
</body>
</html>

You might also like