JSFiddle - React, Tailwind, and code Playground
HTML
<!--Pleae refer to Lesson 9.txt for a full description on this lesson -->
<html>
<head>
<title>Basic Incremental Game</title>
<link rel="stylesheet" type="text/css" href="css/Incremental.css">
<script src="js/Incremental.js"></script>
</head>
<body>
<div class="container" onclick="GatherMoney();">
<h1 style="cursor:default"> Money: <span id="money">0</span> </h1>
<div class="Lemon" onclick="Build(0);">Lemon: <span id="Building1Cost">10</span>
</br>PerSec: <span id="Building1PerSec">1</span>
</br>Quantity: <span id="Building1Qty">0</span>
</div>
<div class="Lemon2" onclick="Build(1);">Ape: <span id="Building2Cost">10</span>
</br>PerSec: <span id="Building2PerSec">25</span>
</br>Quantity: <span id="Building2Qty">0</span>
</div>
<div class="saveButton" style="cursor:default" onclick="save()">
<h2>Save </h2>
</div>
</div>
</body>
JavaScript
//The timer to run code every second
var Timer = window.setInterval(function () {
Tick()
}, 1000);
var buildings = [];
//The object declaration for game saves
function GameSave() {
this.money = 1000;
this.buildings = [];
for (var i = 0; i < buildings.length; i++) {
this.buildings[i] = 0;
}
}
//The object declaration for buildings
function Building() {
this.Name = "Lemonade Stand";
this.Cost = 10;
this.PerSec = 1;
}
//The function to initialise all buildings
function InitBuildings() {
LoadBuilding("Lemonade Stand", 10, 1);
LoadBuilding("Lanade Stand", 100, 25);
}
//The function to automatically load a building into the buildings array
function LoadBuilding(name, cost, persec) {
var cur = buildings.length;
buildings[cur] = new Building();
buildings[cur].Name = name;
buildings[cur].Cost = cost;
buildings[cur].PerSec = persec;
}
//The function used to gather money
function GatherMoney() {
game.money++; //++ tells javascript to add 1 to the variable
//Display the player's current money
document.getElementById("money").innerHTML = game.money;
}
//The function that gets run every second
function Tick() {
for (var i = 0; i < buildings.length; i++) {
game.money += game.buildings[i] * buildings[i].PerSec;
}
document.getElementById("money").innerHTML = game.money;
}
//The function to buy a lemonade stand
function Build(id) {
buildings[id].Cost = Math.floor(buildings[id].Cost * Math.pow(1.2, game.buildings[id])); //Increases building costs by buildingcost* (1.2^building).
if (game.money >= buildings[id].Cost) { //Check if the player has enough money, then subtract it and add a new building if they do
game.money -= buildings[id].Cost;
game.buildings[id] = game.buildings[id] + 1;
document.getElementById("money").innerHTML = game.money;
document.getElementById("Building1Qty").innerHTML = game.buildings[0];
...