SEPTEMBER 15, 2023
<aside> 🌳 The simulation models a natural system focusing on the growth of a plant in a controlled environment. The system allows interaction through soil selection and watering.
</aside>
The environment is a simplistic representation of a garden setting. The primary forces at play are soil type (loamy or clay), water availability, and the plant's biological growth process. While not explicitly modeled, these forces imply the presence of sunlight and other necessary conditions for plant growth.
Screen Recording 2023-12-27 at 1.29.09 PM.mov
// Plant Growth Simulation
// Global Variables
boolean chooseSoil = true; // Flag to choose soil type
boolean loamySoil = false; // Flag for loamy soil selection
boolean claySoil = false; // Flag for clay soil selection
boolean growing = false; // Flag to indicate if the plant is growing
boolean watered = false; // Flag to indicate if the plant has been watered
boolean plantDead = false; // Flag to indicate if the plant is dead
boolean showMessage = false;// Flag to show messages
float jugX, jugY; // Coordinates for the water jug
int growthStage = 0; // Tracks the growth stage of the plant
int waterAmount = 0; // Amount of water given to the plant
int correctWaterAmount; // Correct amount of water based on soil type
int consecutiveWrongWaterings = 0; // Count of consecutive incorrect waterings
String fruitName = ""; // Name of the fruit produced by the plant
// Setup function for initial settings
void setup() {
size(800, 600); // Canvas size
jugX = width - 50;
jugY = height - 130;
// Set correct water amount based on soil type
correctWaterAmount = loamySoil ? 12 : 36;
}
// Draw function for the main loop
void draw() {
background(255); // Set background color
// Soil selection or main scene rendering
if (chooseSoil) {
drawSoilOptions();
} else {
drawScene();
// Show message if needed
if (showMessage) {
drawMessage();
}
}
}
// Function to draw messages on screen
void drawMessage() {
fill(0, 150); // Set color and transparency for message background
rect(0, height/2 - 30, width, 60); // Draw message background
fill(255); // Set text color
textSize(25);
textAlign(CENTER, CENTER);
text(fruitName, width/2, height/2); // Display fruit name
}
// Mouse interaction handling for soil selection
void mousePressed() {
handleSoilSelection();
}
// Keyboard interaction for watering and message handling
void keyPressed() {
if (!plantDead) {
handleWatering(key);
}
// Hide message on space or enter press
if (showMessage && (key == ' ' || keyCode == ENTER)) {
showMessage = false;
}
}
// Plant Drawing and Growth Handling
// Array of fruits for selection
String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
// Function to draw the plant
void drawPlant() {
if (!plantDead) {
// Draw stem
fill(34, 139, 34); // Set color to green
rect(width / 2 - 5, height - 100 - growthStage * 10, 10, growthStage * 10);
// Draw branches and leaves at each growth stage
for (int i = 1; i <= growthStage / 2; i++) {
// Drawing branches
line(width / 2, height - 100 - i * 20, width / 2 - i * 10, height - 100 - (i * 20) + 10);
line(width / 2, height - 100 - i * 20, width / 2 + i * 10, height - 100 - (i * 20) + 10);
// Drawing leaves on branches
ellipse(width / 2 - i * 10, height - 100 - (i * 20) + 10, 15, 8); // Left branch leaf
ellipse(width / 2 + i * 10, height - 100 - (i * 20) + 10, 15, 8); // Right branch leaf
}
// Draw flowers or fruits depending on growth stage
if (growthStage >= 5 && growthStage < 10) {
fill(255, 0, 0); // Set color to red for flowers
ellipse(width / 2, height - 100 - growthStage * 10 - 10, 10, 10);
} else if (growthStage >= 10) {
fill(255, 165, 0); // Set color to orange for fruit
ellipse(width / 2, height - 100 - growthStage * 10 - 10, 15, 15);
// Display harvested fruit message
if (showMessage) {
fill(0);
text(fruitName + " harvested!", width / 2, 50);
}
}
} else {
// Draw dead plant
fill(139, 69, 19); // Set color to brown for dead plant
rect(width / 2 - 5, height - 100 - growthStage * 10, 10, growthStage * 10);
}
}
// Function to handle watering based on keyboard input
void handleWatering(char keyChar) {
// 'W' or 'w' for watering with 12 oz, 'E' or 'e' for 36 oz
if (keyChar == 'w' || keyChar == 'W') {
waterAmount = 12;
} else if (keyChar == 'e' || keyChar == 'E') {
waterAmount = 36;
}
// Check if watering is correct and grow or kill the plant accordingly
if (waterAmount == correctWaterAmount) {
growPlant();
} else {
consecutiveWrongWaterings++;
if (consecutiveWrongWaterings >= 2) {
plantDead = true;
}
}
}
// Function to handle plant growth
void growPlant() {
// Adjust growth stage based on soil type
if (loamySoil) {
growthStage += 2; // Faster growth for loamy soil
} else {
growthStage += 1; // Slower growth for clay soil
}
// Handle fruit generation at a specific growth stage
if (growthStage == 10) {
showMessage = true;
fruitName = fruits[int(random(fruits.length))]; // Randomly select a fruit
}
// Cap the growth stage to prevent exceeding design limits
if (growthStage > 12) {
growthStage = 12;
}
}
// Scene Drawing and Display
// Function to draw the entire scene
void drawScene() {
// Display instructions
fill(0);
textSize(20);
textAlign(CENTER, CENTER);
text("Water the plant using W for 12 oz and E for 36 oz.", width/2, 30);
// Draw components of the scene
drawPot();
drawPlant();
drawJug();
// Show message if the plant is dead
if (plantDead) {
showMessage();
}
}
// Function to draw the pot
void drawPot() {
fill(139, 69, 19); // Set color for the pot
rect(width / 2 - 50, height - 100, 100, 100); // Draw pot
}
// Function to draw the watering jug
void drawJug() {
fill(0, 0, 255); // Set color for the jug
rect(jugX, jugY, 40, 60); // Draw jug
}
// Function to show a message when the plant dies
void showMessage() {
fill(255, 228, 196); // Light background for the message box
rect(150, 200, 500, 200); // Draw message box
fill(0); // Set text color to black
textSize(24);
text("You watered the plant incorrectly!\\nIt has died.", width / 2, height / 2);
}
// Soil Selection and Handling
// Function to draw soil options
void drawSoilOptions() {
// Draw buttons for soil selection
fill(210, 105, 30); // Color for loamy soil button
rect(width / 4 - 50, height / 2 - 50, 100, 100);
fill(139, 69, 19); // Color for clay soil button
rect(3 * width / 4 - 50, height / 2 - 50, 100, 100);
// Soil type labels
fill(0);
textSize(16);
textAlign(CENTER, CENTER);
text("Loamy Soil\\nFast Growth", width / 4, height / 2 + 70);
text("Clay Soil\\nSlow Growth", 3 * width / 4, height / 2 + 70);
}
// Function to handle soil selection based on mouse input
void handleSoilSelection() {
// Check if soil selection is active and process mouse clicks
if (chooseSoil) {
if (mouseX > width / 4 - 50 && mouseX < width / 4 + 50 && mouseY > height / 2 - 50 && mouseY < height / 2 + 50) {
loamySoil = true; // Select loamy soil
chooseSoil = false;
} else if (mouseX > 3 * width / 4 - 50 && mouseX < 3 * width / 4 + 50 && mouseY > height / 2 - 50 && mouseY < height / 2 + 50) {
claySoil = true; // Select clay soil
chooseSoil = false;
}
}
}