Home
About
Javascript-code
Calculator
Clock
Animation
Game
Basic
☰
AS Program's
Friday, 23 March 2018
GAMES :
Boll Game
Select code
var gameWidth = 360 ; var gameHeight = 320; var intervalOne,intervalTwo,timeoutOne,x var angle = 2;var tempX = 0;var tempY = 0; var block = 1;var square = 0; var squareTop = 0;var squareLeft = 0; var squareMotion = 1;var speed = 80;var getPad = 0; var nextScore = 0;var score = 0; var count = 0;var collisionOne = 0;var collisionTwo = 0;var collisionThree = 0; document.body.style.margin = "0px" document.body.style.padding = "0px" function setupGame(){ document.getElementById("game").style.borderRight = "1px solid #aaa" document.getElementById("game").style.borderRight = "1px solid #aaa" document.getElementById("game").style.borderBottom = "1px solid #aaa" document.getElementById("game").style.width = gameWidth+"px" document.getElementById("game").style.height = gameHeight+"px" document.getElementById("square0").style.position = "absolute" document.getElementById("square0").style.width = "40px" document.getElementById("square0").style.height = "40px" document.getElementById("square0").style.backgroundColor = "blue" document.getElementById("square0").style.display = "none" document.getElementById("square1").style.position = "absolute" document.getElementById("square1").style.width = "40px" document.getElementById("square1").style.height = "40px" document.getElementById("square1").style.backgroundColor = "red" document.getElementById("square1").style.display = "none" document.getElementById("pad").style.position = "absolute" document.getElementById("pad").style.width = "110px" document.getElementById("pad").style.height = "32px" document.getElementById("pad").style.paddingTop = "10px" document.getElementById("pad").style.textAlign = "center" document.getElementById("pad").style.font = "15px Verdana, sans-serif" document.getElementById("pad").style.backgroundColor = "#000" document.getElementById("pad").style.color = "#fff" document.getElementById("pad").innerHTML = "
PLAY
" document.getElementById("play").style.color = "#fff" document.getElementById("play").style.textDecoration = "none" padTop = Math.floor(gameHeight/2)-20 padLeft = Math.floor(gameWidth/2)-30 document.getElementById("pad").style.top = padTop+"px" document.getElementById("pad").style.left = padLeft+"px" document.getElementById("notepad").innerHTML = "BounceGame" document.getElementById("notepad").style.padding = "10px" document.getElementById("notepad").style.textAlign = "center" document.getElementById("notepad").style.font = "2.0em Georgia, serif" document.getElementById("notepad").style.fontWeight = "normal" document.getElementById("notepad").style.color = "#222" timeoutOne = setTimeout("intervalTwo = setInterval('demoGame()', speed)", 7000) }function demoGame(){angle = 2 clearTimeout(timeoutOne) document.getElementById("square0").style.display = "block" document.getElementById("square1").style.display = "block" if(square == 0) {x = document.getElementById("square0") square = 1} else{x = document.getElementById("square1") square = 0} bounceGame()} function newGame(){ block = 0 angle = 2 tempX = 0 tempY = 0 square = 0 squareTop = 0 squareLeft = 0 squareMotion = 1 nextScore = 0 score = 0 count = 0 collisionOne = 0 collisionTwo = 0 collisionThree = 0 clearTimeout(timeoutOne) clearInterval(intervalOne) clearInterval(intervalTwo) document.getElementById("square0").style.left = "0px" document.getElementById("square0").style.top = "0px" document.getElementById("square0").style.display = "block" document.getElementById("square1").style.left = "0px" document.getElementById("square1").style.top = "0px" document.getElementById("square1").style.display = "block" document.getElementById("pad").style.top = (gameHeight-40)+"px" document.getElementById("pad").innerHTML = "" document.getElementById("notepad").innerHTML = "" intervalOne = setInterval("playGame()", speed) }function playGame() { if(block) { return } if(square == 0) { x = document.getElementById("square0") square = 1 } else { x = document.getElementById("square1") square = 0 } bounceGame() checkCollision() } function assignM(aM) { squareMotion = aM } function bounceGame() { if(squareMotion==1) { if(squareTop>=(gameHeight-40) && squareLeft>=(gameWidth-40)) { assignM(3) moveDR(-40) } if(squareTop>=(gameHeight-40)) { assignM(2) moveDL(-40) } else if(squareLeft>=(gameWidth-40)) { assignM(4) moveDL(40) } else { moveDR(40) } } else if(squareMotion==2) { if(squareTop<=0 && squareLeft>=(gameWidth-40)) { assignM(4) moveDL(40) } if(squareLeft>=(gameWidth-40)) { assignM(3) moveDR(-40) } else if(squareTop<=0) { assignM(1) moveDR(40) } else { moveDL(-40) } } else if(squareMotion==3) { if(squareTop<=0 && squareLeft<=0) { assignM(1) moveDR(40) } if(squareTop<=0) { assignM(4) moveDL(40) } else if(squareLeft<=0) { assignM(2) moveDL(-40) } else { moveDR(-40) } } else if(squareMotion==4) { if(squareTop>=(gameHeight-40) && squareLeft<=0) { assignM(2) moveDL(-40) } if(squareLeft<=0) { assignM(1) moveDR(40) } else if(squareTop>=(gameHeight-40)) { assignM(3) moveDR(-40) } else { moveDL(40) } } } function moveDR(amount) { save = amount amount = Math.floor(amount/angle) if(angle == 0) { amount = 0 } squareLeft += amount x.style.left = squareLeft+"px" squareTop += save x.style.top = squareTop+"px" } function moveDL(amount) { save = amount amount = Math.floor(amount/angle) if(angle == 0) { amount = 0 } squareLeft -= amount x.style.left = squareLeft+"px" squareTop += save x.style.top = squareTop+"px" } function assignAngle(aa) { if(aa==1) { angle = 0 nextScore = 1000 } if(aa==2) { angle = 2 nextScore = 100 } score += nextScore document.getElementById("pad").innerHTML = nextScore } function flashScore() { if(score > 0) { if(nextScore == "BounceGame") { nextScore = score } else { nextScore = "BounceGame" } document.getElementById("notepad").innerHTML = nextScore } else { document.getElementById("notepad").innerHTML = "BounceGame" } } function countUp() { if(count < (Math.floor(score/10)*8)) { count += Math.floor(score/10) } else if(count >= (Math.floor(score/10)*8) && count <= (Math.floor(score/10)*9)) { if((Math.floor(score/10)*9) > 200) { count += Math.floor(score/10) } else { count += 10 } } else { if(Math.floor(score/10) > 30) { count += 10 } else { count += 1 } } if(count > score) { count = score clearInterval(intervalOne) intervalOne = setInterval("flashScore()", 2000) } document.getElementById("notepad").innerHTML = count } function checkCollision() { var actualLeft = getPad-30 if(squareTop == 0) { document.getElementById("pad").innerHTML = "" } if((squareTop+40)==(gameHeight-40)) { difference = Math.floor(squareLeft-actualLeft) if(difference>=(-39)&&difference<4) { collisionOne++ collisionTwo = 0 collisionThree = 0 if(collisionOne > 3) { assignM(Math.floor(Math.random() * 2) + 2) } else { assignM(3) } assignAngle(2) } else if(difference>=5&&difference<15) { collisionOne = 0 collisionTwo++ collisionThree = 0 if(collisionTwo > 3) { assignM(Math.floor(Math.random() * 2) + 2) assignAngle(2) } else { assignM(3) assignAngle(1) } } else if(difference>=15&&difference<59) { collisionOne = 0 collisionTwo = 0 collisionThree++ if(collisionThree > 3) { assignM(Math.floor(Math.random() * 2) + 2) } else { assignM(2) } assignAngle(2) } } else if((squareTop+40)==gameHeight) { block = 1 clearInterval(intervalOne) setupGame() intervalOne = setInterval("countUp()", speed) } } function getMouseXY(e) { if(navigator.appName=="Netscape") { tempX = e.pageX tempY = e.pageY } else { tempX = event.clientX + document.body.scrollLeft tempY = event.clientY + document.body.scrollTop } if(tempX < 0) { tempX = 0 } getPad = tempX if(getPad <= 30) { getPad = 30 } if((getPad-30) > Math.floor(gameWidth-60)) { getPad = Math.floor(gameWidth-60)+30 } if(!block) { document.getElementById("pad").style.left = (getPad-30)+"px"}} document.onmousemove = getMouseXY setupGame()
Paddle Game
Select code
var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var ballRadius = 10; var x = canvas.width/2; var y = canvas.height-30; var dx = 2; var dy = -2; var paddleHeight = 10; var paddleWidth = 75; var paddleX = (canvas.width-paddleWidth)/2; var rightPressed = false; var leftPressed = false; var brickRowCount = 5; var brickColumnCount = 3; var brickWidth = 75; var brickHeight = 20; var brickPadding = 10; var brickOffsetTop = 30; var brickOffsetLeft = 30; var score = 0; var lives = 3; var bricks = []; for(c=0; c
0 && relativeX < canvas.width) { paddleX = relativeX - paddleWidth/2; } } function collisionDetection() { for(c=0; c
b.x && x < b.x+brickWidth && y > b.y && y < b.y+brickHeight) { dy = -dy; b.status = 0; score++; if(score == brickRowCount*brickColumnCount) { alert("YOU WIN, CONGRATS!"); document.location.reload(); } } } } } } function drawBall() { ctx.beginPath(); ctx.arc(x, y, ballRadius, 0, Math.PI*2); ctx.fillStyle = "#F00"; ctx.fill(); ctx.closePath(); } function drawPaddle() { ctx.beginPath(); ctx.rect(paddleX, canvas.height-paddleHeight, paddleWidth, paddleHeight); ctx.fillStyle = "#000"; ctx.fill(); ctx.closePath(); } function drawBricks() { for(c=0; c
canvas.width-ballRadius || x + dx < ballRadius) { dx = -dx; } if(y + dy < ballRadius) { dy = -dy; } else if(y + dy > canvas.height-ballRadius) { if(x > paddleX && x < paddleX + paddleWidth) { dy = -dy; } else { lives--; if(!lives) { alert("GAME OVER"); document.location.reload(); } else { x = canvas.width/2; y = canvas.height-30; dx = 3; dy = -3; paddleX = (canvas.width-paddleWidth)/2; } } } if(rightPressed && paddleX < canvas.width-paddleWidth) { paddleX += 7; } else if(leftPressed && paddleX > 0) { paddleX -= 7; } x += dx; y += dy; requestAnimationFrame(draw); } draw();
Snake Game
Select code
$(document).ready(function(){ var canvas = $("#canvas")[0]; var ctx = canvas.getContext("2d"); var w = $("#canvas").width(); var h = $("#canvas").height(); var cw = 11; var d; var food; var score; var snake_array; function init() { d = "right"; create_snake(); create_food(); score = 0.5; if(typeof game_loop != "undefined") clearInterval(game_loop); game_loop = setInterval(paint, 80); } init(); function create_snake() { var length = 5; snake_array = []; for(var i = length-1; i>=0; i--) { snake_array.push({x: i, y:0}); } } function create_food() { food = { x: Math.round(Math.random()*(w-cw)/cw), y: Math.round(Math.random()*(h-cw)/cw), }; } function paint() { ctx.fillStyle = "white"; ctx.fillRect(0, 0, w, h); ctx.strokeStyle = "black"; ctx.strokeRect(0, 0, w, h); var nx = snake_array[0].x; var ny = snake_array[0].y; if(d == "right") nx++; else if(d == "left") nx--; else if(d == "up") ny--; else if(d == "down") ny++; if(nx == -1 || nx == w/cw || ny == -1 || ny == h/cw || check_collision(nx, ny, snake_array)) { init(); return; } if(nx == food.x && ny == food.y) { var tail = {x: nx, y: ny}; score++; create_food(); } else { var tail = snake_array.pop(); tail.x = nx; tail.y = ny; } snake_array.unshift(tail); for(var i = 0; i < snake_array.length; i++) { var c = snake_array[i]; paint_cell(c.x, c.y); } paint_cell(food.x, food.y); var score_text = "Score: " + score; ctx.fillText(score_text, 5, h-5); } function paint_cell(x, y) { ctx.fillStyle = "red"; ctx.fillRect(x*cw, y*cw, cw, cw); ctx.strokeStyle = "white"; ctx.strokeRect(x*cw, y*cw, cw, cw); } function check_collision(x, y, array) { for(var i = 0; i < array.length; i++) { if(array[i].x == x && array[i].y == y) return true; } return false; } $(document).keydown(function(e){ var key = e.which; if(key == "37" && d != "right") d = "left"; else if(key == "38" && d != "down") d = "up"; else if(key == "39" && d != "left") d = "right"; else if(key == "40" && d != "up") d = "down"; }) })
Chess Game
Select code
var B,i,y,u,b,I=[],G=120,x=10,z=15,M=1e4,l=[5,3,4,6,2,4,3,5,1,1,1,1,1,1,1,1,9,9 ,9,9,9,9,9,9,13,11,12,14,10,12,11,13,0,99,0,306,297,495,846,-1,0,1,2,2,1,0,-1,- 1,1,-10,10,-11,-9,9,11,10,20,-9,-11,-10,-20,-21,-19,-12,-8,8,12,19,21]; function X(w,c,h,e,S,s) {var t,o,L,E,d,O=e,N=-M*M,K=78-h<
=h&&X(0,0,0,21,0,0)>M; do{if(o=I[p=O]){q=o&z^y;if(q<7){A=q--& 2?8:4;C=o-9&z?[53,47,61,51,47,47][q]:57; do{r=I[p+=l[C]]; if(!w|p==w){g=q|p+a-S?0 :S; if(!r&(!!q|A<3||!!g)||(r+1&z^y)>9&&q|A>2) {if(m=!(r-2&7))return y^=8,I[G--]= O,K;J=n=o&z;E=I[p-a]&z;t=q|E-7?n:(n+=2,6^y); while(n<=t){L=r?l[r&7|32]-h-q:0; if( s)L+=(1-q?l[(p-p%x)/x+37]-l[(O-O%x)/x+37]+l[p%x+38]*(q?1:2)-l[O%x+38]+(o&16)/2: !!m*9)+(!q?!(I[p-1]^n)+!(I[p+1]^n)+l[n&7|32]-99+!!g*99+(A<2):0)+!(E^y^9); if(s>h ||1
z|d){I[p]=n,I[O]=m?(I[g]=I[m],I[m]=0):g?I[g]=0:0;L-=X(s>h|d?0:p,L -N,h+1,I[G+1],J=q|A>1?0:p,s); if(!(h||s-1|B-O|i-n|p-b|L<-M)) return W(),G--,u=J; J=q-1|A<7||m||!s|d|r|o
M;I[O]=o;I[p]=r;m?(I[m]=I[g],I[g]=0): g?I[g]=9^y:0;} if(L>N||s>1&&L==N&&!h&&Math.random()<.5){I[G]=O;if(s>1) {if(h&&c-L <0)return y^=8,G--,L;if(!h)i=n,B=O,b=p;}N=L;}n+=J||(g=p,m=p
2||(p=O,q|A>2|o>z&!r&&++C*--A));}}}while( ++O>98?O=20:e-O);return y^=8,G--,N+M*M&&N>-K+1924|d?N:0;}B=i=y=u=0;while(B++< 120)I[B-1]=B%x?B/x%x<2|B%x<2?7:B/x&4?0:l[i++]|16:7;for(a= "
",i=18;i<100;a+=++i%10-9? "
":"c0c0ff>"):(i++,"
")); a+="
"; document.write(a+"
N
"); function W(){B=b;for(p=21;p<99;++p)if(q=document.getElementById("o"+p)){q. innerHTML="
";q. style.borderColor=p==B?"red":"white";}}W(); function Y(s){i=(I[s]^y)&z;if(i>8){b=s;W();}else if(B&&i<9){b=s;i=I[B]&z;if((i& 7)==1&(b<29|b>90))i=14-document.getElementById("t").selectedIndex^y;X(0,0,0,21, u,1);if(y)setTimeout("X(0,0,0,21,u,2/*ply*/),X(0,0,0,21,u,1)",250);}} for(B=i=y=u=b=i=5-5,x=10,I=[],l=[];B++<304; I[B-1]=B%x?B/x%x<2|B%x<2?7:B/x&4?0:l[i++]=("cheetah").charCodeAt(y++)-64:7);function X(c,h,e,s){c^=8;for(var c,h,e,e,t,a,h,d=e&&X(c,0 )>1e4,n,N=-1e8,O=20,K=78-h<<9;++O<99;)if((o=I[T=O])&&(G=o^c)<7){A=G--&2?8:4;C= o-9?l[61+G]:49;do if(!(R=I[T+=l[C]])&&!!G|A<3||(R+1^c)>9&&G|A>2){if(!(R-2&7) )return K;n=G|(c?T>29:T<91)?o:6^c;S=(R&&l[R&7|32]*2-h-G)+(n-o?110:!G&&(A<2)+1); if(e>h||1
2|d){I[T]=n;I[O]=0;S-=X(c,h+1,e,S-N);if(!(h||e-1|B-O|T-b|S< -1e4))return W(),c&&setTimeout("X(8,0,2),X(8,0,1)",75);I[O]=o;I[T]=R}if(S>N||!h &S==N&&Math.random()<.5)if(N=S,e>1)if(h?s-S<0:(B=O,b=T,0))break}while(!R&G>2||( T=O,(G||A>2|(c?O>78:O<41)&!R)&&++C*--A))}return-K+768
";for(u=18;u<99;document.body.innerHTML=i+=++u%x-9? "
"+(I[u]?9808+l[67+I[u]]:160):u++&&"
")B=b}W() for(B=i=y=u=b=i=5-5,x=10,z=15,I=[],l=[];l[B]=("ustvrtsuqqqqqqqq"+"yyyyyyyy}{|~z|{}@G@TSb~?A6J57IKJT576,+-48HLSUmgukgg OJNMLK IDHGFE").charCodeAt(B)-64,B++<120;I[B-1]=B%x?B/x%x<2|B%x<2?7:B/x&4?0:l[u++]:7);function X(c,h,e,S,s){c^=8;for(var T,o,L,E,D,O=20,G,N=-1e8,n,g,d=S&&X(c,0)>1e4,C,R,A,K=78-h<<9,a=c?x:-x;++O<99;)if((o=I[T=O])&&(G=o&z^c)<7){A=G--&2?8:4;C=9-o& z?l[61+G]:49;do{R=I[T+=l[C]];g=D=G|T+a-e?0:e;if(!R&&(G||A<3||g)||(1+R&z^c)>9&&G |A>2){if(!(2-R&7))return K;for(E=n=G|I[T-a]-7?o&z:6^c;E;E=!E&&!d&&!(g=T,D=T
h||1
2|d){I[T]=n,I[g]=I[D],I[O]=D?I[D]=0:0;L-=X(c,h+1 ,E=G|A>1?0:T,S,L-N);if(!(h||S-1|B-O|T-b|L<-1e4))return W(y=E),c&&setTimeout( "X(8,0,y,2),X(8,0,y,1)",75);E=1-G|A<7|D|!S|R|o
1e4;I[O]=o;I[T]=R;I[D ]=I[g];D?I[g]=G?0:9^c:0}if(L>N||!h&L==N&&Math.random()<.5)if(N=L,S>1)if(h?s-L<0 :(B=O,b=T,0))return N}}}while(!R&G>2||(T=O,G|A>2|z
";for(u=18;u<98;document.body.innerHTML=i+=++ u%x-9?"
"+(I[u]&15?9808+l[67+(I[u]&15)]:160)+";":u++&&"
")B=b}W()
Piono Tiles
Select code
Piono-Tiles
body{ background-position: center; background-size: cover; background-repeat: no-repeat; margin: 0px; padding: 0px; width: 100%; height: 100%; background-image:url(rainbow_texture679.jpg); } .gameContainer{ margin: 0 auto; background: transparent; } .gamePianotitle{ background-color: #00F; width: 300px; height: 500px; margin: 0 auto; display: block; border: 1px solid #111; cursor: pointer; border-left: none; box-shadow: 2px 3px 10px #000; border:inset; border-collapse:collapse; border-color:#FFF; } .replay{ display: none; } .center{ text-align: center; } .fullButton{ width: 300px; padding: 10px 20px; font-size: 1.2rem; font-family: sans-serif; color: #000; background-color: #FFF; margin-top: 10px; border: 1px solid #333; outline: none; cursor: pointer; border-radius:15px 20px 5px; font-size:26px; box-shadow:2px 4px 8px #000000; } function _(id) { return document.getElementById(id); } var config = { playing: false, isGameOver: false, cols: 4, rows: 5, width: 300, height: 500, speed: 8, tile: { border: 1, color: { default: "#FFF", clicked: "#CCCCCC", unclicked: "#000", wrong: "#FF0000" } }, gameInterval: null, score: 0, maxScore: 0, accent: "#18f3ad", font: "bold 30px monospace", fontNormal: "bold 20px monospace", intervalTime: 40, incrementSpeedAfterTile: 10 }; function tileObject() { this.width = config.width / config.cols; this.height = config.height / config.rows; this.x = 0; this.y = 0; this.bgColor = config.tile.color.default; this.borderColor = "#000"; this.border = 1; this.clickable = false; this.isClicked = false; this.row = 0; this.col = 0; } var tileHolder = []; function init() { tileHolder = []; var halfRow = Math.round(config.rows / 2); var startTile = null; for (var i = 0; i < config.rows + 2; i++) { var selectedRandomTile = false; for (var j = 0; j < (config.cols); j++) { var tile = new tileObject(); tile.x = tile.width * j; tile.y = config.height - tile.height * i; tile.row = i; tile.col = j; if (!selectedRandomTile) { if ( (Math.ceil(Math.random() * 5) == 2) || j == config.cols - 1 ) { selectedRandomTile = true; if (i >= halfRow) { if (startTile == null) { startTile = tile; } makeTileClickable(tile); } } } tileHolder.push(tile); } } draw(); drawStartTile(startTile); } function makeTileClickable(tile) { tile.clickable = true; tile.isClicked = false; tile.bgColor = config.tile.color.unclicked; } function draw() { if (config.playing) { moveToNextFrame(); } var c = _("gameCanvas").getContext('2d'); c.clearRect(0, 0, config.width, config.height); for (var i = 0; i < tileHolder.length; i++) { var tempTile = tileHolder[i]; drawTile(tempTile); } }function drawTile(tempTile) { var c = _("gameCanvas").getContext('2d'); c.fillStyle = tempTile.borderColor; c.fillRect(tempTile.x, tempTile.y, tempTile.width, tempTile.height); c.fillStyle = tempTile.bgColor; c.fillRect(tempTile.x + tempTile.border, tempTile.y + tempTile.border, tempTile.width - tempTile.border, tempTile.height - tempTile.border); c.fill(); } function moveToNextFrame() { var len = tileHolder.length; var maxPosition = getMaxRowPosition(); var tempTileHolder = []; for (var i = 0; i < len; i++) { var tempTile = tileHolder[i]; if (tempTile.clickable) { if (!tempTile.isClicked) { if (tempTile.y + tempTile.height + config.speed >= config.height) { tempTile.bgColor = config.tile.color.wrong; gameOver(); } } } if (tempTile.y > config.height) { tempTile.y = maxPosition - tempTile.height; resetTileExceptXYPosition(tempTile); tempTileHolder.push(tempTile); } tempTile.y += config.speed; } if (tempTileHolder.length > 0) { var randomeNumber = Math.ceil(Math.random() * config.cols - 1); makeTileClickable(tempTileHolder[randomeNumber]); } }function resetTileExceptXYPosition(tile) { var y = tile.y; var x = tile.x; var tempTile = new tileObject(); for (var k in tempTile) { tile[k] = tempTile[k]; } tile.y = y; tile.x = x; } function getMaxRowPosition() { var len = tileHolder.length; var maxTile = null; for (var i = 0; i < len; i += config.cols) { var tempTile = tileHolder[i]; if (maxTile == null) { maxTile = tempTile; } else if (maxTile.y > tempTile.y) { maxTile = tempTile; } } return maxTile.y; } function draw() { if (config.playing) { moveToNextFrame(); } var c = _("gameCanvas").getContext('2d'); c.clearRect(0, 0, config.width, config.height); for (var i = 0; i < tileHolder.length; i++) { var tempTile = tileHolder[i]; drawTile(tempTile); } } function startGame() { config.playing = true; config.gameInterval = setInterval(draw, 40); }function gameOver() { config.isGameOver = true; config.playing = false; config.score = 0; clearInterval(config.gameInterval); var c = _("gameCanvas").getContext('2d'); c.font = config.font; c.fillStyle = config.accent; c.textAlign = "center"; c.fillText("Game Over", config.width / 2, 150); var img = _("replay"); c.drawImage(img, config.width / 2 - 30, config.height - 50 - 30, 60, 60); } function getTileInPosition(coords) { var x = coords.x; var y = coords.y; var len = tileHolder.length; for (var i = 0; i < len; i++) { var tempTile = tileHolder[i]; if (x > tempTile.x && x < tempTile.x + tempTile.width) { if (y > tempTile.y && y < tempTile.y + tempTile.height) { return tempTile; } } } return null; }function gameMouseClick(e) { if (config.isGameOver) { init(); config.isGameOver = false; return; } else { if (!config.playing) { startGame(); } } var x = e.clientX - _("gameCanvas").offsetLeft + window.scrollX; var y = e.clientY - _("gameCanvas").offsetTop + window.scrollY; var clickedTile = getTileInPosition({x: x, y: y}); if (clickedTile != null) { if (clickedTile.clickable) { if (!clickedTile.isClicked) { console.log("Nice!"); clickedTile.isClicked = true; clickedTile.bgColor = config.tile.color.clicked; config.score++; } else { clickedTile.bgColor = config.tile.color.wrong; drawTile(clickedTile); gameOver(); } } else { clickedTile.bgColor = config.tile.color.wrong; drawTile(clickedTile); gameOver(); } } else { console.log("Tile Not Found"); } } function drawText(c) { c.font = config.font; c.fillStyle = config.accent; c.textAlign = "center"; c.fillText(config.score, config.width / 2, 30); } function controleSpeed() { var num = config.incrementSpeedAfterTile; if (config.score % num == num - 1) { var speed = config.intervalTime - 5 * Math.round(config.score / num); if (speed < 5) { speed = 5; } else { clearInterval(config.gameInterval); config.gameInterval = setInterval(draw, speed); } } } function draw() { if (config.playing) { moveToNextFrame(); } var c = _("gameCanvas").getContext('2d'); c.clearRect(0, 0, config.width, config.height); for (var i = 0; i < tileHolder.length; i++) { var tempTile = tileHolder[i]; drawTile(tempTile); } drawText(c); controleSpeed(); } function drawStartTile(tile) { var c = _("gameCanvas").getContext('2d'); c.font = config.fontNormal; c.fillStyle = config.tile.color.default; c.textAlign = "center"; c.textBaseline = "middle"; var x = tile.x + tile.width / 2; var y = tile.y + tile.height / 2; c.fillText("Start", x, y); } init();
Tic Tac Toe
Select code
24X Game
Select code
2 X
2 X
0
0
New-Game
Keep going
Try again
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
Animations :
Animated Disk Select code Please p...
Introduction
Introduction To JavaScript : Let’s see what’s so special about JavaScript, what we can achieve with it and which other tec...
About-me
My name is "Abdul Sami Khan" I'm Software Engineer & Seo Expert and i'm student of Gexton and Aptech My E...
Animations :
Animated Disk Select code Please p...