Bringing Back the Classic Lemonade Stand Game with PHP

Nate Smith-Manley
5 min readJan 27, 2024

Few computer games hold the timeless charm and nostalgia of the Lemonade Stand Game. This beloved classic has been a part of computing history for decades, captivating players young and old alike. In this article, we’ll embark on a nostalgic journey into the origins of the Lemonade Stand Game and guide you through the process of creating your very own version using HTML, PHP, JavaScript, and CSS.

Working example at https://www.smith-manley.com/lemonade

A Brief History of the Lemonade Stand Game

The Lemonade Stand Game’s history dates back to the early days of personal computing. Originally developed in 1973 by Bob Jamison, a computer engineer at the Minnesota Educational Computing Consortium (MECC), this game was part of an educational software package designed for schools. MECC’s educational games, including The Oregon Trail, became iconic and introduced countless students to the world of computers.

The objective of the Lemonade Stand Game is deceptively simple: players must manage a virtual lemonade stand for a set number of days, adjusting the price of their lemonade and supplies based on factors like weather and customer demand. The goal is to maximize profits through wise decisions.

Why Create Your Own Lemonade Stand Game?

Recreating this classic game is not only a fun coding project but also an excellent way to learn or practice HTML, PHP, JavaScript, and CSS programming. It encompasses various programming concepts, such as user input, calculations, and conditional statements, making it suitable for both beginners and experienced developers.

Building the Lemonade Stand Game

To create your own PHP Lemonade Stand Game, you’ll need to set up the project structure and implement various functions. Let’s break down the key components of the PHP code for this project:

<?php
session_start();

setlocale(LC_MONETARY, 'en_US');

function get_sales_amount($potential, $weather, $price) {
$weatherModifier = ['Sunny' => 1.2, 'Cloudy' => 0.8, 'Rainy' => 0.5, 'Hot' => 1.5, 'Mild' => 1.0, 'Cold' => 0.7];
$randomFactor = rand(70, 130) / 100.0;
$adjustedPotential = $potential * $weatherModifier[$weather] * $randomFactor;
$priceSensitivity = 0.75 / $price;
return min($adjustedPotential * $priceSensitivity, $_SESSION['inventory']['cups']);
}

function simulateWeather() {
$forecasts = ['Sunny', 'Cloudy', 'Rainy', 'Hot', 'Mild', 'Cold'];
return $forecasts[array_rand($forecasts)];
}

function initializeGame() {
$_SESSION['cash'] = 20.00;
$_SESSION['inventory'] = ['cups' => 50, 'lemons' => 30, 'sugar' => 20];
$_SESSION['prices'] = ['cup' => 0.10, 'lemon' => 0.50, 'sugar' => 0.05];
$_SESSION['score'] = 0;
$_SESSION['week'] = 1;
$_SESSION['game_started'] = true;
}

if (isset($_POST['reset'])) {
initializeGame();
}

if (!isset($_SESSION['game_started'])) {
initializeGame();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['buy'])) {
foreach (['cups', 'lemons', 'sugar'] as $item) {
if (isset($_POST[$item])) {
$buyAmount = $_POST[$item];
if (is_numeric($buyAmount) && $buyAmount >= 0) {
$cost = $buyAmount * $_SESSION['prices'][$item];
if ($cost <= $_SESSION['cash']) {
$_SESSION['inventory'][$item] += $buyAmount;
$_SESSION['cash'] -= $cost;
}
}
}
}
}

if (isset($_POST['next_week'])) {
$weather = simulateWeather();
$_SESSION['current_weather'] = $weather;
$lemonadePrice = $_POST['lemonadePrice'] ?? 0.50;
$potentialSales = rand(30, 100);
$sales = get_sales_amount($potentialSales, $weather, $lemonadePrice);
$_SESSION['last_sales'] = $sales;
$revenue = $sales * $lemonadePrice;
$_SESSION['cash'] += $revenue;
$_SESSION['inventory']['cups'] -= min($_SESSION['inventory']['cups'], $sales);
$_SESSION['inventory']['lemons'] -= min($_SESSION['inventory']['lemons'], $sales);
$_SESSION['inventory']['sugar'] -= min($_SESSION['inventory']['sugar'], $sales);
$_SESSION['score'] += $revenue;
$_SESSION['week']++; // Increment the week number
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Lemonade Stand Game</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="your-integrity-code" crossorigin="anonymous">
</head>
<body>
<div class="lemonade-stand-header">
<h2>Lemonade Stand - Week <?php echo $_SESSION['week']; ?></h2>
<p>Current Cash: $<?php echo number_format($_SESSION['cash'], 2); ?></p>
<i class="fas fa-lemon"></i>
</div>
<div class="weather-info">
<p>Weather Forecast: <?php echo $_SESSION['current_weather'] ?? 'Not Available'; ?></p>
</div>
<div class="sales-info">
<p>Last Week's Sales: $<?php echo number_format($_SESSION['last_sales'] ?? 0, 2); ?></p>
</div>
<div class="inventory">
<p>Inventory: Cups - <?php echo $_SESSION['inventory']['cups']; ?>, Lemons - <?php echo $_SESSION['inventory']['lemons']; ?>, Sugar - <?php echo $_SESSION['inventory']['sugar']; ?></p>
</div>
<div class="action-selection">
<button id="buyButton">Buy Supplies</button>
<button id="priceButton">Play Next Week</button>
</div>
<div id="buyItemsForm" style="display:none;">
<form method="post">
<h3>Buy Items</h3>
<label for="cups">Cups:</label>
<input type="number" id="cups" name="cups" min="0">
<label for="lemons">Lemons:</label>
<input type="number" id="lemons" name="lemons" min="0">
<label for="sugar">Sugar:</label>
<input type="number" id="sugar" name="sugar" min="0">
<input type="submit" name="buy" value="Buy Items">
</form>
</div>
<div id="setPriceForm" style="display:none;">
<form method="post">
<h3>Set Lemonade Price and Proceed to Next Week</h3>
<label for="lemonadePrice">Lemonade Price:</label>
<input type="number" id="lemonadePrice" name="lemonadePrice" step="0.01" min="0">
<input type="submit" name="next_week" value="Next Week">
</form>
</div>
<div class="reset-game">
<form method="post"><br>
<input type="submit" name="reset" value="New Game">
</form>
</div>
<script>
document.getElementById("buyButton").addEventListener("click", function() {
document.getElementById("buyItemsForm").style.display = "block";
document.getElementById("setPriceForm").style.display = "none";
});
document.getElementById("priceButton").addEventListener("click", function() {
document.getElementById("buyItemsForm").style.display = "none";
document.getElementById("setPriceForm").style.display = "block";
});
</script>
</body>
</html>

This stylesheet is where aesthetics meet functionality, allowing us to define the game’s appearance and layout.

body {
font-family: 'Comic Sans MS', cursive, sans-serif;
background-color: #fff3e0;
color: #333;
margin: 0;
padding: 20px;
text-align: center;
font-size: larger;
}

.lemonade-stand-header {
background-color: #ffeb3b;
padding: 20px;
border-radius: 10px;
margin-bottom: 20px;
font-size: larger;
}

.inventory {
background-color: #ffe0b2;
padding: 20px;
border-radius: 10px;
margin-bottom: 20px;
font-size: larger;
}

.action-selection button {
background-color: #4caf50;
color: white;
padding: 15px 20px;
margin: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 20px;
}

.action-selection button:hover {
background-color: #388e3c;
}

#buyItemsForm, #setPriceForm {
background-color: #fff;
border: 2px dashed #ff9800;
padding: 30px;
margin-bottom: 20px;
display: none;
text-align: center;
}

form h3, form label, form input[type="number"], form input[type="submit"] {
display: block;
margin-left: auto;
margin-right: auto;
text-align: center;
}

form h3 {
color: #e67e22;
margin-bottom: 15px;
font-size: larger;
}

form label, form input[type="number"] {
margin-bottom: 10px;
padding: 15px;
border-radius: 4px;
font-size: larger;
}

form input[type="number"] {
border: 1px solid #ddd;
width: 50%;
}

form input[type="submit"] {
background-color: #ff9800;
color: white;
padding: 15px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 50%;
box-sizing: border-box;
font-size: larger;
}

form input[type="submit"]:hover {
background-color: #fb8c00;
}

.reset-game form {
background-color: #fff;
border: 2px solid #f44336;
padding: 50px;
margin-top: 20px;
text-align: center;
}

@media (max-width: 600px) {
form input[type="number"], form input[type="submit"] {
width: 100%;
}
}

Conclusion

Creating your PHP Lemonade Stand Game is not only a delightful coding exercise but also a homage to the rich history of educational computer games. As you develop and customize your game, you’ll gain valuable experience in HTML, PHP, JavaScript, CSS, web development, and game design.

This project offers limitless opportunities for expansion and creativity. You can add features like high scores, graphical enhancements, or even multiplayer functionality. So, roll up your sleeves, embrace the nostalgia, and embark on a coding journey to bring the Lemonade Stand Game back to life in your unique way. Happy coding!

--

--