-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
235 lines (202 loc) · 6.06 KB
/
script.js
File metadata and controls
235 lines (202 loc) · 6.06 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
window.onload = function() {
// Canvas setup
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener("resize", () => {
// For simplicity, restart the game on resize.
resetGame();
resizeCanvas();
});
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
let frames = 0;
let gameSpeed = Math.max(canvasWidth,canvasHeight) / 500;
let gravity = 0.15;
let gameOver = false;
// Load assets.
const backgroundImg = new Image();
backgroundImg.src = "assets/background.png";
const birdUpImg = new Image();
birdUpImg.src = "assets/ghibli_bird_up.png";
const birdDownImg = new Image();
birdDownImg.src = "assets/ghibli_bird_down.png";
const pipeNorthImg = new Image();
pipeNorthImg.src = "assets/pipe_top.png";
const pipeSouthImg = new Image();
pipeSouthImg.src = "assets/pipe_bottom.png";
// Define maximum tilt angles (in radians)
const maxUpAngle = -Math.PI / 10; // about -30°
const maxDownAngle = Math.PI / 1.5; // about 60°
// Bird object definition
const bird = {
x: canvas.width * 0.1,
y: canvas.height * 0.5,
width: 67,
height: 69.8,
velocity: 0,
jump: 4.6,
angle: 0,
flapTime: 0, // number of frames to show wings-up
draw: function() {
ctx.save();
// Move the origin to the center of the bird.
ctx.translate(this.x + this.width / 2, this.y + this.height / 2);
ctx.rotate(this.angle);
// Draw the appropriate bird asset.
if (this.flapTime > 0) {
ctx.drawImage(birdUpImg, -this.width / 2, -this.height / 2, this.width, this.height);
} else {
ctx.drawImage(birdDownImg, -this.width / 2, -this.height / 2, this.width, this.height);
}
ctx.restore();
},
update: function() {
this.velocity += gravity;
this.y += this.velocity;
// Decrease flap timer.
if (this.flapTime > 0) {
this.flapTime--;
}
// If falling, gradually rotate downward.
if (this.velocity > 0 && this.angle < maxDownAngle) {
this.angle += 0.02;
if (this.angle > maxDownAngle) {
this.angle = maxDownAngle;
}
} else {
if (this.angle < maxUpAngle) {
this.angle = maxUpAngle;
}
}
// Game over if bird hits the bottom.
if (this.y + this.height >= canvas.height) {
gameOver = true;
}
if(this.y <= 0) {
this.y = 0;
}
},
flap: function() {
this.velocity = -this.jump;
this.angle = maxUpAngle; // tilt upward immediately on flap
this.flapTime = 10; // show wings-up for 10 frames
}
};
// Pipe constructor (each pipe pair is one object)
function Pipe() {
this.gap = canvasHeight/3; // vertical gap between top and bottom pipes
this.top = Math.random() * (canvasHeight / 2);
this.bottom = canvasHeight - (this.top + this.gap);
this.x = canvasWidth;
this.width = 50;
this.speed = gameSpeed;
this.passed = false;
this.draw = function() {
// Draw top pipe
ctx.drawImage(pipeNorthImg, this.x, 0, this.width, this.top);
// Draw bottom pipe
ctx.drawImage(pipeSouthImg, this.x, canvasHeight - this.bottom, this.width, this.bottom);
};
this.update = function() {
this.x -= this.speed;
};
}
let pipes = [];
let pipeInterval = canvas.width < 800 ? canvas.width / 2 : 300;
let score = 0;
// Event listeners for controls
document.addEventListener("keydown", function(e) {
if (e.code === "Space" || e.key === " ") {
bird.flap();
if (gameOver) {
resetGame();
}
}
});
canvas.addEventListener("click", function() {
bird.flap();
if (gameOver) {
resetGame();
}
});
canvas.addEventListener("touchstart", function() {
bird.flap();
if (gameOver) {
resetGame();
}
});
// Reset the game state
function resetGame() {
bird.y = 150;
bird.velocity = 0;
bird.angle = 0;
pipes = [];
frames = 0;
score = 0;
gameOver = false;
draw();
}
// Collision detection between bird and pipes
function checkCollision(pipe) {
// Check horizontal collision and vertical overlap with top or bottom pipe
if (bird.x < pipe.x + pipe.width &&
bird.x + bird.width > pipe.x &&
(bird.y < pipe.top || bird.y + bird.height > canvasHeight - pipe.bottom)) {
return true;
}
return false;
}
// Main game loop
function draw() {
// Draw the background
ctx.drawImage(backgroundImg, 0, 0, canvasWidth, canvasHeight);
// Create new pipes at regular intervals
if (frames % pipeInterval === 0) {
pipes.push(new Pipe());
}
// Loop through pipes to draw, update, and check collisions
for (let i = 0; i < pipes.length; i++) {
let pipe = pipes[i];
pipe.draw();
pipe.update();
// Collision check
if (checkCollision(pipe)) {
gameOver = true;
}
// Score update: bird passed the pipe if its right side is behind the bird
if (!pipe.passed && pipe.x + pipe.width < bird.x) {
pipe.passed = true;
score++;
}
}
// Remove pipes that have gone off-screen
pipes = pipes.filter(pipe => pipe.x + pipe.width > 0);
// Update and draw the bird
bird.update();
bird.draw();
// Draw the score
ctx.fillStyle = "#FFF";
ctx.font = "20px Arial";
ctx.fillText("Score: " + score, 10, 25);
// Game over screen
if (gameOver) {
ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
ctx.fillStyle = "#FFF";
ctx.font = "40px Arial";
ctx.fillText("Game Over", canvasWidth / 2 - 100, canvasHeight / 2);
ctx.font = "20px Arial";
ctx.fillText("Press Space or Click to Restart", canvasWidth / 2 - 140, canvasHeight / 2 + 40);
return;
}
frames++;
requestAnimationFrame(draw);
}
// Start the game loop
draw();
};