| 1 |
<!DOCTYPE html> |
| 2 |
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"> |
| 3 |
<TITLE>Jumps</TITLE> |
| 4 |
<html><head><link rel="stylesheet" href="../styles.css"></head> |
| 5 |
<body onkeydown="keydown(event)" onblur="needPause()" onload="startGame()"> |
| 6 |
<div class="topnav" id="myTopnav"></div><div class="main"> |
| 7 |
|
| 8 |
<h2>"Jumps"</h2> |
| 9 |
|
| 10 |
<p>Max result: <span id="hiscore">0</span>.</p> |
| 11 |
<canvas id="myCanvas" width="500" height="400" onmousedown="mousedown(event)" onmouseup="accelerate(0.05)"> |
| 12 |
Your browser does not support the HTML5 canvas tag.</canvas></div> |
| 13 |
|
| 14 |
<script type="text/javascript" src="../accordion.js"></script> |
| 15 |
<script> |
| 16 |
var ctx; |
| 17 |
var width = 500; |
| 18 |
var height = 300; |
| 19 |
var obstacles = []; |
| 20 |
var bX, bY, vX, vY; |
| 21 |
var gravity; |
| 22 |
var time; |
| 23 |
var interval; |
| 24 |
var hiscore = 0; |
| 25 |
var paused = 0; |
| 26 |
var finished = 0; |
| 27 |
|
| 28 |
const bR=15; |
| 29 |
const minGap = 50; |
| 30 |
const maxGap = 200; |
| 31 |
|
| 32 |
function startGame() |
| 33 |
{ |
| 34 |
ctx = document.getElementById("myCanvas").getContext("2d"); |
| 35 |
ctx.lineCap="round"; |
| 36 |
vX = 0; bX = bR; |
| 37 |
vY = 0; bY = height/2; |
| 38 |
gravity = 0.05; time = 0; |
| 39 |
paused = 0; finished = 0; |
| 40 |
obstacles = []; |
| 41 |
hiscore = localStorage.getItem("hiscoreJumps"); |
| 42 |
if(!hiscore) hiscore = 0; |
| 43 |
document.getElementById("hiscore").innerHTML = hiscore; |
| 44 |
ctx.clearRect(0, 0, width, height); |
| 45 |
interval = setInterval(updateCanvas, 20); |
| 46 |
} |
| 47 |
|
| 48 |
function finish(score) // game over |
| 49 |
{ |
| 50 |
finished = 1; draw(); |
| 51 |
ctx.font="40px Arial"; |
| 52 |
ctx.fillStyle = 'black'; |
| 53 |
ctx.fillText("GAME OVER", width/4, height/2); |
| 54 |
if(score > hiscore) |
| 55 |
{ hiscore = score; |
| 56 |
localStorage.setItem("hiscoreJumps", hiscore); } |
| 57 |
document.getElementById("hiscore").innerHTML = hiscore; |
| 58 |
clearInterval(interval); |
| 59 |
} |
| 60 |
|
| 61 |
function updateCanvas() |
| 62 |
{ |
| 63 |
if(paused) { draw(); return; } |
| 64 |
// check crashes |
| 65 |
for(var i = 0; i < obstacles.length; i += 1) |
| 66 |
if(crash(obstacles[i])) { finish(time); return; } |
| 67 |
time = time + 1; |
| 68 |
if(time%100==0) // new obstacle(s) |
| 69 |
{ |
| 70 |
if(Math.random()<0.5) // one obstacle |
| 71 |
{ |
| 72 |
var y1 = Math.floor(Math.random()*(maxGap-minGap)+minGap); |
| 73 |
var y2 = height - Math.floor(Math.random()*(maxGap-minGap)+minGap); |
| 74 |
if(y2<y1) { var y=y1; y1=y2; y2=y; } |
| 75 |
if(y2<y1+bR) { y2 += bR; y1 -= bR; } |
| 76 |
obstacles.push([width,y1,y2]); |
| 77 |
} |
| 78 |
else // 2 obstacles (one hole) |
| 79 |
{ |
| 80 |
var gap = Math.floor(Math.random()*(maxGap-minGap)+minGap); |
| 81 |
var y0 = Math.floor(Math.random()*height); |
| 82 |
var y1 = y0-gap/2, y2 = y0+gap/2; |
| 83 |
if(y1 > bR/2) |
| 84 |
obstacles.push([width,0,y1]); |
| 85 |
if(y2 < height-bR/2) |
| 86 |
obstacles.push([width,y2,height]); |
| 87 |
} |
| 88 |
} |
| 89 |
// move ball |
| 90 |
bX += vX; bY += vY; vY += gravity; |
| 91 |
// check boundaries |
| 92 |
if(bX < bR) { bX = bR; vX = 0; } |
| 93 |
if(bX > width-bR) { bX = width-bR; vX = 0; } |
| 94 |
if(bY < bR) { bY = bR; vY = 0; } |
| 95 |
if(bY > height-bR) { bY = height-bR; vY = 0; } |
| 96 |
// move obstacles |
| 97 |
for (var i = 0;i < obstacles.length; i += 1) obstacles[i][0] -= 1; |
| 98 |
// draw it |
| 99 |
draw(); |
| 100 |
} |
| 101 |
|
| 102 |
function draw() |
| 103 |
{ |
| 104 |
cwidth = ctx.canvas.width; |
| 105 |
cheight = ctx.canvas.height; |
| 106 |
var s = cwidth/width; |
| 107 |
ctx.clearRect(0, 0, cwidth, cheight); |
| 108 |
ctx.strokeStyle = 'black'; |
| 109 |
ctx.strokeRect(0, 0, s*width, s*height); |
| 110 |
|
| 111 |
ctx.font="12px Arial"; // score |
| 112 |
text = "SCORE: " + time; |
| 113 |
ctx.fillStyle = 'black'; |
| 114 |
ctx.fillText(text, 2*bR*s, bR*s); |
| 115 |
|
| 116 |
var x = bX*s, y = bY*s, r = bR*s; // ball |
| 117 |
var grd=ctx.createRadialGradient(x-0.3*r,y-r/5,r/10,x,y,r); |
| 118 |
grd.addColorStop(0,"white"); |
| 119 |
grd.addColorStop(1,"red"); |
| 120 |
ctx.beginPath(); ctx.fillStyle = grd; |
| 121 |
ctx.arc(x, y, r, 0, 2*Math.PI); ctx.fill(); |
| 122 |
|
| 123 |
ctx.fillStyle = 'green'; // obstacles |
| 124 |
for (var i = 0;i < obstacles.length; i += 1) |
| 125 |
{ |
| 126 |
var o = obstacles[i]; |
| 127 |
ctx.fillRect((o[0]-bR/10)*s, o[1]*s, bR/5*s, (o[2]-o[1])*s); |
| 128 |
} |
| 129 |
|
| 130 |
if(cheight > s*height) // buttons |
| 131 |
{ |
| 132 |
var dh = cheight - s*height, b = dh/10; |
| 133 |
var y0 = b+s*height, x0 = s*width/3; |
| 134 |
|
| 135 |
if(gravity<0) // button Up |
| 136 |
{ |
| 137 |
ctx.fillStyle = 'lightblue'; |
| 138 |
ctx.fillRect(b, y0, x0-2*b, dh-2*b); |
| 139 |
} |
| 140 |
ctx.strokeStyle = 'blue'; |
| 141 |
ctx.strokeRect(b, y0, x0-2*b, dh-2*b); |
| 142 |
ctx.beginPath(); ctx.fillStyle = 'blue'; |
| 143 |
ctx.moveTo(x0/4,y0-b+3*dh/4); |
| 144 |
ctx.lineTo(3*x0/4,y0-b+3*dh/4); |
| 145 |
ctx.lineTo(x0/2,y0-b+dh/4); ctx.fill(); |
| 146 |
|
| 147 |
if(finished) // button New |
| 148 |
{ |
| 149 |
ctx.strokeStyle = 'red'; |
| 150 |
ctx.strokeRect(b+x0, y0, x0-2*b, dh-2*b); |
| 151 |
ctx.beginPath(); ctx.fillStyle = 'red'; |
| 152 |
ctx.moveTo(x0+x0/4,y0-b+dh/4); |
| 153 |
ctx.lineTo(x0+x0/4,y0-b+3*dh/4); |
| 154 |
ctx.lineTo(x0+x0/2,y0-b+dh/2); ctx.fill(); |
| 155 |
ctx.moveTo(x0+x0/2,y0-b+dh/4); |
| 156 |
ctx.lineTo(x0+x0/2,y0-b+3*dh/4); |
| 157 |
ctx.lineTo(x0+3*x0/4,y0-b+dh/2); ctx.fill(); |
| 158 |
} |
| 159 |
else // button Pause |
| 160 |
{ |
| 161 |
if(paused) |
| 162 |
{ |
| 163 |
ctx.fillStyle = 'pink'; |
| 164 |
ctx.fillRect(b+x0, y0, x0-2*b, dh-2*b); |
| 165 |
} |
| 166 |
ctx.strokeStyle = 'red'; |
| 167 |
ctx.strokeRect(b+x0, y0, x0-2*b, dh-2*b); |
| 168 |
ctx.beginPath(); ctx.fillStyle = 'red'; |
| 169 |
ctx.rect(x0+x0/4,y0-b+dh/4,x0/8,dh/2); |
| 170 |
ctx.rect(x0+5*x0/8,y0-b+dh/4,x0/8,dh/2); |
| 171 |
ctx.fill(); |
| 172 |
} |
| 173 |
|
| 174 |
if(gravity>0.05) // button Down |
| 175 |
{ |
| 176 |
ctx.fillStyle = 'palegreen'; |
| 177 |
ctx.fillRect(2*x0+b, y0, x0-2*b, dh-2*b); |
| 178 |
} |
| 179 |
ctx.strokeStyle = 'green'; |
| 180 |
ctx.strokeRect(2*x0+b, y0, x0-2*b, dh-2*b); |
| 181 |
ctx.beginPath(); ctx.fillStyle = 'green'; |
| 182 |
ctx.moveTo(2*x0+x0/4,y0-b+dh/4); |
| 183 |
ctx.lineTo(2*x0+3*x0/4,y0-b+dh/4); |
| 184 |
ctx.lineTo(2*x0+x0/2,y0-b+3*dh/4); ctx.fill(); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
function crash(o) // return 1 if crash on an obstacle |
| 189 |
{ |
| 190 |
const xx = o[0], y1 = o[1], y2 = o[2]; |
| 191 |
if((bX-xx)*(bX-xx)+(bY-y1)*(bY-y1)<=bR*bR) return 1; |
| 192 |
if((bX-xx)*(bX-xx)+(bY-y2)*(bY-y2)<=bR*bR) return 1; |
| 193 |
if(Math.abs(bX-xx)<=bR && bY>y1 && bY<y2) return 1; |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
function accelerate(n) |
| 198 |
{ |
| 199 |
gravity = n; |
| 200 |
} |
| 201 |
|
| 202 |
function keydown(event) // key is pressed |
| 203 |
{ |
| 204 |
if(event.key == 'ArrowUp' || event.key == ' ') gravity = -0.2; |
| 205 |
else if(event.key == 'ArrowDown') gravity = 0.2; |
| 206 |
else gravity = 0.05; |
| 207 |
} |
| 208 |
|
| 209 |
function mousedown(event) // mouse button is pressed (default Up) |
| 210 |
{ |
| 211 |
cwidth = ctx.canvas.width; |
| 212 |
cheight = ctx.canvas.height; |
| 213 |
var s = cwidth/width; |
| 214 |
if(event.offsetY>s*height && event.offsetX>cwidth*2/3) |
| 215 |
gravity = 0.2; |
| 216 |
else if(event.offsetY>s*height && event.offsetX>cwidth/3) |
| 217 |
{ |
| 218 |
if(finished) startGame(); |
| 219 |
else paused = 1-paused; |
| 220 |
} |
| 221 |
else gravity = -0.2; |
| 222 |
} |
| 223 |
|
| 224 |
function pause() // useless |
| 225 |
{ |
| 226 |
paused = 1-paused; |
| 227 |
} |
| 228 |
</script> |
| 229 |
</body> |
| 230 |
</html> |