Using HTML + JS (one file) create trader game, add dynamic economy, items, prices
- User
Using HTML + JS (one file) create trader game, add dynamic economy, items, prices
- Assistant
Using HTML + JS (one file) create trader game, add dynamic economy, items, prices
Sure! Below is a complete HTML file that creates a simple trader game using HTML and JavaScript. The game includes a dynamic economy where item prices fluctuate based on random events. Players can buy and sell items to earn money.
Simply copy the code into a file with a .html extension (e.g., trader_game.html) and open it in your web browser to play.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Trader Game</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
padding: 20px;
}
h1, h2 {
text-align: center;
}
#game {
display: flex;
justify-content: space-between;
}
#market, #inventory {
width: 48%;
background-color: #fff;
padding: 15px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.item {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
button {
padding: 5px 10px;
border: none;
background-color: #28a745;
color: #fff;
border-radius: 4px;
cursor: pointer;
}
button.sell {
background-color: #dc3545;
}
#money {
text-align: center;
margin-bottom: 20px;
font-size: 1.2em;
}
#log {
margin-top: 20px;
background-color: #fff;
padding: 10px;
height: 100px;
overflow-y: auto;
border-radius: 8px;
box-shadow: inset 0 0 5px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<h1>Trader Game</h1>
<div id="money">Money: $1000</div>
<div id="game">
<div id="market">
<h2>Market</h2>
<div id="market-items"></div>
</div>
<div id="inventory">
<h2>Inventory</h2>
<div id="inventory-items"></div>
</div>
</div>
<div id="log"></div>
<script>
// Game Data
const items = [
{ name: 'Apple', basePrice: 10 },
{ name: 'Banana', basePrice: 8 },
{ name: 'Cherry', basePrice: 15 },
{ name: 'Date', basePrice: 12 },
{ name: 'Elderberry', basePrice: 20 }
];
let money = 1000;
const inventory = {};
// Dynamic Prices
const market = items.map(item => ({
...item,
price: item.basePrice
}));
// DOM Elements
const moneyEl = document.getElementById('money');
const marketEl = document.getElementById('market-items');
const inventoryEl = document.getElementById('inventory-items');
const logEl = document.getElementById('log');
// Initialize Inventory
items.forEach(item => {
inventory[item.name] = 0;
});
// Update Money Display
function updateMoney() {
moneyEl.textContent = `Money: $${money.toFixed(2)}`;
}
// Update Market Display
function updateMarket() {
marketEl.innerHTML = '';
market.forEach((item, index) => {
const div = document.createElement('div');
div.className = 'item';
div.innerHTML = `
<span>${item.name} - $${item.price.toFixed(2)}</span>
<button onclick="buyItem(${index})">Buy</button>
`;
marketEl.appendChild(div);
});
}
// Update Inventory Display
function updateInventory() {
inventoryEl.innerHTML = '';
items.forEach((item, index) => {
const qty = inventory[item.name];
const price = market[index].price;
const div = document.createElement('div');
div.className = 'item';
div.innerHTML = `
<span>${item.name} x${qty} - $${price.toFixed(2)} each</span>
<button class="sell" onclick="sellItem(${index})">Sell</button>
`;
inventoryEl.appendChild(div);
});
}
// Buy Item
function buyItem(index) {
const item = market[index];
if (money >= item.price) {
money -= item.price;
inventory[item.name]++;
addLog(`Bought 1 ${item.name} for $${item.price.toFixed(2)}`);
updateMoney();
updateInventory();
} else {
addLog(`Not enough money to buy ${item.name}`);
}
}
// Sell Item
function sellItem(index) {
const item = market[index];
if (inventory[item.name] > 0) {
money += item.price;
inventory[item.name]--;
addLog(`Sold 1 ${item.name} for $${item.price.toFixed(2)}`);
updateMoney();
updateInventory();
} else {
addLog(`No ${item.name} to sell`);
}
}
// Add Log Entry
function addLog(message) {
const entry = document.createElement('div');
entry.textContent = message;
logEl.prepend(entry);
}
// Dynamic Economy: Update Prices Every 5 Seconds
function updatePrices() {
market.forEach(item => {
// Simple Price Fluctuation: +/- 10%
const changePercent = (Math.random() * 0.2) - 0.1;
item.price += item.basePrice * changePercent;
// Ensure price doesn't drop below 1
if (item.price < 1) item.price = 1;
});
addLog('Market prices have been updated.');
updateMarket();
updateInventory();
}
// Initialize Game
function init() {
updateMoney();
updateMarket();
updateInventory();
// Update prices every 5 seconds
setInterval(updatePrices, 5000);
}
// Make buyItem and sellItem accessible globally
window.buyItem = buyItem;
window.sellItem = sellItem;
init();
</script>
</body>
</html>
HTML Structure: The page is divided into two main sections: the Market and the Inventory. There's also a display for the player's money and a log area to show recent actions.
CSS Styling: Basic styling is applied to make the game visually appealing and easy to navigate.
JavaScript Functionality:
Interactivity: Buttons are provided next to each item in both the Market and Inventory sections to perform buy and sell actions respectively.
items array with different name and basePrice values.changePercent calculation in the updatePrices function to make the economy more or less volatile.Feel free to expand and modify the game to suit your preferences!
Not yet included in a published release.