View
Theme
Font Style
7pt
8pt
9pt
10pt
11pt
Line Style
100%
110%
120%
130%
140%
Bold Keyword
Default
Inspector
Kkaefer
Eclipse
SQ Light
Lesser
Dark
Cobalt
Monokai
Rubyblue
Night
SQ Dark
Ambiance
Blackboard
Line Num.
Wrap Lines
Preview
Redraw
JS Tab
HTML Tab
CSS Tab
Live Tab
Prev. Tab
Next Tab
Browser
History…
Help
Edit
Settings
Auto Complete
Match Brackets
Match Highlight
Strip Whitespace
Auto Close Brackets
Auto Close Quotes
Show Print Margin
Undo
Redo
Delete
Select Line
Select All
Find & Replace
Find
Find in Repo.
Find Next
Find Previous
Replace Single
Replace All
Wrap Search
Revert
As Template
Diff Revision
Format
Compress
Text
Zen Coding
Indent
Tab Width
1
2
3
4
5
6
7
8
Indent Unit
1
2
3
4
5
6
7
8
Smart Indent
Use Tabs
Visible Tabs
Shift Left
Shift Right
Put Indent
Number
Increment by 1
Decrement by 1
Increment by 0.1
Decrement by 0.1
Increment by 10
Decrement by 10
Simple Math
Comment
Line
Move Up
Move Down
Copy Up
Copy Down
Go to Line…
Remove Line
Next Point
Prev. Point
Help
Share
Login
You can jump to the latest bin by adding
/latest
to your URL
×
z
Find
→
←
⟲
Replace
⊗
All
Replace
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); }();
JS1k, 1k demo submission
Pop out
Help
About
×
×