The Farm Game starts on a small farm where you have a barn and a field. You start the game with a few animals, a few fields, and $5000. Your goal is to produce goods every week, sell them in the marketplace, and invest the money you earn. But beware! Prices change every week. Sometimes they crash, and sometimes they soar.
It's your turn: will you sell what you have now, or wait for better prices?
To get started:
Download the project and open the `index.html` file in your browser. That's it!
- Manage 3 types of animals and 3 types of plants.
- Produce and sell 3 animal products and 3 plant products.
- Weekly consumption of feed and fertilizer for your farm.
- Dynamic weekly production and price fluctuations.
- View price history (last two weeks).
- Detailed UI design for ease of use.
- Buy and sell resources in the marketplace.
- Track your farm statistics.
It's a turn-based farm management game. Your ultimate goal is to build the largest and most profitable farm. Here are some key gameplay tips:
- Resource Management: Make sure you have enough animal feed and plant fertilizer. These are consumed every week based on the number of animals and plants you own.
- Production: Animals and plants produce goods weekly, depending on their type and production coefficients.
- Marketplace: Decide when to sell your products. Prices change weekly, so timing your sales is crucial.
- Invest Wisely: Use your earnings to buy more animals, plants, or resources to expand your farm.
- Survival: Avoid running out of essential resources. If you don't have enough feed or fertilizer, your animals and plants may perish.
Good luck managing your farm! πΎπ
- HTML: For structuring the web application.
- CSS: For styling and layout.
- Bootstrap v5.3: For responsive and modern UI components.
- JavaScript: Core game logic and interactivity.
- JS DOM: Dynamic updates to the game interface.
The Pricing()
function updates the price of all items (animals, plants, and products) based on random coefficients. Prices fluctuate weekly, mimicking a dynamic market system.
function Pricing() {
function getRandomCoefficient(min, max) {
return Math.random() * (max - min) + min;
}
for (let i of animalList) {
i.lastLastPrice = i.lastPrice;
i.lastPrice = i.price;
i.price = Math.round(i.price * getRandomCoefficient(0.97, 1.03) * 100) / 100;
}
for (let i of plantList) {
i.lastLastPrice = i.lastPrice;
i.lastPrice = i.price;
i.price = Math.round(i.price * getRandomCoefficient(0.94, 1.07) * 100) / 100;
}
for (let i of productionList) {
i.lastLastPrice = i.lastPrice;
i.lastPrice = i.price;
i.price = Math.round(i.price * getRandomCoefficient(0.9, 1.1) * 100) / 100;
}
}
The Production()
function calculates the weekly production of goods based on the number of animals and plants, as well as their respective production coefficients.
function Production() {
milk.number = milk.number + cow.number * cow.productionCoefficient;
wool.number = wool.number + sheep.number * sheep.productionCoefficient;
egg.number = egg.number + chicken.number * chicken.productionCoefficient;
fabric.number = fabric.number + cotton.number * cotton.productionCoefficient;
fame.number = fame.number + wheat.number * wheat.productionCoefficient;
oil.number = oil.number + sunflower.number * sunflower.productionCoefficient;
}
The WeeklyConsumption()
function manages the weekly consumption of feed and fertilizer. If resources run out, animals or plants are lost.
function WeeklyConsumption() {
for (let i of animalList) {
feed.number -= i.consumptionCoefficient * i.number;
}
if (feed.number < 0) {
alert("Animal Feed is Low. Buy feed or animals will die.");
cow.number -= 1;
sheep.number -= 2;
chicken.number -= 5;
feed.number = 0;
}
for (let i of plantList) {
fertilizer.number -= i.consumptionCoefficient * i.number;
}
if (fertilizer.number < 0) {
alert("Plant Fertilizer is Low. Buy fertilizer or plants will fade.");
cotton.number -= 1;
wheat.number -= 2;
sunflower.number -= 2;
fertilizer.number = 0;
}
}
Allows players to buy products from the marketplace if they have enough money.
function MarketplaceBuy() {
if (MarketplaceBuyForm.value < 1) {
alert("Must be bigger than zero");
} else {
let selectedProduct = productList[Number(MarketplaceBuySelect.value)];
if (myFarm.money < selectedProduct.price * Number(MarketplaceBuyForm.value)) {
alert("Money not enough.");
} else {
myFarm.money -= selectedProduct.price * Number(MarketplaceBuyForm.value);
selectedProduct.number += Number(MarketplaceBuyForm.value);
}
}
}
Allows players to sell products to the marketplace and earn money.
function MarketplaceSell() {
if (MarketplaceSellForm.value < 1) {
alert("Must be bigger than zero");
} else {
let selectedProduct = productList[Number(MarketplaceSellSelect.value)];
if (selectedProduct.number < Number(MarketplaceSellForm.value)) {
alert("Not enough products.");
} else {
myFarm.money += selectedProduct.price * Number(MarketplaceSellForm.value);
selectedProduct.number -= Number(MarketplaceSellForm.value);
}
}
}
The DisplayAllItems()
function dynamically updates the UI with current stats, weekly production, prices, and inventory.
function DisplayAllItems() {
// Update stats and inventory
TotalFarmValue.innerHTML = myFarm.TotalFarmValue();
Money.innerHTML = myFarm.money;
WeekCounter.innerText = `${week} week`;
week++;
}
This project is licensed under the MIT License.
If you have any questions or feedback, feel free to reach out to me at elagzeren@gmail.com.
Happy farming! π±