From 409d7dfe9f01dba5d7b8cc187a2ad31429f5f6a6 Mon Sep 17 00:00:00 2001 From: Matthew Florentino Date: Mon, 1 Dec 2025 09:04:14 +0700 Subject: [PATCH] feat: fitur utama game and have err at adding score --- src/css/game.css | 14 ++--- src/game/firstperson.html | 14 ++--- src/js/firstperson.js | 118 ++++++++++++++++++++++++++++++++++++++ src/score.php | 33 +++++++++++ 4 files changed, 165 insertions(+), 14 deletions(-) create mode 100644 src/score.php diff --git a/src/css/game.css b/src/css/game.css index d652429..73779cd 100644 --- a/src/css/game.css +++ b/src/css/game.css @@ -47,7 +47,7 @@ } .container-first { - background-image: url(/src/assets/Design/CAVEBOSSFIGHT.png); + /* background-image: url(/src/assets/Design/CAVEBOSSFIGHT.png); */ background-repeat: no-repeat; background-size: cover; image-rendering: pixelated; @@ -72,7 +72,7 @@ background-size: cover; } -.container-first .content .text { +.container-first .content .quiz { width: 1000px; height: 200px; bottom: 40px; @@ -92,20 +92,20 @@ z-index: 1; } -.container-first .content .text p { +.container-first .content .quiz h2 { margin-bottom: 30px; - padding: 20px; + padding: 10px; color: #ffdf12; - font-size: 50px; + font-size: 40px; } -.container-first .content .text .opt { +.container-first .content .quiz #btn-answer { color: #ffdf12; display: flex; justify-content: center; } -.container-first .content .text .opt p { +.container-first .content .quiz #btn-answer button { padding: 10px; color: #ffdf12; font-size: 30px; diff --git a/src/game/firstperson.html b/src/game/firstperson.html index 40b370d..4902153 100644 --- a/src/game/firstperson.html +++ b/src/game/firstperson.html @@ -13,13 +13,13 @@
-
-

selamat datang di python games _____

-
-

a. panteks

-

b. anjas

-

c. hell nah

-

d. my kisah

+
+

Question Goes Here ......... !!

+
+ + + +
diff --git a/src/js/firstperson.js b/src/js/firstperson.js index e69de29..a1e9dd4 100644 --- a/src/js/firstperson.js +++ b/src/js/firstperson.js @@ -0,0 +1,118 @@ +const questions = [ + { + question: "what is Python ?", + answers: [ + {text: "Programming Lang", correct: true}, + {text: "Animal", correct: false}, + {text: "Danger", correct: false}, + {text: "All Answer Correct", correct: false}, + ] + }, + { + question: "Data type at python ?", + answers: [ + {text: "Docx", correct: false}, + {text: "CSV", correct: false}, + {text: "String", correct: true}, + {text: "SQL", correct: false}, + ] + }, + { + question: "Index at python start from ?", + answers: [ + {text: "1", correct: false}, + {text: "-1", correct: false}, + {text: "10", correct: false}, + {text: "0", correct: true}, + ] + }, +]; + +const questionElement = document.getElementById("question") +const answerBtn = document.getElementById("btn-answer") + +let currentQuestionIndex = 0; +let score = 0; + +function startQuiz(){ + currentQuestionIndex = 0; + score = 0; + showQuestion(); +} + +function showQuestion(){ + resetState(); + let currentQuestion = questions[currentQuestionIndex] + let questionNumber = currentQuestionIndex + 1 + questionElement.innerHTML = questionNumber + ". " + currentQuestion.question; + currentQuestion.answers.forEach(answer => { + const button = document.createElement("button"); + button.innerHTML = answer.text; + button.classList.add("btn"); + answerBtn.appendChild(button); + button.dataset.correct = answer.correct; + button.addEventListener("click", selectAnswer) + }); +} + +function resetState(){ + while(answerBtn.firstChild){ + answerBtn.removeChild(answerBtn.firstChild) + } +} + +function selectAnswer(e){ + const selectedBtn = e.target; + const isCorrect = selectedBtn.dataset.correct === "true"; + if(isCorrect){ + selectedBtn.classList.add("correct"); + score++; + } else { + selectedBtn.classList.add("Incorrect"); + } + + Array.from(answerBtn.children).forEach(button => { + if(button.dataset.correct === "true") button.classList.add("correct"); + button.disabled = true; + }, + setTimeout(nextQuestion, 1000) + ); +} + +function showScore(){ + resetState(); + const show = questionElement.innerHTML = `you scored ${score} out of ${questions.length}!`; + postScore(score); + alert(show); + window.location.href = "../leaderboard.php" +} + +function handleNextBtn(){ + currentQuestionIndex++; + if(currentQuestionIndex < questions.length){ + showQuestion(); + } else { + showScore(); + } +} + +function nextQuestion(){ + if(currentQuestionIndex < questions.length){ + handleNextBtn() + } else { + showScore() + } +} + +function postScore(score){ + fetch('/score.php', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ score }) + }) + .then(r => r.text()) + .then(t => console.log("Server response:", t)) + .catch(err => console.error("Fetch error:", err)); +} + +startQuiz() \ No newline at end of file diff --git a/src/score.php b/src/score.php new file mode 100644 index 0000000..cc09e09 --- /dev/null +++ b/src/score.php @@ -0,0 +1,33 @@ + "score missing"]); + exit; +} + +$score = (int)$data['score']; +$user_id = isset($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : null; + +if (!$user_id) { + echo json_encode(["error" => "no session user"]); + exit; +} + +$stmt = $db->prepare("INSERT INTO scores (user_id, score) VALUES (?, ?)"); +$stmt->bind_param("ii", $user_id, $score); + +if ($stmt->execute()) { + echo json_encode(["success" => true]); +} else { + echo json_encode(["error" => "insert failed"]); +} + +$stmt->close(); +$db->close(); +?> \ No newline at end of file