Certainly! Below is a simple implementation of the game using HTML, CSS, and JavaScript. In this game, a player can perform activities that affect their energy (tenaga) and points (poin). Each activity is represented by a button, and the player's status is displayed on the screen.
HTML Code (index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Game Function</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Permainan Sederhana</h1>
<div class="player-status">
<p>Nama Pemain: <span id="playerName">John Doe</span></p>
<p>Tenaga: <span id="playerEnergy">100</span></p>
<p>Poin: <span id="playerPoints">0</span></p>
</div>
<div class="actions">
<button onclick="main()">Main</button>
<button onclick="makan()">Makan</button>
<button onclick="tidur()">Tidur</button>
<button onclick="lembur()">Lembur</button>
</div>
<script src="script.js"></script>
</body>
</html>
CSS Code (style.css):
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
h1 {
margin-top: 50px;
}
.player-status {
margin: 20px auto;
padding: 20px;
width: 300px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.player-status p {
font-size: 18px;
}
.actions {
margin: 20px auto;
}
.actions button {
margin: 10px;
padding: 15px 25px;
font-size: 16px;
cursor: pointer;
}
JavaScript Code (script.js):
// Initialize player object
const player = {
name: "John Doe",
energy: 100,
points: 0
};
// Function to update the displayed player status
function updateStatus() {
document.getElementById('playerName').innerText = player.name;
document.getElementById('playerEnergy').innerText = player.energy;
document.getElementById('playerPoints').innerText = player.points;
}
// Activity functions
function main() {
// Player plays, points increase by 10
player.points += 10;
updateStatus();
alert(`${player.name} bermain. Poin bertambah menjadi ${player.points}.`);
}
function makan() {
// Player eats, energy increases by 20
player.energy += 20;
updateStatus();
alert(`${player.name} makan. Tenaga bertambah menjadi ${player.energy}.`);
}
function tidur() {
// Player sleeps, energy increases by 30
player.energy += 30;
updateStatus();
alert(`${player.name} tidur. Tenaga bertambah menjadi ${player.energy}.`);
}
function lembur() {
// Player overworks, energy decreases by 40
player.energy -= 40;
if (player.energy < 0) player.energy = 0; // Prevent negative energy
updateStatus();
alert(`${player.name} lembur. Tenaga berkurang menjadi ${player.energy}.`);
}
// Initial status update
updateStatus();
How to Run the Game:
-
Create Files: Create three files in the same directory: index.html, style.css, and script.js.
-
Copy Code: Copy the corresponding code snippets into each file.
-
Open in Browser: Open index.html using a web browser (e.g., Chrome, Firefox).
-
Interact: Click on the buttons to perform activities and see how tenaga and poin change.
Explanation:
-
HTML: Structures the webpage with a title, a section to display the player's status, and buttons for each activity.
-
CSS: Styles the webpage for better visuals, including centering content and styling buttons.
-
JavaScript:
- Defines a
player object with initial name, energy, and points.
updateStatus() updates the displayed values on the webpage.
- Each activity function (
main(), makan(), tidur(), lembur()) modifies the player object accordingly and calls updateStatus() to refresh the display.
- Alerts inform the player about the action taken and the new status.
Customization:
-
Player Name: You can change the player's name by modifying the name property in the player object within script.js.
-
Energy and Point Values: Adjust the numbers within the activity functions to change how much tenaga and poin increase or decrease.
-
Negative Energy Prevention: The lembur() function includes a check to prevent energy from going below zero.
Additional Features to Consider:
-
Energy Limit: You might want to set a maximum limit for energy.
-
Disable Buttons: If energy reaches zero, you could disable certain buttons (e.g., main() and lembur()) until the player makan() or tidur().
-
Visual Feedback: Instead of alerts, update the status area to include messages about the latest action.
Example with Additional Feature (Disable Buttons when Energy is Low):
Modify the updateStatus() function and add a new function checkEnergy():
function updateStatus() {
document.getElementById('playerName').innerText = player.name;
document.getElementById('playerEnergy').innerText = player.energy;
document.getElementById('playerPoints').innerText = player.points;
checkEnergy();
}
function checkEnergy() {
// Disable 'Main' and 'Lembur' buttons if energy is 0
document.querySelector('button[onclick="main()"]').disabled = player.energy <= 0;
document.querySelector('button[onclick="lembur()"]').disabled = player.energy <= 0;
}
Now, after updating the status, the game will check if the energy is too low and disable certain actions accordingly.
Feel free to ask if you need further assistance or additional features!