-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
39 lines (35 loc) · 777 Bytes
/
game.js
File metadata and controls
39 lines (35 loc) · 777 Bytes
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
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Car object
const car = {
x: 100,
y: 300,
width: 50,
height: 50,
speed: 5,
draw: function() {
ctx.fillStyle = 'red';
ctx.fillRect(this.x, this.y, this.width, this.height);
},
moveLeft: function() {
this.x -= this.speed;
},
moveRight: function() {
this.x += this.speed;
}
};
// Game loop
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
car.draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
// Event listeners for car movement
document.addEventListener('keydown', function(event) {
if (event.key === 'ArrowLeft') {
car.moveLeft();
} else if (event.key === 'ArrowRight') {
car.moveRight();
}
});