var canvasWidth=300; var canvasHeight=100; var player; var playerY; var fallSpeed=0; var runSpeed=4; var gravity=0.7; var titleclock=0; var isJumping=false; var jumpSpeed=8; var spike=[]; var numberOfSpikes=0; var spikeIndex=0; var spikeArraySize=4; var spikeCountdown=0; var gameOver=false; var score=0; var scoreLabel; var background; document.addEventListener('keydown', keyDownHandler, false); document.addEventListener('keyup', keyUpHandler, false); document.addEventListener('mousedown', mDown, false); document.addEventListener('mouseup', mUp, false); var jump=false; var reset=false; var canJump=true; var canReset=false; var title=true; var animationSpeed=0.2; var runningSpr=new Image(); runningSpr.src='auto_runner/runnerSprites/CharacterRun.png'; runningSpr.onload=function(){ console.log("image_loaded"); } var enemySpr=[] enemySpr[0]=new Image(); enemySpr[0].src='auto_runner/runnerSprites/RatRun.png'; enemySpr[0].onload=function(){ console.log("image_loaded"); } enemySpr[1]=new Image(); enemySpr[1].src='auto_runner/runnerSprites/BatFly.png'; enemySpr[1].onload=function(){ console.log("image_loaded"); } var backgroundSpr=new Image(); backgroundSpr.src='auto_runner/runnerSprites/Background.png'; backgroundSpr.onload=function(){ console.log("image_loaded"); } var audioOnSpr=new Image(); audioOnSpr.src='auto_runner/runnerSprites/AudioIcon0.png'; audioOnSpr.onload=function(){ console.log("image_loaded"); } var audioOffSpr=new Image(); audioOffSpr.src='auto_runner/runnerSprites/AudioIcon1.png'; audioOffSpr.onload=function(){ console.log("image_loaded"); } var jumpSnd=new Audio(); jumpSnd.src='auto_runner/runnerAudio/Jump.wav'; jumpSnd.volume=0.8; var footstepSnd=new Audio(); footstepSnd.src='auto_runner/runnerAudio/Footstep.wav'; footstepSnd.volume=0.7; var deathSnd=new Audio(); deathSnd.src='auto_runner/runnerAudio/Death.wav'; var startSnd=new Audio(); startSnd.src='auto_runner/runnerAudio/Start.wav'; var squeek0Snd=new Audio(); squeek0Snd.src='auto_runner/runnerAudio/Mouse0.wav'; var squeek1Snd=new Audio(); squeek1Snd.src='auto_runner/runnerAudio/Mouse1.wav'; var swoopSnd=new Audio(); swoopSnd.src='auto_runner/runnerAudio/Swoop.wav'; swoopSnd.volume=0.2; var mouseStepSnd=new Audio(); mouseStepSnd.src='auto_runner/runnerAudio/MouseStep.wav'; mouseStepSnd.volume=0.2; function mDown(event){ if(title){ return; } if(gameOver){ canReset=true; return; } if(canJump&&!isJumping){ jump=true; canJump=false; } } function mUp(){ if(title){ title=false; startGame(); //player=new createPlayer(32,32,40); //scoreLabel=new createScoreLabel(10,30); return; } if(gameOver&&canReset){ reset=true; return; } jump=false; canJump=true; } function keyDownHandler(event){ if(event.keyCode==32){ if(title){ return; } if(gameOver){ canReset=true; return; } if(canJump&&!isJumping){ jump=true; canJump=false; } } } function keyUpHandler(event){ if(event.keyCode==32){ if(title){ title=false; //player=new createPlayer(32,32,40); startGame(); //scoreLabel=new createScoreLabel(10,30); return; } if(gameOver&&canReset){ reset=true; return; } jump=false; canJump=true; } } function startGame(){ gameCanvas.start(); player=new createPlayer(32,32,40); scoreLabel=new createScoreLabel(10,30); background=new createBackground(); spike=[]; numberOfSpikes=0; spikeIndex=0; spikeArraySize=4; spikeCountdown=0; startSnd.load(); startSnd.play(); } var gameCanvas={ canvas: document.getElementsByTagName("canvas")[0], start: function(){ this.canvas.width=canvasWidth; this.canvas.height=canvasHeight; this.context=this.canvas.getContext("2d"); } } function createBackground(){ this.x=0; this.width=512; this.move=function(){ this.x+=runSpeed*0.7-1; if(this.x>this.width){ this.x-=this.width; } } this.draw=function(){ ctx=gameCanvas.context; ctx.drawImage(backgroundSpr,0,0,this.width,canvasHeight,Math.round(-this.x),0,this.width,canvasHeight); if(this.x>this.width-canvasWidth){ ctx.drawImage(backgroundSpr,0,0,this.width,canvasHeight,Math.round(this.width-this.x),0,this.width,canvasHeight); } } } function createScoreLabel(x,y){ this.x=x; this.y=y; this.draw=function(){ if(gameOver){ return; } ctx=gameCanvas.context; ctx.font="bold 15px OCR A Std"; ctx.fillStyle="black"; ctx.fillText(String(score),this.x+3,this.y+3); ctx.fillStyle="lightgrey"; ctx.fillText(String(score),this.x,this.y); } } function createPlayer(width,height,x){ this.width=width; this.height=height; this.x=x; this.step=0; this.y=canvasHeight-height; this.currentFrame=0; this.maxFrame=6; this.imgBorder=8; this.draw=function(){ ctx=gameCanvas.context; ctx.drawImage(runningSpr,Math.floor(this.currentFrame)*32,0,32,32,Math.round(this.x),Math.round(this.y),32,32); } this.move=function(){ if(jump&&!isJumping){//Jumping isJumping=true; fallSpeed=-(jumpSpeed+(runSpeed-4)/2); jump=false; jumpSnd.load(); jumpSnd.mozPreservesPitch = false; jumpSnd.playbackRate = 0.75+Math.random()*1.75; jumpSnd.play(); this.step=0; }else if(!jump&&isJumping){//Falling this.y+=fallSpeed; fallSpeed+=gravity+(runSpeed-4)/4; this.stopPlayer(); }else if(!jump&&!isJumping){ this.currentFrame+=animationSpeed+(runSpeed-4)/22; if(this.step<=0){ footstepSnd.load(); footstepSnd.play(); this.step=17.5-((runSpeed-4)); } this.step-=1; if(this.currentFrame>this.maxFrame){ this.currentFrame-=this.maxFrame; } } } this.stopPlayer=function(){ var ground=canvasHeight-this.height; if(this.y>ground){ this.y=ground; isJumping=false; fallspeed=0; } } } function createSpike(type){ this.active=true; this.scored=false; this.type=type; if(type==0){ this.width=48; this.height=24; this.x=canvasWidth; this.y=canvasHeight-this.height; this.imgBorder=12; if(!title){ if(Math.floor(Math.random()*4)==0){ squeek0Snd.load(); squeek0Snd.mozPreservesPitch = false; squeek0Snd.playbackRate = 0.75+Math.random()*1.75; squeek0Snd.play(); }else if(Math.floor(Math.random()*4)==0){ squeek1Snd.load(); squeek1Snd.mozPreservesPitch = false; squeek1Snd.playbackRate = 0.75+Math.random()*1.75; squeek1Snd.play(); } } }else if(type==1){ this.width=48; this.height=32; this.x=canvasWidth; this.y=canvasHeight-this.height-40; this.imgBorder=12; } this.currentFrame=0; this.maxFrame=5; this.draw=function(){ ctx=gameCanvas.context; ctx.drawImage(enemySpr[type],Math.floor(this.currentFrame)*this.width,0,48,32,Math.round(this.x),Math.round(this.y),48,32); } this.move=function(){ this.x-=runSpeed*0.75; if(!title){ if(this.xthis.maxFrame){ this.currentFrame-=this.maxFrame; if(this.type==1){ swoopSnd.load(); swoopSnd.mozPreservesPitch = false; swoopSnd.playbackRate = 2+Math.random(); swoopSnd.play(); } if(this.type==0){ mouseStepSnd.load(); mouseStepSnd.mozPreservesPitch = true; mouseStepSnd.playbackRate = 5+Math.random()*0.5; mouseStepSnd.play(); } } } this.detectCollision=function(){ if(this.x+this.imgBorder>player.x+player.width-player.imgBorder){return false;} if(this.x+this.width-this.imgBorderplayer.y+player.height-player.imgBorder){return false;} if(this.y+this.height-this.imgBorder=spikeArraySize){ spikeIndex=0; } spikeCountdown=40-(runSpeed*1.3); } spike.forEach(a => {if(a.active){ a.move(); a.draw(); }}); ctx.font = "bold 20px OCR A Std"; ctx.textAlign = "center"; ctx.fillStyle = "black"; ctx.fillText("Castle Run", canvasWidth/2+3, canvasHeight/3+3); ctx.fillStyle = "lightgrey"; ctx.fillText("Castle Run", canvasWidth/2, canvasHeight/3); if(--titleclock<=0){ titleclock=80; } if(titleclock>=40){ ctx.font = "bold 10px OCR A Std"; ctx.textAlign = "center"; ctx.fillStyle = "black"; ctx.fillText("click to start", canvasWidth/2+3, canvasHeight*0.7+3); ctx.fillStyle = "lightgrey"; ctx.fillText("click to start", canvasWidth/2, canvasHeight*0.7); } ctx.font = "bold 6px OCR A Std"; ctx.textAlign = "center"; ctx.fillStyle = "black"; ctx.fillText("a game by Isaac Ebert", canvasWidth-50, canvasHeight-6); ctx.fillStyle = "lightgrey"; ctx.fillText("a game by Isaac Ebert", canvasWidth-52, canvasHeight-8); } function drawHighScores(){ ctx=gameCanvas.context; ctx.font = "bold 10px OCR A Std"; ctx.textAlign = "center"; ctx.fillStyle = "black"; ctx.fillText("Hi-Scores", canvasWidth/2+3, canvasHeight/2+3-14); ctx.fillStyle = "lightgrey"; ctx.fillText("Hi-Scores", canvasWidth/2, canvasHeight/2-14); //console.log(String(localStorage.getItem('scores'))); var scores=JSON.parse(localStorage.getItem('scores')); ctx.textAlign = "left"; var scoreColorCancel=false; for(var i=0;i0){ ctx.fillStyle = "lightgreen"; scoreColorCancel=true; }else{ ctx.fillStyle = "lightgrey"; } ctx.fillText(String(i+1)+") "+String(scores[i]), canvasWidth/2-55, canvasHeight/2+i*12-2); } } function updateHighScores(){ if(localStorage.getItem('scores')==null){ var scores=[score,0,0,0,0]; localStorage.setItem('scores','['+String(scores)+']'); }else{ //console.log(String(localStorage.getItem('scores'))); var scores=JSON.parse(localStorage.getItem('scores')); for (var i = 0; i < scores.length; i++) { if(score>scores[i]){ for(var j=scores.length-2;j>=i;j--){ scores[j+1]=scores[j]; } scores[i]=score; break; } } localStorage.setItem('scores',JSON.stringify(scores)); } } function updateCanvas(){ if(title){ drawTitleScreen(); return; } if(!gameOver){ runSpeed = Math.min(runSpeed+0.002,16); ctx=gameCanvas.context; //ctx.clearRect(0,0,canvasWidth,canvasHeight); background.move(); background.draw(); player.move(); player.draw(); if((--spikeCountdown<0&&numberOfSpikes<=2&&Math.floor(Math.random()*10)==0)||numberOfSpikes==0){ numberOfSpikes+=1; spike[spikeIndex]=new createSpike(Math.floor(Math.random()*1.2)); if(++spikeIndex>=spikeArraySize){ spikeIndex=0; } spikeCountdown=40-(runSpeed*2); } spike.forEach(a => {if(a.active){ a.move(); a.draw(); if(a.detectCollision()){ deathSnd.load(); deathSnd.play(); gameover(); return } }}); scoreLabel.draw(); }else if(gameOver){ if(reset){ startSnd.load(); startSnd.play(); gameCanvas.start(); player=new createPlayer(32,32,40); spike=[]; numberOfSpikes=0; spikeIndex=0; spikeArraySize=4; spikeCountdown=0; gameOver=false; score=0; fallSpeed=0; runSpeed=4; canJump=true; jump=false; canReset=false; reset=false; } } } startGame(); var interval=setInterval(updateCanvas,20);