-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJump_Game.html
64 lines (58 loc) · 1.36 KB
/
Jump_Game.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
<head>
<title>Jump Game</title>
</head>
<body>
<canvas id="game"></canvas>
<script>
// Initialize canvas
var canvas = document.getElementById("game");
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Create player object
var player = {
x: 50,
y: canvas.height - 100,
width: 50,
height: 50,
speed: 10,
jumping: false,
jumpSpeed: 15
};
// Draw player on canvas
function drawPlayer() {
ctx.fillRect(player.x, player.y, player.width, player.height);
}
// Handle player jump
function jump() {
if (!player.jumping) {
player.jumping = true;
setInterval(function() {
player.y -= player.jumpSpeed;
player.jumpSpeed -= 0.5;
if (player.jumpSpeed <= 0) {
clearInterval();
player.jumping = false;
player.jumpSpeed = 15;
}
}, 10);
}
}
// Handle key press events
document.onkeydown = function(event) {
if (event.keyCode == 32) { // Spacebar
jump();
}
}
// Main game loop
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>