merge
This commit is contained in:
commit
5eb3013dd6
58
GameLogic.js
58
GameLogic.js
@ -234,29 +234,53 @@ function PenambahanTembok() {
|
||||
}
|
||||
}
|
||||
|
||||
//set dan update score
|
||||
function UpdateScore(amount) {
|
||||
score = amount > 0 ? score + amount : 0;
|
||||
highscore = score > highscore ? score : highscore;
|
||||
|
||||
Text.innerHTML =
|
||||
"Score: " + score + "<br>Highscore: " + highscore + "<br>Speed: " + speed;
|
||||
//set dan update score
|
||||
function UpdateScore(amount) {
|
||||
score = amount > 0 ? score + amount : 0;
|
||||
console.log("UPDATE SCORE , score:",score);
|
||||
highscore = score > highscore ? score : highscore;
|
||||
Text.innerHTML =
|
||||
"Score: " +
|
||||
score +
|
||||
"<br>Highscore: " +
|
||||
highscore +
|
||||
"<br>Speed: " +
|
||||
speed;
|
||||
|
||||
PenambahanTembok();
|
||||
}
|
||||
|
||||
//tampilin gameover
|
||||
function GameOver() {
|
||||
GameStart = false;
|
||||
ClearCanvas();
|
||||
//tampilin gameover
|
||||
function GameOver() {
|
||||
GameStart = false;
|
||||
ClearCanvas();
|
||||
|
||||
const GameMode = ModeH ? "Tambahan" : "Normal";
|
||||
if (score > 0) {
|
||||
}
|
||||
// Simpan score ke database
|
||||
saveScore(score);
|
||||
|
||||
ScoreMain.innerHTML = "Score: " + score;
|
||||
UpDead.style.display = "flex";
|
||||
}
|
||||
// TAMPILKAN POP-UP GAME OVER
|
||||
ScoreMain.innerHTML = "Score: " + score;
|
||||
UpDead.style.display = "flex";
|
||||
}
|
||||
|
||||
//fungsi untuk menyimpan score ke database
|
||||
function saveScore(finalScore) {
|
||||
console.log("SAVING SCORE , score:",finalScore);
|
||||
// Buat form tersembunyi untuk submit score
|
||||
var form = document.createElement('form');
|
||||
form.method = 'POST';
|
||||
form.action = 'simpan_score.php';
|
||||
form.style.display = 'none';
|
||||
|
||||
var scoreInput = document.createElement('input');
|
||||
scoreInput.type = 'hidden';
|
||||
scoreInput.name = 'score';
|
||||
scoreInput.value = finalScore;
|
||||
|
||||
form.appendChild(scoreInput);
|
||||
document.body.appendChild(form);
|
||||
form.submit();
|
||||
}
|
||||
|
||||
//reset isi canvas doang
|
||||
function ClearCanvas() {
|
||||
|
||||
28
api_score.php
Normal file
28
api_score.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once "koneksi.php";
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
if (isset($input['score']) && isset($_SESSION['users'])) {
|
||||
$score = (int)$input['score'];
|
||||
$username = $_SESSION['users']['username'];
|
||||
|
||||
$sql = "UPDATE users SET score = $score WHERE username = '$username' AND score < $score";
|
||||
$result = mysqli_query($koneksi, $sql);
|
||||
|
||||
if ($result) {
|
||||
echo json_encode(['success' => true, 'message' => 'Score saved', 'score' => $score]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Database error']);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid data']);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Method not allowed']);
|
||||
}
|
||||
?>
|
||||
66
game.php
Normal file
66
game.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
// Cek apakah user sudah login
|
||||
if (!isset($_SESSION['users'])) {
|
||||
header("Location: login.php");
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="GameStyle.css" />
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
||||
<link
|
||||
href="https://fonts.googleapis.com/css2?family=Coral+Pixels&family=Jacquarda+Bastarda+9&family=Pixelify+Sans:wght@400..700&display=swap"
|
||||
rel="stylesheet"
|
||||
/>
|
||||
</head>
|
||||
<body>
|
||||
<!-- header -->
|
||||
<header>
|
||||
<div class="centered">
|
||||
<div class="icon"></div>
|
||||
<h1>Snake - <?php echo htmlspecialchars($_SESSION['users']['nama']); ?></h1>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<!-- main konten -->
|
||||
<div class="HalamanFull">
|
||||
<canvas width="1056" height="480" id="game"></canvas>
|
||||
<div id="gameover-overlay" class="starting" style="display: none">
|
||||
<div class="popAwal">
|
||||
<h1 style="color: red" id="gameover-title">YOU DIED</h1>
|
||||
<p id="gameover-score">Score: 0</p>
|
||||
<button id="ulangi" class="modegame">Main lagi?</button>
|
||||
<a href="index.php">
|
||||
<button id="keluar" class="">Keluar?</button>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<div id="gameover" class="gameover"></div>
|
||||
<div id="start" class="starting">
|
||||
<div class="popAwal">
|
||||
<h1>Start Game?</h1>
|
||||
<p>Silakan Pilih Mode</p>
|
||||
<button id="mode-normal" class="modegame">Normal</button>
|
||||
<button id="mode-tambahan" class="modegame">Normal++</button>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div id="text" class="text"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- footer -->
|
||||
<footer class="centered">
|
||||
<div class="centered">
|
||||
<p></p>
|
||||
</div>
|
||||
</footer>
|
||||
<!-- akhir html body -->
|
||||
<script src="GameLogic.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
11
index.php
11
index.php
@ -1,8 +1,9 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
if(!isset($_SESSION['users'])) {
|
||||
if(!isset($_SESSION['username'])) {
|
||||
header('location:login.php');
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
|
||||
@ -57,23 +58,23 @@ if(!isset($_SESSION['users'])) {
|
||||
<a href="logout.php">Logout</a> -->
|
||||
<hr>
|
||||
|
||||
<h3>Selamat datang, <?php echo $_SESSION['users']['nama'] ?></h3>
|
||||
<h3>Selamat datang, <?php echo isset($_SESSION['users']['nama']) ? htmlspecialchars($_SESSION['users']['nama']) : 'User' ?></h3>
|
||||
<br>
|
||||
|
||||
<!-- <h1>EAT THAT APPLE</h1> -->
|
||||
<div class="container">
|
||||
<div class="b2">
|
||||
<a href="game.html">
|
||||
<a href="game.php">
|
||||
<div id="button">Play Game</div>
|
||||
</a>
|
||||
</div>
|
||||
<div class="b2">
|
||||
<a href="http://localhost/uas_sem_1/leaderboard.php">
|
||||
<a href="leaderboard.php">
|
||||
<div id="button">Leaderboard</div>
|
||||
</a>
|
||||
</div>
|
||||
<div class="b2">
|
||||
<a href="http://localhost/uas_sem_1/logout.php">
|
||||
<a href="logout.php">
|
||||
<div id="button">Logout</div>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@ -1,22 +1,22 @@
|
||||
<?php
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', 1);
|
||||
session_start();
|
||||
require_once "koneksi.php";
|
||||
|
||||
$nama = "";
|
||||
if (isset($_SESSION['username'])) {
|
||||
$nama = $_SESSION['username'];
|
||||
} else {
|
||||
$nama = "";
|
||||
$nama = (string)$_SESSION['username'];
|
||||
}
|
||||
|
||||
$score = 0;
|
||||
|
||||
if (!empty($nama)) {
|
||||
$getScore = "SELECT score FROM users WHERE username = '$nama'";
|
||||
$getScore = "SELECT score FROM users WHERE username = '" . mysqli_real_escape_string($koneksi, $nama) . "'";
|
||||
$resultMe = mysqli_query($koneksi, $getScore);
|
||||
|
||||
if ($resultMe && mysqli_num_rows($resultMe) > 0) {
|
||||
$row = mysqli_fetch_assoc($resultMe);
|
||||
$score = $row['score'];
|
||||
$score = (int)$row['score'];
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ $sql = "SELECT username, score FROM users ORDER BY score DESC LIMIT 10";
|
||||
$result = mysqli_query($koneksi, $sql);
|
||||
$leaderboard = [];
|
||||
|
||||
if($result) {
|
||||
if($result && mysqli_num_rows($result) > 0) {
|
||||
$leaderboard = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
||||
}
|
||||
?>
|
||||
@ -57,7 +57,7 @@ if($result) {
|
||||
<tbody>
|
||||
<?php
|
||||
$peringkat = 1;
|
||||
if (!empty($leaderboard)) {
|
||||
if (!empty($leaderboard) && is_array($leaderboard)) {
|
||||
foreach ($leaderboard as $pemain):
|
||||
?>
|
||||
|
||||
@ -65,7 +65,7 @@ if($result) {
|
||||
<td><?php echo $peringkat; ?></td>
|
||||
<td><?php echo htmlspecialchars($pemain['username']); ?></td>
|
||||
|
||||
<td><?php echo $pemain['score']; ?> PTS</td>
|
||||
<td><?php echo (int)$pemain['score']; ?> PTS</td>
|
||||
|
||||
</tr>
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ if(isset($_POST['username'])) {
|
||||
if(mysqli_num_rows($query) > 0) {
|
||||
$data = mysqli_fetch_array($query);
|
||||
$_SESSION['users'] = $data;
|
||||
$_SESSION['username'] = $data = ['username'];
|
||||
$_SESSION['username'] = $data['username'];
|
||||
echo '<script>location.href="index.php";</script>';
|
||||
} else {
|
||||
echo '<script>alert("Username atau Password tidak sesuai");</script>';
|
||||
|
||||
38
save_score.php
Normal file
38
save_score.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once "koneksi.php";
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$score = isset($input['score']) ? (int)$input['score'] : 0;
|
||||
|
||||
if (!isset($_SESSION['users'])) {
|
||||
echo json_encode(['success' => false, 'message' => 'User not logged in']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$username = $_SESSION['users']['username'];
|
||||
|
||||
// Update score jika lebih tinggi dari score sebelumnya
|
||||
$sql = "UPDATE users SET score = ? WHERE username = ? AND score < ?";
|
||||
$stmt = mysqli_prepare($koneksi, $sql);
|
||||
|
||||
if ($stmt) {
|
||||
mysqli_stmt_bind_param($stmt, "isi", $score, $username, $score);
|
||||
mysqli_stmt_execute($stmt);
|
||||
|
||||
if (mysqli_stmt_affected_rows($stmt) > 0) {
|
||||
echo json_encode(['success' => true, 'message' => 'Score updated']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Score not higher than current']);
|
||||
}
|
||||
mysqli_stmt_close($stmt);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Database error']);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid request method']);
|
||||
}
|
||||
?>
|
||||
19
simpan_score.php
Normal file
19
simpan_score.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once "koneksi.php";
|
||||
|
||||
if (isset($_POST['score']) && isset($_SESSION['users'])) {
|
||||
$score = (int)$_POST['score'];
|
||||
$username = $_SESSION['users']['username'];
|
||||
|
||||
$sql = "UPDATE users SET score = $score WHERE username = '$username' AND (score < $score OR score IS NULL)";
|
||||
$result = mysqli_query($koneksi, $sql);
|
||||
|
||||
if ($result) {
|
||||
echo "<script>console.log('Score $score tersimpan untuk $username');</script>";
|
||||
}
|
||||
}
|
||||
|
||||
// Redirect kembali ke game
|
||||
echo "<script>window.history.back();</script>";
|
||||
?>
|
||||
@ -3,7 +3,7 @@
|
||||
-- https://www.phpmyadmin.net/
|
||||
--
|
||||
-- Host: localhost:3306
|
||||
-- Generation Time: Dec 03, 2025 at 05:17 AM
|
||||
-- Generation Time: Dec 14, 2025 at 08:58 AM
|
||||
-- Server version: 8.0.30
|
||||
-- PHP Version: 8.1.10
|
||||
|
||||
@ -32,7 +32,7 @@ CREATE TABLE `users` (
|
||||
`nama` varchar(255) DEFAULT NULL,
|
||||
`username` varchar(225) DEFAULT NULL,
|
||||
`password` varchar(255) DEFAULT NULL,
|
||||
`score` int NOT NULL
|
||||
`score` int NOT NULL DEFAULT '0'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
--
|
||||
@ -41,7 +41,13 @@ CREATE TABLE `users` (
|
||||
|
||||
INSERT INTO `users` (`id_user`, `nama`, `username`, `password`, `score`) VALUES
|
||||
(1, 'Chris Daud Koroh', 'daudkoroh', '88d602f1ad6d62b9a11c688ab47fed22', 20),
|
||||
(2, 'Zefanya Isaac', 'zefanya', 'de413c0365e3c88d8b3315f9d90b98ae', 68);
|
||||
(2, 'Zefanya Isaac', 'zefanya', 'de413c0365e3c88d8b3315f9d90b98ae', 68),
|
||||
(4, 'ygygyg', 'yatim', 'c929fedbfce1bdc014ed835a0cdf35f4', 0),
|
||||
(5, 'hentai', 'untung', '25f9e794323b453885f5181f1b624d0b', 0),
|
||||
(6, 'kepin', 'kepin', '202cb962ac59075b964b07152d234b70', 0),
|
||||
(8, 'ayam', 'ayam', '202cb962ac59075b964b07152d234b70', 2),
|
||||
(9, 'ayam2', 'ayam2', '289dff07669d7a23de0ef88d2f7129e7', 35),
|
||||
(10, 'terserah', 'terserah', '698d51a19d8a121ce581499d7b701668', 12);
|
||||
|
||||
--
|
||||
-- Indexes for dumped tables
|
||||
@ -61,7 +67,7 @@ ALTER TABLE `users`
|
||||
-- AUTO_INCREMENT for table `users`
|
||||
--
|
||||
ALTER TABLE `users`
|
||||
MODIFY `id_user` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
|
||||
MODIFY `id_user` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
|
||||
COMMIT;
|
||||
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
Loading…
x
Reference in New Issue
Block a user