-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (138 loc) · 4.75 KB
/
Copy pathindex.js
File metadata and controls
155 lines (138 loc) · 4.75 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
const gameCanvas = document.createElement("canvas");
const gameCtx = gameCanvas.getContext("2d");
gameCanvas.width = window.innerWidth;
gameCanvas.height = window.innerHeight;
document.body.appendChild(gameCanvas);
const imageCache = {}; // Cache for loaded images
function loadImage(src) {
return new Promise((resolve, reject) => {
if (imageCache[src]) {
resolve(imageCache[src]);
} else {
const img = new Image();
img.onload = () => {
imageCache[src] = img;
resolve(img);
};
img.onerror = (error) => {
console.error("Error loading image:", error);
reject(error);
};
img.src = src;
}
});
}
(async function() {
const bgPic = await loadImage("images/background.png");
const birdPic = await loadImage("images/bird.png");
const upperPics = await Promise.all([...Array(3)].map(() => loadImage("images/upper.png")));
const lowerPics = await Promise.all([...Array(3)].map(() => loadImage("images/lower.png")));
// Bird properties
const birdState = {
horSpeed: 0,
verSpeed: 0,
horAccel: 0,
verAccel: 300, // Increased vertical acceleration
posX: 50,
posY: gameCanvas.height / 2,
gameScore: 0
};
// Pipes properties
const pipesState = [];
for (let i = 0; i < 3; i++) {
pipesState.push({
upper: { posX: gameCanvas.width + i * (gameCanvas.width / 3), posY: -200 },
lower: { posX: gameCanvas.width + i * (gameCanvas.width / 3), posY: gameCanvas.height - 250 },
pipeSpeed: -300 // Increased pipe speed
});
}
// Key events
const activeKeys = {};
addEventListener("keydown", function(e) {
activeKeys[e.keyCode] = true;
}, false);
addEventListener("keyup", function(e) {
delete activeKeys[e.keyCode];
isFlapped = false;
}, false);
// Reset game
function resetGame() {
birdState.horSpeed = 0;
birdState.verSpeed = 0;
birdState.posX = 50;
birdState.posY = gameCanvas.height / 2;
birdState.gameScore = 0;
for (let i = 0; i < 3; i++) {
pipesState[i].upper.posX = gameCanvas.width + i * (gameCanvas.width / 3);
pipesState[i].lower.posX = gameCanvas.width + i * (gameCanvas.width / 3);
}
}
let isFlapped = false;
const gapBetweenPipes = 100; // Decreased gap between pipes
// Update game objects
function updateGame(modifier) {
birdState.gameScore += modifier;
if (32 in activeKeys && !isFlapped) { // Space key for moving up
birdState.verSpeed = -200; // Increased bird flap strength
isFlapped = true;
}
birdState.posX += birdState.horSpeed * modifier;
birdState.posY += birdState.verSpeed * modifier;
birdState.horSpeed += birdState.horAccel * modifier;
birdState.verSpeed += birdState.verAccel * modifier;
for (let i = 0; i < 3; i++) {
const pipe = pipesState[i];
pipe.upper.posX += pipe.pipeSpeed * modifier;
pipe.lower.posX += pipe.pipeSpeed * modifier;
if (pipe.upper.posX < -50) {
pipe.upper.posX = gameCanvas.width;
pipe.upper.posY = -Math.random() * 200; // Increased upper pipe height
pipe.lower.posX = pipe.upper.posX;
pipe.lower.posY = gameCanvas.height - (pipe.upper.posY + gapBetweenPipes + 200); // Adjusted lower pipe position
}
// Collision detection
if ((pipe.upper.posX < birdState.posX + 50 && pipe.upper.posX + 50 > birdState.posX && birdState.posY < pipe.upper.posY + 250) ||
(pipe.lower.posX < birdState.posX + 50 && pipe.lower.posX + 50 > birdState.posX && birdState.posY + 50 > pipe.lower.posY)) {
resetGame();
}
}
if (birdState.posY > gameCanvas.height || birdState.posY < 0) {
resetGame();
}
}
// Render everything
function renderGame() {
if (bgPic) {
gameCtx.drawImage(bgPic, 0, 0, gameCanvas.width, gameCanvas.height);
}
if (birdPic) {
gameCtx.drawImage(birdPic, birdState.posX, birdState.posY);
}
for (let i = 0; i < 3; i++) {
if (upperPics[i]) {
gameCtx.drawImage(upperPics[i], pipesState[i].upper.posX, pipesState[i].upper.posY, 50, 250); // Increased pipe height
}
if (lowerPics[i]) {
gameCtx.drawImage(lowerPics[i], pipesState[i].lower.posX, pipesState[i].lower.posY, 50, 250); // Increased pipe height
}
}
// Score
gameCtx.fillStyle = "rgb(250, 250, 250)";
gameCtx.font = "24px Helvetica";
gameCtx.textAlign = "left";
gameCtx.textBaseline = "top";
gameCtx.fillText("Score: " + Math.floor(birdState.gameScore), 12, 32);
}
// The main game loop
function mainLoop() {
const now = Date.now();
const delta = now - previousTime;
updateGame(delta / 1000);
renderGame();
previousTime = now;
requestAnimationFrame(mainLoop);
}
let previousTime = Date.now();
resetGame();
mainLoop();
})();