-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (127 loc) · 3.7 KB
/
script.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
144
145
146
147
148
149
150
151
152
153
154
155
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Adjust canvas size for mobile
canvas.width = window.innerWidth; // Set canvas width to full screen width
canvas.height = window.innerHeight; // Set canvas height to full screen height
// Load images
const backgroundImg = new Image();
backgroundImg.src = "https://i.postimg.cc/VNwR8YjS/Background.png";
const birdImg = new Image();
birdImg.src = "https://i.postimg.cc/bJN9Z3Vp/Bird.png";
const pipeImg = new Image();
pipeImg.src = "https://i.postimg.cc/pdBBVgZs/Pipe.png";
const birdWidth = 40;
const birdHeight = 40;
let gravity = 0.4; // Increased gravity for a faster fall
const lift = -6; // Further reduced lift to make jumps smaller
let birdY = canvas.height / 2;
let birdVelocity = 0;
const pipes = [];
const pipeWidth = 100;
const pipeGap = 140; // Gap between pipes, increased to make room for the bird
let pipeSpeed = 3; // Increased pipe speed to make the game a little faster
let score = 0;
let gameOver = false;
let lastTime = 0;
const frameRate = 1000/ 60; // Targeting ~60 frames per second
// Listen for touch events to make the bird fly
document.addEventListener('touchstart', flapBird);
document.addEventListener('keydown', flapBird); // For keyboard support
function flapBird(event) {
if (!gameOver) {
birdVelocity = lift;
}
// Prevent page scrolling on mobile
if (event.preventDefault) {
event.preventDefault();
}
}
function drawBackground() {
ctx.drawImage(backgroundImg, 0, 0, canvas.width, canvas.height);
}
function drawBird() {
birdVelocity += gravity;
birdY += birdVelocity;
ctx.drawImage(birdImg, 50, birdY, birdWidth, birdHeight);
}
function createPipes() {
if (pipes.length === 0 || pipes[pipes.length - 1].x < canvas.width - 300) {
const pipeHeight = Math.floor(Math.random() * (canvas.height - pipeGap - 20));
const bottomPipeY = pipeHeight + pipeGap;
if (bottomPipeY < canvas.height - 20) {
pipes.push({ x: canvas.width, top: pipeHeight, bottom: bottomPipeY });
}
}
}
function drawPipes() {
pipes.forEach((pipe, index) => {
pipe.x -= pipeSpeed;
// Draw top pipe
ctx.drawImage(pipeImg, pipe.x, 0, pipeWidth, pipe.top);
// Draw bottom pipe
ctx.drawImage(pipeImg, pipe.x, pipe.bottom, pipeWidth, canvas.height - pipe.bottom);
if (pipe.x + pipeWidth < 0) {
pipes.splice(index, 1);
score++;
}
});
}
function checkCollisions() {
if (birdY < 0 || birdY + birdHeight > canvas.height) {
gameOver = true;
}
pipes.forEach(pipe => {
if (
50 + birdWidth > pipe.x &&
50 < pipe.x + pipeWidth &&
(birdY < pipe.top || birdY + birdHeight > pipe.bottom)
) {
gameOver = true;
}
});
}
function drawScore() {
ctx.fillStyle = 'black';
ctx.font = '20px Arial';
ctx.fillText('Score: ' + score, 10, 30);
}
function resetGame() {
birdY = canvas.height / 2;
birdVelocity = 0;
pipes.length = 0;
score = 0;
gameOver = false;
}
function gameLoop(timestamp) {
if (gameOver) {
ctx.fillStyle = 'black';
ctx.font = '30px Arial';
ctx.fillText('Game Over! Tap to Restart', 50, canvas.height / 2);
return;
}
if (timestamp - lastTime > frameRate) {
lastTime = timestamp;
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBackground();
drawBird();
createPipes();
drawPipes();
checkCollisions();
drawScore();
}
requestAnimationFrame(gameLoop);
}
// Touch or keyboard to restart game
document.addEventListener('touchstart', function () {
if (gameOver) {
resetGame();
gameLoop();
}
});
document.addEventListener('keydown', function (event) {
if (event.keyCode === 32 && gameOver) {
resetGame();
gameLoop();
}
});
gameLoop(0);