-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
143 lines (123 loc) · 3.69 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let spaceship = { x: canvas.width / 2 - 25, y: canvas.height - 80, width: 50, height: 50, color: 'cyan' };
let bullets = [];
let enemies = [];
let stars = [];
let score = 0;
let lives = 3;
let gameRunning = true;
// Background stars
function createStars() {
for (let i = 0; i < 100; i++) {
stars.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
size: Math.random() * 2 + 1,
speed: Math.random() * 1 + 0.5
});
}
}
createStars();
function drawStars() {
stars.forEach(star => {
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(star.x, star.y, star.size, 0, Math.PI * 2);
ctx.fill();
star.y += star.speed;
if (star.y > canvas.height) {
star.y = 0;
star.x = Math.random() * canvas.width;
}
});
}
// Controls
window.addEventListener('keydown', (e) => {
if (!gameRunning) return;
if (e.key === 'ArrowLeft' && spaceship.x > 0) spaceship.x -= 20;
if (e.key === 'ArrowRight' && spaceship.x + spaceship.width < canvas.width) spaceship.x += 20;
if (e.key === ' ') bullets.push({ x: spaceship.x + spaceship.width / 2 - 2.5, y: spaceship.y, width: 5, height: 10 });
});
// Spawn enemies
function spawnEnemies() {
if (Math.random() < 0.03) {
let size = Math.random() * 30 + 20;
enemies.push({
x: Math.random() * (canvas.width - size),
y: -size,
size: size,
speed: Math.random() * 2 + 1,
color: 'red'
});
}
}
// Draw and update enemies
function drawEnemies() {
enemies.forEach((enemy, index) => {
enemy.y += enemy.speed;
ctx.fillStyle = enemy.color;
ctx.beginPath();
ctx.arc(enemy.x, enemy.y, enemy.size / 2, 0, Math.PI * 2);
ctx.fill();
if (enemy.y > canvas.height) {
enemies.splice(index, 1);
lives--;
if (lives <= 0) gameOver();
}
});
}
// Check for collisions
function checkCollisions() {
bullets.forEach((bullet, bIndex) => {
enemies.forEach((enemy, eIndex) => {
if (
bullet.x < enemy.x + enemy.size / 2 &&
bullet.x > enemy.x - enemy.size / 2 &&
bullet.y < enemy.y + enemy.size / 2 &&
bullet.y > enemy.y - enemy.size / 2
) {
bullets.splice(bIndex, 1);
enemies.splice(eIndex, 1);
score += 10;
}
});
});
}
// Game over
function gameOver() {
gameRunning = false;
alert(`Game Over! Final Score: ${score}`);
document.location.reload();
}
// Game loop
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background stars
drawStars();
// Draw spaceship
ctx.fillStyle = spaceship.color;
ctx.fillRect(spaceship.x, spaceship.y, spaceship.width, spaceship.height);
// Draw bullets
bullets.forEach((bullet, index) => {
bullet.y -= 10;
if (bullet.y < 0) bullets.splice(index, 1);
ctx.fillStyle = 'yellow';
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
});
// Spawn and draw enemies
spawnEnemies();
drawEnemies();
// Check collisions
checkCollisions();
// Draw score and lives
ctx.fillStyle = 'white';
ctx.font = '20px Arial';
ctx.fillText(`Score: ${score}`, 10, 30);
ctx.fillText(`Lives: ${lives}`, 10, 60);
requestAnimationFrame(gameLoop);
}
gameLoop();