From dbc6e8c548a9a6618d75133cbc65077406be18eb Mon Sep 17 00:00:00 2001 From: bdnugget Date: Fri, 23 May 2025 13:54:12 +0200 Subject: [PATCH] Statusbars for stront tamagotchi game --- .vscode/c_cpp_properties.json | 16 ++++ strontgame.ino | 135 ++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 strontgame.ino diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..07c08df --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "linux-clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/strontgame.ino b/strontgame.ino new file mode 100644 index 0000000..0db39d2 --- /dev/null +++ b/strontgame.ino @@ -0,0 +1,135 @@ +#include +#include + +// OLED display size +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 + +// Create display object +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); + +// Pin assignments +#define FEED_BUTTON_PIN 2 +#define POOP_BUTTON_PIN 3 + +// Game constants +#define MAX_FOOD 100 +#define MAX_POOP 100 +#define FOOD_BAR_Y 0 +#define POOP_BAR_Y 8 +#define BAR_HEIGHT 7 +#define BAR_WIDTH 120 +#define BAR_X 4 +#define BAR_LABEL_X 0 +#define BAR_LABEL_WIDTH 4 +#define YELLOW_BAR_HEIGHT 16 + +// Game state +int food = MAX_FOOD / 2; +int poop = MAX_POOP / 2; +bool gameOver = false; +unsigned long lastUpdate = 0; +const unsigned long updateInterval = 1000; // ms + +void drawBars() { + // Draw yellow background for status area + display.fillRect(0, 0, SCREEN_WIDTH, YELLOW_BAR_HEIGHT, SSD1306_WHITE); + // Draw Food bar label (F) + display.setTextColor(SSD1306_BLACK); + display.setCursor(BAR_LABEL_X, FOOD_BAR_Y); + display.print("F"); + // Draw Poop bar label (P) + display.setCursor(BAR_LABEL_X, POOP_BAR_Y); + display.print("P"); + // Draw Food bar + int foodBarLen = (food * BAR_WIDTH) / MAX_FOOD; + display.fillRect(BAR_X, FOOD_BAR_Y, foodBarLen, BAR_HEIGHT, SSD1306_BLACK); + display.drawRect(BAR_X, FOOD_BAR_Y, BAR_WIDTH, BAR_HEIGHT, SSD1306_BLACK); + // Draw Poop bar + int poopBarLen = (poop * BAR_WIDTH) / MAX_POOP; + display.fillRect(BAR_X, POOP_BAR_Y, poopBarLen, BAR_HEIGHT, SSD1306_BLACK); + display.drawRect(BAR_X, POOP_BAR_Y, BAR_WIDTH, BAR_HEIGHT, SSD1306_BLACK); +} + +void drawGameOver(const char* message) { + display.clearDisplay(); + display.setTextColor(SSD1306_WHITE); + display.setCursor(0, 24); + display.setTextSize(1); + display.println("GAME OVER!"); + display.println(message); + display.display(); +} + +void setup() { + if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { + while (true); // Don't proceed, loop forever + } + pinMode(FEED_BUTTON_PIN, INPUT_PULLUP); + pinMode(POOP_BUTTON_PIN, INPUT_PULLUP); + display.clearDisplay(); + drawBars(); + display.display(); + lastUpdate = millis(); +} + +void loop() { + if (gameOver) return; + // Handle buttons (active LOW) + bool feedPressed = digitalRead(FEED_BUTTON_PIN) == LOW; + bool poopPressed = digitalRead(POOP_BUTTON_PIN) == LOW; + static bool lastFeedPressed = false; + static bool lastPoopPressed = false; + + // Feed button: increase food, increase poop a bit + if (feedPressed && !lastFeedPressed) { + food += 15; + if (food > MAX_FOOD) food = MAX_FOOD; + poop += 5; + if (poop > MAX_POOP) poop = MAX_POOP; + } + // Poop button: decrease poop + if (poopPressed && !lastPoopPressed) { + poop -= 20; + if (poop < 0) poop = 0; + } + lastFeedPressed = feedPressed; + lastPoopPressed = poopPressed; + + // Food decreases, poop increases over time + if (millis() - lastUpdate > updateInterval) { + lastUpdate = millis(); + food--; + if (food < 0) food = 0; + if (food > 0) poop++; + if (poop > MAX_POOP) poop = MAX_POOP; + } + + // Check game over conditions + if (food == 0) { + gameOver = true; + drawGameOver("Starved!"); + return; + } + if (food == MAX_FOOD) { + gameOver = true; + drawGameOver("Overate! Exploded!"); + return; + } + if (poop == MAX_POOP) { + gameOver = true; + drawGameOver("Poop explosion!"); + return; + } + if (poop == 0) { + gameOver = true; + drawGameOver("Constipation!"); + return; + } + + // Redraw bars + display.clearDisplay(); + drawBars(); + display.display(); + delay(50); +}