Statusbars for stront tamagotchi game

This commit is contained in:
bdnugget 2025-05-23 13:54:12 +02:00
commit dbc6e8c548
2 changed files with 151 additions and 0 deletions

16
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -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
}

135
strontgame.ino Normal file
View File

@ -0,0 +1,135 @@
#include <Wire.h>
#include <Adafruit_SSD1306.h>
// 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);
}