Spaces:
Running
Running
prithivMLmods
commited on
Commit
•
6f69eeb
1
Parent(s):
d1f39a6
Update index.html
Browse files- index.html +157 -19
index.html
CHANGED
@@ -1,19 +1,157 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Flappy Bird</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
margin: 0;
|
10 |
+
overflow: hidden;
|
11 |
+
display: flex;
|
12 |
+
justify-content: center;
|
13 |
+
align-items: center;
|
14 |
+
height: 100vh;
|
15 |
+
background-color: #70c5ce;
|
16 |
+
}
|
17 |
+
|
18 |
+
canvas {
|
19 |
+
border: 2px solid #333;
|
20 |
+
}
|
21 |
+
</style>
|
22 |
+
</head>
|
23 |
+
<body>
|
24 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
|
25 |
+
<script>
|
26 |
+
let bird;
|
27 |
+
let pipes = [];
|
28 |
+
let gameState = 0; // 0: not started, 1: playing, 2: game over
|
29 |
+
let score = 0;
|
30 |
+
|
31 |
+
function setup() {
|
32 |
+
createCanvas(windowWidth / 2, windowHeight / 1.5);
|
33 |
+
bird = new Bird();
|
34 |
+
pipes.push(new Pipe());
|
35 |
+
}
|
36 |
+
|
37 |
+
function draw() {
|
38 |
+
background(220);
|
39 |
+
|
40 |
+
if (gameState === 1) {
|
41 |
+
for (let i = pipes.length - 1; i >= 0; i--) {
|
42 |
+
pipes[i].show();
|
43 |
+
pipes[i].update();
|
44 |
+
|
45 |
+
if (pipes[i].hits(bird)) {
|
46 |
+
gameState = 2; // Game Over
|
47 |
+
}
|
48 |
+
|
49 |
+
if (pipes[i].offscreen()) {
|
50 |
+
pipes.splice(i, 1);
|
51 |
+
score++;
|
52 |
+
}
|
53 |
+
}
|
54 |
+
|
55 |
+
bird.update();
|
56 |
+
bird.show();
|
57 |
+
|
58 |
+
if (frameCount % 100 === 0) {
|
59 |
+
pipes.push(new Pipe());
|
60 |
+
}
|
61 |
+
} else if (gameState === 0) {
|
62 |
+
fill(0);
|
63 |
+
textAlign(CENTER);
|
64 |
+
text("Press Space to Start", width / 2, height / 2);
|
65 |
+
} else {
|
66 |
+
fill(255, 0, 0);
|
67 |
+
textSize(32);
|
68 |
+
textAlign(CENTER);
|
69 |
+
text("Game Over", width / 2, height / 2);
|
70 |
+
text("Score: " + score, width / 2, height / 2 + 40);
|
71 |
+
}
|
72 |
+
}
|
73 |
+
|
74 |
+
function keyPressed() {
|
75 |
+
if (key === ' ') {
|
76 |
+
if (gameState === 0 || gameState === 2) {
|
77 |
+
pipes = [];
|
78 |
+
bird = new Bird();
|
79 |
+
score = 0;
|
80 |
+
gameState = 1;
|
81 |
+
} else {
|
82 |
+
bird.up();
|
83 |
+
}
|
84 |
+
}
|
85 |
+
}
|
86 |
+
|
87 |
+
class Bird {
|
88 |
+
constructor() {
|
89 |
+
this.y = height / 2;
|
90 |
+
this.x = 64;
|
91 |
+
this.gravity = 0.6;
|
92 |
+
this.lift = -15;
|
93 |
+
this.velocity = 0;
|
94 |
+
this.size = 32;
|
95 |
+
}
|
96 |
+
|
97 |
+
show() {
|
98 |
+
fill(255, 200, 0);
|
99 |
+
ellipse(this.x, this.y, this.size, this.size);
|
100 |
+
}
|
101 |
+
|
102 |
+
update() {
|
103 |
+
this.velocity += this.gravity;
|
104 |
+
this.velocity *= 0.9;
|
105 |
+
this.y += this.velocity;
|
106 |
+
|
107 |
+
if (this.y > height) {
|
108 |
+
this.y = height;
|
109 |
+
this.velocity = 0;
|
110 |
+
}
|
111 |
+
|
112 |
+
if (this.y < 0) {
|
113 |
+
this.y = 0;
|
114 |
+
this.velocity = 0;
|
115 |
+
}
|
116 |
+
}
|
117 |
+
|
118 |
+
up() {
|
119 |
+
this.velocity += this.lift;
|
120 |
+
}
|
121 |
+
}
|
122 |
+
|
123 |
+
class Pipe {
|
124 |
+
constructor() {
|
125 |
+
this.top = random(height / 2);
|
126 |
+
this.bottom = random(height / 2);
|
127 |
+
this.x = width;
|
128 |
+
this.w = 20;
|
129 |
+
this.speed = 3;
|
130 |
+
}
|
131 |
+
|
132 |
+
show() {
|
133 |
+
fill(0, 200, 0);
|
134 |
+
rect(this.x, 0, this.w, this.top);
|
135 |
+
rect(this.x, height - this.bottom, this.w, this.bottom);
|
136 |
+
}
|
137 |
+
|
138 |
+
update() {
|
139 |
+
this.x -= this.speed;
|
140 |
+
}
|
141 |
+
|
142 |
+
offscreen() {
|
143 |
+
return this.x < -this.w;
|
144 |
+
}
|
145 |
+
|
146 |
+
hits(bird) {
|
147 |
+
if (bird.y < this.top || bird.y > height - this.bottom) {
|
148 |
+
if (bird.x > this.x && bird.x < this.x + this.w) {
|
149 |
+
return true;
|
150 |
+
}
|
151 |
+
}
|
152 |
+
return false;
|
153 |
+
}
|
154 |
+
}
|
155 |
+
</script>
|
156 |
+
</body>
|
157 |
+
</html>
|