-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
177 lines (154 loc) · 4.56 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>Flarpy Blurb 1.4</title>
</head>
<body>
<div id="gameContainer">
<div id="bird"></div>
<div class="pipe" id="pipeTop"></div>
<div class="pipe" id="pipeBottom"></div>
<div id="tooltip" class="tooltip">Ouch!</div>
</div>
<script>
const bird = document.getElementById("bird");
const pipeTop = document.getElementById("pipeTop");
const pipeBottom = document.getElementById("pipeBottom");
const tooltip = document.getElementById("tooltip");
let birdTop = 200;
let gravity = 2;
let pipeLeft = 400;
let pipeHeight = Math.random() * (window.innerHeight - 200) + 50; // viewable area height
document.addEventListener("keydown", flap);
document.addEventListener("touchstart", handleTouchStart);
let touchStartX = 0;
let touchStartY = 0;
function flap() {
birdTop -= 50;
bird.style.top = birdTop + "px";
updateTooltipPosition();
}
function handleTouchStart(event) {
if (event.touches.length === 1) {
const touch = event.touches[0];
touchStartX = touch.clientX;
touchStartY = touch.clientY;
}
}
document.addEventListener(
"touchmove",
function (event) {
if (event.touches.length === 1) {
event.preventDefault();
}
},
{ passive: false }
);
document.addEventListener("touchend", function (event) {
if (event.changedTouches.length === 1) {
const touch = event.changedTouches[0];
const distX = touch.clientX - touchStartX;
const distY = touch.clientY - touchStartY;
if (Math.abs(distX) < 10 && Math.abs(distY) < 10) {
flap();
}
}
});
function updateTooltipPosition() {
tooltip.style.top = birdTop + "px";
tooltip.style.left = bird.offsetLeft + bird.offsetWidth + 10 + "px"; // Adjust as needed
}
function gameLoop() {
birdTop += gravity;
bird.style.top = birdTop + "px";
updateTooltipPosition();
pipeLeft -= 5;
pipeTop.style.left = pipeBottom.style.left = pipeLeft + "px";
if (pipeLeft <= -50) {
pipeLeft = window.innerWidth; // viewable area width
pipeHeight = Math.random() * (window.innerHeight - 200) + 50; // viewable area height
}
pipeTop.style.height = pipeHeight + "px";
pipeBottom.style.height = window.innerHeight - pipeHeight - 100 + "px"; // height of bottom pipe
if (birdTop <= 0 || birdTop >= window.innerHeight - 30) {
gameOver();
}
// collision
if (
(birdTop <= pipeHeight ||
birdTop + 30 >= window.innerHeight - pipeHeight - 100) &&
pipeLeft <= 90 &&
pipeLeft + 50 >= 50
) {
console.log("Collision detected!"); // Check if collision is detected
showTooltip();
gameOver();
}
requestAnimationFrame(gameLoop);
}
function showTooltip() {
console.log("Showing tooltip..."); // Check if the function is called
tooltip.style.visibility = "visible";
setTimeout(() => {
tooltip.style.visibility = "hidden";
}, 400); // Hide the tooltip after 2 seconds
}
function gameOver() {
showTooltip();
//alert("Game Over!");
birdTop = 200;
bird.style.top = birdTop + "px";
}
gameLoop();
</script>
<style>
body,
html {
margin: 0;
padding: 0;
overflow: hidden;
width: 100%;
height: 100%;
}
#gameContainer {
position: relative;
width: 100%;
height: 100%;
}
#bird {
position: absolute;
top: 50%;
left: 50px;
width: 40px;
height: 30px;
background-color: #c6ab12;
border-radius: 50%;
transition: top 0.3s;
}
.pipe {
position: absolute;
width: 50px;
background-color: #56aa67;
}
#pipeTop {
top: 0;
}
#pipeBottom {
bottom: 0;
}
.tooltip {
position: absolute;
background-color: #ffffff;
border: 2px solid #000;
padding: 10px;
border-radius: 10px;
visibility: hidden;
}
</style>
</body>
</html>