Pages
Home
Who I am
Star War
Word Game
Snake Game
Tetris Game
Plane Rider
Sudoku
Casino
Snake Game
easy
medium
hard
score :
0
Powered by
Mostafa Rastegar
Source:
<div id="sceneContainer" align="center"></div> <script type="text/javascript"> var tbl; var lblScore; var lblExtraFood; var severitySelect; var maxX = 20; var maxY = 20; var endGame = false; var score = 0; var exteraFood = null; var canExtraFood = true; var currentPosX = parseInt(maxX / 2); var currentPosY = parseInt(maxY / 2); var colors = ["red", "green", "blue", "cyan", "brown", "yellow" , "cyan", "blue", "red", "green", "cyan", "brown", "yellow" , "cyan", "purple"]; var currentDirection = 6; // 6 -> right | 8 -> up | 4 -> left | 2 -> down function appendToScene(el) { document.getElementById("sceneContainer").appendChild(el); } function createScene() { severitySelect = document.createElement("select"); appendToScene(severitySelect); var easy = document.createElement("option"); easy.innerHTML = "easy"; easy.value = 250; var medium = document.createElement("option"); medium.innerHTML = "medium"; medium.value = 150; var hard = document.createElement("option"); hard.innerHTML = "hard"; hard.value = 50; severitySelect.appendChild(easy); severitySelect.appendChild(medium); severitySelect.appendChild(hard); tbl = document.createElement("table"); tbl.style.borderColor = "black"; tbl.style.borderWidth = "1px"; tbl.style.borderStyle = "solid"; tbl.style.width = "200px"; tbl.style.height = "200px"; var tbody = document.createElement("tbody"); tbl.appendChild(tbody); for (var i = 0; i <= maxX; i ++) { var tr = document.createElement("tr"); tbody.appendChild(tr); for (var j = 0; j <= maxY; j ++) { var td = document.createElement("td"); tr.appendChild(td); td.style.fontSize = "4px"; td.innerHTML = " "; td.player = false; td.food = false; td.score = 0; td.currentX = i; td.currentY = j; td.nextCell = null; } } appendToScene(tbl); lblScore = document.createElement("span"); lblScore.innerHTML = score; var div = document.createElement("div"); var scoreLabel = document.createElement("span"); scoreLabel.innerHTML = "score : "; div.appendChild(scoreLabel); div.appendChild(lblScore); lblExtraFood = document.createElement("span"); div.appendChild(lblExtraFood); appendToScene(div); var copyRightDiv = document.createElement("div"); appendToScene(copyRightDiv); copyRightDiv.style.textAlign = "center"; copyRightDiv.innerHTML = "Powered by <a href='mailto:mostafarastgar@gmail.com'>Mostafa Rastegar</a>"; } function addSubtractX(x, count, as) { if (as == "a") { if (x + count > maxX) { return (x + count) - maxX - 1; } else { return x + count } } else { if (x - count < 0) { return maxX - (count - x) + 1; } else { return x - count; } } } function addSubtractY(y, count, as) { if (as == "a") { if (y + count > maxY) { return (y + count) - maxY - 1; } else { return y + count } } else { if (y - count < 0) { return maxY - (count - y) + 1; } else { return y - count; } } } function canChangeDirection(newDirection) { if ((currentDirection == 6 && newDirection == 4) || (currentDirection == 8 && newDirection == 2) || (currentDirection == 4 && newDirection == 6) || (currentDirection == 2 && newDirection == 8)) return false; else return true; } function colorPlayer() { var cell0 = tbl.rows[currentPosX].cells[currentPosY]; cell0.style.backgroundColor = "brown"; cell0.player = true; var cell1 = tbl.rows[currentPosX].cells[addSubtractY(currentPosY, 1, "s")]; cell1.style.backgroundColor = "black"; cell1.player = true; cell0.nextCell = cell1; var cell2 = tbl.rows[currentPosX].cells[addSubtractY(currentPosY, 2, "s")]; cell2.style.backgroundColor = "black"; cell2.player = true; cell1.nextCell = cell2; var cell3 = tbl.rows[currentPosX].cells[addSubtractY(currentPosY, 3, "s")]; cell3.style.backgroundColor = "black"; cell3.player = true; cell2.nextCell = cell3; cell3.nextCell = null; } function gameOver() { endGame = true; var div = document.createElement("div"); div.style.opacity = "0.4"; div.style.filter = "alpha(opacity:40)"; colorDiv(div); div.style.width = "100%"; div.style.height = "100%"; div.style.textAlign = "center"; div.innerHTML = " "; appendToScene(div); div.style.position = "absolute"; div.style.top = "0px"; div.style.left = "0px"; var goDiv = document.createElement("div"); goDiv.innerHTML = "GAME OVER"; goDiv.style.position = "absolute"; goDiv.style.top = "50%"; goDiv.style.left = "50%"; appendToScene(goDiv); } function colorDiv(div) { if (div.style.backgroundColor == "red") div.style.backgroundColor = "green"; else if (div.style.backgroundColor == "green") div.style.backgroundColor = "blue"; else div.style.backgroundColor = "red"; setTimeout(function() { colorDiv(div); }, 1000); } function carryPlayer() { if (!endGame) { moveCell(tbl.rows[currentPosX].cells[currentPosY], null, null); setTimeout(function() { carryPlayer(); }, severitySelect.value); } } function makeFood() { var y = 0; var x = 0; do{ var d = new Date(); x = d.getMilliseconds() % (maxX + 1) y = d.getMilliseconds() % (maxY + 1) } while (tbl.rows[x].cells[y].player == true) tbl.rows[x].cells[y].food = true; if (score == 0 || score % 10 != 0 || !canExtraFood) { tbl.rows[x].cells[y].style.backgroundColor = "green"; tbl.rows[x].cells[y].score = 1; canExtraFood = true; } else { tbl.rows[x].cells[y].score = 10; exteraFood = tbl.rows[x].cells[y]; setTimer(15); } } function setTimer(leftTime) { if (!endGame) { lblExtraFood.innerHTML = ""; if (exteraFood != null) { if (leftTime == 0) { exteraFood.food = false; exteraFood.style.backgroundColor = ""; exteraFood.score = 0; exteraFood = null; canExtraFood = false; makeFood(); } else { lblExtraFood.innerHTML = " " + leftTime; exteraFood.style.backgroundColor = colors[leftTime - 1]; setTimeout(function() { setTimer(leftTime - 1) }, severitySelect.value); } } } } function init() { createScene(); colorPlayer(); carryPlayer(); makeFood(); document.onkeydown = function movePlayer(event) { var event = document.all ? window.event : event; if (!endGame) { if (event.keyCode == 37) { if (canChangeDirection(4)) { currentDirection = 4; moveCell(tbl.rows[currentPosX].cells[currentPosY], null, null); } } if (event.keyCode == 38) { if (canChangeDirection(8)) { currentDirection = 8; moveCell(tbl.rows[currentPosX].cells[currentPosY], null, null); } } if (event.keyCode == 39) { if (canChangeDirection(6)) { currentDirection = 6; moveCell(tbl.rows[currentPosX].cells[currentPosY], null, null); } } if (event.keyCode == 40) { if (canChangeDirection(2)) { currentDirection = 2; moveCell(tbl.rows[currentPosX].cells[currentPosY], null, null); } } } if (event.keyCode == 27) { alert("waiting ..."); } return false; } } function addScore(scoreAmount) { score = score + scoreAmount; lblScore.innerHTML = score; } function cutCell(newCell, oldCell) { newCell.player = oldCell.player; newCell.style.backgroundColor = oldCell.style.backgroundColor; return newCell; } function getDirection(cell, preCellOld) { if (tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "a")] == preCellOld) return 6; else if (tbl.rows[addSubtractX(cell.currentX, 1, "s")].cells[cell.currentY] == preCellOld) return 8; else if (tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "s")] == preCellOld) return 4; else return 2; } function moveCell(cell, preCell, preCellOld) { var newCell; var food = false; if (preCell == null) { if (currentDirection == 6) { if (tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "a")].player == true) { gameOver(); return; } if (tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "a")].food == true) { tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "a")].food = false; food = true; } newCell = cutCell(tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "a")], cell); currentPosY = addSubtractY(currentPosY, 1, "a"); } else if (currentDirection == 8) { if (tbl.rows[addSubtractX(cell.currentX, 1, "s")].cells[cell.currentY].player == true) { gameOver(); return; } if (tbl.rows[addSubtractX(cell.currentX, 1, "s")].cells[cell.currentY].food == true) { tbl.rows[addSubtractX(cell.currentX, 1, "s")].cells[cell.currentY].food = false; food = true; } newCell = cutCell(tbl.rows[addSubtractX(cell.currentX, 1, "s")].cells[cell.currentY], cell); currentPosX = addSubtractX(currentPosX, 1, "s"); } else if (currentDirection == 4) { if (tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "s")].player == true) { gameOver(); return; } if (tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "s")].food == true) { tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "s")].food = false; food = true; } newCell = cutCell(tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "s")], cell); currentPosY = addSubtractY(currentPosY, 1, "s"); } else { if (tbl.rows[addSubtractX(cell.currentX, 1, "a")].cells[cell.currentY].player == true) { gameOver(); return; } if (tbl.rows[addSubtractX(cell.currentX, 1, "a")].cells[cell.currentY].food == true) { tbl.rows[addSubtractX(cell.currentX, 1, "a")].cells[cell.currentY].food = false; food = true; } newCell = cutCell(tbl.rows[addSubtractX(cell.currentX, 1, "a")].cells[cell.currentY], cell); currentPosX = addSubtractX(currentPosX, 1, "a"); } } else { switch (getDirection(cell, preCellOld)) { case 6: newCell = cutCell(tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "a")], cell); break; case 8: newCell = cutCell(tbl.rows[addSubtractX(cell.currentX, 1, "s")].cells[cell.currentY], cell); break; case 4: newCell = cutCell(tbl.rows[cell.currentX].cells[addSubtractY(cell.currentY, 1, "s")], cell); break; default: newCell = cutCell(tbl.rows[addSubtractX(cell.currentX, 1, "a")].cells[cell.currentY], cell); } preCell.nextCell = newCell; } if (food == true) { newCell.nextCell = cell; cell.player = true; cell.style.backgroundColor = "black"; addScore(newCell.score); if (newCell.score > 1) canExtraFood = false; newCell.food = false; newCell.score = 0; exteraFood = null; makeFood(); } else { if (cell.nextCell != null) moveCell(cell.nextCell, newCell, cell); else { cell.player = false; newCell.nextCell = null; cell.style.backgroundColor = ""; } } } init(); </script>
Home
Subscribe to:
Posts (Atom)