<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<title>Yılan Oyunu</title>
<style>
body {
margin: 0;
background-color: #222;
color: #fff;
font-family: Arial, sans-serif;
overflow: hidden;
}
#game-container {
position: relative;
margin: auto;
margin-top: 50px;
width: 400px;
height: 400px;
background-color: #333;
overflow: hidden;
border: 2px solid #555;
}
#game-canvas {
background-color: #000;
}
#start-screen, #settings-screen, #ai-screen {
position: absolute;
top: 0;
left: 0;
width: 400px;
height: 400px;
background-color: rgba(0, 0, 0, 0.9);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 2;
}
button {
padding: 10px 20px;
margin: 5px;
font-size: 16px;
cursor: pointer;
}
#score-board {
position: absolute;
top: 10px;
left: 10px;
}
#money-depot {
position: absolute;
top: 10px;
right: 10px;
}
#control-buttons {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.control-btn {
width: 60px;
height: 60px;
margin: 5px;
opacity: 0.7;
}
#up-btn { }
#down-btn { }
#left-btn { }
#right-btn { }
</style>
</head>
<body>
<div id="game-container">
<canvas id="game-canvas" width="400" height="400"></canvas>
<div id="start-screen">
<h1>Yılan Oyunu</h1>
<button onclick="startGame()">Oyuna Başla</button>
<button onclick="openSettings()">Ayarlar</button>
<button onclick="startAI()">Yapay Zeka Modu</button>
</div>
<div id="settings-screen" style="display: none;">
<h2>Ayarlar</h2>
<label>Skor Başına Para:
<input type="number" id="money-per-score" value="10">
</label>
<br><br>
<label>Para Birimi:
<input type="text" id="currency-unit" value="TL">
</label>
<br><br>
<button onclick="saveSettings()">Kaydet</button>
<button onclick="closeSettings()">Kapat</button>
</div>
<div id="ai-screen" style="display: none;">
<h2>Yapay Zeka Modu</h2>
<button onclick="startAIGame()">Başlat</button>
<button onclick="closeAI()">Kapat</button>
</div>
<div id="score-board">En Yüksek Skor: <span id="high-score">0</span></div>
<div id="money-depot">Toplam Para: <span id="total-money">0</span> <span id="currency-symbol">TL</span></div>
<div id="control-buttons">
<button id="up-btn" class="control-btn" onclick="changeDirection('up')">↑</button><br>
<button id="left-btn" class="control-btn" onclick="changeDirection('left')">←</button>
<button id="down-btn" class="control-btn" onclick="changeDirection('down')">↓</button>
<button id="right-btn" class="control-btn" onclick="changeDirection('right')">→</button>
</div>
</div>
<script>
let canvas = document.getElementById("game-canvas");
let ctx = canvas.getContext("2d");
let box = 20;
let snake = [];
snake[0] = {
x: 9 * box,
y: 9 * box
};
let direction;
let food = {
x: Math.floor(Math.random()*19 + 1) * box,
y: Math.floor(Math.random()*19 + 1) * box
};
let score = 0;
let highScore = localStorage.getItem("highScore") || 0;
let totalMoney = localStorage.getItem("totalMoney") || 0;
let moneyPerScore = localStorage.getItem("moneyPerScore") || 10;
let currencyUnit = localStorage.getItem("currencyUnit") || "TL";
document.getElementById("high-score").innerText = formatNumber(highScore);
document.getElementById("total-money").innerText = formatNumber(totalMoney);
document.getElementById("currency-symbol").innerText = currencyUnit;
document.addEventListener("keydown",directionControl);
function directionControl(event){
if(event.keyCode == 37 && direction != "right"){
direction = "left";
} else if(event.keyCode == 38 && direction != "down"){
direction = "up";
} else if(event.keyCode == 39 && direction != "left"){
direction = "right";
} else if(event.keyCode == 40 && direction != "up"){
direction = "down";
}
}
function changeDirection(dir){
if(dir == "left" && direction != "right"){
direction = "left";
} else if(dir == "up" && direction != "down"){
direction = "up";
} else if(dir == "right" && direction != "left"){
direction = "right";
} else if(dir == "down" && direction != "up"){
direction = "down";
}
}
function drawGame(){
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
for(let i = 0; i < snake.length; i++){
ctx.fillStyle = (i == 0)? "#0f0" : "#0a0";
ctx.fillRect(snake[i].x, snake[i].y, box, box);
}
ctx.fillStyle = "#f00";
ctx.fillRect(food.x, food.y, box, box);
let snakeX = snake[0].x;
let snakeY = snake[0].y;
if(direction == "left") snakeX -= box;
if(direction == "up") snakeY -= box;
if(direction == "right") snakeX += box;
if(direction == "down") snakeY += box;
if(snakeX == food.x && snakeY == food.y){
score++;
totalMoney = parseInt(totalMoney) + parseInt(moneyPerScore);
document.getElementById("total-money").innerText = formatNumber(totalMoney);
localStorage.setItem("totalMoney", totalMoney);
food = {
x: Math.floor(Math.random()*19 + 1) * box,
y: Math.floor(Math.random()*19 + 1) * box
};
} else {
snake.pop();
}
let newHead = {
x: snakeX,
y: snakeY
};
if(snakeX < 0 || snakeX >= canvas.width || snakeY < 0 || snakeY >= canvas.height || collision(newHead, snake)){
clearInterval(game);
if(score > highScore){
highScore = score;
localStorage.setItem("highScore", highScore);
document.getElementById("high-score").innerText = formatNumber(highScore);
}
localStorage.setItem("totalMoney", totalMoney);
alert("Oyun Bitti! Skor: " + formatNumber(score));
location.reload();
}
snake.unshift(newHead);
}
function collision(head, array){
for(let i = 0; i < array.length; i++){
if(head.x == array[i].x && head.y == array[i].y){
return true;
}
}
return false;
}
function startGame(){
document.getElementById("start-screen").style.display = "none";
game = setInterval(drawGame, 100);
}
function openSettings(){
document.getElementById("settings-screen").style.display = "flex";
}
function closeSettings(){
document.getElementById("settings-screen").style.display = "none";
}
function saveSettings(){
moneyPerScore = document.getElementById("money-per-score").value;
currencyUnit = document.getElementById("currency-unit").value;
localStorage.setItem("moneyPerScore", moneyPerScore);
localStorage.setItem("currencyUnit", currencyUnit);
document.getElementById("currency-symbol").innerText = currencyUnit;
alert("Ayarlar Kaydedildi!");
}
function formatNumber(num){
num = parseInt(num);
if(num >= 1e12){
return (num/1e12).toFixed(2) + "T";
} else if(num >= 1e9){
return (num/1e9).toFixed(2) + "B";
} else if(num >= 1e6){
return (num/1e6).toFixed(2) + "M";
} else if(num >= 1e3){
return (num/1e3).toFixed(2) + "K";
} else {
return num;
}
}
function startAI(){
document.getElementById("ai-screen").style.display = "flex";
}
function closeAI(){
document.getElementById("ai-screen").style.display = "none";
}
function startAIGame(){
document.getElementById("ai-screen").style.display = "none";
aiMode = true;
game = setInterval(drawAIGame, 100);
}
let aiMode = false;
function drawAIGame(){
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
for(let i = 0; i < snake.length; i++){
ctx.fillStyle = (i == 0)? "#0f0" : "#0a0";
ctx.fillRect(snake[i].x, snake[i].y, box, box);
}
ctx.fillStyle = "#f00";
ctx.fillRect(food.x, food.y, box, box);
let snakeX = snake[0].x;
let snakeY = snake[0].y;
// Basit AI için en kısa yolu bulma
if(snakeX < food.x && direction != "left") direction = "right";
else if(snakeX > food.x && direction != "right") direction = "left";
else if(snakeY < food.y && direction != "up") direction = "down";
else if(snakeY > food.y && direction != "down") direction = "up";
if(direction == "left") snakeX -= box;
if(direction == "up") snakeY -= box;
if(direction == "right") snakeX += box;
if(direction == "down") snakeY += box;
if(snakeX == food.x && snakeY == food.y){
score++;
totalMoney = parseInt(totalMoney) + parseInt(moneyPerScore);
document.getElementById("total-money").innerText = formatNumber(totalMoney);
localStorage.setItem("totalMoney", totalMoney);
food = {
x: Math.floor(Math.random()*19 + 1) * box,
y: Math.floor(Math.random()*19 + 1) * box
};
} else {
snake.pop();
}
let newHead = {
x: snakeX,
y: snakeY
};
if(snakeX < 0 || snakeX >= canvas.width || snakeY < 0 || snakeY >= canvas.height || collision(newHead, snake)){
clearInterval(game);
if(score > highScore){
highScore = score;
localStorage.setItem("highScore", highScore);
document.getElementById("high-score").innerText = formatNumber(highScore);
}
localStorage.setItem("totalMoney", totalMoney);
alert("Yapay Zeka Oyunu Bitti! Skor: " + formatNumber(score));
location.reload();
}
snake.unshift(newHead);
}
</script>
</body>
</html>