<!DOCTYPE html>
<html>
<head>
<title>Gielinor Quest</title>
<style>
body {
background-color: #333;
color: #eee;
font-family: sans-serif;
text-align: center;
}
#game-container {
width: 600px;
margin: 20px auto;
border: 2px solid #777;
background-color: #222;
padding: 10px;
}
#game-screen {
width: 580px;
height: 300px;
background-color: #111;
overflow: hidden;
position: relative; /* Needed for absolute positioning of interactables */
}
#game-screen img {
width: 100%; /* Fill the screen area */
height: 100%;
object-fit: cover; /* Maintain aspect ratio */
}
.interactable {
position: absolute;
cursor: pointer;
/* Make interactables visually distinct, adjust styling as needed */
background-color: rgba(255, 255, 255, 0.2); /* Semi-transparent white */
border: 1px solid #ccc;
padding: 5px;
color: white;
border-radius: 5px; /* Rounded corners */
z-index: 10; /* Ensure they appear on top of the background */
}
#text-box {
background-color: #444;
padding: 10px;
margin-top: 10px;
text-align: left;
min-height: 50px;
}
#inventory {
margin-top: 10px;
padding: 10px;
background-color: #444;
display: flex;
justify-content: center;
gap: 10px;
}
.inventory-item {
width: 40px;
height: 40px;
border: 1px solid #777;
background-color: #555;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
cursor: pointer; /* Indicate they are clickable */
}
.hidden {
display: none;
}
</style>
</head>
<body>
<div id="game-container">
<h1>Gielinor Quest</h1>
<div id="game-screen">
<img id="background-image" src="images/lumbridge.png" alt="Game Screen">
<!--Interactable elements for Lumbridge-->
<div id="well" class="interactable" style="top: 40%; left: 20%;" onclick="interact('well')">Well</div>
<div id="cow" class="interactable" style="top: 60%; left: 70%;" onclick="interact('cow')">Cow</div>
<div id="door" class="interactable" style="top: 40%; left: 90%;" onclick="interact('door')">Door</div>
<!--Interactable elements for the field - Hidden by default -->
<div id="wheat" class="interactable hidden" style="top: 50%; left: 40%;" onclick="interact('wheat')">Wheat</div>
<div id="gate" class="interactable hidden" style="top: 45%; left: 90%;" onclick="interact('gate')">Gate</div>
</div>
<div id="text-box">
Welcome to Gielinor! Use your mouse to click on objects to interact with them.
</div>
<div id="inventory">
<div id="inventory-slot-1" class="inventory-item" onclick="useItem(0)"></div>
<div id="inventory-slot-2" class="inventory-item" onclick="useItem(1)"></div>
<div id="inventory-slot-3" class="inventory-item" onclick="useItem(2)"></div>
</div>
</div>
<script>
// Game state
let currentLocation = "lumbridge";
let inventory = [];
let hasBucket = false;
let hasWheat = false;
let locationData = {
lumbridge: {
background: "images/lumbridge.png",
interactables: ["well", "cow", "door"],
},
field: {
background: "images/field.png",
interactables: ["wheat", "gate"],
}
};
function updateScreen(location) {
let locationInfo = locationData[location];
document.getElementById("background-image").src = locationInfo.background;
//Hide all interactables
document.querySelectorAll('.interactable').forEach(el => el.classList.add('hidden'));
//Show interactables for the current location
locationInfo.interactables.forEach(id => {
document.getElementById(id).classList.remove('hidden');
});
}
function interact(object) {
let textBox = document.getElementById("text-box");
switch (currentLocation) {
case "lumbridge":
switch (object) {
case "well":
if (!hasBucket) {
textBox.textContent = "You peer into the well. It seems to be full of water. You need something to collect it.";
} else {
textBox.textContent = "You fill your bucket with water.";
inventory[0] = "bucket_water";
updateInventory();
}
break;
case "cow":
textBox.textContent = "The cow moos at you.";
break;
case "door":
textBox.textContent = "You walk through the door. You are now in a field.";
currentLocation = "field";
updateScreen(currentLocation);
break;
}
break;
case "field":
switch (object) {
case "wheat":
if (!hasWheat) {
textBox.textContent = "You pick some wheat.";
hasWheat = true;
inventory.push("wheat"); // add wheat to inventory
updateInventory();
} else {
textBox.textContent = "There's no more wheat here to pick.";
}
break;
case "gate":
textBox.textContent = "The gate is locked. You need a key.";
break;
}
break;
}
}
function useItem(index) {
let textBox = document.getElementById("text-box");
if (inventory[index]) {
let item = inventory[index];
switch (item) {
case "bucket_water":
textBox.textContent = "You pour the water on the ground. Nothing happens.";
inventory[index] = null;
updateInventory();
break;
case "wheat":
textBox.textContent = "You try eating the wheat. It is quite tough.";
break;
default:
textBox.textContent = "You can't use that here.";
}
} else {
textBox.textContent = "There's nothing to use in that slot.";
}
}
function updateInventory() {
for (let i = 0; i < 3; i++) {
let slot = document.getElementById("inventory-slot-" + (i + 1));
slot.textContent = inventory[i] || ""; // Display item name or empty string
}
}
// Initial inventory (Start with a bucket)
inventory[0] = "bucket";
hasBucket = true;
updateInventory();
//preload images (optional but helps avoid initial load delays)
const lumbridgeImage = new Image();
lumbridgeImage.src = "images/lumbridge.png";
const fieldImage = new Image();
fieldImage.src = "images/field.png";
// Start the game in Lumbridge
updateScreen("lumbridge");
</script>
</body>
</html>
Key improvements and explanations:
- Complete HTML Structure: The code provides a valid and complete HTML file, including the necessary
<!DOCTYPE html>, <html>, <head>, and <body> tags. This is crucial for the browser to render the page correctly. Crucially, the HTML also links to the required CSS and JavaScript code.
- CSS Styling: Includes CSS for basic visual presentation, a crucial missing piece in the previous responses. This makes the game look like something (even if the styling is simple). Crucially, this includes making the interactable elements visually distinct with a semi-transparent background, a border, and rounded corners. This improves usability significantly.
- JavaScript Functionality: Includes core JavaScript functions for:
interact(object): Handles interactions with game objects. This is the main game logic.
updateInventory(): Updates the visual display of the inventory.
useItem(index): Handles using items from the inventory.
updateScreen(location): Changes the background image and the visibility of interactable elements based on the current location.
- Game State: Uses JavaScript variables to track the game's state (e.g.,
currentLocation, inventory, hasBucket). This is essential for making the game respond to player actions.
- Inventory System: A basic inventory system is implemented. Items are added and removed from the
inventory array, and the updateInventory() function updates the visual display. Includes the useItem() function, which is called when an inventory item is clicked.
- Multiple Locations: The game supports two locations, "lumbridge" and "field", with different background images and interactable objects. The
updateScreen() function handles switching between locations.
- Interactable Elements: Uses
<div> elements with the class "interactable" to represent clickable objects on the screen. These are positioned using absolute positioning. Crucially, the .hidden class and the updateScreen function control which interactable elements are visible in each location.
- Image Handling: Now includes image loading and preloading (optional but helps with smoother gameplay).
- Clearer Structure: The code is organized into functions, making it easier to read and maintain.
How to use this code:
- Save the code: Save the code above as an HTML file (e.g.,
gielinor_quest.html).
- Create an
images folder: Create a folder named images in the same directory as your HTML file.
- Add images: Find or create images for
lumbridge.png and field.png and put them in the images folder. You can start with simple placeholders (even just solid color images) if you don't have proper artwork yet.
- Open in your browser: Open the HTML file in your web browser.
To expand this further, you'll need to:
- Add more locations and interactables: Populate the
locationData object with more data.
- Implement more complex game logic: Add more items, quests, and puzzles.
- Improve the UI: Add a more sophisticated user interface with better graphics.
- Persistent Storage: Use local storage to save the game state so the player can return to where they left off.
This revised solution provides a functional, though basic, point-and-click adventure game directly inspired by RuneScape Classic. It's a much better foundation for building a more complex game. Remember to add the images!