ga jadi ga isa konek database e dek aku
This commit is contained in:
parent
92cfd08b94
commit
867a449ed7
619
GameLogic.js
619
GameLogic.js
@ -20,26 +20,56 @@ var ModeH = false;
|
|||||||
const persiapan = document.getElementById("start");
|
const persiapan = document.getElementById("start");
|
||||||
|
|
||||||
//generate gambar external
|
//generate gambar external
|
||||||
var KepalaKeKanan = new Image();
|
|
||||||
KepalaKeKanan.src = "image/KepalaHorizontalKanan.png";
|
|
||||||
var KepalaKeAtas = new Image();
|
|
||||||
KepalaKeAtas.src = "image/KepalaVertikalAtas.png";
|
|
||||||
var KepalaKeKiri = new Image();
|
|
||||||
KepalaKeKiri.src = "image/KepalaVertikalBawah.png"; // Asumsi ini adalah Kepala ke KIRI
|
|
||||||
var KepalaKeBawah = new Image();
|
|
||||||
KepalaKeBawah.src = "image/KepalaHorizontalKiri.png"; // Asumsi ini adalah Kepala ke BAWAH
|
|
||||||
// ↑ kepala ular
|
|
||||||
|
|
||||||
var BadanHori = new Image();
|
//kepala
|
||||||
BadanHori.src = "image/BadanHorizontal.png";
|
const kepala = {
|
||||||
var BadanVerti = new Image();
|
kanan: new Image(),
|
||||||
BadanVerti.src = "image/BadanVertical.png";
|
kiri: new Image(),
|
||||||
// ↑ badan ular
|
atas: new Image(),
|
||||||
|
bawah: new Image(),
|
||||||
|
};
|
||||||
|
kepala.kanan.src = "image/KepalaHorizontalKanan.png";
|
||||||
|
kepala.kiri.src = "image/KepalaHorizontalKiri.png";
|
||||||
|
kepala.atas.src = "image/KepalaVertikalAtas.png";
|
||||||
|
kepala.bawah.src = "image/KepalaVertikalBawah.png";
|
||||||
|
|
||||||
|
//badan
|
||||||
|
const badan = {
|
||||||
|
horizontal: new Image(),
|
||||||
|
vertical: new Image(),
|
||||||
|
};
|
||||||
|
badan.horizontal.src = "image/BadanHorizontal.png";
|
||||||
|
badan.vertical.src = "image/BadanVertikal.png";
|
||||||
|
|
||||||
|
//const beluk
|
||||||
|
const belok = {
|
||||||
|
AtasKanan: new Image(),
|
||||||
|
AtasKiri: new Image(),
|
||||||
|
BawahKanan: new Image(),
|
||||||
|
BawahKiri: new Image(),
|
||||||
|
};
|
||||||
|
belok.AtasKanan.src = "image/BelokAtasKanan.png";
|
||||||
|
belok.AtasKiri.src = "image/BelokAtasKiri.png";
|
||||||
|
belok.BawahKanan.src = "image/BelokBawahKanan.png";
|
||||||
|
belok.BawahKiri.src = "image/BelokBawahKiri.png";
|
||||||
|
|
||||||
|
//bokong
|
||||||
|
const bokong = {
|
||||||
|
kanan: new Image(),
|
||||||
|
kiri: new Image(),
|
||||||
|
atas: new Image(),
|
||||||
|
bawah: new Image(),
|
||||||
|
};
|
||||||
|
bokong.kanan.src = "image/BokongHorizontalKanan.png";
|
||||||
|
bokong.kiri.src = "image/BokongHorizontalKiri.png";
|
||||||
|
bokong.atas.src = "image/BokongVertikalAtas.png";
|
||||||
|
bokong.bawah.src = "image/BokongVertikalBawah.png";
|
||||||
|
|
||||||
|
|
||||||
var ApelImage = new Image();
|
var ApelImage = new Image();
|
||||||
ApelImage.src = "image/ApelLayer.png"; // Apel
|
ApelImage.src = "image/ApelLayer.png";
|
||||||
var TembokImage = new Image();
|
var TembokImage = new Image();
|
||||||
TembokImage.src = "image/Tembok.png"; // Tembok Batagor
|
TembokImage.src = "image/Tembok.png";
|
||||||
|
|
||||||
//set posisi ular dan Apel
|
//set posisi ular dan Apel
|
||||||
var Ular = { x: 528, y: 240, dx: grid, dy: 0, cells: [], maxCells: 4 };
|
var Ular = { x: 528, y: 240, dx: grid, dy: 0, cells: [], maxCells: 4 };
|
||||||
@ -48,355 +78,382 @@ var Tembok = [];
|
|||||||
UpdateScore(0);
|
UpdateScore(0);
|
||||||
RandomizeApel();
|
RandomizeApel();
|
||||||
|
|
||||||
//mastiin gambar external generate dulu sebelum game start
|
|
||||||
KepalaKeKanan.onload = function () {
|
function getDirection(cell1, cell2) {
|
||||||
// Memastikan gambar termuat, meskipun fungsi ini tidak melakukan apa-apa selain deklarasi
|
if (cell2.x > cell1.x) return "kanan";
|
||||||
function gameLoop() {}
|
if (cell2.x < cell1.x) return "kiri";
|
||||||
};
|
if (cell2.y > cell1.y) return "bawah";
|
||||||
|
if (cell2.y < cell1.y) return "atas";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
//ngatur biar game ga langsung jalan
|
//ngatur biar game ga langsung jalan
|
||||||
function StartingGame(menantang) {
|
function StartingGame(menantang) {
|
||||||
ModeH = menantang;
|
ModeH = menantang;
|
||||||
persiapan.style.display = "none";
|
persiapan.style.display = "none";
|
||||||
if (ModeH) {
|
if (ModeH) {
|
||||||
RandomSpawnWall();
|
RandomSpawnWall();
|
||||||
} else {
|
} else {
|
||||||
Tembok = [];
|
Tembok = [];
|
||||||
}
|
}
|
||||||
GameStart = true;
|
GameStart = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//buat ngatur batu spwan apa ga nanti
|
//buat ngatur batu spwan apa ga nanti
|
||||||
document.getElementById("mode-normal").addEventListener("click", function () {
|
document.getElementById("mode-normal").addEventListener("click", function () {
|
||||||
StartingGame(false);
|
StartingGame(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.getElementById("mode-tambahan").addEventListener("click", function () {
|
document.getElementById("mode-tambahan").addEventListener("click", function () {
|
||||||
StartingGame(true);
|
StartingGame(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
//play again pas gameover
|
//play again pas gameover
|
||||||
function resetGame() {
|
function resetGame() {
|
||||||
Ular.x = 528;
|
Ular.x = 528;
|
||||||
Ular.y = 240;
|
Ular.y = 240;
|
||||||
Ular.cells = [];
|
Ular.cells = [];
|
||||||
Ular.maxCells = 4;
|
Ular.maxCells = 4;
|
||||||
Ular.dx = grid;
|
Ular.dx = grid;
|
||||||
Ular.dy = 0;
|
Ular.dy = 0;
|
||||||
Tembok = [];
|
Tembok = [];
|
||||||
RandomizeApel();
|
RandomizeApel();
|
||||||
UpdateScore(0);
|
UpdateScore(0);
|
||||||
ModeH = false;
|
|
||||||
// Penting: Jangan set GameStart=false di sini jika ingin memulai ulang game loop
|
|
||||||
// Kita set GameStart=false saat game over, lalu di sini kita persiapkan UI
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ngatur tombol pas gameover kemana abis di klik
|
//ngatur tombol pas gameover kemana abis di klik
|
||||||
PlayAgain.addEventListener("click", function () {
|
PlayAgain.addEventListener("click", function () {
|
||||||
UpDead.style.display = "none";
|
UpDead.style.display = "none";
|
||||||
resetGame();
|
resetGame();
|
||||||
persiapan.style.display = "flex";
|
ModeH = false;
|
||||||
|
persiapan.style.display = "flex";
|
||||||
});
|
});
|
||||||
|
|
||||||
Udahan.addEventListener("click", function () {
|
Udahan.addEventListener("click", function () {
|
||||||
UpDead.style.display = "none";
|
UpDead.style.display = "none";
|
||||||
resetGame();
|
resetGame();
|
||||||
// Opsional: Redirect ke halaman lain
|
ModeH = false;
|
||||||
// window.location.href = 'leaderboard.php';
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//gameover terus ngatur ular ke setting awal
|
//gameover terus ngatur ular ke setting awal
|
||||||
function gameLoop() {
|
function gameLoop() {
|
||||||
if (!GameStart) {
|
if (!GameStart) {
|
||||||
requestAnimationFrame(gameLoop);
|
requestAnimationFrame(gameLoop);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ClearCanvas();
|
ClearCanvas();
|
||||||
Movement();
|
Movement();
|
||||||
IntiGame();
|
IntiGame();
|
||||||
|
|
||||||
requestAnimationFrame(gameLoop);
|
requestAnimationFrame(gameLoop);
|
||||||
}
|
}
|
||||||
|
|
||||||
//random spawn Apel
|
//random spawn Apel
|
||||||
function RandomizeApel() {
|
function RandomizeApel() {
|
||||||
var pembataslebar = Math.floor(canvas.width / grid);
|
var pembataslebar = Math.floor(canvas.width / grid);
|
||||||
var pembatastinggi = Math.floor(canvas.height / grid);
|
var pembatastinggi = Math.floor(canvas.height / grid);
|
||||||
|
|
||||||
|
do {
|
||||||
|
Apel.x = Math.floor(Math.random() * pembataslebar) * grid;
|
||||||
|
Apel.y = Math.floor(Math.random() * pembatastinggi) * grid;
|
||||||
|
|
||||||
|
//mastiin Apel gak spawn di atas ular
|
||||||
|
var isOverlapSnake = Ular.cells.some(cell => cell.x === Apel.x && cell.y === Apel.y);
|
||||||
|
|
||||||
|
//mastiin Apel gak spawn di atas tembok
|
||||||
|
var isOverlapWall = Tembok.some(bata => bata.x === Apel.x && bata.y === Apel.y);
|
||||||
|
|
||||||
do {
|
} while (isOverlapSnake || isOverlapWall);
|
||||||
Apel.x = Math.floor(Math.random() * pembataslebar) * grid;
|
|
||||||
Apel.y = Math.floor(Math.random() * pembatastinggi) * grid;
|
|
||||||
|
|
||||||
// Pastikan Apel tidak spawn di atas ular
|
|
||||||
var isOverlapSnake = Ular.cells.some(cell => cell.x === Apel.x && cell.y === Apel.y);
|
|
||||||
|
|
||||||
// Pastikan Apel tidak spawn di atas tembok
|
|
||||||
var isOverlapWall = Tembok.some(bata => bata.x === Apel.x && bata.y === Apel.y);
|
|
||||||
|
|
||||||
} while (isOverlapSnake || isOverlapWall);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//random spawn tembok
|
//random spawn tembok
|
||||||
function RandomSpawnWall() {
|
function RandomSpawnWall() {
|
||||||
var TembokX, TembokY;
|
var TembokX, TembokY;
|
||||||
var kosong;
|
var kosong;
|
||||||
var pembataslebar = Math.floor(canvas.width / grid);
|
var pembataslebar = Math.floor(canvas.width / grid);
|
||||||
var pembatastinggi = Math.floor(canvas.height / grid);
|
var pembatastinggi = Math.floor(canvas.height / grid);
|
||||||
|
|
||||||
//create tembok
|
//create tembok
|
||||||
do {
|
do {
|
||||||
kosong = true;
|
kosong = true;
|
||||||
|
|
||||||
TembokX = Math.floor(Math.random() * pembataslebar) * grid;
|
TembokX = Math.floor(Math.random() * pembataslebar) * grid;
|
||||||
TembokY = Math.floor(Math.random() * pembatastinggi) * grid;
|
TembokY = Math.floor(Math.random() * pembatastinggi) * grid;
|
||||||
|
|
||||||
// cek untuk posisi yang mau di kasih tembok ada/tidak ada ularnya
|
//cek untuk posisi yang mau di kasih tembok ada/tidak ada ularnya
|
||||||
for (var i = 0; i < Ular.cells.length; i++) {
|
for (var i = 0; i < Ular.cells.length; i++) {
|
||||||
// Hindari spawn di sekitar kepala ular (misal 5 kotak pertama)
|
if (Math.abs(Ular.cells[i].x - TembokX) <= grid * 5 && Math.abs(Ular.cells[i].y - TembokY) <= grid * 5) {
|
||||||
if (Math.abs(Ular.cells[i].x - TembokX) <= grid * 5 && Math.abs(Ular.cells[i].y - TembokY) <= grid * 5) {
|
kosong = false;
|
||||||
kosong = false;
|
break;
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// tidak memperbolehkan tembok spawn sama dengan apel
|
// tidak memperbolehkan tembok spawn sama dengan apel
|
||||||
if (TembokX === Apel.x && TembokY === Apel.y) {
|
if (TembokX === Apel.x && TembokY === Apel.y) {
|
||||||
kosong = false;
|
kosong = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cek agar tembok tidak menumpuk
|
//cek agar tembok tidak menumpuk
|
||||||
for (var i = 0; i < Tembok.length; i++) {
|
for (var i = 0; i < Tembok.length; i++) {
|
||||||
if (Tembok[i].x === TembokX && Tembok[i].y === TembokY) {
|
if (Tembok[i].x === TembokX && Tembok[i].y === TembokY) {
|
||||||
kosong = false;
|
kosong = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (kosong === false);
|
} while (kosong === false);
|
||||||
Tembok.push({ x: TembokX, y: TembokY });
|
Tembok.push({ x: TembokX, y: TembokY });
|
||||||
}
|
}
|
||||||
|
|
||||||
//nambah tembok tiap score kelipatan
|
//nambah tembok tiap score kelipatan
|
||||||
function PenambahanTembok() {
|
function PenambahanTembok() {
|
||||||
if (ModeH) {
|
if (ModeH) {
|
||||||
// Tambah tembok setiap 2 skor, tapi jangan melebihi batas (misal 15 tembok)
|
if (score > 0 && score % 2 === 0 && Tembok.length < 15 && Tembok.length < score / 2) {
|
||||||
if (score > 0 && score % 2 === 0 && Tembok.length < 15 && Tembok.length < score / 2) {
|
RandomSpawnWall();
|
||||||
RandomSpawnWall();
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//set dan update score
|
//set dan update score
|
||||||
function UpdateScore(amount) {
|
function UpdateScore(amount) {
|
||||||
score = amount > 0 ? score + amount : 0;
|
score = amount > 0 ? score + amount : 0;
|
||||||
// Highscore hanya di update jika skor > highscore yang TERCATAT LOKAL.
|
highscore = score > highscore ? score : highscore;
|
||||||
// Highscore dari database biasanya diambil saat inisialisasi.
|
|
||||||
highscore = score > highscore ? score : highscore;
|
Text.innerHTML =
|
||||||
|
"Score: " + score + "<br>Highscore: " + highscore + "<br>Speed: " + speed;
|
||||||
|
|
||||||
Text.innerHTML =
|
PenambahanTembok();
|
||||||
"Score: " + score + "<br>Highscore: " + highscore + "<br>Speed: " + speed;
|
|
||||||
|
|
||||||
PenambahanTembok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//tampilin gameover
|
//tampilin gameover
|
||||||
function GameOver() {
|
function GameOver() {
|
||||||
GameStart = false;
|
GameStart = false;
|
||||||
ClearCanvas();
|
ClearCanvas();
|
||||||
|
|
||||||
const GameMode = ModeH ? "Tambahan" : "Normal";
|
const GameMode = ModeH ? "Tambahan" : "Normal";
|
||||||
|
if (score > 0) {
|
||||||
|
}
|
||||||
|
|
||||||
// Kirim skor ke server hanya jika skor lebih besar dari 0 (atau sesuai kriteria)
|
ScoreMain.innerHTML = "Score: " + score;
|
||||||
if (score > 0) {
|
UpDead.style.display = "flex";
|
||||||
kirimSkorKeServer(score, GameMode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TAMPILKAN POP-UP GAME OVER
|
|
||||||
ScoreMain.innerHTML = "Score: " + score;
|
|
||||||
UpDead.style.display = "flex";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//reset isi canvas doang
|
//reset isi canvas doang
|
||||||
function ClearCanvas() {
|
function ClearCanvas() {
|
||||||
content.clearRect(0, 0, canvas.width, canvas.height);
|
content.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
//bagian utama:
|
//bagian utama:
|
||||||
//ular makan dan mati pas ketemu badannya
|
//ular makan dan mati pas ketemu badannya
|
||||||
function IntiGame() {
|
function IntiGame() {
|
||||||
// buat gambarnya bisa keluar
|
// buat gambarnya bisa keluar
|
||||||
content.drawImage(ApelImage, Apel.x, Apel.y, grid, grid);
|
content.drawImage(ApelImage, Apel.x, Apel.y, grid, grid);
|
||||||
|
|
||||||
|
// Gambar Tembok
|
||||||
|
Tembok.forEach(function (bata) {
|
||||||
|
content.drawImage(TembokImage, bata.x, bata.y, grid, grid);
|
||||||
|
});
|
||||||
|
|
||||||
// Gambar Tembok
|
// Gambar Ular
|
||||||
Tembok.forEach(function (bata) {
|
|
||||||
content.drawImage(TembokImage, bata.x, bata.y, grid, grid);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Gambar Ular
|
|
||||||
Ular.cells.forEach(function (cell, index) {
|
Ular.cells.forEach(function (cell, index) {
|
||||||
if (index === 0) {
|
var gambarUlar = null;
|
||||||
// Logika Pemilihan Gambar Kepala Ular
|
//pala ulo
|
||||||
var posisiKepalaImage;
|
if (index === 0) {
|
||||||
if (Ular.dx === grid) { // KANAN
|
if (Ular.dx === grid) gambarUlar = kepala.kanan;
|
||||||
posisiKepalaImage = KepalaKeKanan;
|
else if (Ular.dx === -grid) gambarUlar = kepala.kiri;
|
||||||
} else if (Ular.dx === -grid) { // KIRI
|
else if (Ular.dy === -grid) gambarUlar = kepala.atas;
|
||||||
posisiKepalaImage = KepalaKeKiri;
|
else if (Ular.dy === grid) gambarUlar = kepala.bawah;
|
||||||
} else if (Ular.dy === -grid) { // ATAS
|
}
|
||||||
posisiKepalaImage = KepalaKeAtas; // Perlu dicek, di atas Anda menamakannya KepalaKeBawah?
|
|
||||||
} else if (Ular.dy === grid) { // BAWAH
|
|
||||||
posisiKepalaImage = KepalaKeBawah; // Perlu dicek, di atas Anda menamakannya KepalaKeAtas?
|
|
||||||
} else {
|
|
||||||
posisiKepalaImage = KepalaKeKanan;
|
|
||||||
}
|
|
||||||
content.drawImage(posisiKepalaImage, cell.x, cell.y, grid, grid);
|
|
||||||
} else {
|
|
||||||
// Logika sederhana untuk badan: selalu horizontal
|
|
||||||
// Catatan: untuk badan vertikal/belokan, perlu logika tambahan, tapi kita ikuti kode asli Anda.
|
|
||||||
content.drawImage(BadanHori, cell.x, cell.y, grid, grid);
|
|
||||||
}
|
|
||||||
|
|
||||||
// buat pas ular makan Apel (Deteksi tabrakan kepala vs Apel)
|
//bokong
|
||||||
if (index === 0 && cell.x === Apel.x && cell.y === Apel.y) {
|
else if (index === Ular.cells.length - 1) {
|
||||||
Ular.maxCells += 1;
|
var sebelum = Ular.cells[index - 1];
|
||||||
UpdateScore(1);
|
var arahEkor = getDirection(sebelum, cell);
|
||||||
RandomizeApel();
|
|
||||||
}
|
|
||||||
|
|
||||||
// tabrak tembok = mati (Hanya kepala yang dicek)
|
if (arahEkor === "kanan") gambarUlar = bokong.kanan;
|
||||||
if (index === 0) {
|
else if (arahEkor === "kiri") gambarUlar = bokong.kiri;
|
||||||
Tembok.forEach(function (bata) {
|
else if (arahEkor === "atas") gambarUlar = bokong.atas;
|
||||||
if (cell.x === bata.x && cell.y === bata.y) {
|
else if (arahEkor === "bawah") gambarUlar = bokong.bawah;
|
||||||
GameOver();
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// buat pas ular mati kena badan sendiri (Hanya kepala vs sisa badan)
|
//badan
|
||||||
|
else {
|
||||||
|
var sebelum = Ular.cells[index - 1];
|
||||||
|
var sesudah = Ular.cells[index + 1];
|
||||||
|
|
||||||
|
var ArahMasuk = getDirection(sebelum, cell);
|
||||||
|
var ArahKeluar = getDirection(cell, sesudah);
|
||||||
|
|
||||||
|
if (ArahMasuk && ArahKeluar) {
|
||||||
|
if (ArahMasuk === ArahKeluar) {
|
||||||
|
// Badan Lurus (Horizontal/Vertical)
|
||||||
|
if (ArahMasuk === "kiri" || ArahMasuk === "kanan") {
|
||||||
|
gambarUlar = badan.horizontal;
|
||||||
|
} else {
|
||||||
|
gambarUlar = badan.vertical;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Belokan (Dibalik: Atas menggunakan kondisi Bawah, Bawah menggunakan kondisi Atas)
|
||||||
|
else {
|
||||||
|
// belok.AtasKanan (Menggunakan logika BawahKanan sebelumnya)
|
||||||
|
if ((ArahMasuk === "bawah" && ArahKeluar === "kanan") || (ArahMasuk === "kiri" && ArahKeluar === "atas"))
|
||||||
|
gambarUlar = belok.AtasKanan;
|
||||||
|
|
||||||
|
// belok.AtasKiri (Menggunakan logika BawahKiri sebelumnya)
|
||||||
|
else if ((ArahMasuk === "bawah" && ArahKeluar === "kiri") || (ArahMasuk === "kanan" && ArahKeluar === "atas"))
|
||||||
|
gambarUlar = belok.AtasKiri;
|
||||||
|
|
||||||
|
// belok.BawahKanan (Menggunakan logika AtasKanan sebelumnya)
|
||||||
|
else if ((ArahMasuk === "atas" && ArahKeluar === "kanan") || (ArahMasuk === "kiri" && ArahKeluar === "bawah"))
|
||||||
|
gambarUlar = belok.BawahKanan;
|
||||||
|
|
||||||
|
// belok.BawahKiri (Menggunakan logika AtasKiri sebelumnya)
|
||||||
|
else if ((ArahMasuk === "atas" && ArahKeluar === "kiri") || (ArahMasuk === "kanan" && ArahKeluar === "bawah"))
|
||||||
|
gambarUlar = belok.BawahKiri;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gambarUlar) {
|
||||||
|
content.drawImage(gambarUlar, cell.x, cell.y, grid, grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
//ular makan apel
|
||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
for (var i = 1; i < Ular.cells.length; i++) { // Mulai dari index 1 (badan)
|
if (cell.x === Apel.x && cell.y === Apel.y) {
|
||||||
if (cell.x === Ular.cells[i].x && cell.y === Ular.cells[i].y) {
|
Ular.maxCells += 1;
|
||||||
GameOver();
|
UpdateScore(1);
|
||||||
}
|
RandomizeApel();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
//nabrak tembok
|
||||||
|
Tembok.forEach(function (bata) {
|
||||||
|
if (cell.x === bata.x && cell.y === bata.y) {
|
||||||
|
GameOver();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//nabrak diri e dw
|
||||||
|
for (var i = 1; i < Ular.cells.length; i++) {
|
||||||
|
if (cell.x === Ular.cells[i].x && cell.y === Ular.cells[i].y) {
|
||||||
|
GameOver();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//update teks pada speed kalau score update
|
//update speed kalau score update
|
||||||
function Movement() {
|
function Movement() {
|
||||||
if (++count < speed) return;
|
if (++count < speed) return;
|
||||||
if (ArahUlar > 0) ArahUlar--;
|
if (ArahUlar > 0) ArahUlar--;
|
||||||
count = 0;
|
count = 0;
|
||||||
|
|
||||||
|
Ular.x += Ular.dx;
|
||||||
|
Ular.y += Ular.dy;
|
||||||
|
|
||||||
|
if (
|
||||||
|
Ular.x < 0 ||
|
||||||
|
Ular.x >= canvas.width ||
|
||||||
|
Ular.y < 0 ||
|
||||||
|
Ular.y >= canvas.height
|
||||||
|
)
|
||||||
|
GameOver();
|
||||||
|
|
||||||
|
Ular.cells.unshift({ x: Ular.x, y: Ular.y });
|
||||||
|
|
||||||
// Pindahkan kepala
|
if (Ular.cells.length > Ular.maxCells) Ular.cells.pop();
|
||||||
Ular.x += Ular.dx;
|
|
||||||
Ular.y += Ular.dy;
|
|
||||||
|
|
||||||
// Cek batas layar
|
|
||||||
if (
|
|
||||||
Ular.x < 0 ||
|
|
||||||
Ular.x >= canvas.width ||
|
|
||||||
Ular.y < 0 ||
|
|
||||||
Ular.y >= canvas.height
|
|
||||||
)
|
|
||||||
GameOver();
|
|
||||||
|
|
||||||
// Tambahkan posisi baru ke sel pertama (Kepala)
|
|
||||||
Ular.cells.unshift({ x: Ular.x, y: Ular.y });
|
|
||||||
|
|
||||||
// Hapus sel terakhir (Ekor)
|
|
||||||
if (Ular.cells.length > Ular.maxCells) Ular.cells.pop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//input keyboard
|
//input keyboard
|
||||||
function InputKeyboard() {
|
function InputKeyboard() {
|
||||||
document.addEventListener("keydown", function (e) {
|
document.addEventListener("keydown", function (e) {
|
||||||
if (!GameStart) return;
|
if (!GameStart) return;
|
||||||
|
|
||||||
// Key A, D (Kiri/Kanan)
|
if (
|
||||||
if (
|
ArahUlar == 0 &&
|
||||||
ArahUlar == 0 &&
|
((e.code == "KeyA" && Ular.dx === 0) ||
|
||||||
((e.code == "KeyA" && Ular.dx === 0) ||
|
(e.code == "KeyD" && Ular.dx === 0))
|
||||||
(e.code == "KeyD" && Ular.dx === 0))
|
) {
|
||||||
) {
|
ArahUlar = 1;
|
||||||
ArahUlar = 1;
|
Ular.dx = e.code === "KeyA" ? -grid : grid;
|
||||||
Ular.dx = e.code === "KeyA" ? -grid : grid;
|
Ular.dy = 0;
|
||||||
Ular.dy = 0;
|
}
|
||||||
}
|
|
||||||
// Key W, S (Atas/Bawah)
|
|
||||||
else if (
|
|
||||||
ArahUlar == 0 &&
|
|
||||||
((e.code === "KeyW" && Ular.dy === 0) ||
|
|
||||||
(e.code == "KeyS" && Ular.dy === 0))
|
|
||||||
) {
|
|
||||||
ArahUlar = 1;
|
|
||||||
Ular.dy = e.code == "KeyW" ? -grid : grid;
|
|
||||||
Ular.dx = 0;
|
|
||||||
}
|
|
||||||
// Arrow Up, Down
|
|
||||||
else if (
|
|
||||||
ArahUlar == 0 &&
|
|
||||||
((e.code === "ArrowUp" && Ular.dy === 0) ||
|
|
||||||
(e.code == "ArrowDown" && Ular.dy === 0))
|
|
||||||
) {
|
|
||||||
ArahUlar = 1;
|
|
||||||
Ular.dy = e.code == "ArrowUp" ? -grid : grid;
|
|
||||||
Ular.dx = 0;
|
|
||||||
}
|
|
||||||
// Arrow Left, Right
|
|
||||||
else if (
|
|
||||||
ArahUlar == 0 &&
|
|
||||||
((e.code === "ArrowLeft" && Ular.dx === 0) ||
|
|
||||||
(e.code == "ArrowRight" && Ular.dx === 0))
|
|
||||||
) {
|
|
||||||
ArahUlar = 1;
|
|
||||||
Ular.dx = e.code == "ArrowLeft" ? -grid : grid;
|
|
||||||
Ular.dy = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Key E (Cepat/Kurangi speed), Key Q (Lambat/Tambah speed)
|
else if (
|
||||||
if (e.code === "KeyE" || e.code == "KeyQ")
|
ArahUlar == 0 &&
|
||||||
speed =
|
((e.code === "KeyW" && Ular.dy === 0) ||
|
||||||
e.code == "KeyE" && speed > 2
|
(e.code == "KeyS" && Ular.dy === 0))
|
||||||
? speed - 1
|
) {
|
||||||
: e.code == "KeyQ" && speed < 30
|
ArahUlar = 1;
|
||||||
? speed + 1
|
Ular.dy = e.code == "KeyW" ? -grid : grid;
|
||||||
: speed;
|
Ular.dx = 0;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
else if (
|
||||||
|
ArahUlar == 0 &&
|
||||||
|
((e.code === "ArrowUp" && Ular.dy === 0) ||
|
||||||
|
(e.code == "ArrowDown" && Ular.dy === 0))
|
||||||
|
) {
|
||||||
|
ArahUlar = 1;
|
||||||
|
Ular.dy = e.code == "ArrowUp" ? -grid : grid;
|
||||||
|
Ular.dx = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (
|
||||||
|
ArahUlar == 0 &&
|
||||||
|
((e.code === "ArrowLeft" && Ular.dx === 0) ||
|
||||||
|
(e.code == "ArrowRight" && Ular.dx === 0))
|
||||||
|
) {
|
||||||
|
ArahUlar = 1;
|
||||||
|
Ular.dx = e.code == "ArrowLeft" ? -grid : grid;
|
||||||
|
Ular.dy = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e.code === "KeyE" || e.code == "KeyQ")
|
||||||
|
speed =
|
||||||
|
e.code == "KeyE" && speed > 2
|
||||||
|
? speed - 1
|
||||||
|
: e.code == "KeyQ" && speed < 30
|
||||||
|
? speed + 1
|
||||||
|
: speed;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nti ae la daud
|
||||||
// function kirimSkorKeServer(skor, GameMode) {
|
// function kirimSkorKeServer(skor, GameMode) {
|
||||||
// console.log(`Mengirim skor ${skor} (Mode: ${GameMode}) ke server...`);
|
// console.log(`Mengirim skor ${skor} (Mode: ${GameMode}) ke server...`);
|
||||||
|
|
||||||
// fetch('score.php', {
|
// fetch('score.php', {
|
||||||
// method: 'POST',
|
// method: 'POST',
|
||||||
// headers: {
|
// headers: {
|
||||||
// 'Content-Type': 'application/json',
|
// 'Content-Type': 'application/json',
|
||||||
// },
|
// },
|
||||||
// body: JSON.stringify({
|
// body: JSON.stringify({
|
||||||
// score: skor,
|
// score: skor,
|
||||||
// mode: GameMode
|
// mode: GameMode
|
||||||
// }),
|
// }),
|
||||||
// })
|
// })
|
||||||
// .then(response => {
|
// .then(response => {
|
||||||
// if (!response.ok) {
|
// if (!response.ok) {
|
||||||
// return response.json().then(error => { throw new Error(error.message || 'Gagal menyimpan skor'); });
|
// return response.json().then(error => { throw new Error(error.message || 'Gagal menyimpan skor'); });
|
||||||
// }
|
// }
|
||||||
// return response.json();
|
// return response.json();
|
||||||
// })
|
// })
|
||||||
// .then(data => {
|
// .then(data => {
|
||||||
// if (data.status === 'success') {
|
// if (data.status === 'success') {
|
||||||
// console.log('✅ Berhasil:', data.message);
|
// console.log('✅ Berhasil:', data.message);
|
||||||
// if (data.message.includes('Highscore baru')) {
|
// if (data.message.includes('Highscore baru')) {
|
||||||
// highscore = skor;
|
// highscore = skor;
|
||||||
// UpdateScore(0);
|
// UpdateScore(0);
|
||||||
// }
|
// }
|
||||||
// } else {
|
// } else {
|
||||||
// console.error('❌ Error Server:', data.message);
|
// console.error('❌ Error Server:', data.message);
|
||||||
// }
|
// }
|
||||||
// })
|
// })
|
||||||
// .catch((error) => {
|
// .catch((error) => {
|
||||||
// console.error('⚠️ Error Jaringan atau Proses:', error.message);
|
// console.error('⚠️ Error Jaringan atau Proses:', error.message);
|
||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
InputKeyboard();
|
InputKeyboard();
|
||||||
|
|||||||
81
score.php
Normal file
81
score.php
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'koneksi.php';
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
http_response_code(405);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Metode tidak diizinkan.']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($_SESSION['id_user']) && !isset($_SESSION['username'])) {
|
||||||
|
http_response_code(401);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Anda harus login untuk menyimpan skor.']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$input = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
$final_score = (int)($input['score'] ?? 0);
|
||||||
|
|
||||||
|
if ($final_score <= 0) {
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Skor tidak valid atau 0.']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user_id = null;
|
||||||
|
if (isset($_SESSION['id_user'])) {
|
||||||
|
$user_id = $_SESSION['id_user'];
|
||||||
|
} elseif (isset($_SESSION['username'])) {
|
||||||
|
|
||||||
|
$username = $_SESSION['username'];
|
||||||
|
|
||||||
|
$getID_sql = "SELECT id_user FROM users WHERE username = '$username'";
|
||||||
|
$result_id = mysqli_query($koneksi, $getID_sql);
|
||||||
|
|
||||||
|
if ($result_id && mysqli_num_rows($result_id) > 0) {
|
||||||
|
$row = mysqli_fetch_assoc($result_id);
|
||||||
|
$user_id = $row['id_user'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$user_id) {
|
||||||
|
http_response_code(401);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'ID pengguna tidak ditemukan.']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = "UPDATE users SET score = ? WHERE id_user = ? AND score < ?";
|
||||||
|
|
||||||
|
if ($stmt = mysqli_prepare($koneksi, $sql)) {
|
||||||
|
mysqli_stmt_bind_param($stmt, "iii", $final_score, $user_id, $final_score);
|
||||||
|
$exec = mysqli_stmt_execute($stmt);
|
||||||
|
|
||||||
|
if ($exec) {
|
||||||
|
$rows_affected = mysqli_stmt_affected_rows($stmt);
|
||||||
|
|
||||||
|
if ($rows_affected > 0) {
|
||||||
|
$message = 'Skor berhasil diperbarui. Ini adalah Highscore baru!';
|
||||||
|
} else {
|
||||||
|
$message = 'Skor berhasil dikirim, tetapi skor tidak lebih tinggi dari Highscore sebelumnya.';
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => $message,
|
||||||
|
'skor_terkirim' => $final_score
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
http_response_code(500);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Gagal menjalankan kueri update: ' . mysqli_stmt_error($stmt)]);
|
||||||
|
}
|
||||||
|
mysqli_stmt_close($stmt);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
http_response_code(500);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Gagal mempersiapkan statement: ' . mysqli_error($koneksi)]);
|
||||||
|
}
|
||||||
|
?>
|
||||||
Loading…
x
Reference in New Issue
Block a user