This commit is contained in:
Daud 2025-12-05 16:00:32 +07:00
parent 77de57304e
commit d0459d99f8
9 changed files with 91 additions and 121 deletions

View File

@ -336,35 +336,5 @@
: speed; : speed;
}); });
} }
function updatedatabase(scoreBaru) {
// 1. Buat objek data yang akan dikirim
const KirimScore = {
action: 'SaveScore', // Identifier untuk PHP
score: scoreBaru
};
// 2. Gunakan Fetch API untuk mengirim permintaan POST
fetch('score.php', { // Ganti dengan nama file PHP Anda
method: 'POST',
headers: {
'Content-Type': 'application/json' // Beri tahu server bahwa yang dikirim adalah JSON
},
body: JSON.stringify(KirimScore) // Konversi data JS menjadi string JSON
})
.then(response => response.json()) // Ubah respons server kembali menjadi objek JS
.then(data => {
// 3. Tangani respons dari PHP
if (data.status === 'success') {
console.log('Score berhasil disimpan:', data.message);
// Lakukan sesuatu (misalnya: tampilkan pesan sukses di UI)
} else {
console.error('Gagal menyimpan score:', data.message);
}
})
.catch(error => {
console.error('Terjadi kesalahan koneksi:', error);
});
}
InputKeyboard(); InputKeyboard();
gameLoop(); gameLoop();

BIN
gameOver/gameOver.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

48
home.html Normal file
View File

@ -0,0 +1,48 @@
<html>
<style>
body {
background-image: url();
}
h1 {
text-align: center;
margin-top: 10%;
font-size: 85px;
}
.container {
padding: 5px;
border: 2px solid; /* Nanti dihapus */
border-radius: 7pt; /* Nanti dihapus */
width: 70%;
justify-self: center;
background-image: url();
}
#button {
font-size: 35px;
margin-top: 5%;
width: 50%;
margin-bottom: 5%;
border: 1px solid;
border-radius: 3pt;
padding: 2px;
justify-self: center;
text-align: center;
}
</style>
<body>
<h1>EAT THAT APPLE</h1>
<div class="container">
<a href="game.html">
<div id="button">Play Game</div>
</a>
<div id="button">Leaderboard</div>
<div id="button">Logout</div>
</div>
<input type="text">
</body>
<script></script>
</html>

View File

@ -20,14 +20,23 @@ if(isset($_POST['username'])) {
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="id">
<head> <style>
body {
background-color: limegreen;
}
.login {
background-image: url('login/log.png');
}
</style>
<head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halaman: Login</title> <title>Halaman: Login</title>
</head> </head>
<body> <body>
<div class="login">
<form action="" method="post"> <form action="" method="post">
<table align="center"> <table align="center">
<tr> <tr>
@ -53,6 +62,8 @@ if(isset($_POST['username'])) {
</tr> </tr>
</table> </table>
</form> </form>
</div>
</body>
</body>
</html> </html>

BIN
login/log.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
login/login.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
mapGame/mapgame .png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

@ -1,59 +0,0 @@
<?php
session_start();
header('Content-Type: application/json'); // Penting: Beri tahu klien bahwa respons adalah JSON
// --- 1. Koneksi Database (Ganti dengan detail Anda) ---
$host = 'localhost';
$db = 'nama_database_anda';
$user = 'user_db';
$pass = 'password_db';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
http_response_code(500); // Internal Server Error
echo json_encode(['status' => 'error', 'message' => 'Gagal koneksi database.']);
exit;
}
// --- 2. Ambil Data dari JavaScript (AJAX) ---
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['action']) || $data['action'] !== 'simpan_score') {
http_response_code(400); // Bad Request
echo json_encode(['status' => 'error', 'message' => 'Aksi tidak valid.']);
exit;
}
$score = filter_var($data['score'] ?? 0, FILTER_SANITIZE_NUMBER_INT);
$userId = 1; // Contoh: Asumsikan ID pengguna 1 (Anda harus mengambilnya dari sesi atau input)
if (!is_numeric($score) || $score < 0) {
http_response_code(400);
echo json_encode(['status' => 'error', 'message' => 'Score tidak valid.']);
exit;
}
// --- 3. Simpan ke Database (menggunakan Prepared Statements) ---
try {
$sql = "INSERT INTO scores (user_id, score, created_at) VALUES (?, ?, NOW())";
$stmt = $pdo->prepare($sql);
$stmt->execute([$userId, $score]);
// Berikan respons sukses ke JavaScript
echo json_encode(['status' => 'success', 'message' => 'Score berhasil disimpan.']);
} catch (\PDOException $e) {
http_response_code(500);
echo json_encode(['status' => 'error', 'message' => 'Gagal menyimpan score ke DB: ' . $e->getMessage()]);
}
?>

BIN
succes/loginSuccess.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB