var canvas;var ctx;var imgs={};var loaded_count=0;var program_mode="init";var window_width=1920;var window_height=1100;var canvas_width=1280;var canvas_height=720;var settings_angle=0;var game_started=!1;var MIN_DISTANCE_SWIPE=25;var START_LIVES=4;var TELEPORT_RANGE_START=30;var TELEPORT_RANGE_END=39;var MAX_LEVELS=27;var SPEED_MONSTER_BASE=5;var SPEED_PACMAN_BASE=10;var BLOCK_SIZE=70;var status_height=48;var last_sound=1;var last_sound_dot=1;var last_music=1;var last_scene=1;var last_difficulty=0;var last_scaling=1.0;var last_show_zoom=0;var last_random_game=0;var last_turbo_mode=0;var counter_loop;var game_fully_started=!1;var game_was_started=!1;var bg_offset_x=0;var bg_offset_y=0;var PROXIMITY_SHOW_MONSTER=9;var scaleFactor=0.5;var pacman;var walls=[];var points=[];var teleports=[];var monsters=[];var cherries=[];var pills=[];var time_in_game=0;var initial_dots=0;var time_game_started;var time_monster_shown=0;var time_cherry_shown;var time_pill_shown;var time_rocket_shown;var time_cap_shown;var lives_left;var eat_cherries;var best_time_level=0;var time_on_level=0;var speedPacman;var speedMonster;var important_move="";var location_x;var location_y;var state_prevX;var state_prevY;var state_prevDir;var state_prev_offsetX;var state_prev_offsetY;var map;var map_cols;var map_rows;var map_packman_start_col;var map_packman_start_row;var map_monster_start_col;var map_monster_start_row;var map_monsters_count_max;var monster_smart_level=100;var time_zoom_shown;var time_score_shown;var touchX;var touchY;var prevX;var prevY;var future_move="";var current_level=1;var debug_level=1;var paused=!1;var game_score;var last_score=0;var use_scene=!0;var found_pref=!1;var last_level;var last_lives;var last_score;var xDown=null;var yDown=null;window.mobileAndTabletcheck=function(){return!0;var check=!1;(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4)))check=!0})(navigator.userAgent||navigator.vendor||window.opera);return check};window.mobileAndTabletcheck2=function(){var check=!1;(function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4)))check=!0})(navigator.userAgent||navigator.vendor||window.opera);return check};function body_load(){canvas=document.getElementById('canvas');canvas.style.width=canvas_width+"px";canvas.style.height=canvas_height+"px";canvas.width=window_width;canvas.height=window_height;var el=document.getElementById("setup");el.style.width=canvas_width+"px";el.style.height=canvas_height+"px";ctx=canvas.getContext('2d');loaded_count=0;preload("bkgr","./img/bkgr_b.png?dummy="+microTime());preload("settings","./img/settings.png?dummy="+microTime());preload("bpacman","./img/pacman"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacmanL","./img/pacmanL"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacmanT","./img/pacmanT"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacmanB","./img/pacmanB"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacman_neo","./img/pacman_neo"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacman_neoL","./img/pacman_neoL"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacman_neoT","./img/pacman_neoT"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacman_neoB","./img/pacman_neoB"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacman_win","./img/pacman_win"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpacman_die","./img/pacman_die"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bmonster","./img/monster"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bcherry","./img/cherry"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpill","./img/pill"+BLOCK_SIZE+".png?dummy="+microTime());preload("bteleport","./img/teleport"+BLOCK_SIZE+".png?dummy="+microTime());preload("bpoint_blue","./img/point_b"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("bpoint","./img/point_s"+BLOCK_SIZE+"a.png?dummy="+microTime());preload("life","./img/life.png?dummy="+microTime());preload("cherry1","./img/cherry1.png?dummy="+microTime());document.addEventListener('keydown',function(event){setKey(event.keyCode)});last_level=load_stored_parameter("last_level",1);last_lives=load_stored_parameter("last_lives",START_LIVES);last_score=load_stored_parameter("last_score",0);last_sound=load_stored_parameter("last_sound",last_sound);last_sound_dot=load_stored_parameter("last_sound_dot",last_sound_dot);last_music=load_stored_parameter("last_music",last_music);last_scene=load_stored_parameter("last_scene",last_scene);last_difficulty=load_stored_parameter("last_difficulty",last_difficulty);last_turbo_mode=load_stored_parameter("last_turbo_mode",last_turbo_mode);last_random_game=load_stored_parameter("last_random_game",last_random_game);var found_stored_level=load_stored_parameter("level_saved",0);found_pref=!1;if(!(last_level==1&&last_lives==START_LIVES&&last_score==0)||found_stored_level==1){found_pref=!0} document.addEventListener('touchstart',handleTouchStart,!1);document.addEventListener('touchmove',handleTouchMove,!1);window.addEventListener("blur",function(){if(program_mode==="game")SaveLevel();StartScreen()});window.onbeforeunload=function(){if(program_mode==="game")SaveLevel();StartScreen();return!1} if(mobileAndTabletcheck()){disableScroll();window.addEventListener('resize',setWindowSize);setWindowSize()} start()} function preventDefault(e){e.preventDefault()} function disableScroll(){document.body.addEventListener('touchmove',preventDefault,{passive:!1})} function enableScroll(){document.body.removeEventListener('touchmove',preventDefault)} function getTouches(evt){return evt.touches||evt.originalEvent.touches} function handleTouchStart(evt){const firstTouch=getTouches(evt)[0];xDown=firstTouch.clientX;yDown=firstTouch.clientY};function handleTouchMove(evt){if(!xDown||!yDown){return} var xUp=evt.touches[0].clientX;var yUp=evt.touches[0].clientY;var xDiff=xDown-xUp;var yDiff=yDown-yUp;var move_detected=!1;if(Math.abs(xDiff)<30&&Math.abs(yDiff)<30){return} if(Math.abs(xDiff)>Math.abs(yDiff)){if(xDiff>0){future_move="LEFT";move_detected=!0}else{future_move="RIGHT";move_detected=!0}}else{if(yDiff>0){future_move="TOP";move_detected=!0}else{future_move="BOTTOM";move_detected=!0}} if(!move_detected){xDown=null;yDown=null}else{xDown=xUp;yDown=yUp}};function load_stored_parameter(name,value){var v=getCookie(name);console.log("cookie",name,"=",v);if(v==null){setCookie(name,value);return value}else{if(v==="")v=0;return parseInt(v)}} function start(){if(game_started)return;canvas.style.display="";game_started=!0;setInterval(render,30)} function startPlayer2(){var el=document.getElementById("soundGame");if(last_music>0&&el.paused){var n=randInt(1,7);if(n==1)el.src="./sounds/loop1.mp3";else if(n==2)el.src="./sounds/loop2.mp3";else if(n==3)el.src="./sounds/loop3.mp3";else if(n==4)el.src="./sounds/loop4.mp3";else if(n==5)el.src="./sounds/loop5.mp3";else if(n==6)el.src="./sounds/loop6.mp3";else el.src="./sounds/loop7.mp3";el.load();el.play();console.log(el.src)}} function stopPlayer2(){document.getElementById("soundGame").pause()} function render(){if(program_mode==="init"){program_mode="start_screen";if(mobileAndTabletcheck())setWindowSize();StartScreen()}else if(program_mode==="start_screen"){ctx.clearRect(0,0,canvas.width,canvas.height);ctx.drawImage(imgs.bkgr,0,0,window_width,window_height);settings_angle=settings_angle+1;drawImage(ctx,imgs.settings,0.05*window_width,0.3*window_height,0.2*window_height,0.2*window_height,settings_angle)}else if(program_mode==="setup"){}else if(program_mode==="game"){if(!game_fully_started){return} if(!pacman.isDied()&&!pacman.isWin())startPlayer2();SetSpeed();speedPacman=SPEED_PACMAN_BASE;speedMonster=SPEED_MONSTER_BASE;if(pacman.isWin()||pacman.isDied()){future_move="";important_move="";pacman.setDirection("");pacman.update();if(pacman.isDied()&&!pacman.isPlayingOnce()){if(last_random_game!=1){if(lives_left>0){lives_left--;console.log("lives_left=",lives_left)}else{if(!1){current_level--;lives_left=START_LIVES;last_level=current_level;setCookie("last_level",last_level)}else{GameOver();draw();return}}} ResetSavedGame();start_game()} if(pacman.isWin()&&!pacman.isPlayingOnce()){if(last_random_game!=1){if(current_level==MAX_LEVELS){program_mode="win";if(mobileAndTabletcheck())setWindowSize();document.getElementById("win").style.display=""} if(current_level=2000&&monster_elapsed>=10000)show_new_monster=!0;if(monsters.length>0&&monster_elapsed>=10000)show_new_monster=!0;if(show_new_monster){var iPacman=Math.floor(pacman.getY()/BLOCK_SIZE);var jPacman=Math.floor(pacman.getX()/BLOCK_SIZE);if(!(Math.abs(map_monster_start_col-jPacman)15000){GetPossibleLocation();if(location_x!=0){cherries.push(new Cherry(imgs.bcherry,location_x,location_y,BLOCK_SIZE,BLOCK_SIZE))}}}else{time_cherry_shown=microTime()} if(pills.length==0){var pill_elapsed=Math.floor((microTime()-time_pill_shown)/1000);if(pill_elapsed>30000){console.log(pill_elapsed);GetPossibleLocation();if(location_x!=0){pills.push(new Pill(imgs.bpill,location_x,location_y,BLOCK_SIZE,BLOCK_SIZE))}}}else{time_pill_shown=microTime()} var prevX=pacman.getX();var prevY=pacman.getY();SavePacmanState();var isCollision=!1;var move_done=!1;if(future_move!==""&&(prevX%BLOCK_SIZE!=0||prevY%BLOCK_SIZE!=0)){if(important_move===""&&future_move!==pacman.getDirection())important_move=future_move} if(future_move!==""&&((prevX%BLOCK_SIZE==0&&prevY%BLOCK_SIZE==0)||pacman.getDirection()==="")){if(important_move!==""){pacman.setDirection(important_move)}else{pacman.setDirection(future_move)} ModifyPacman();isCollision=PacmanCollision();if(isCollision){RestorePacmanState();important_move=""}else{move_done=!0;if(important_move!==""){important_move=""}else{future_move=""}}} if(!move_done){ModifyPacman();if(PacmanCollision()){RestorePacmanState();pacman.setDirection("")}} for(var i=0;iBLOCK_SIZE/2)nearX+=BLOCK_SIZE;var nearY=(Math.floor(m.getY()*1.0/BLOCK_SIZE))*BLOCK_SIZE;if(m.getY()-nearY>BLOCK_SIZE/2)nearY+=BLOCK_SIZE;if(Math.abs(m.getX()-nearX)<=speedMonster/2)m.setX(nearX);if(Math.abs(m.getY()-nearY)<=speedMonster/2)m.setY(nearY);if(m.getDirection()!==""&&randInt(1,monster_smart_level)==1&&!pacman.isNeo()){m.setRandomMove(!0)} var isMCollision=!1;for(var j=0;j120000){cherries.splice(i,1);time_cherry_shown=microTime()-28000*1000}} for(var i=0;i0)bg_offset_x=0;if(bg_offset_x<-(map_cols*BLOCK_SIZE-BLOCK_SIZE))bg_offset_x=-(map_cols*BLOCK_SIZE-BLOCK_SIZE);if(map_cols*BLOCK_SIZE>getWidth()/scaleFactor){if(map_cols*BLOCK_SIZE+bg_offset_x0)bg_offset_y=0;if(bg_offset_y<-(map_rows*BLOCK_SIZE-BLOCK_SIZE))bg_offset_y=-(map_rows*BLOCK_SIZE-BLOCK_SIZE);if(map_rows*BLOCK_SIZE>getHeight()/scaleFactor-status_height){if(map_rows*BLOCK_SIZE+bg_offset_yTELEPORT_RANGE_END)return;for(var i=0;i0)break} if(iFound>0){var iPacman=Math.floor(pacman.getY()/BLOCK_SIZE);var jPacman=Math.floor(pacman.getX()/BLOCK_SIZE);pacman.setTeleported();var new_y=iFound*BLOCK_SIZE;var new_x=jFound*BLOCK_SIZE;pacman.setX(new_x);pacman.setY(new_y);pacman.setDirection("");future_move="";important_move="";EnsureVisible(iPacman!=iFound,jPacman!=jFound);if(last_sound!=0){document.getElementById("soundTeleport").play()}}} function rectsEqual(r1,r2){return r1.left==r2.left&&r1.top==r2.top&&r1.right==r2.right&&r1.bottom==r2.bottom} function draw(){if(!game_fully_started)return;ctx.clearRect(0,0,canvas.width,canvas.height);var screenWidth=Math.floor(getWidth()/scaleFactor);var screenHeight=Math.floor(getHeight()/scaleFactor)+status_height;var progress1=(initial_dots-points.length)*1.0/initial_dots;if(last_scene==0||!use_scene){ctx.fillStyle="rgb(133, 193, 95)";ctx.fillRect(0,0,Math.min(screenWidth,map_cols*BLOCK_SIZE),status_height+Math.min(screenHeight+map_rows*BLOCK_SIZE))}else{var el=document.getElementById("bkimg");el.style.left=bg_offset_x/(window_width/canvas_width)+"px";el.style.top=(bg_offset_y+status_height)/(window_height/canvas_height)+"px";ctx.fillStyle="rgba(70, 130, 180, "+(0.8-0.6*progress1)+")";ctx.fillRect(0,0,map_cols*BLOCK_SIZE,status_height+map_rows*BLOCK_SIZE)} for(var i=0;i0){for(var i=1;i<=eat_cherries;i++){ctx.drawImage(imgs.cherry1,ibase,10,32,32);ibase+=36} ibase+=36} var clr1="red";if(progress1>0.8)clr1="lightgreen";else if(progress1>0.4)clr1="yellow";ctx.beginPath();ctx.lineWidth="2";ctx.strokeStyle=clr1;ctx.rect(ibase,9,120,30);ctx.stroke();ctx.fillStyle=clr1;ctx.fillRect(ibase,9,progress1*120,30);ibase+=160;var sStatus=GetDifficultyLevel(last_difficulty);ctx.fillStyle="rgb(222, 250, 230)";ctx.fillText(sStatus,ibase,34);if(last_random_game==1){var elapsed=(microTime()-time_score_shown)/1000;if(elapsed<=3000){var world_width=Math.min(screenWidth,map_cols*BLOCK_SIZE);var world_height=Math.min(screenHeight,map_rows*BLOCK_SIZE);ctx.font="80px Tahoma";var txt="YOU vs GHOSTS";var tw=ctx.measureText(txt).width;ctx.fillText(txt,world_width/2-tw/2,0.45*world_height);var score_my=load_stored_parameter("last_score_pacman",0);var score_ghosts=load_stored_parameter("last_score_ghosts",0);var txt2=score_my+" : "+score_ghosts;var tw2=ctx.measureText(txt2).width;console.log(tw,tw2);ctx.fillStyle="rgba(255, 255, 255, 0.7)";ctx.fillRect(world_width/2-Math.max(tw,tw2)/2-40,0.3*world_height,Math.max(tw,tw2)+80,0.45*world_height);ctx.fillStyle="rgb(211,84,0)";ctx.fillText(txt,world_width/2-tw/2,0.45*world_height);ctx.fillText(txt2,world_width/2-tw2/2,0.65*world_height)}}} function GetDifficultyLevel(difficulty){return difficulty==0?"EASY":(difficulty==1?"MEDIUM":"HARD")} function GameOver(){console.log("game_over");start_new_game()} function FindDirectionMonster(m){var iPacman=Math.floor(pacman.getY()/BLOCK_SIZE);var jPacman=Math.floor(pacman.getX()/BLOCK_SIZE);var iMonster=Math.floor(m.getY()/BLOCK_SIZE);var jMonster=Math.floor(m.getX()/BLOCK_SIZE);var maptest=new Array(map_rows);for(var i=0;i=TELEPORT_RANGE_START&&!(i==iPacman&&j==jPacman))maptest[i][j]=0}} counter_loop=0;var found=!1;maptest[iPacman][jPacman]=100;var points=[];points.push({x:iPacman,y:jPacman});var i;var j;while(!found){counter_loop++;for(var k=0;k0){if(maptest[i-1][j]==0){maptest[i-1][j]=maptest[i][j]+1;points.push({x:i-1,y:j})}} if(i0){if(maptest[i][j-1]==0){maptest[i][j-1]=maptest[i][j]+1;points.push({x:i,y:j-1})}} if(j=100){if(randInt(0,1)==0){if(maptest[iMonster-1][jMonster]==nMonsterCell-1)return"TOP";if(maptest[iMonster+1][jMonster]==nMonsterCell-1)return"BOTTOM";if(maptest[iMonster][jMonster-1]==nMonsterCell-1)return"LEFT";if(maptest[iMonster][jMonster+1]==nMonsterCell-1)return"RIGHT"}else{if(maptest[iMonster][jMonster-1]==nMonsterCell-1)return"LEFT";if(maptest[iMonster][jMonster+1]==nMonsterCell-1)return"RIGHT";if(maptest[iMonster-1][jMonster]==nMonsterCell-1)return"TOP";if(maptest[iMonster+1][jMonster]==nMonsterCell-1)return"BOTTOM"} found=!0} if(counter_loop>map_rows*map_cols)found=!0} return""} function randInt(min,max){min=Math.ceil(min);max=Math.floor(max);return Math.floor(Math.random()*(max-min+1))+min} function collision(a,b){if(intersectRect(a.getRectangle(),b.getRectangle())){return!0} return!1} function PacmanCollision(){if(pacman.getX()<0)return!0;if(pacman.getX()+BLOCK_SIZE>map_cols*BLOCK_SIZE)return!0;if(pacman.getY()<0)return!0;if(pacman.getY()+BLOCK_SIZE>map_rows*BLOCK_SIZE)return!0;for(var i=0;i100)return}} function SavePacmanState(){state_prevX=pacman.getX();state_prevY=pacman.getY();state_prevDir=pacman.getDirection();state_prev_offsetX=bg_offset_x;state_prev_offsetY=bg_offset_y} function RestorePacmanState(){pacman.setX(state_prevX);pacman.setY(state_prevY);pacman.setDirection(state_prevDir);bg_offset_x=state_prev_offsetX;bg_offset_y=state_prev_offsetY} function preload(img_name,src,next){imgs[img_name]=new Image;imgs[img_name].onload=function(){loaded_count++;if(next)next()} imgs[img_name].src=src} function drawImage(ctx,image,x,y,w,h,degrees){ctx.save();ctx.translate(x+w/2,y+h/2);ctx.rotate(degrees*Math.PI/180.0);ctx.translate(-x-w/2,-y-h/2);ctx.drawImage(image,x,y,w,h);ctx.restore()} function StartScreen(){program_mode="start_screen";stopPlayer2();document.getElementById("setup").style.display="none";document.getElementById("win").style.display="none";document.getElementById("btnSetup").style.display="";document.getElementById("btnNewGame").style.display="";document.getElementById("btnContinueGame").style.display=game_was_started||found_pref?"":"none";if(mobileAndTabletcheck())setWindowSize()} function start_new_game(){game_fully_started=!1;if(last_random_game!=1){lives_left=START_LIVES;game_score=0;current_level=debug_level;eat_cherries=0} document.getElementById("btnSetup").style.display="none";document.getElementById("btnNewGame").style.display="none";document.getElementById("btnContinueGame").style.display="none";setCookie("last_level",current_level);setCookie("level_saved",current_level);level_saved=current_level;start_game(!0)} function start_game(is_new_game){console.log("start_game");game_fully_started=!1;if(last_random_game!=1){setCookie("last_level",current_level);setCookie("last_lives",lives_left);setCookie("last_score",game_score)} bg_offset_x=0;bg_offset_y=0;pacman=new Pacman(imgs.bpacman,imgs.bpacmanL,imgs.bpacmanT,imgs.bpacmanB,imgs.bpacman_die,imgs.bpacman_win,imgs.bpacman_neo,imgs.bpacman_neoL,imgs.bpacman_neoT,imgs.bpacman_neoB,BLOCK_SIZE,BLOCK_SIZE);var next=function(){InitMonsterIQ();scaleFactor=last_scaling;pacman.setX(map_packman_start_col*BLOCK_SIZE);pacman.setY(map_packman_start_row*BLOCK_SIZE);walls=[];points=[];teleports=[];time_game_started=microTime();time_cherry_shown=microTime();time_pill_shown=microTime();time_rocket_shown=microTime();time_cap_shown=microTime();monsters=[];cherries=[];pills=[];if(!is_new_game){if(!RestoreMap()){setCookie("level_saved",0);LoadLevelAjax(current_level,next)}} for(var i=0;i0?map[i-1][j]:0;if((n0>=TELEPORT_RANGE_START&&n0<=TELEPORT_RANGE_END)||n0<0)n0=0;var n1=i>0&&j=TELEPORT_RANGE_START&&n1<=TELEPORT_RANGE_END)||n1<0)n1=0;var n3=j=TELEPORT_RANGE_START&&n3<=TELEPORT_RANGE_END)||n3<0)n3=0;var n5=i=TELEPORT_RANGE_START&&n5<=TELEPORT_RANGE_END)||n5<0)n5=0;var n6=i=TELEPORT_RANGE_START&&n6<=TELEPORT_RANGE_END)||n6<0)n6=0;var n7=i0?map[i+1][j-1]:0;if((n7>=TELEPORT_RANGE_START&&n7<=TELEPORT_RANGE_END)||n7<0)n7=0;var n9=j>0?map[i][j-1]:0;if((n9>=TELEPORT_RANGE_START&&n9<=TELEPORT_RANGE_END)||n9<0)n9=0;var n11=i>0&&j>0?map[i-1][j-1]:0;if((n11>=TELEPORT_RANGE_START&&n11<=TELEPORT_RANGE_END)||n11<0)n11=0;var wtype=16;if(n3==0&&n9==0&&n6==0&&n0==0){}else if(n3==0&&n9==0&&n6==0){wtype=13}else if(n3==0&&n9==0&&n0==0){wtype=12}else if(n3==0&&n9==0){wtype=7}else if(n0==0&&n6==0&&n9==0){wtype=15}else if(n0==0&&n6==0&&n3==0){wtype=14}else if(n0==0&&n6==0){wtype=6}else if(n11==0&&n0==0&&n9==0&&n6!=0&&n3!=0){wtype=2}else if(n1==0&&n0==0&&n3==0&&n6!=0&&n9!=0){wtype=3}else if(n6==0&&n5==0&&n3==0&&n0!=0&&n9!=0){wtype=4}else if(n6==0&&n7==0&&n9==0&&n0!=0&&n3!=0){wtype=5}else if(n6!=0&&n0!=0&&n9!=0&&n3==0){wtype=8}else if(n6==0&&n0!=0&&n9!=0&&n3!=0){wtype=9}else if(n6!=0&&n0==0&&n9!=0&&n3!=0){wtype=11}else if(n6!=0&&n0!=0&&n9==0&&n3!=0){wtype=10} walls.push(new Wall(wtype,j*BLOCK_SIZE,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE))}else if(map[i][j]>=2&&map[i][j]<=16){walls.push(new Wall(map[i][j],j*BLOCK_SIZE,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE))}else if(map[i][j]>=TELEPORT_RANGE_START&&map[i][j]<=TELEPORT_RANGE_END){teleports.push(new Teleport(imgs.bteleport,j*BLOCK_SIZE,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE))}else if(map[i][j]==0&&!(i==map_packman_start_row&&j==map_packman_start_col)){points.push(new Point(imgs.bpoint,imgs.bpoint_blue,j*BLOCK_SIZE+BLOCK_SIZE/2-imgs.bpoint.width/2,i*BLOCK_SIZE+BLOCK_SIZE/2-imgs.bpoint.height/2,imgs.bpoint.width,imgs.bpoint.height,i,j))}}} for(var i=0;iMAX_LEVELS){current_level=MAX_LEVELS} LoadLevelAjax(current_level,next)} function continue_game(){document.getElementById("btnSetup").style.display="none";document.getElementById("btnNewGame").style.display="none";document.getElementById("btnContinueGame").style.display="none";if(game_was_started){program_mode="game";if(mobileAndTabletcheck())setWindowSize();scaleFactor=last_scaling;EnsureVisible(!0,!0);time_zoom_shown=microTime();time_score_shown=microTime();time_pill_shown=microTime();time_cherry_shown=microTime();InitMonsterIQ()}else{current_level=last_level;lives_left=last_lives;game_score=last_score;start_game()}} function zeroPad(n,width,z){z=z||'0';n=n+'';return n.length>=width?n:new Array(width-n.length+1).join(z)+n} var Animation=function(){this.currentFrame=0;this.startTime=0;this.delay=0;this.playedOnce=!1;this.setFrames=function(frames){this.frames=frames;this.currentFrame=0;this.startTime=microTime();this.framesCount=Math.floor(frames.width/BLOCK_SIZE)} this.setDelay=function(d){this.delay=d} this.setFrame=function(i){this.currentFrame=i;this.playedOnce=!1};this.update=function(){var elapsed=(microTime()-this.startTime)/1000;if(elapsed>this.delay){this.currentFrame++;this.startTime=microTime()} if(this.currentFrame==this.framesCount){this.currentFrame=0;this.playedOnce=!0}} this.getImage=function(){return this.frames} this.getFrame=function(){return this.currentFrame}} var Pacman=function(resR,resL,resT,resB,res_die,res_win,res_neo_R,res_neo_L,res_neo_T,res_neo_B,w,h){this.animation=new Animation();this.animationR=new Animation();this.animationL=new Animation();this.animationT=new Animation();this.animationB=new Animation();this.animation_die=new Animation();this.animation_win=new Animation();this.animation_neo_r=new Animation();this.animation_neo_l=new Animation();this.animation_neo_t=new Animation();this.animation_neo_b=new Animation();this.move_direction="";this.play_once=!1;this.died=!1;this.win=!1;this.neo=!1;this.teleported=!1;this.time_start_neo=0;this.time_switch=0;this.switch_neo=!0;this.last_move="";this.x=0;this.y=0;this.dx=0;this.dy=0;this.height=h;this.width=w;this.play_once=!1;this.died=!1;this.win=!1;this.animationR.setFrames(resR);this.animationR.setDelay(90);this.animationL.setFrames(resL);this.animationL.setDelay(90);this.animationT.setFrames(resT);this.animationT.setDelay(90);this.animationB.setFrames(resB);this.animationB.setDelay(90);this.animation_die.setFrames(res_die);this.animation_die.setDelay(100);this.animation_win.setFrames(res_win);this.animation_win.setDelay(100);this.animation_neo_r.setFrames(res_neo_R);this.animation_neo_r.setDelay(90);this.animation_neo_l.setFrames(res_neo_L);this.animation_neo_l.setDelay(90);this.animation_neo_t.setFrames(res_neo_T);this.animation_neo_t.setDelay(90);this.animation_neo_b.setFrames(res_neo_B);this.animation_neo_b.setDelay(90);this.animation=this.animationR;this.setX=function(x){this.x=x} this.setY=function(y){this.y=y} this.getX=function(){return this.x} this.getY=function(){return this.y} this.getWidth=function(){return this.width} this.getHeight=function(){return this.height} this.getRectangle=function(){return new Rect(this.x,this.y,this.x+this.width-1,this.y+this.height-1)} this.setNeo=function(){this.neo=!0;this.time_start_neo=microTime();this.switch_neo=!0;this.time_switch=0} this.isNeo=function(){return this.neo} this.setTeleported=function(){this.teleported=!0} this.resetTeleported=function(){this.teleported=!1} this.isTeleported=function(){return this.teleported} this.isPlayingOnce=function(){return this.play_once} this.isDied=function(){return this.died} this.isWin=function(){return this.win} this.setDied=function(died){this.died=died;if(died){this.move_direction="";this.play_once=!0;this.animation_die.setFrame(0)}else{this.play_once=!1}} this.setWin=function(win){this.win=win;if(win){this.move_direction="";this.play_once=!0;this.animation_win.setFrame(0)}else{this.play_once=!1}} this.getDirection=function(){return this.move_direction} this.setDirection=function(dir){this.move_direction=dir} this.getState=function(){return this.x+"|"+this.y+"|"+this.move_direction+"|"+(this.play_once?"1":"0")+"|"+(this.died?"1":"0")+"|"+(this.win?"1":"0")+"|"+(this.neo?"1":"0")+"|"+(this.teleported?"1":"0")+"|"+this.time_start_neo} this.applyState=function(sState){var a=sState.split("|");if(a.length!=9)return;this.x=parseInt(a[0]);this.y=parseInt(a[1]);this.move_direction=a[2];this.play_once=a[3]==="1";this.died=a[4]==="1";this.win=a[5]==="1";this.neo=a[6]==="1";this.teleported=a[7]==="1";this.time_start_neo=parseInt(a[8]);if(this.move_direction===""){if(this.neo){this.animation=this.animation_neo_r}else{this.animation=this.animationR}}} this.update=function(){if(this.neo){var elapsed_neo=Math.floor((microTime()-this.time_start_neo)/1000);if(elapsed_neo>15000){if(this.last_move==="RIGHT"){this.animation=this.animationR}else if(this.last_move==="LEFT"){this.animation=this.animationL}else if(this.last_move==="TOP"){this.animation=this.animationT}else if(this.last_move==="BOTTOM"){this.animation=this.animationB}else{this.animation=this.animationR} this.neo=!1}else if(elapsed_neo>12000){var elapced_switch=Math.floor(microTime()-this.time_switch)/1000;if(elapced_switch>50){this.switch_neo=!this.switch_neo;this.time_switch=microTime()}}} if(this.died){this.animation=this.animation_die;this.animation_die.update();if(this.animation_die.playedOnce)this.play_once=!1}else if(this.win){this.animation=this.animation_win;this.animation_win.update();if(this.animation_win.playedOnce)this.play_once=!1}else if(this.neo){var frame=this.animation.currentFrame;if(this.move_direction!=="")this.last_move=this.move_direction;if(this.last_move==="RIGHT"){this.animation=this.switch_neo?this.animation_neo_r:this.animationR}else if(this.last_move==="LEFT"){this.animation=this.switch_neo?this.animation_neo_l:this.animationL}else if(this.last_move==="TOP"){this.animation=this.switch_neo?this.animation_neo_t:this.animationT}else if(this.last_move==="BOTTOM"){this.animation=this.switch_neo?this.animation_neo_b:this.animationB} this.animation_neo_r.setFrame(frame);this.animation_neo_l.setFrame(frame);this.animation_neo_t.setFrame(frame);this.animation_neo_b.setFrame(frame);this.animationR.setFrame(frame);this.animationL.setFrame(frame);this.animationT.setFrame(frame);this.animationB.setFrame(frame);this.animation_neo_r.update();this.animation_neo_l.update();this.animation_neo_t.update();this.animation_neo_b.update();this.animationR.update();this.animationL.update();this.animationT.update();this.animationB.update()}else{if(this.move_direction==="RIGHT"){this.animation=this.animationR}else if(this.move_direction==="LEFT"){this.animation=this.animationL}else if(this.move_direction==="TOP"){this.animation=this.animationT}else if(this.move_direction==="BOTTOM"){this.animation=this.animationB} this.animationR.update();this.animationL.update();this.animationT.update();this.animationB.update()}} this.draw=function(ctx,offset_x,offset_y){ctx.drawImage(this.animation.getImage(),this.animation.currentFrame*BLOCK_SIZE,0,BLOCK_SIZE,BLOCK_SIZE,this.x+offset_x,this.y+offset_y,BLOCK_SIZE,BLOCK_SIZE)}} function backPressed(){touchX=0;touchY=0;if(program_mode==="start_screen"){}else if(program_mode==="settings"){StartScreen()}else if(program_mode==="win"){StartScreen()}else{SaveLevel();StartScreen()}} function microTime(){return Math.floor(window.performance.now()*1000)} function ModifyPacman(){if(pacman.getDirection()==="RIGHT"){pacman.setX(pacman.getX()+speedPacman)}else if(pacman.getDirection()==="LEFT"){pacman.setX(pacman.getX()-speedPacman)}else if(pacman.getDirection()==="TOP"){pacman.setY(pacman.getY()-speedPacman)}else if(pacman.getDirection()==="BOTTOM"){pacman.setY(pacman.getY()+speedPacman)} var nearX=(Math.floor(pacman.getX()*1.0/BLOCK_SIZE))*BLOCK_SIZE;if(pacman.getX()-nearX>BLOCK_SIZE/2)nearX+=BLOCK_SIZE;var nearY=(Math.floor(pacman.getY()*1.0/BLOCK_SIZE))*BLOCK_SIZE;if(pacman.getY()-nearY>BLOCK_SIZE/2)nearY+=BLOCK_SIZE;if(Math.abs(pacman.getX()-nearX)<=speedPacman/2)pacman.setX(nearX);if(Math.abs(pacman.getY()-nearY)<=speedPacman/2)pacman.setY(nearY);bg_offset_x=Math.floor(getWidth()/scaleFactor/2)-pacman.getX()-pacman.getWidth()/2;if(map_cols*BLOCK_SIZE+bg_offset_x0)bg_offset_x=0;bg_offset_y=Math.floor((getHeight()-status_height)/scaleFactor/2)-pacman.getY()-pacman.getHeight()/2-Math.floor(status_height/scaleFactor/2);if(map_rows*BLOCK_SIZE+bg_offset_y0)bg_offset_y=0} function LoadRandomGame(){map_cols=randInt(15,30);map_rows=randInt(10,25);map=new Array(map_rows);for(var i=0;i0){if(map[i-1][j]==0){map[i-1][j]=map[i][j]+1;points.push({x:i-1,y:j});found=!0;last_known++}} if(i0){if(map[i][j-1]==0){map[i][j-1]=map[i][j]+1;points.push({x:i,y:j-1});found=!0;last_known++}} if(j=20)map[i][j]=0}} for(var nt=0;nt<3;nt++){var iTele1=randInt(1,map_rows-1);var jTele1=randInt(1,map_cols-1);var iTele2=randInt(1,map_rows-1);var jTele2=randInt(1,map_cols-1);if(!(iTele1==map_packman_start_row&&jTele1==map_packman_start_col)&&!(iTele1==map_monster_start_row&&jTele1==map_monster_start_col)&&!(iTele2==map_packman_start_row&&jTele2==map_packman_start_col)&&!(iTele2==map_monster_start_row&&jTele2==map_monster_start_col)&&map[iTele1][jTele1]==0&&map[iTele2][jTele2]==0&&!(iTele1==iTele2&&jTele1==jTele2)){map[iTele1][jTele1]=TELEPORT_RANGE_START+nt;map[iTele2][jTele2]=TELEPORT_RANGE_START+nt}}} function LoadLevelAjax(nLevel,next){if(last_random_game==1){LoadRandomGame();next();return} $.ajax({url:"levels.php",type:"post",dataType:'json',data:{level:nLevel,difficulty:last_difficulty},success:function(result){console.log(result);PROXIMITY_SHOW_MONSTER=result.PROXIMITY_SHOW_MONSTER;map=result.map;map_cols=result.map_cols;map_rows=result.map_rows;map_packman_start_col=result.map_packman_start_col;map_packman_start_row=result.map_packman_start_row;map_monster_start_col=result.map_monster_start_col;map_monster_start_row=result.map_monster_start_row;map_monsters_count_max=result.map_monsters_count_max;monster_smart_level=result.monster_smart_level;next()}})} function InitMonsterIQ(){if(last_difficulty==0){monster_smart_level=30}else if(last_difficulty==1){monster_smart_level=100}else{monster_smart_level=1000}} function intersectRect(r1,r2){return!(r2.left>r1.right||r2.rightr1.bottom||r2.bottommyHeight&&mobileAndTabletcheck2()){window_width=1980;window_height=myHeight/myWidth*1980}else{window_height=1600;window_width=myWidth/myHeight*1600} canvas.width=window_width;canvas.height=window_height;canvas.style.width=canvas_width+"px";canvas.style.height=canvas_height+"px";var el=document.getElementById("setup");el.style.width=canvas_width+"px";el.style.height=canvas_height+"px";el=document.getElementById("imgcontainer");el.style.width=canvas_width+"px";el.style.height=canvas_height+"px";if(program_mode==="game"){el=document.getElementById("bkimg");var imgWidth=map_cols*BLOCK_SIZE/(window_width/canvas_width);var imgHeight=map_rows*BLOCK_SIZE/(window_height/canvas_height);el.style.width=imgWidth+"px";el.style.height=imgHeight+"px"} el=document.getElementById("btnSetup");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(800/1280*myWidth)+"px";el.style.top=Math.floor(30/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("btnNewGame");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(800/1280*myWidth)+"px";el.style.top=Math.floor(140/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("btnContinueGame");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(800/1280*myWidth)+"px";el.style.top=Math.floor(250/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("setupBack");el.style.width=Math.floor(120/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(20/1280*myWidth)+"px";el.style.top=Math.floor(40/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el.style["border-radius"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("setupMusic");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(180/1280*myWidth)+"px";el.style.top=Math.floor(40/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("setupSound");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(180/1280*myWidth)+"px";el.style.top=Math.floor(160/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("setupSoundDot");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(180/1280*myWidth)+"px";el.style.top=Math.floor(280/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("setupDifficulty");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(700/1280*myWidth)+"px";el.style.top=Math.floor(40/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("setupTurboMode");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(700/1280*myWidth)+"px";el.style.top=Math.floor(160/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px";el=document.getElementById("setupUseScene");el.style.width=Math.floor(400/1280*myWidth)+"px";el.style.height=Math.floor(80/1280*myWidth)+"px";el.style.left=Math.floor(700/1280*myWidth)+"px";el.style.top=Math.floor(280/1280*myWidth)+"px";el.style["font-size"]=Math.floor(30/1280*myWidth)+"px"}}