GarGerry commited on
Commit
1c9fd5a
·
verified ·
1 Parent(s): 993e97a

Create game.js

Browse files
Files changed (1) hide show
  1. game.js +144 -0
game.js ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const canvas = document.getElementById('gameCanvas');
2
+ const ctx = canvas.getContext('2d');
3
+
4
+ canvas.width = 800;
5
+ canvas.height = 600;
6
+
7
+ let spaceship;
8
+ let bullets = [];
9
+ let enemies = [];
10
+ let gameOver = false;
11
+
12
+ function startGame() {
13
+ spaceship = new Spaceship();
14
+ bullets = [];
15
+ enemies = [];
16
+ gameOver = false;
17
+ document.getElementById('gameOver').style.display = 'none';
18
+ gameLoop();
19
+ }
20
+
21
+ class Spaceship {
22
+ constructor() {
23
+ this.width = 50;
24
+ this.height = 50;
25
+ this.x = canvas.width / 2 - this.width / 2;
26
+ this.y = canvas.height - this.height - 10;
27
+ this.speed = 5;
28
+ this.image = new Image();
29
+ this.image.src = 'https://via.placeholder.com/50'; // Add spaceship image URL here
30
+ }
31
+
32
+ move(direction) {
33
+ if (direction === 'left' && this.x > 0) {
34
+ this.x -= this.speed;
35
+ } else if (direction === 'right' && this.x + this.width < canvas.width) {
36
+ this.x += this.speed;
37
+ }
38
+ }
39
+
40
+ draw() {
41
+ ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
42
+ }
43
+
44
+ shoot() {
45
+ const bullet = new Bullet(this.x + this.width / 2 - 5, this.y);
46
+ bullets.push(bullet);
47
+ }
48
+ }
49
+
50
+ class Bullet {
51
+ constructor(x, y) {
52
+ this.x = x;
53
+ this.y = y;
54
+ this.width = 10;
55
+ this.height = 20;
56
+ this.speed = 5;
57
+ }
58
+
59
+ move() {
60
+ this.y -= this.speed;
61
+ }
62
+
63
+ draw() {
64
+ ctx.fillStyle = '#00ff99';
65
+ ctx.fillRect(this.x, this.y, this.width, this.height);
66
+ }
67
+ }
68
+
69
+ class Enemy {
70
+ constructor() {
71
+ this.width = 50;
72
+ this.height = 50;
73
+ this.x = Math.random() * (canvas.width - this.width);
74
+ this.y = -50;
75
+ this.speed = 2;
76
+ }
77
+
78
+ move() {
79
+ this.y += this.speed;
80
+ }
81
+
82
+ draw() {
83
+ ctx.fillStyle = '#ff4444';
84
+ ctx.fillRect(this.x, this.y, this.width, this.height);
85
+ }
86
+ }
87
+
88
+ function gameLoop() {
89
+ if (gameOver) {
90
+ document.getElementById('gameOver').style.display = 'block';
91
+ return;
92
+ }
93
+
94
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
95
+
96
+ spaceship.draw();
97
+
98
+ bullets.forEach((bullet, index) => {
99
+ bullet.move();
100
+ bullet.draw();
101
+ if (bullet.y < 0) {
102
+ bullets.splice(index, 1);
103
+ }
104
+ });
105
+
106
+ if (Math.random() < 0.02) {
107
+ enemies.push(new Enemy());
108
+ }
109
+
110
+ enemies.forEach((enemy, index) => {
111
+ enemy.move();
112
+ enemy.draw();
113
+
114
+ if (enemy.y > canvas.height) {
115
+ enemies.splice(index, 1);
116
+ }
117
+
118
+ bullets.forEach((bullet, bulletIndex) => {
119
+ if (
120
+ bullet.x < enemy.x + enemy.width &&
121
+ bullet.x + bullet.width > enemy.x &&
122
+ bullet.y < enemy.y + enemy.height &&
123
+ bullet.y + bullet.height > enemy.y
124
+ ) {
125
+ enemies.splice(index, 1);
126
+ bullets.splice(bulletIndex, 1);
127
+ }
128
+ });
129
+ });
130
+
131
+ requestAnimationFrame(gameLoop);
132
+ }
133
+
134
+ document.addEventListener('keydown', (e) => {
135
+ if (e.key === 'ArrowLeft') {
136
+ spaceship.move('left');
137
+ } else if (e.key === 'ArrowRight') {
138
+ spaceship.move('right');
139
+ } else if (e.key === ' ') {
140
+ spaceship.shoot();
141
+ }
142
+ });
143
+
144
+ startGame();