Skip to content

Instantly share code, notes, and snippets.

@aemkei
Forked from maettig/LICENSE.txt
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save aemkei/cbb8434989d2e18aa1c2 to your computer and use it in GitHub Desktop.

Select an option

Save aemkei/cbb8434989d2e18aa1c2 to your computer and use it in GitHub Desktop.

A simple variation of the Pac-Man game (almost) made with 140byt.es snippets.

My setInnerHTML uses tricks learned from @jed's cssSelect. Everything else is created from scratch. Tested with Opera and Firefox. Does not work in Internet Explorer 8.

function(l, k, w) //level, keyCode, level width
{
l[l.x] = ' '; //remove player from level
if (l[l.x + l.d] == '#') //check if previous walking direction is possible
l.d = 0; //stop
if (k > 36 && k < 41 && //check if a cursor key was pressed
l[l.x + (k = k % 2 //calculate walking direction
? k - 38 //-1 is left, +1 is right
: (k - 39) * w //-width is up, +width is down
)] != '#') //check if not walking into a wall
l.d = k; //set new walking direction
l.p += l[l.x += l.d //walk
] == '.'; //and increase points
l[l.x] = 'P' //place player in level
}
function(l,k,w){l[l.x]=' ';if(l[l.x+l.d]=='#')l.d=0;if(k>36&&k<41&&l[l.x+(k=k%2?k-38:(k-39)*w)]!='#')l.d=k;l.p+=l[l.x+=l.d]=='.';l[l.x]='P'}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Thiemo Mättig <http://maettig.com/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "pacMan",
"description": "Pac-Man made with 140byte.es snippets.",
"keywords": [
"game",
"pacman"
]
}
<!DOCTYPE HTML>
<pre>Loading...</pre>
<span id="points">0</span> points
<style type="text/css">
body { background: #000; color: #FFF; font-family: Consolas, monospace; margin: 4em; text-shadow: 0 0 0.2em #FFF; }
pre { font-size: 4em; line-height: 0.55em; margin: 0; }
.air { color: #222; text-shadow: 0 0 0.2em #222; }
.wall { color: #30F; text-shadow: 0 0 0.2em #30F; }
.player { color: #FF0; text-shadow: 0 0 0.2em #FF0; }
#points { font-size: 2em; line-height: 2em; }
</style>
<script>
// 153 bytes, any idea how to save 13 bytes?
var setInnerHTML = function(a, h)
{
a = /(\W?)(.*)/.exec(a);
(a[1] == '#'
? document.getElementById(a[2])
: document['getElementsBy' + (a[1] ? 'Class' : 'Tag') + 'Name'](a[2])[0]
).innerHTML = h
}
// 97 bytes, maybe use the remaining 43 bytes to make this more flexible?
var parseLevel = function(l, w)
{
l = l.join ? l.join('\n') : l;
w = l.indexOf('\n') + 1;
l = l.split('');
l.w = w;
l.d = l.p = 0;
return l
}
// 99 bytes, should be enough to add a "you won" screen.
var renderLevel = function(l, t)
{
for (var h = '', x = 0; x < l.length; x++)
{
if (l[x] == 'P') l.x = x;
h += t[l[x]] ? t[l[x]] : l[x]
}
return h
}
// 140 bytes
var updateLevel = function(l, k, w)
{
l[l.x] = ' ';
if (l[l.x + l.d] == '#')
l.d = 0;
if (k > 36 && k < 41 && l[l.x + (k = k % 2 ? k - 38 : (k - 39) * w)] != '#')
l.d = k;
l.p += l[l.x += l.d] == '.';
l[l.x] = 'P'
}
var theme = {
' ': '<span class="air">\u2219</span>',
'.': '<span class="gold">\u2219</span>',
'#': '<span class="wall">\u25A1</span>',
'P': '<span class="player">\u263B</span>'
};
var level = parseLevel(
'###################\n' +
'#....#.......#....#\n' +
'#.##.#.#####.#.##.#\n' +
'#.#.............#.#\n' +
'#.#.##.##.##.##.#.#\n' +
'#......#...#......#\n' +
'#.#.##.#####.##.#.#\n' +
'#.#......P......#.#\n' +
'#.##.#.#####.#.##.#\n' +
'#....#.......#....#\n' +
'###################');
window.onkeydown = function(e)
{
level.keyCode = (e || window.event).keyCode;
}
window.setInterval(function()
{
setInnerHTML('PRE', renderLevel(level, theme));
setInnerHTML('#points', level.p);
updateLevel(level, level.keyCode, level.w);
}, 250)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment