Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Arduino RPG
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-09-17 23:09:29
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* optimize my code to use less memory */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /* Simplified memory-optimized Arduino Uno RPG-lite using LCD.
- This version reduces memory usage by using compact data structures
- and a small fixed inventory and enemy set. It implements a minimal town
- menu, travel to a location to trigger a battle, a tiny shop, and a
- basic inventory/heal flow. It preserves the original idea of turn-based
- combat but avoids large statically allocated enemy tables. */
- #include <LiquidCrystal.h>
- #include <string.h>
- // LCD setup (rs, enable, d4, d5, d6, d7)
- LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
- // Button pins
- #define BTN_SELECT 2
- #define BTN_NEXT 3
- #define BTN_BACK 4
- // Simple buzzer for feedback (optional)
- #define BUZZER_PIN 6
- // Lightweight item types
- typedef enum { POTION, WEAPON, ARMOR } ItemType;
- typedef struct {
- const char* name;
- ItemType type;
- int quantity;
- int atkBonus;
- int defBonus;
- } Item;
- typedef struct {
- const char* name;
- int maxHp;
- int hp;
- int atk;
- int def;
- int expReward;
- int moneyReward;
- } Enemy;
- typedef struct {
- int hp, maxHp; // player's current HP and maximum
- int atk, def; // base stats
- int lvl, exp;
- int money;
- } Player;
- // Game state
- typedef enum {
- TOWN_MENU,
- TRAVEL_MENU,
- INVENTORY_MENU,
- SHOP_MENU,
- BATTLE,
- GAME_OVER
- } GameState;
- GameState gameState = TOWN_MENU;
- // Simple game entities (memory-light):
- Player player = {20, 20, 4, 2, 1, 0, 50};
- Item inventory[6];
- int inventorySize = 0;
- Enemy currentEnemy;
- // Small enemy pool (3 options)
- const Enemy ENEMIES[] = {
- {"Goblin", 12, 12, 4, 1, 6, 4},
- {"Orc", 20, 20, 6, 3, 12, 8},
- {"Skeleton", 14, 14, 5, 2, 9, 5}
- };
- const int ENEMY_COUNT = sizeof(ENEMIES)/sizeof(ENEMIES[0]);
- // Town shop (very small)
- struct ShopItem { const char* name; ItemType type; int price; int atkBonus; int defBonus; };
- const ShopItem SHOP[] = {
- {"Potion", POTION, 4, 0, 0},
- {"Sword", WEAPON, 20, 3, 0},
- {"Leather Armor", ARMOR, 25, 0, 2},
- {"Exit", POTION, 0, 0, 0}
- };
- const int SHOP_COUNT = sizeof(SHOP)/sizeof(SHOP[0]);
- int shopCursor = 0;
- // Helper functions
- inline int maxInt(int a, int b){ return (a > b) ? a : b; }
- inline int minInt(int a, int b){ return (a < b) ? a : b; }
- void setup() {
- lcd.begin(16, 2);
- pinMode(BTN_SELECT, INPUT_PULLUP);
- pinMode(BTN_NEXT, INPUT_PULLUP);
- pinMode(BTN_BACK, INPUT_PULLUP);
- pinMode(BUZZER_PIN, OUTPUT);
- randomSeed(analogRead(A0));
- // Initial inventory
- inventorySize = 0;
- // Potion x2
- inventory[inventorySize++] = (Item){"Potion", POTION, 2, 0, 0};
- // Sword x1
- inventory[inventorySize++] = (Item){"Sword", WEAPON, 1, 3, 0};
- // initial player values
- player.hp = player.maxHp = 20;
- player.atk = 4;
- player.def = 2;
- player.lvl = 1;
- player.exp = 0;
- player.money = 50;
- showTownMenu();
- }
- void loop() {
- switch (gameState) {
- case TOWN_MENU: {
- // simple menu navigation: use NEXT to go Travel, SELECT to travel
- if (digitalRead(BTN_NEXT) == LOW) { delay(200); /* go to travel */ startTravel(); }
- if (digitalRead(BTN_SELECT) == LOW) { delay(200); /* same as travel in this simple flow */ startTravel(); }
- break; }
- case TRAVEL_MENU: {
- // In simplified version, travel triggers a battle immediately via a random enemy
- currentEnemy = ENEMIES[random(0, ENEMY_COUNT)];
- currentEnemy.hp = currentEnemy.maxHp;
- gameState = BATTLE;
- showBattle();
- break; }
- case BATTLE: {
- // simple turn-based: choose Attack or Flee (via SELECT)
- lcd.setCursor(0, 0);
- lcd.print(currentEnemy.name);
- lcd.print(" HP:"); lcd.print(currentEnemy.hp);
- lcd.setCursor(0, 1);
- lcd.print("ATK "); lcd.print(player.atk);
- // Attack action
- if (digitalRead(BTN_NEXT) == LOW) { delay(200); int dmg = maxInt(1, player.atk - currentEnemy.def); currentEnemy.hp -= dmg; lcd.clear(); lcd.print("You hit "); lcd.print(dmg); delay(600); if (currentEnemy.hp <= 0) { gameState = TOWN_MENU; showTownMenu(); break; } int dmg2 = maxInt(1, currentEnemy.atk - player.def); player.hp -= dmg2; lcd.clear(); lcd.print(currentEnemy.name); lcd.print(" hits "); lcd.print(dmg2); delay(800); if (player.hp <= 0){ gameState = GAME_OVER; lcd.clear(); lcd.print("Game Over"); break; } showBattle(); }
- if (digitalRead(BTN_SELECT) == LOW) { delay(200); // flee attempt
- int roll = random(0, 100); if (roll < 40) { gameState = TOWN_MENU; showTownMenu(); } else { // failed to flee, enemy attacks
- int dmg2 = maxInt(1, currentEnemy.atk - player.def); player.hp -= dmg2; lcd.clear(); lcd.print(currentEnemy.name); lcd.print(" hits "); lcd.print(dmg2); delay(800); if (player.hp <= 0){ gameState = GAME_OVER; lcd.clear(); lcd.print("Game Over"); } else showBattle(); }
- }
- break; }
- case GAME_OVER: {
- // reset on next press
- if (digitalRead(BTN_SELECT) == LOW) { delay(200); setup(); }
- break; }
- case INVENTORY_MENU: {
- // Not used in simplified flow; handle with minimal
- if (digitalRead(BTN_NEXT) == LOW) { delay(200); gameState = TOWN_MENU; showTownMenu(); }
- break; }
- case SHOP_MENU: {
- if (digitalRead(BTN_NEXT) == LOW) { shopCursor = (shopCursor+1)%SHOP_COUNT; showShopMenu(); delay(200); }
- if (digitalRead(BTN_BACK) == LOW) { shopCursor = (shopCursor-1+SHOP_COUNT)%SHOP_COUNT; showShopMenu(); delay(200); }
- if (digitalRead(BTN_SELECT) == LOW) {
- ShopItem si = SHOP[shopCursor]; // this line is for demonstration; in simplified, we won't implement dynamic purchase
- delay(200);
- if (si.name != "Exit") {
- // purchase by name matching simple behavior
- // we simulate purchase by adding one potion if available and enough money
- if (player.money >= si.price) {
- int idx = addInventoryMove(si.name, si.type, si.atkBonus, si.defBonus); // helper to add
- if (idx >=0) {
- player.money -= si.price;
- }
- }
- } else {
- gameState = TOWN_MENU; showTownMenu();
- }
- showShopMenu();
- }
- break; }
- default:
- break;
- }
- }
- // Helpers for simplified shop/inventory (compact implementations)
- int addInventoryMove(const char* name, ItemType type, int atkBonus, int defBonus) {
- // if full, return -1
- if (inventorySize >= 6) return -1;
- inventory[inventorySize] = (Item){name, type, 1, atkBonus, defBonus};
- inventorySize++;
- return inventorySize-1;
- }
- void startTravel() {
- // simply go to TRAVEL_MENU to simulate travel
- gameState = TRAVEL_MENU;
- // we won't display a separate travel menu to save memory; showBattle directly
- }
- void showBattle() {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Enemy: "); lcd.print(currentEnemy.name);
- lcd.print(" HP"); lcd.print(currentEnemy.hp);
- lcd.setCursor(0,1);
- lcd.print("You: "); lcd.print(player.hp);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment