-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
56 lines (50 loc) · 1.14 KB
/
game.js
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
class Pacman {
constructor() {
this.x = 0;
this.y = 0;
this.radius = 10;
this.direction = 'right';
}
draw(canvas) {
canvas.context.fillStyle = '#FF0000';
if (this.direction === 'right') {
canvas.context.drawCircle(this.x, this.y, this.radius);
}
// Add more directions and shapes as needed
}
update() {
switch (this.direction) {
case 'right':
this.x += 1;
break;
case 'left':
this.x -= 1;
break;
case 'up':
this.y -= 1;
break;
case 'down':
this.y += 1;
break;
}
}
}
class Game {
constructor(canvas) {
this.canvas = canvas;
this.context = canvas.getContext('2d');
this.pacman = new Pacman();
this.interval = setInterval(() => {
this.clear();
this.pacman.draw(this);
this.pacman.update();
}, 1000 / 60); // 60 FPS
}
clear() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
// Create a canvas element and pass it to the Game constructor
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
new Game(canvas);