-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloppy-gs.php
More file actions
201 lines (144 loc) · 3.25 KB
/
Copy pathfloppy-gs.php
File metadata and controls
201 lines (144 loc) · 3.25 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
<?php
/**
* Module Name: Floppy GS
* Module ID: floppygs
* Description: Floppy bird game.
* Version: 1.0
* Default W: 4
* Default H: 7
*/
if (!defined('IN_GS')) { die('You cannot load this page directly.'); }
// Add $i18n_m for i18n lang files in Modules
$i18n_m = dash_module_i18n('floppygs');
?>
<style>
.flappy-wrap{
text-align:center;
max-width:340px;
margin:auto;
font-family:sans-serif;
padding-top:35px;
}
canvas{
background:#cfe9ff;
border-radius:6px;
box-shadow:0 3px 8px rgba(0,0,0,.2);
cursor:pointer;
}
</style>
<div class="flappy-wrap">
<canvas id="flappy" width="320" height="420"></canvas>
<p>Click or press Space</p>
</div>
<script>
const canvas = document.getElementById("flappy");
const ctx = canvas.getContext("2d");
/* ---------- SVG BIRD ---------- */
const birdSVG = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60">
<circle cx="30" cy="30" r="20" fill="#ff9800"/>
<circle cx="40" cy="25" r="4" fill="#000"/>
<polygon points="48,30 60,35 48,40" fill="#ff5722"/>
</svg>
`;
const birdImg = new Image();
birdImg.src = "data:image/svg+xml;base64," + btoa(birdSVG);
/* ---------- GAME VARIABLES ---------- */
let birdY, velocity, pipeX, pipeTop, score;
let gravity = 0.45;
let jump = -7;
let gap = 130;
let gameState = "start";
let highScore = localStorage.getItem("flappyHigh") || 0;
/* ---------- INIT ---------- */
function init(){
birdY = 200;
velocity = 0;
pipeX = 320;
pipeTop = Math.random()*180+40;
score = 0;
gameState = "playing";
}
/* ---------- INPUT ---------- */
function flap(){
if(gameState==="start"){
init();
}
else if(gameState==="playing"){
velocity = jump;
}
else if(gameState==="gameover"){
init();
}
}
canvas.addEventListener("click", flap);
document.addEventListener("keydown", e=>{
if(e.code==="Space"){
e.preventDefault();
flap();
}
});
/* ---------- GAME UPDATE ---------- */
function update(){
if(gameState==="playing"){
velocity += gravity;
birdY += velocity;
pipeX -= 2;
if(pipeX < -60){
pipeX = 320;
pipeTop = Math.random()*180+40;
score++;
if(score > highScore){
highScore = score;
localStorage.setItem("flappyHigh", highScore);
}
}
if(
birdY < 0 ||
birdY > 420 ||
(80 > pipeX && 80 < pipeX+60 &&
(birdY < pipeTop || birdY > pipeTop+gap))
){
gameState = "gameover";
}
}
}
/* ---------- DRAW ---------- */
function draw(){
ctx.clearRect(0,0,320,420);
ctx.fillStyle="#cfe9ff";
ctx.fillRect(0,0,320,420);
/* pipes */
ctx.fillStyle="#4caf50";
ctx.fillRect(pipeX,0,60,pipeTop);
ctx.fillRect(pipeX,pipeTop+gap,60,420);
/* bird rotation */
ctx.save();
ctx.translate(80, birdY);
let angle = velocity * 0.05;
if(angle > 0.5) angle = 0.5;
if(angle < -0.5) angle = -0.5;
ctx.rotate(angle);
ctx.drawImage(birdImg, -20, -20, 40, 40);
ctx.restore();
/* score */
ctx.fillStyle="#000";
ctx.font="18px sans-serif";
ctx.fillText("Score: "+score,10,25);
ctx.fillText("Best: "+highScore,10,45);
if(gameState==="start"){
ctx.fillText("Click or Press Space",70,200);
}
if(gameState==="gameover"){
ctx.fillText("Game Over",110,200);
ctx.fillText("Click or Space to restart",60,230);
}
}
/* ---------- LOOP ---------- */
function loop(){
update();
draw();
requestAnimationFrame(loop);
}
loop();
</script>