Index
Index
DOCTYPE html>
<html>
<head>
<title>pacman </title>
<link rel="stylesheet" type="text/css"
href="https://fonts.googleapis.com/css?family=Permanent+Marker">
<style type="text/css">
body {
background-color: black;
}
#pacman {
height: 470px;
width: 382px;
border-radius: 5px;
margin: 20px auto;
}
#shim {
font-family: 'Permanent Marker', cursive;
position: absolute;
visibility: hidden
}
h1 {
font-family: 'Permanent Marker', cursive;
text-align: center;
color: yellow;
}
body {
width: 342px;
margin: 0px auto;
font-family: sans-serif;
}
p {
text-decoration: none;
color: #0000FF;
}
</style>
</head>
<body>
<div id="shim">shim for font face</div>
<h1>Maintenance</h1>
<p style="text-align:center;">Sedang migrasi server dan upgrade engine 23:00
s/d 05:00. Mohon doanya :)</p>
<div id="pacman"></div>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></
script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
/*jslint browser: true, undef: true, eqeqeq: true, nomen: true, white:
true */
/*global window: false, document: false */
/*
* fix looped audio
* add fruits + levels
* fix what happens when a ghost is eaten (should go back to base)
* do proper ghost mechanics (blinky/wimpy etc)
*/
var NONE = 4,
UP = 3,
LEFT = 2,
DOWN = 1,
RIGHT = 11,
WAITING = 5,
PAUSE = 6,
PLAYING = 7,
COUNTDOWN = 8,
EATEN_PAUSE = 9,
DYING = 10,
Pacman = {};
Pacman.FPS = 30;
return {
"x": addBounded(current.x, xSpeed),
"y": addBounded(current.y, ySpeed)
};
};
function isVunerable() {
return eatable !== null;
};
function isDangerous() {
return eaten === null;
};
function isHidden() {
return eatable === null && eaten !== null;
};
function getRandomDirection() {
var moves = (direction === LEFT || direction === RIGHT)
? [UP, DOWN] : [LEFT, RIGHT];
return moves[Math.floor(Math.random() * 2)];
};
function reset() {
eaten = null;
eatable = null;
position = {"x": 90, "y": 80};
direction = getRandomDirection();
due = getRandomDirection();
};
function onWholeSquare(x) {
return x % 10 === 0;
};
function oppositeDirection(dir) {
return dir === LEFT && RIGHT ||
dir === RIGHT && LEFT ||
dir === UP && DOWN || UP;
};
function makeEatable() {
direction = oppositeDirection(direction);
eatable = game.getTick();
};
function eat() {
eatable = null;
eaten = game.getTick();
};
function pointToCoord(x) {
return Math.round(x / 10);
};
function onGridSquare(pos) {
return onWholeSquare(pos.y) && onWholeSquare(pos.x);
};
function secondsAgo(tick) {
return (game.getTick() - tick) / Pacman.FPS;
};
function getColour() {
if (eatable) {
if (secondsAgo(eatable) > 5) {
return game.getTick() % 20 > 10 ? "#FFFFFF" : "#0000BB";
} else {
return "#0000BB";
}
} else if(eaten) {
return "#222";
}
return colour;
};
function draw(ctx) {
var s = map.blockSize,
top = (position.y/10) * s,
left = (position.x/10) * s;
var tl = left + s;
var base = top + s - 3;
var inc = s / 10;
ctx.fillStyle = getColour();
ctx.beginPath();
ctx.moveTo(left, base);
ctx.closePath();
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "#FFF";
ctx.arc(left + 6,top + 6, s / 6, 0, 300, false);
ctx.arc((left + s) - 6,top + 6, s / 6, 0, 300, false);
ctx.closePath();
ctx.fill();
var f = s / 12;
var off = {};
off[RIGHT] = [f, 0];
off[LEFT] = [-f, 0];
off[UP] = [0, -f];
off[DOWN] = [0, f];
ctx.beginPath();
ctx.fillStyle = "#000";
ctx.arc(left+6+off[direction][0], top+6+off[direction][1],
s / 15, 0, 300, false);
ctx.arc((left+s)-6+off[direction][0], top+6+off[direction][1],
s / 15, 0, 300, false);
ctx.closePath();
ctx.fill();
};
function pane(pos) {
if (pos.y === 100 && pos.x >= 190 && direction === RIGHT) {
return {"y": 100, "x": -10};
}
if (pos.y === 100 && pos.x <= -10 && direction === LEFT) {
return position = {"y": 100, "x": 190};
}
return false;
};
function move(ctx) {
if (onGrid &&
map.isFloorSpace({
"y":pointToCoord(nextSquare(npos.y, due)),
"x":pointToCoord(nextSquare(npos.x, due))})) {
direction = due;
} else {
npos = null;
}
}
if (onGrid &&
map.isWallSpace({
"y" : pointToCoord(nextSquare(npos.y, direction)),
"x" : pointToCoord(nextSquare(npos.x, direction))
})) {
due = getRandomDirection();
return move(ctx);
}
position = npos;
due = getRandomDirection();
return {
"new" : position,
"old" : oldPos
};
};
return {
"eat" : eat,
"isVunerable" : isVunerable,
"isDangerous" : isDangerous,
"makeEatable" : makeEatable,
"reset" : reset,
"move" : move,
"draw" : draw
};
};
keyMap[KEY.ARROW_LEFT] = LEFT;
keyMap[KEY.ARROW_UP] = UP;
keyMap[KEY.ARROW_RIGHT] = RIGHT;
keyMap[KEY.ARROW_DOWN] = DOWN;
function addScore(nScore) {
score += nScore;
if (score >= 10000 && score - nScore < 10000) {
lives += 1;
}
};
function theScore() {
return score;
};
function loseLife() {
lives -= 1;
};
function getLives() {
return lives;
};
function initUser() {
score = 0;
lives = 3;
newLevel();
}
function newLevel() {
resetPosition();
eaten = 0;
};
function resetPosition() {
position = {"x": 90, "y": 120};
direction = LEFT;
due = LEFT;
};
function reset() {
initUser();
resetPosition();
};
function keyDown(e) {
if (typeof keyMap[e.keyCode] !== "undefined") {
due = keyMap[e.keyCode];
e.preventDefault();
e.stopPropagation();
return false;
}
return true;
};
function onWholeSquare(x) {
return x % 10 === 0;
};
function pointToCoord(x) {
return Math.round(x/10);
};
function onGridSquare(pos) {
return onWholeSquare(pos.y) && onWholeSquare(pos.x);
};
function move(ctx) {
if (isOnSamePlane(due, direction) ||
(onGridSquare(position) &&
map.isFloorSpace(next(npos, due)))) {
direction = due;
} else {
npos = null;
}
}
if (npos.y === 100 && npos.x >= 190 && direction === RIGHT) {
npos = {"y": 100, "x": -10};
}
if (npos.y === 100 && npos.x <= -12 && direction === LEFT) {
npos = {"y": 100, "x": 190};
}
position = npos;
nextWhole = next(position, direction);
block = map.block(nextWhole);
map.setBlock(nextWhole, Pacman.EMPTY);
addScore((block === Pacman.BISCUIT) ? 10 : 50);
eaten += 1;
return {
"new" : position,
"old" : oldPosition
};
};
function isMidSquare(x) {
var rem = x % 10;
return rem > 3 || rem < 7;
};
if (amount >= 1) {
return;
}
ctx.fillStyle = "#FFFF00";
ctx.beginPath();
ctx.moveTo(((position.x/10) * size) + half,
((position.y/10) * size) + half);
ctx.fill();
};
function draw(ctx) {
var s = map.blockSize,
angle = calcAngle(direction, position);
ctx.fillStyle = "#FFFF00";
ctx.beginPath();
ctx.moveTo(((position.x/10) * s) + s / 2,
((position.y/10) * s) + s / 2);
ctx.arc(((position.x/10) * s) + s / 2,
((position.y/10) * s) + s / 2,
s / 2, Math.PI * angle.start,
Math.PI * angle.end, angle.direction);
ctx.fill();
};
initUser();
return {
"draw" : draw,
"drawDead" : drawDead,
"loseLife" : loseLife,
"getLives" : getLives,
"score" : score,
"addScore" : addScore,
"theScore" : theScore,
"keyDown" : keyDown,
"move" : move,
"newLevel" : newLevel,
"reset" : reset,
"resetPosition" : resetPosition
};
};
function withinBounds(y, x) {
return y >= 0 && y < height && x >= 0 && x < width;
}
function isWall(pos) {
return withinBounds(pos.y, pos.x) && map[pos.y][pos.x] === Pacman.WALL;
}
function isFloorSpace(pos) {
if (!withinBounds(pos.y, pos.x)) {
return false;
}
var peice = map[pos.y][pos.x];
return peice === Pacman.EMPTY ||
peice === Pacman.BISCUIT ||
peice === Pacman.PILL;
}
function drawWall(ctx) {
var i, j, p, line;
ctx.strokeStyle = "#0000FF";
ctx.lineWidth = 5;
ctx.lineCap = "round";
p = line[j];
if (p.move) {
ctx.moveTo(p.move[0] * blockSize, p.move[1] * blockSize);
} else if (p.line) {
ctx.lineTo(p.line[0] * blockSize, p.line[1] * blockSize);
} else if (p.curve) {
ctx.quadraticCurveTo(p.curve[0] * blockSize,
p.curve[1] * blockSize,
p.curve[2] * blockSize,
p.curve[3] * blockSize);
}
}
ctx.stroke();
}
}
function reset() {
map = Pacman.MAP.clone();
height = map.length;
width = map[0].length;
};
function block(pos) {
return map[pos.y][pos.x];
};
function drawPills(ctx) {
ctx.fillStyle = "#000";
ctx.fillRect((j * blockSize), (i * blockSize),
blockSize, blockSize);
ctx.fillStyle = "#FFF";
ctx.arc((j * blockSize) + blockSize / 2,
(i * blockSize) + blockSize / 2,
Math.abs(5 - (pillSize/3)),
0,
Math.PI * 2, false);
ctx.fill();
ctx.closePath();
}
}
}
};
function draw(ctx) {
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, width * size, height * size);
drawWall(ctx);
ctx.beginPath();
ctx.fillStyle = "#000";
ctx.fillRect((x * blockSize), (y * blockSize),
blockSize, blockSize);
reset();
return {
"draw" : draw,
"drawBlock" : drawBlock,
"drawPills" : drawPills,
"block" : block,
"setBlock" : setBlock,
"reset" : reset,
"isWallSpace" : isWall,
"isFloorSpace" : isFloorSpace,
"height" : height,
"width" : width,
"blockSize" : blockSize
};
};
Pacman.Audio = function(game) {
function disableSound() {
for (var i = 0; i < playing.length; i++) {
files[playing[i]].pause();
files[playing[i]].currentTime = 0;
}
playing = [];
};
function ended(name) {
function play(name) {
if (!game.soundDisabled()) {
endEvents[name] = function() { ended(name); };
playing.push(name);
files[name].addEventListener("ended", endEvents[name], true);
files[name].play();
}
};
function pause() {
for (var i = 0; i < playing.length; i++) {
files[playing[i]].pause();
}
};
function resume() {
for (var i = 0; i < playing.length; i++) {
files[playing[i]].play();
}
};
return {
"disableSound" : disableSound,
"load" : load,
"play" : play,
"pause" : pause,
"resume" : resume
};
};
function getTick() {
return tick;
};
function dialog(text) {
ctx.fillStyle = "#FFFF00";
ctx.font = "18px Calibri";
var width = ctx.measureText(text).width,
x = ((map.width * map.blockSize) - width) / 2;
ctx.fillText(text, x, (map.height * 10) + 8);
}
function soundDisabled() {
return localStorage["soundDisabled"] === "true";
};
function startLevel() {
user.resetPosition();
for (var i = 0; i < ghosts.length; i += 1) {
ghosts[i].reset();
}
audio.play("start");
timerStart = tick;
setState(COUNTDOWN);
}
function startNewGame() {
setState(WAITING);
level = 1;
user.reset();
map.reset();
map.draw(ctx);
startLevel();
}
function keyDown(e) {
if (e.keyCode === KEY.N) {
startNewGame();
} else if (e.keyCode === KEY.S) {
audio.disableSound();
localStorage["soundDisabled"] = !soundDisabled();
} else if (e.keyCode === KEY.P && state === PAUSE) {
audio.resume();
map.draw(ctx);
setState(stored);
} else if (e.keyCode === KEY.P) {
stored = state;
setState(PAUSE);
audio.pause();
map.draw(ctx);
dialog("Paused");
} else if (state !== PAUSE) {
return user.keyDown(e);
}
return true;
}
function loseLife() {
setState(WAITING);
user.loseLife();
if (user.getLives() > 0) {
startLevel();
}
}
function setState(nState) {
state = nState;
stateChanged = true;
};
function drawFooter() {
ctx.fillStyle = "#000000";
ctx.fillRect(0, topLeft, (map.width * map.blockSize), 30);
ctx.fillStyle = "#FFFF00";
ctx.fillStyle = "#FFFF00";
ctx.font = "14px Calibri";
ctx.fillText("Score: " + user.theScore(), 30, textBase);
ctx.fillText("Level: " + level, 260, textBase);
}
function redrawBlock(pos) {
map.drawBlock(Math.floor(pos.y/10), Math.floor(pos.x/10), ctx);
map.drawBlock(Math.ceil(pos.y/10), Math.ceil(pos.x/10), ctx);
}
function mainDraw() {
ghostPos = [];
userPos = u["new"];
function mainLoop() {
var diff;
map.drawPills(ctx);
if (diff === 0) {
map.draw(ctx);
setState(PLAYING);
} else {
if (diff !== lastTime) {
lastTime = diff;
map.draw(ctx);
dialog("Starting in: " + diff);
}
}
}
drawFooter();
}
function eatenPill() {
audio.play("eatpill");
timerStart = tick;
eatenCount = 0;
for (i = 0; i < ghosts.length; i += 1) {
ghosts[i].makeEatable(ctx);
}
};
function completedLevel() {
setState(WAITING);
level += 1;
map.reset();
user.newLevel();
startLevel();
};
function keyPress(e) {
if (state !== WAITING && state !== PAUSE) {
e.preventDefault();
e.stopPropagation();
}
};
wrapper.appendChild(canvas);
ctx = canvas.getContext('2d');
map.draw(ctx);
dialog("Loading ...");
var audio_files = [
["start", root + "audio/opening_song." + extension],
["die", root + "audio/die." + extension],
["eatghost", root + "audio/eatghost." + extension],
["eatpill", root + "audio/eatpill." + extension],
["eating", root + "audio/eating.short." + extension],
["eating2", root + "audio/eating.short." + extension]
];
if (arr.length === 0) {
callback();
} else {
var x = arr.pop();
audio.load(x[0], x[1], function() { load(arr, callback); });
}
};
function loaded() {
dialog("Press N to Start");
return {
"init" : init
};
}());
(function () {
/* 0 - 9 */
for (var i = 48; i <= 57; i++) {
KEY['' + (i - 48)] = i;
}
/* A - Z */
for (i = 65; i <= 90; i++) {
KEY['' + String.fromCharCode(i)] = i;
}
/* NUM_PAD_0 - NUM_PAD_9 */
for (i = 96; i <= 105; i++) {
KEY['NUM_PAD_' + (i - 96)] = i;
}
/* F1 - F12 */
for (i = 112; i <= 123; i++) {
KEY['F' + (i - 112 + 1)] = i;
}
})();
Pacman.WALL = 0;
Pacman.BISCUIT = 1;
Pacman.EMPTY = 2;
Pacman.BLOCK = 3;
Pacman.PILL = 4;
Pacman.MAP = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 4, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 4, 0],
[0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[2, 2, 2, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 2, 2, 2],
[0, 0, 0, 0, 1, 0, 1, 0, 0, 3, 0, 0, 1, 0, 1, 0, 0, 0, 0],
[2, 2, 2, 2, 1, 1, 1, 0, 3, 3, 3, 0, 1, 1, 1, 2, 2, 2, 2],
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
[2, 2, 2, 0, 1, 0, 1, 1, 1, 2, 1, 1, 1, 0, 1, 0, 2, 2, 2],
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
[0, 4, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 4, 0],
[0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
Pacman.WALLS = [
[{"move": [6, 2.5]}, {"line": [7, 2.5]}, {"curve": [7.5, 2.5, 7.5, 3]},
{"curve": [7.5, 3.5, 7, 3.5]}, {"line": [6, 3.5]},
{"curve": [5.5, 3.5, 5.5, 3]}, {"curve": [5.5, 2.5, 6, 2.5]}],
[{"move": [12, 2.5]}, {"line": [13, 2.5]}, {"curve": [13.5, 2.5, 13.5, 3]},
{"curve": [13.5, 3.5, 13, 3.5]}, {"line": [12, 3.5]},
{"curve": [11.5, 3.5, 11.5, 3]}, {"curve": [11.5, 2.5, 12, 2.5]}],
[{"move": [7.5, 5.5]}, {"line": [9, 5.5]}, {"curve": [9.5, 5.5, 9.5, 6]},
{"line": [9.5, 7.5]}],
[{"move": [9.5, 6]}, {"curve": [9.5, 5.5, 10.5, 5.5]},
{"line": [11.5, 5.5]}],
[{"move": [5.5, 5.5]}, {"line": [5.5, 7]}, {"curve": [5.5, 7.5, 6, 7.5]},
{"line": [7.5, 7.5]}],
[{"move": [6, 7.5]}, {"curve": [5.5, 7.5, 5.5, 8]}, {"line": [5.5, 9.5]}],
[{"move": [0, 11.5]}, {"line": [3, 11.5]}, {"curve": [3.5, 11.5, 3.5, 12]},
{"line": [3.5, 13]}, {"curve": [3.5, 13.5, 3, 13.5]}, {"line": [1, 13.5]},
{"curve": [0.5, 13.5, 0.5, 14]}, {"line": [0.5, 17]},
{"curve": [0.5, 17.5, 1, 17.5]}, {"line": [1.5, 17.5]}],
[{"move": [1, 17.5]}, {"curve": [0.5, 17.5, 0.5, 18]}, {"line": [0.5, 21]},
{"curve": [0.5, 21.5, 1, 21.5]}, {"line": [18, 21.5]},
{"curve": [18.5, 21.5, 18.5, 21]}, {"line": [18.5, 18]},
{"curve": [18.5, 17.5, 18, 17.5]}, {"line": [17.5, 17.5]}],
[{"move": [18, 17.5]}, {"curve": [18.5, 17.5, 18.5, 17]},
{"line": [18.5, 14]}, {"curve": [18.5, 13.5, 18, 13.5]},
{"line": [16, 13.5]}, {"curve": [15.5, 13.5, 15.5, 13]},
{"line": [15.5, 12]}, {"curve": [15.5, 11.5, 16, 11.5]},
{"line": [19, 11.5]}],
[{"move": [8.5, 9.5]}, {"line": [8, 9.5]}, {"curve": [7.5, 9.5, 7.5, 10]},
{"line": [7.5, 11]}, {"curve": [7.5, 11.5, 8, 11.5]},
{"line": [11, 11.5]}, {"curve": [11.5, 11.5, 11.5, 11]},
{"line": [11.5, 10]}, {"curve": [11.5, 9.5, 11, 9.5]},
{"line": [10.5, 9.5]}]
];
Object.prototype.clone = function () {
var i, newObj = (this instanceof Array) ? [] : {};
for (i in this) {
if (i === 'clone') {
continue;
}
if (this[i] && typeof this[i] === "object") {
newObj[i] = this[i].clone();
} else {
newObj[i] = this[i];
}
}
return newObj;
};
$(function(){
var el = document.getElementById("pacman");
</script>
</body>