-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
318 lines (287 loc) · 10.8 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<meta charset="utf-8"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
.noselect {
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Safari */
-khtml-user-select: none; /* Konqueror HTML */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently
supported by Chrome and Opera */
}
.bttonpress{
background: gray;
}
.btnnormal{
background: gold;
}
</style>
<title>Angry Smily | Game by @SonuAuti</title>
</head>
<body onload="startGame()" style="text-align: center;">
<script>
var myGamePiece;
var myObstacles = [];
var myScore;
var timer;
var scoreCount=0;
var w = window.innerWidth;
var h = window.innerHeight;
if (w>500) {w=500;}
if (h>400) {h=400;}
var cwidth=w;
window.AudioContext = window.AudioContext || window.webkitAudioContext;
window.actx = new window.AudioContext();
//create nodes
window.osc=""; //create in event listener so we can press the button more than once
window.masterGain = window.actx.createGain();
window.analyser = window.actx.createAnalyser();
//console.log(w);
function startGame() {
setupControl();
setupSound();
myGamePiece = new component(30, 30, "smiley.gif", 10, 120, "image");
//myGamePiece = new component(30, 30, "gold", 10, 120);
myGamePiece.gravity = 0.05;
myScore = new component("24px", "Consolas", "red", cwidth-80, 30, "text");
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = cwidth;
this.canvas.height = h-100;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
timer=this.interval;
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 0;
this.gravitySpeed = 0;
this.mycolor=color;
if (type == "image") {
this.image = new Image();
this.image.src = this.mycolor;
}
this.update = function() {
ctx = myGameArea.context;
if (type == "image") {
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
}else if (this.type == "text") {
ctx.font = this.width + " " + this.height;
ctx.fillStyle = this.mycolor;
ctx.fillText(this.text, this.x, this.y);
}else if(type!="image" && type!="text") {
ctx.fillStyle = this.mycolor;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
if (Math.abs(this.y)<500) {
this.y += this.speedY + this.gravitySpeed;
}else{
this.y = this.speedY + this.gravitySpeed;
}
//console.log(this.x+","+this.y);
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = 0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
}
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
//console.log("crashed !");
if(myObstacles[i].mycolor=="pink" || myObstacles[i].mycolor=="gold"){
scoreCount++;
if (myObstacles[i].height<=27) {
scoreCount++;
}
console.log(myObstacles[i].height);
//console.log('got credit');
}else{
playsound();
myGamePiece.image.src="angry.gif";
myGamePiece.update();
clearInterval(timer);
showfinalScore(scoreCount*10);
return;
}
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(100)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 180;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 60;
maxGap = 180;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
topbar="yellowgreen";
bottombar="pink";
if (height>(x - height - gap)) {
topbar="yellowgreen";
bottombar="pink";
}else{
topbar="pink";
bottombar="yellowgreen";
}
var nh=(x-height-gap);
if (height<=27 && topbar=="pink") {
topbar="gold"
}else if(nh<=27 && bottombar=="pink"){bottombar="gold";}
myObstacles.push(new component(10, height, topbar, x, 0));
myObstacles.push(new component(10, nh, bottombar, x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}
myScore.text=""+scoreCount*10; //""+myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
function showfinalScore(score){
//gameover
// myGameArea.clear();
myScore.text="Game Over";
myScore.width = "40px";
myScore.x = myScore.x/3;
myScore.y = myScore.x;
myScore.mycolor="red";
// myGameArea.clear();
myScore.update();
myScore.text=""+score;
myScore.x = myScore.x;
myScore.y = myScore.y+40;
myScore.mycolor="red";
myScore.update();
//update highest score
console.log(score);
if (!window.localStorage['myScore']) {
window.localStorage['myScore']=score;
}else if(parseInt(window.localStorage['myScore'])<score){
window.localStorage['myScore']=score;
}
//console.log("your score "+window.localStorage['myScore']);
var sharelink='<a class="noselect" style="text-decoration:none;font-size:14px;color:gray;" href="whatsapp://send?text=I scored '+window.localStorage['myScore']+' playing angry smily at http://bit.ly/AngrySmily" data-action="share/whatsapp/share">share on WhatsApp</a>';
document.getElementById("lastscore").innerHTML="Your last high "+window.localStorage['myScore'] +" · "+sharelink;
stopsound();
}
function accelerate(n) {
myGamePiece.gravity = n;
}
function resetGame(){
clearInterval(timer);
scoreCount=0;
myObstacles = [];
startGame();
}
function setupControl(){
var actionBtn=document.getElementById("actionbtn");
// Add event listeners
actionBtn.addEventListener("mousedown", onPointerDown);
actionBtn.addEventListener("pointerdown", onPointerDown);
actionBtn.addEventListener("touchstart", onPointerDown);
actionBtn.addEventListener("mouseup", onPointerUp);
actionBtn.addEventListener("pointerup", onPointerUp);
actionBtn.addEventListener("touchend", onPointerUp);
function onPointerDown(event){
accelerate(-0.2);
actionBtn.style.background="gray";
}
function onPointerUp(event){
accelerate(0.05);
actionBtn.style.background="gold";
}
}
function setupSound(){
//routing
window.masterGain.connect(window.analyser);
window.analyser.connect(window.actx.destination);
window.osc = window.actx.createOscillator();
window.osc.frequency.value = 500;
imag=new Float32Array([4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,22,22,22,23]);
var real = new Float32Array(imag.length); // cos
var customWave = window.actx.createPeriodicWave(real, imag); // cos,sine
osc.setPeriodicWave(customWave);
}
function playsound(){
window.osc.connect(masterGain);
if(window.osc){
window.osc.start(0);
isPlaying = true;
}
}
function stopsound(){
if (isPlaying && osc) {
setTimeout(function(){
window.osc.stop(0);
},500);
}
}
</script>
<br>
<div id="lastscore" style="text-align: center;margin-bottom: 4px;"></div>
<button id="actionbtn" class="noselect" style="text-decoration: none; width: 160px;height: 40px;font-size: 20px;background: gold;color: #fff;border: none;border-radius: 12px;">JUMP</button>
<button onclick="resetGame()" class="noselect" style="text-decoration: none; width: 160px;height: 40px;background:#FF0101; font-size: 20px;color: #fff;border: none;border-radius: 12px;">RESET</button>
<p style="text-align: center;">Tap and hold jump button to stay in air, hit pink bar to get points, enjoy !</p>
<p style="text-align: center;font-size: 12px;">Inspired and derived from w3c, modified with <span style="font-size:16px;color: #ff0000;">♥</span> by <a style="text-decoration: none;" href="https://www.instagram.com/matki_paw/">@sonuauti</a></p>
</body>
</html>