Compare commits
No commits in common. "main" and "daud" have entirely different histories.
340
GameLogic.js
@ -1,340 +0,0 @@
|
|||||||
//bagian tempat main ama buat generate game 2d
|
|
||||||
var canvas = document.getElementById("game");
|
|
||||||
var content = canvas.getContext("2d");
|
|
||||||
const Text = document.getElementById("text");
|
|
||||||
const UpDead = document.getElementById("gameover-overlay");
|
|
||||||
const ScoreMain = document.getElementById("gameover-score");
|
|
||||||
const PlayAgain = document.getElementById("ulangi");
|
|
||||||
const Udahan = document.getElementById("keluar");
|
|
||||||
|
|
||||||
//settingan awal
|
|
||||||
var grid = 24;
|
|
||||||
var count = 0;
|
|
||||||
var speed = 10;
|
|
||||||
var score = 0;
|
|
||||||
var highscore = 0;
|
|
||||||
var ArahUlar = 0;
|
|
||||||
var GameOverTimer = 0;
|
|
||||||
var GameStart = false;
|
|
||||||
var ModeH = false;
|
|
||||||
const persiapan = document.getElementById("start");
|
|
||||||
|
|
||||||
//generate gambar external
|
|
||||||
var KepalaUlarImage = new Image();
|
|
||||||
KepalaUlarImage.src = "image/KepalaHorizontalKanan.png";
|
|
||||||
var KepalAtas = new Image();
|
|
||||||
KepalAtas.src = "image/KepalaVertikalAtas.png";
|
|
||||||
var KepalaBawah = new Image();
|
|
||||||
KepalaBawah.src = "image/KepalaVertikalBawah.png";
|
|
||||||
var KepalaKiri = new Image();
|
|
||||||
KepalaKiri.src = "image/KepalaHorizontalKiri.png";
|
|
||||||
var KepalaKanan = new Image();
|
|
||||||
KepalaKanan.src = "image/KepalaHorizontalKanan.png";
|
|
||||||
// ↑ kepala ular
|
|
||||||
|
|
||||||
var BadanUlarImage = new Image();
|
|
||||||
BadanUlarImage.src = "image/BadanVertikal.png";
|
|
||||||
// ↑ badan ular
|
|
||||||
|
|
||||||
var ApelImage = new Image();
|
|
||||||
ApelImage.src = "image/ApelLayer.png"; // Apel
|
|
||||||
var TembokImage = new Image();
|
|
||||||
TembokImage.src = "image/Tembok.png"; // Tembok Batagor
|
|
||||||
|
|
||||||
//set posisi ular dan Apel
|
|
||||||
var Ular = { x: 528, y: 240, dx: grid, dy: 0, cells: [], maxCells: 4 };
|
|
||||||
var Apel = { x: 0, y: 0 };
|
|
||||||
var Tembok = [];
|
|
||||||
UpdateScore(0);
|
|
||||||
RandomizeApel();
|
|
||||||
|
|
||||||
//mastiin gambar external generate dulu sebelum game start
|
|
||||||
KepalaUlarImage.onload = function () {
|
|
||||||
function gameLoop() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
//ngatur biar game ga langsung jalan
|
|
||||||
function StartingGame(menantang) {
|
|
||||||
ModeH = menantang;
|
|
||||||
persiapan.style.display = "none";
|
|
||||||
if (ModeH) {
|
|
||||||
RandomSpawnWall();
|
|
||||||
} else {
|
|
||||||
Tembok = [];
|
|
||||||
}
|
|
||||||
GameStart = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//buat ngatur batu spwan apa ga nanti
|
|
||||||
document
|
|
||||||
.getElementById("mode-normal")
|
|
||||||
.addEventListener("click", function () {
|
|
||||||
StartingGame(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
document
|
|
||||||
.getElementById("mode-tambahan")
|
|
||||||
.addEventListener("click", function () {
|
|
||||||
StartingGame(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
//play again pas gameover
|
|
||||||
function resetGame() {
|
|
||||||
Ular.x = 528;
|
|
||||||
Ular.y = 240;
|
|
||||||
Ular.cells = [];
|
|
||||||
Ular.maxCells = 4;
|
|
||||||
Ular.dx = grid;
|
|
||||||
Ular.dy = 0;
|
|
||||||
Tembok = [];
|
|
||||||
RandomizeApel();
|
|
||||||
UpdateScore(0);
|
|
||||||
ModeH = false;
|
|
||||||
GameStart = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//ngatur tombol pas gameover kemana abis di klik
|
|
||||||
PlayAgain.addEventListener("click", function () {
|
|
||||||
UpDead.style.display = "none";
|
|
||||||
resetGame();
|
|
||||||
persiapan.style.display = "flex";
|
|
||||||
});
|
|
||||||
|
|
||||||
Udahan.addEventListener("click", function () {
|
|
||||||
UpDead.style.display = "none";
|
|
||||||
resetGame();
|
|
||||||
});
|
|
||||||
|
|
||||||
//gameover terus ngatur ular ke setting awal
|
|
||||||
function gameLoop() {
|
|
||||||
if (!GameStart) {
|
|
||||||
requestAnimationFrame(gameLoop);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ClearCanvas();
|
|
||||||
Movement();
|
|
||||||
IntiGame();
|
|
||||||
|
|
||||||
requestAnimationFrame(gameLoop);
|
|
||||||
}
|
|
||||||
|
|
||||||
//random spawn Apel
|
|
||||||
function RandomizeApel() {
|
|
||||||
var pembataslebar = Math.floor(canvas.width / grid);
|
|
||||||
var pembatastinggi = Math.floor(canvas.height / grid);
|
|
||||||
Apel.x = Math.floor(Math.random() * pembataslebar) * grid;
|
|
||||||
Apel.y = Math.floor(Math.random() * pembatastinggi) * grid;
|
|
||||||
}
|
|
||||||
|
|
||||||
//random spawn tembok
|
|
||||||
function RandomSpawnWall() {
|
|
||||||
var TembokX, TembokY;
|
|
||||||
var kosong;
|
|
||||||
var pembataslebar = Math.floor(canvas.width / grid);
|
|
||||||
var pembatastinggi = Math.floor(canvas.height / grid);
|
|
||||||
|
|
||||||
//create tembok
|
|
||||||
do {
|
|
||||||
kosong = true;
|
|
||||||
|
|
||||||
TembokX = Math.floor(Math.random() * pembataslebar) * grid;
|
|
||||||
TembokY = Math.floor(Math.random() * pembatastinggi) * grid;
|
|
||||||
|
|
||||||
//cek untuk posisi yang mau di kasih tembok ada/tidak ada ularnya
|
|
||||||
for (var i = 0; i < Ular.cells.length; i++) {
|
|
||||||
if (Ular.cells[i].x === TembokX && Ular.cells[i].y === TembokY) {
|
|
||||||
kosong = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//tidak memperbolehkan tembok spawn sama dengan apel
|
|
||||||
if (TembokX === Apel.x && TembokY === Apel.y) {
|
|
||||||
kosong = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < Tembok.length; i++) {
|
|
||||||
if (Tembok[i].x === TembokX && Tembok[i].y === TembokY) {
|
|
||||||
kosong = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (kosong === false);
|
|
||||||
Tembok.push({ x: TembokX, y: TembokY });
|
|
||||||
}
|
|
||||||
|
|
||||||
//nambah tembok tiap score kelipatan ... berapa enaknya ya? 😁
|
|
||||||
function PenambahanTembok() {
|
|
||||||
if (ModeH) {
|
|
||||||
if (Tembok.length < Math.floor(score / 2)) {
|
|
||||||
RandomSpawnWall();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//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;
|
|
||||||
|
|
||||||
PenambahanTembok();
|
|
||||||
}
|
|
||||||
|
|
||||||
//tampilin gameover
|
|
||||||
function GameOver() {
|
|
||||||
GameStart = false;
|
|
||||||
ClearCanvas();
|
|
||||||
|
|
||||||
// TAMPILKAN POP-UP GAME OVER
|
|
||||||
ScoreMain.innerHTML = "Score: " + score;
|
|
||||||
UpDead.style.display = "flex";
|
|
||||||
}
|
|
||||||
|
|
||||||
//reset isi canvas doang
|
|
||||||
function ClearCanvas() {
|
|
||||||
content.clearRect(0, 0, canvas.width, canvas.height);
|
|
||||||
}
|
|
||||||
|
|
||||||
//bagian utama:
|
|
||||||
//ular makan dan mati pas ketemu badannya
|
|
||||||
function IntiGame() {
|
|
||||||
//buat gambarnya bisa keluar
|
|
||||||
content.drawImage(ApelImage, Apel.x, Apel.y, grid, grid);
|
|
||||||
Tembok.forEach(function (bata) {
|
|
||||||
content.drawImage(TembokImage, bata.x, bata.y, grid, grid);
|
|
||||||
});
|
|
||||||
|
|
||||||
Ular.cells.forEach(function (cell, index) {
|
|
||||||
if (index === 0) {
|
|
||||||
// Logika Pemilihan Gambar Kepala Ular
|
|
||||||
var posisiKepalaImage;
|
|
||||||
if (Ular.dx === grid) {
|
|
||||||
// KANAN
|
|
||||||
posisiKepalaImage = KepalaKanan;
|
|
||||||
} else if (Ular.dx === -grid) {
|
|
||||||
// KIRI
|
|
||||||
posisiKepalaImage = KepalaKiri;
|
|
||||||
} else if (Ular.dy === -grid) {
|
|
||||||
// ATAS
|
|
||||||
posisiKepalaImage = KepalAtas;
|
|
||||||
} else if (Ular.dy === grid) {
|
|
||||||
// BAWAH
|
|
||||||
posisiKepalaImage = KepalaBawah;
|
|
||||||
} else {
|
|
||||||
// Default, misalnya saat game baru mulai (dx=grid, dy=0, atau default awal)
|
|
||||||
posisiKepalaImage = KepalaKanan;
|
|
||||||
}
|
|
||||||
|
|
||||||
content.drawImage(posisiKepalaImage, cell.x, cell.y, grid, grid);
|
|
||||||
} else {
|
|
||||||
content.drawImage(BadanUlarImage, cell.x, cell.y, grid, grid);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//bagian generate ular
|
|
||||||
Ular.cells.forEach(function (cell, index) {
|
|
||||||
if (index === 0) {
|
|
||||||
content.drawImage(KepalaUlarImage, cell.x, cell.y, grid, grid);
|
|
||||||
} else {
|
|
||||||
content.drawImage(BadanUlarImage, cell.x, cell.y, grid, grid);
|
|
||||||
}
|
|
||||||
|
|
||||||
//buat pas ular makan Apel
|
|
||||||
if (cell.x === Apel.x && cell.y === Apel.y) {
|
|
||||||
Ular.maxCells += 1;
|
|
||||||
UpdateScore(1);
|
|
||||||
RandomizeApel();
|
|
||||||
}
|
|
||||||
|
|
||||||
//tabrak tembok = mati
|
|
||||||
if (index === 0) {
|
|
||||||
Tembok.forEach(function (bata) {
|
|
||||||
if (cell.x === bata.x && cell.y === bata.y) {
|
|
||||||
GameOver();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//buat pas ular mati kena badan sendiri
|
|
||||||
for (var i = index + 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
|
|
||||||
function Movement() {
|
|
||||||
if (++count < speed) return;
|
|
||||||
if (ArahUlar > 0) ArahUlar--;
|
|
||||||
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 });
|
|
||||||
if (Ular.cells.length > Ular.maxCells) Ular.cells.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
//input keyboard
|
|
||||||
function InputKeyboard() {
|
|
||||||
document.addEventListener("keydown", function (e) {
|
|
||||||
if (!GameStart) return;
|
|
||||||
|
|
||||||
// jalan buat ular
|
|
||||||
if (
|
|
||||||
ArahUlar == 0 &&
|
|
||||||
((e.code == "KeyA" && Ular.dx === 0) ||
|
|
||||||
(e.code == "KeyD" && Ular.dx === 0))
|
|
||||||
) {
|
|
||||||
ArahUlar = 1;
|
|
||||||
Ular.dx = e.code === "KeyA" ? -grid : grid;
|
|
||||||
Ular.dy = 0;
|
|
||||||
} 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;
|
|
||||||
} 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 > 5
|
|
||||||
? speed - 1
|
|
||||||
: e.code == "KeyQ" && speed < 15
|
|
||||||
? speed + 1
|
|
||||||
: speed;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
InputKeyboard();
|
|
||||||
gameLoop();
|
|
||||||
141
GameStyle.css
@ -1,141 +0,0 @@
|
|||||||
body {
|
|
||||||
background-color: rgb(24, 24, 24);
|
|
||||||
color: white;
|
|
||||||
text-align: left;
|
|
||||||
font-family: "Pixelify Sans", sans-serif;
|
|
||||||
font-optical-sizing: auto;
|
|
||||||
font-weight: 400;
|
|
||||||
font-style: normal;
|
|
||||||
font-size: 24px;
|
|
||||||
}
|
|
||||||
header,
|
|
||||||
footer,
|
|
||||||
.HalamanFull {
|
|
||||||
padding-left: 50px;
|
|
||||||
padding-right: 50px;
|
|
||||||
}
|
|
||||||
header,
|
|
||||||
footer {
|
|
||||||
background-color: rgb(18, 18, 18);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: space-between;
|
|
||||||
}
|
|
||||||
.HalamanFull {
|
|
||||||
position: relative;
|
|
||||||
background-color: rgb(28, 28, 28);
|
|
||||||
color: white;
|
|
||||||
height: 100%;
|
|
||||||
display: flex;
|
|
||||||
align-items: left;
|
|
||||||
justify-content: left;
|
|
||||||
padding-bottom: 20px;
|
|
||||||
padding-top: 20px;
|
|
||||||
}
|
|
||||||
.starting {
|
|
||||||
position: absolute;
|
|
||||||
top: 21px;
|
|
||||||
left: 51px;
|
|
||||||
width: 1056px;
|
|
||||||
height: 480px;
|
|
||||||
background-color: rgba(100, 100, 100, 0.8);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
z-index: 10;
|
|
||||||
}
|
|
||||||
#start {
|
|
||||||
position: absolute;
|
|
||||||
top: 21px;
|
|
||||||
left: 51px;
|
|
||||||
width: 1056px;
|
|
||||||
height: 480px;
|
|
||||||
background-color: rgba(100, 100, 100, 0.8);
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
z-index: 10;
|
|
||||||
}
|
|
||||||
.popAwal {
|
|
||||||
text-align: center;
|
|
||||||
border-radius: 10px;
|
|
||||||
background-color: rgb(50, 50, 50);
|
|
||||||
padding: 30px;
|
|
||||||
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
|
|
||||||
width: 80%;
|
|
||||||
max-width: 400px;
|
|
||||||
}
|
|
||||||
.popAwal h1 {
|
|
||||||
margin-left: 30px;
|
|
||||||
margin-right: 25px;
|
|
||||||
}
|
|
||||||
#keluar {
|
|
||||||
background-color: #f44336;
|
|
||||||
}
|
|
||||||
#keluar:hover {
|
|
||||||
background-color: #d32f2f;
|
|
||||||
}
|
|
||||||
.icon {
|
|
||||||
width: 70px;
|
|
||||||
height: 70px;
|
|
||||||
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/9/9c/Nazi_Swastika.svg/2048px-Nazi_Swastika.svg.png");
|
|
||||||
background-size: cover;
|
|
||||||
margin-right: 20px;
|
|
||||||
margin-left: 20px;
|
|
||||||
}
|
|
||||||
.centered {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
}
|
|
||||||
h1 {
|
|
||||||
font-size: 40px;
|
|
||||||
margin-right: 100px;
|
|
||||||
}
|
|
||||||
.text {
|
|
||||||
padding-top: 30px;
|
|
||||||
padding-left: 50px;
|
|
||||||
color: white;
|
|
||||||
}
|
|
||||||
#gameover {
|
|
||||||
color: red;
|
|
||||||
font-size: 90px;
|
|
||||||
position: absolute;
|
|
||||||
top: 280px;
|
|
||||||
left: 300px;
|
|
||||||
}
|
|
||||||
#game {
|
|
||||||
background: rgb(192, 232, 255);
|
|
||||||
border: 1px solid white;
|
|
||||||
}
|
|
||||||
.modegame {
|
|
||||||
padding: 15px 30px;
|
|
||||||
font-size: 20px;
|
|
||||||
cursor: pointer;
|
|
||||||
color: rgb(255, 255, 255);
|
|
||||||
border: none;
|
|
||||||
border-radius: 5px;
|
|
||||||
margin: 10px 5px;
|
|
||||||
display: block;
|
|
||||||
width: 90%;
|
|
||||||
max-width: 300px;
|
|
||||||
margin-left: auto;
|
|
||||||
margin-right: auto;
|
|
||||||
transition: background-color 0.2s, color 0.25s;
|
|
||||||
font-family: "Pixelify Sans", sans-serif;
|
|
||||||
font-optical-sizing: auto;
|
|
||||||
font-weight: 400;
|
|
||||||
font-style: normal;
|
|
||||||
}
|
|
||||||
#mode-normal {
|
|
||||||
background-color: #73e4c2;
|
|
||||||
}
|
|
||||||
#mode-normal:hover {
|
|
||||||
background-color: #57c9aa;
|
|
||||||
}
|
|
||||||
#mode-tambahan {
|
|
||||||
background-color: #9572e7;
|
|
||||||
}
|
|
||||||
#mode-tambahan:hover {
|
|
||||||
background-color: #703db3;
|
|
||||||
}
|
|
||||||
59
game.html
@ -1,59 +0,0 @@
|
|||||||
<!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</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="http://localhost/uas_sem_1/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 class="text">Controls: <br />Movement: W A S D <br /><br /></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>
|
|
||||||
|
Before Width: | Height: | Size: 14 KiB |
48
home.html
@ -1,48 +0,0 @@
|
|||||||
<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>
|
|
||||||
|
Before Width: | Height: | Size: 605 B |
|
Before Width: | Height: | Size: 284 B |
|
Before Width: | Height: | Size: 302 B |
|
Before Width: | Height: | Size: 415 B |
|
Before Width: | Height: | Size: 432 B |
|
Before Width: | Height: | Size: 382 B |
|
Before Width: | Height: | Size: 397 B |
|
Before Width: | Height: | Size: 316 B |
|
Before Width: | Height: | Size: 291 B |
|
Before Width: | Height: | Size: 280 B |
|
Before Width: | Height: | Size: 327 B |
|
Before Width: | Height: | Size: 491 B |
|
Before Width: | Height: | Size: 488 B |
|
Before Width: | Height: | Size: 450 B |
|
Before Width: | Height: | Size: 486 B |
|
Before Width: | Height: | Size: 574 B |
BIN
image/Tembok.png
|
Before Width: | Height: | Size: 314 B |
60
index.php
@ -1,72 +1,26 @@
|
|||||||
<?php
|
<?php
|
||||||
|
// buat inisialisasi session
|
||||||
session_start();
|
session_start();
|
||||||
|
// mengecek apakah ada session user yang aktif, jika tidak arahkan ke login.php
|
||||||
if(!isset($_SESSION['users'])) {
|
if(!isset($_SESSION['users'])) {
|
||||||
header('location:login.php');
|
header('location:login.php'); // arahkan ke login.php
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="id">
|
<html lang="id">
|
||||||
<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>
|
|
||||||
|
|
||||||
|
|
||||||
<head>
|
<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 ke-1</title>
|
<title>Halaman ke-1</title>
|
||||||
</head>
|
</head>
|
||||||
<body style='text-align:center'>
|
<body style='text-align:center'>
|
||||||
<h1>EAT THAT APPLE</h1>
|
<h1>Halaman ke-1</h1>
|
||||||
<!-- <a href="index.php">Home</a>
|
<a href="index.php">Home</a>
|
||||||
<a href="logout.php">Logout</a> -->
|
<a href="logout.php">Logout</a>
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
<h3>Selamat datang, <?php echo $_SESSION['users']['nama'] ?></h3>
|
<h3>Selamat datang, <?php echo $_SESSION['users']['nama'] ?></h3>
|
||||||
<br>
|
Halaman ini akan tampil setelah user login.
|
||||||
|
|
||||||
<!-- <h1>EAT THAT APPLE</h1> -->
|
|
||||||
<div class="container">
|
|
||||||
<a href="game.html">
|
|
||||||
<div id="button">Play Game</div>
|
|
||||||
</a>
|
|
||||||
<a href="http://localhost/uas_sem_1/leaderboard.php">
|
|
||||||
<div id="button">Leaderboard</div>
|
|
||||||
</a>
|
|
||||||
<a href="http://localhost/uas_sem_1/logout.php">
|
|
||||||
<div id="button">Logout</div>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@ -1,78 +0,0 @@
|
|||||||
<?php
|
|
||||||
session_start();
|
|
||||||
require_once "koneksi.php";
|
|
||||||
|
|
||||||
if (isset($_SESSION['username'])) {
|
|
||||||
$nama = $_SESSION['username'];
|
|
||||||
} else {
|
|
||||||
$nama = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
$score = 0;
|
|
||||||
|
|
||||||
if (!empty($nama)) {
|
|
||||||
$getScore = "SELECT score FROM users WHERE username = '$nama'";
|
|
||||||
$resultMe = mysqli_query($koneksi, $getScore);
|
|
||||||
|
|
||||||
if ($resultMe && mysqli_num_rows($resultMe) > 0) {
|
|
||||||
$row = mysqli_fetch_assoc($resultMe);
|
|
||||||
$score = $row['score'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$sql = "SELECT username, score FROM users ORDER BY score DESC LIMIT 10";
|
|
||||||
$result = mysqli_query($koneksi, $sql);
|
|
||||||
$leaderboard = [];
|
|
||||||
|
|
||||||
if($result) {
|
|
||||||
$leaderboard = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
|
|
||||||
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Leaderboard</title>
|
|
||||||
<link rel="stylesheet" href="">
|
|
||||||
<link rel="stylesheet" href="">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<table>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>#</th>
|
|
||||||
<th>Username</th>
|
|
||||||
<th>Score</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
<?php
|
|
||||||
$peringkat = 1;
|
|
||||||
if (!empty($leaderboard)) {
|
|
||||||
foreach ($leaderboard as $pemain):
|
|
||||||
?>
|
|
||||||
|
|
||||||
<tr>
|
|
||||||
<td><?php echo $peringkat; ?></td>
|
|
||||||
<td><?php echo htmlspecialchars($pemain['username']); ?></td>
|
|
||||||
|
|
||||||
<td><?php echo $pemain['score']; ?> PTS</td>
|
|
||||||
|
|
||||||
</tr>
|
|
||||||
|
|
||||||
<?php
|
|
||||||
$peringkat++;
|
|
||||||
endforeach;
|
|
||||||
} else {
|
|
||||||
echo '<tr><td colspan="3" style="text-align: center;">Belum ada Pemain</td></tr>';
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
||||||
</tbody>
|
|
||||||
|
|
||||||
</table>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
43
login.php
@ -1,7 +1,17 @@
|
|||||||
|
|
||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
include "koneksi.php";
|
include "koneksi.php";
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Halaman: Login</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<?php
|
||||||
if(isset($_POST['username'])) {
|
if(isset($_POST['username'])) {
|
||||||
$username = $_POST['username'];
|
$username = $_POST['username'];
|
||||||
$password = md5($_POST['password']);
|
$password = md5($_POST['password']);
|
||||||
@ -11,32 +21,13 @@ if(isset($_POST['username'])) {
|
|||||||
if(mysqli_num_rows($query) > 0) {
|
if(mysqli_num_rows($query) > 0) {
|
||||||
$data = mysqli_fetch_array($query);
|
$data = mysqli_fetch_array($query);
|
||||||
$_SESSION['users'] = $data;
|
$_SESSION['users'] = $data;
|
||||||
$_SESSION['username'] = $data = ['username'];
|
echo '<script>alert("Selamat datang, '.$data['nama'].'"); location.href="index.php";</script>';
|
||||||
echo '<script>location.href="index.php";</script>';
|
|
||||||
} else {
|
} else {
|
||||||
echo '<script>alert("Username atau Password tidak sesuai");</script>';
|
echo '<script>alert("Username atau Password tidak sesuai");</script>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="id">
|
|
||||||
<style>
|
|
||||||
body {
|
|
||||||
background-color: limegreen;
|
|
||||||
}
|
|
||||||
.login {
|
|
||||||
background-image: url('login/log.png');
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Halaman: Login</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<div class="login">
|
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
<table align="center">
|
<table align="center">
|
||||||
<tr>
|
<tr>
|
||||||
@ -46,12 +37,12 @@ if(isset($_POST['username'])) {
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
||||||
<td></td>
|
<td>Username</td>
|
||||||
<td><input type="text" name="username" placeholder="Username" style="border: none"></td>
|
<td><input type="text" name="username"></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td></td>
|
<td>Password</td>
|
||||||
<td><input type="password" name="password" placeholder="Password" style="border: none"></td>
|
<td><input type="password" name="password"></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td></td>
|
<td></td>
|
||||||
@ -62,8 +53,6 @@ if(isset($_POST['username'])) {
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
BIN
login/log.png
|
Before Width: | Height: | Size: 13 KiB |
BIN
login/login.png
|
Before Width: | Height: | Size: 13 KiB |
|
Before Width: | Height: | Size: 4.1 KiB |
|
Before Width: | Height: | Size: 5.2 KiB |
69
users.sql
@ -1,69 +0,0 @@
|
|||||||
-- phpMyAdmin SQL Dump
|
|
||||||
-- version 5.2.0
|
|
||||||
-- https://www.phpmyadmin.net/
|
|
||||||
--
|
|
||||||
-- Host: localhost:3306
|
|
||||||
-- Generation Time: Dec 03, 2025 at 05:17 AM
|
|
||||||
-- Server version: 8.0.30
|
|
||||||
-- PHP Version: 8.1.10
|
|
||||||
|
|
||||||
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
|
|
||||||
START TRANSACTION;
|
|
||||||
SET time_zone = "+00:00";
|
|
||||||
|
|
||||||
|
|
||||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
|
||||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
|
||||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
|
||||||
/*!40101 SET NAMES utf8mb4 */;
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Database: `game`
|
|
||||||
--
|
|
||||||
|
|
||||||
-- --------------------------------------------------------
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Table structure for table `users`
|
|
||||||
--
|
|
||||||
|
|
||||||
CREATE TABLE `users` (
|
|
||||||
`id_user` int NOT NULL,
|
|
||||||
`nama` varchar(255) DEFAULT NULL,
|
|
||||||
`username` varchar(225) DEFAULT NULL,
|
|
||||||
`password` varchar(255) DEFAULT NULL,
|
|
||||||
`score` int NOT NULL
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Dumping data for 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);
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Indexes for dumped tables
|
|
||||||
--
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Indexes for table `users`
|
|
||||||
--
|
|
||||||
ALTER TABLE `users`
|
|
||||||
ADD PRIMARY KEY (`id_user`);
|
|
||||||
|
|
||||||
--
|
|
||||||
-- AUTO_INCREMENT for dumped tables
|
|
||||||
--
|
|
||||||
|
|
||||||
--
|
|
||||||
-- AUTO_INCREMENT for table `users`
|
|
||||||
--
|
|
||||||
ALTER TABLE `users`
|
|
||||||
MODIFY `id_user` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
|
|
||||||
COMMIT;
|
|
||||||
|
|
||||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
|
||||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
|
||||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
|
||||||