Index.html
Index.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game with No Self-Backtrack</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
#gameContainer {
position: relative;
width: 300px;
height: 300px;
background-color: #fff;
border: 2px solid #000;
overflow: hidden;
}
.snake-segment {
position: absolute;
width: 10px;
height: 10px;
background-color: green;
}
#food {
position: absolute;
width: 10px;
height: 10px;
background-color: red;
}
#gameOver {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
.hidden {
display: none;
}
#score {
position: absolute;
top: 10px;
left: 10px;
font-size: 18px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="score">Score: 0</div>
<div id="food"></div>
<div id="gameOver" class="hidden">
<h1>Game Over</h1>
<button id="resetBtn">Play Again</button>
</div>
</div>
<script>
const gameContainer = document.getElementById('gameContainer');
const food = document.getElementById('food');
const gameOverScreen = document.getElementById('gameOver');
const resetBtn = document.getElementById('resetBtn');
const scoreDisplay = document.getElementById('score');
let snakePosition = [
{ x: 150, y: 150 },
{ x: 140, y: 150 },
{ x: 130, y: 150 }
];
let direction = { x: 0, y: 0 };
let foodPosition = { x: 0, y: 0 };
let gameInterval;
let gameOver = false;
let firstAppleEaten = false;
let score = 0;
function startGame() {
resetGame();
gameInterval = setInterval(moveSnake, 100);
spawnFood();
}
function resetGame() {
snakePosition = [
{ x: 150, y: 150 },
{ x: 140, y: 150 },
{ x: 130, y: 150 }
];
direction = { x: 0, y: 0 };
gameOver = false;
firstAppleEaten = false;
score = 0;
scoreDisplay.textContent = `Score: ${score}`;
gameOverScreen.classList.add('hidden');
renderSnake();
}
function moveSnake() {
if (gameOver) return;
renderSnake();
}
function renderSnake() {
gameContainer.querySelectorAll('.snake-segment').forEach(segment =>
segment.remove());
snakePosition.forEach(segment => {
const segmentDiv = document.createElement('div');
segmentDiv.classList.add('snake-segment');
segmentDiv.style.transform = `translate(${segment.x}px, $
{segment.y}px)`;
gameContainer.appendChild(segmentDiv);
});
}
function spawnFood() {
foodPosition.x = Math.floor(Math.random() * 30) * 10;
foodPosition.y = Math.floor(Math.random() * 30) * 10;
food.style.transform = `translate(${foodPosition.x}px, $
{foodPosition.y}px)`;
}
function handleTouchStart(event) {
const touch = event.touches[0];
startX = touch.clientX;
startY = touch.clientY;
}
function handleTouchEnd(event) {
const touch = event.changedTouches[0];
const xDiff = touch.clientX - startX;
const yDiff = touch.clientY - startY;
resetBtn.addEventListener('click', startGame);
window.addEventListener('touchstart', handleTouchStart);
window.addEventListener('touchend', handleTouchEnd);
startGame();
</script>
</body>
</html>