a=document.body.children[0],b=a.getContext("2d"),c=Math.floor,d=innerWidth,e=a.height=innerHeight-12,f=800,g=d/f,h=e/g,j=40,k=["A5.1VEg254m6","A5..UoS201XU","41UfpSMDAg2+","MC05WlC45WM2"],l=1,m=65537;function p(){l=parseInt(prompt("Level",l)),o=0;var e=n=75,q=l,r=[],s=0,t=0,u=4,v=50,w=0,x=0,y=i=0;function F(){return(q=q*e%m)/m}function H(a,c,d,e){b.fillRect(a*g,c*g,d*g,e*g)}function I(){B=(new Date).getTime(),a.width=d,A=A?(B-A)/10:0,u+=4e-3*A,t=s,s+=u*A,v+=u*A,E=D,D=r[c(v/j)],z<D&&(v-=u*A,D=E),w=o?z==D?2:w-.015*A:w>0?0:w-.05*A,z>=D&&z+w*A<D?(z=D,w=0,v+=.05*A*A,x+=.07*A,x=x%2):(z+=w*A,x=w>0?2.8:3.8),i=y;while(i--)H(i*j-s,h,j,-r[i]);C=k[c(x)],i=72;while(i)i--%6===0&&(D=C.charCodeAt(i/6),E=D<44?0:D<47?1:D<58?D-46:D<91?D-53:D-59),E%2&&H(v-s-7+i%7,h-z-10+c(i/7),1,1),E>>>=1;H(5,5,s*f/y/j,5),v>y*j+j?(++l,p()):v<s?p():(setTimeout(I,15-A*10),A=B)}while(n){D=n--==e?30:c(F()*(n%2?4:9))+(n%2?2:5),E=n%2?0:9+F()*80+l,y+=D;while(D--)r[i+D]=c(E);i=y}z=r[0],A=0,I()}onkeydown=onkeyup=function(a){o=a.type[5]},setTimeout(p,1)
run = function() {
var
canvas = document.body.children[0],
ctx = canvas.getContext('2d'),
floor = Math.floor,
W = innerWidth, // canvas width
H = canvas.height = innerHeight-12, // canvas height
CW = 800,
SW = W / CW, // scale for virtual canvas width. canvas is scaled so that pixel width = W / 800
CH = H / SW, // transformed height
U = 40, // width of world blocks
sprites = ['A5.1VEg254m6','A5..UoS201XU','41UfpSMDAg2+','MC05WlC45WM2'],// anim frames (second last frame is jump up, last frame is jump down)
K = 1, // level selection
rand1 = 65537, // part of Park-Miller random number generator
jump; // boolean, are we jumping?
// Scale canvas
//ctx.scale(W / CW, W / CW);
function G() {
K = parseInt(prompt('Lvl', K));
jump = 0;
var
N = n = 75, // iteration variable - no of generatings
L = K, // level
terrain = [], // terrain heightmap
xCamera = 0, // camera x position in world coords
lastXCamera = 0,
vxCamera = 4, // camera x velocity
xPlayer = 50, // player x position
vyPlayer = 0, // player vertical velocity
s = 0, // sprite animation index
I = i = 0, // I = number of blocks, i = temp iteration variable
yPlayer, // player y position
d,e, // Time at last frame & temp variable
sprite, // animation frame
temp1,temp2; // temp vars
function R() {
return (L = L * N % rand1) / rand1;
};
function fillRect(x,y,w,h) {
ctx.fillRect(x*SW, y*SW, w*SW, h*SW);
};
function l() {
e = new Date().getTime();
canvas.width = W;
// d = milliseconds in last frame
d = d ? (e - d) / 10 : 0;
vxCamera += .004 * d;
lastXCamera = xCamera;
xCamera += vxCamera * d;
// physics
// move player forward
xPlayer += vxCamera * d;
// temp1 = terrain position at player x position
temp2 = temp1;
temp1 = terrain[floor(xPlayer / U)];
// oh no we're underground, undo our move player Forward
if (yPlayer < temp1) {
xPlayer -= vxCamera * d;
temp1 = temp2;
}
vyPlayer =
jump ?
yPlayer == temp1 ?
// on ground, start jump
2
:
// decelerate fall
vyPlayer -.015 * d
:
vyPlayer > 0 ?
// start fall
0
:
// decelerate fall
vyPlayer - .05 * d;
yPlayer >= temp1 && yPlayer + vyPlayer * d < temp1 ? (
// hit ground
yPlayer = temp1,
vyPlayer = 0,
// player on ground, accelerate
xPlayer += .05 * d * d,
// increase anim frame
s += .07 * d,
s = s % 2
) : (
yPlayer += vyPlayer * d,
// anim
s = vyPlayer > 0 ? 2.8 : 3.8 // +.8 so run will start again as soon as we land
);
i = I;
while (i--) {
fillRect(i * U - xCamera, CH, U, -terrain[i]);
}
// draw only relevant rects
//i = floor(lastXCamera / U);
//while (i < floor((xCamera + W) / U) + 2 && i < I) {
// // draw whole quad:
// fillRect(i * U - xCamera, CH, U, -terrain[i]);
// ++i;
//}
// draw dude
sprite = sprites[floor(s)], // animation frame
i = 72;
while (i) {
if (i-- % 6 === 0) {
// temp1 = base64 code for current sprite char
temp1 = sprite.charCodeAt(i/6);
// temp2 = unencoded base64 value
temp2 = temp1 < 44 ? 0 : temp1 < 47 ? 1 : temp1 < 58 ? temp1 - 46 : temp1 < 91 ? temp1 - 53 : temp1 - 59;
}
if (temp2 % 2) {
fillRect(xPlayer - xCamera - 7 + (i % 7),
CH - yPlayer - 10 + floor(i / 7), 1, 1);
}
temp2 >>>= 1;
}
// draw progress meter
fillRect(5, 5, xCamera * CW / I / U, 5);
// death
xPlayer > I*U+U ? (
++K,
G()
) : xPlayer < xCamera ? (
G()
) : (
setTimeout(l, 15 - d * 10),
d = e
);
};
while (n) {
// temp1 = width var when generating
temp1 = n-- == N ? 30 : floor(R() * (n % 2 ? 4 : 9)) + (n % 2 ? 2 : 5);
// temp2 = height var when generating
temp2 = n % 2 ? 0 : 9 + R() * 80 + K; // every second block is small
//temp2 = 2;
I += temp1;
while (temp1--) {
terrain[i + temp1] = floor(temp2);
}
i = I;
}
yPlayer = terrain[0];
d = 0;
l();
}
onkeydown = onkeyup = function(e) {
// Thanks @marijnjh
jump = e.type[5];
};
setTimeout(G, 1);
}();