pleasedontcode

Arduino RPG rev_01

Sep 17th, 2025
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Arduino RPG
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-09-17 23:09:29
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* optimize my code to use less memory */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /* Simplified memory-optimized Arduino Uno RPG-lite using LCD.
  27.    This version reduces memory usage by using compact data structures
  28.    and a small fixed inventory and enemy set. It implements a minimal town
  29.    menu, travel to a location to trigger a battle, a tiny shop, and a
  30.    basic inventory/heal flow. It preserves the original idea of turn-based
  31.    combat but avoids large statically allocated enemy tables. */
  32.  
  33. #include <LiquidCrystal.h>
  34. #include <string.h>
  35.  
  36. // LCD setup (rs, enable, d4, d5, d6, d7)
  37. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
  38.  
  39. // Button pins
  40. #define BTN_SELECT 2
  41. #define BTN_NEXT   3
  42. #define BTN_BACK   4
  43. // Simple buzzer for feedback (optional)
  44. #define BUZZER_PIN 6
  45.  
  46. // Lightweight item types
  47. typedef enum { POTION, WEAPON, ARMOR } ItemType;
  48.  
  49. typedef struct {
  50.   const char* name;
  51.   ItemType type;
  52.   int quantity;
  53.   int atkBonus;
  54.   int defBonus;
  55. } Item;
  56.  
  57. typedef struct {
  58.   const char* name;
  59.   int maxHp;
  60.   int hp;
  61.   int atk;
  62.   int def;
  63.   int expReward;
  64.   int moneyReward;
  65. } Enemy;
  66.  
  67. typedef struct {
  68.   int hp, maxHp; // player's current HP and maximum
  69.   int atk, def;  // base stats
  70.   int lvl, exp;
  71.   int money;
  72. } Player;
  73.  
  74. // Game state
  75. typedef enum {
  76.   TOWN_MENU,
  77.   TRAVEL_MENU,
  78.   INVENTORY_MENU,
  79.   SHOP_MENU,
  80.   BATTLE,
  81.   GAME_OVER
  82. } GameState;
  83.  
  84. GameState gameState = TOWN_MENU;
  85.  
  86. // Simple game entities (memory-light):
  87. Player player = {20, 20, 4, 2, 1, 0, 50};
  88. Item inventory[6];
  89. int inventorySize = 0;
  90.  
  91. Enemy currentEnemy;
  92.  
  93. // Small enemy pool (3 options)
  94. const Enemy ENEMIES[] = {
  95.   {"Goblin", 12, 12, 4, 1, 6, 4},
  96.   {"Orc", 20, 20, 6, 3, 12, 8},
  97.   {"Skeleton", 14, 14, 5, 2, 9, 5}
  98. };
  99. const int ENEMY_COUNT = sizeof(ENEMIES)/sizeof(ENEMIES[0]);
  100.  
  101. // Town shop (very small)
  102. struct ShopItem { const char* name; ItemType type; int price; int atkBonus; int defBonus; };
  103. const ShopItem SHOP[] = {
  104.   {"Potion", POTION, 4, 0, 0},
  105.   {"Sword", WEAPON, 20, 3, 0},
  106.   {"Leather Armor", ARMOR, 25, 0, 2},
  107.   {"Exit", POTION, 0, 0, 0}
  108. };
  109. const int SHOP_COUNT = sizeof(SHOP)/sizeof(SHOP[0]);
  110. int shopCursor = 0;
  111.  
  112. // Helper functions
  113. inline int maxInt(int a, int b){ return (a > b) ? a : b; }
  114. inline int minInt(int a, int b){ return (a < b) ? a : b; }
  115.  
  116. void setup() {
  117.   lcd.begin(16, 2);
  118.   pinMode(BTN_SELECT, INPUT_PULLUP);
  119.   pinMode(BTN_NEXT, INPUT_PULLUP);
  120.   pinMode(BTN_BACK, INPUT_PULLUP);
  121.   pinMode(BUZZER_PIN, OUTPUT);
  122.   randomSeed(analogRead(A0));
  123.  
  124.   // Initial inventory
  125.   inventorySize = 0;
  126.   // Potion x2
  127.   inventory[inventorySize++] = (Item){"Potion", POTION, 2, 0, 0};
  128.   // Sword x1
  129.   inventory[inventorySize++] = (Item){"Sword", WEAPON, 1, 3, 0};
  130.  
  131.   // initial player values
  132.   player.hp = player.maxHp = 20;
  133.   player.atk = 4;
  134.   player.def = 2;
  135.   player.lvl = 1;
  136.   player.exp = 0;
  137.   player.money = 50;
  138.  
  139.   showTownMenu();
  140. }
  141.  
  142. void loop() {
  143.   switch (gameState) {
  144.     case TOWN_MENU: {
  145.       // simple menu navigation: use NEXT to go Travel, SELECT to travel
  146.       if (digitalRead(BTN_NEXT) == LOW) { delay(200); /* go to travel */ startTravel(); }
  147.       if (digitalRead(BTN_SELECT) == LOW) { delay(200); /* same as travel in this simple flow */ startTravel(); }
  148.       break; }
  149.     case TRAVEL_MENU: {
  150.       // In simplified version, travel triggers a battle immediately via a random enemy
  151.       currentEnemy = ENEMIES[random(0, ENEMY_COUNT)];
  152.       currentEnemy.hp = currentEnemy.maxHp;
  153.       gameState = BATTLE;
  154.       showBattle();
  155.       break; }
  156.     case BATTLE: {
  157.       // simple turn-based: choose Attack or Flee (via SELECT)
  158.       lcd.setCursor(0, 0);
  159.       lcd.print(currentEnemy.name);
  160.       lcd.print(" HP:"); lcd.print(currentEnemy.hp);
  161.       lcd.setCursor(0, 1);
  162.       lcd.print("ATK "); lcd.print(player.atk);
  163.       // Attack action
  164.       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(); }
  165.       if (digitalRead(BTN_SELECT) == LOW) { delay(200); // flee attempt
  166.         int roll = random(0, 100); if (roll < 40) { gameState = TOWN_MENU; showTownMenu(); } else { // failed to flee, enemy attacks
  167.           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(); }
  168.       }
  169.       break; }
  170.     case GAME_OVER: {
  171.       // reset on next press
  172.       if (digitalRead(BTN_SELECT) == LOW) { delay(200); setup(); }
  173.       break; }
  174.     case INVENTORY_MENU: {
  175.       // Not used in simplified flow; handle with minimal
  176.       if (digitalRead(BTN_NEXT) == LOW) { delay(200); gameState = TOWN_MENU; showTownMenu(); }
  177.       break; }
  178.     case SHOP_MENU: {
  179.       if (digitalRead(BTN_NEXT) == LOW) { shopCursor = (shopCursor+1)%SHOP_COUNT; showShopMenu(); delay(200); }
  180.       if (digitalRead(BTN_BACK) == LOW) { shopCursor = (shopCursor-1+SHOP_COUNT)%SHOP_COUNT; showShopMenu(); delay(200); }
  181.       if (digitalRead(BTN_SELECT) == LOW) {
  182.         ShopItem si = SHOP[shopCursor]; // this line is for demonstration; in simplified, we won't implement dynamic purchase
  183.         delay(200);
  184.         if (si.name != "Exit") {
  185.           // purchase by name matching simple behavior
  186.           // we simulate purchase by adding one potion if available and enough money
  187.           if (player.money >= si.price) {
  188.             int idx = addInventoryMove(si.name, si.type, si.atkBonus, si.defBonus); // helper to add
  189.             if (idx >=0) {
  190.               player.money -= si.price;
  191.             }
  192.           }
  193.         } else {
  194.           gameState = TOWN_MENU; showTownMenu();
  195.         }
  196.         showShopMenu();
  197.       }
  198.       break; }
  199.     default:
  200.       break;
  201.   }
  202. }
  203.  
  204. // Helpers for simplified shop/inventory (compact implementations)
  205. int addInventoryMove(const char* name, ItemType type, int atkBonus, int defBonus) {
  206.   // if full, return -1
  207.   if (inventorySize >= 6) return -1;
  208.   inventory[inventorySize] = (Item){name, type, 1, atkBonus, defBonus};
  209.   inventorySize++;
  210.   return inventorySize-1;
  211. }
  212.  
  213. void startTravel() {
  214.   // simply go to TRAVEL_MENU to simulate travel
  215.   gameState = TRAVEL_MENU;
  216.   // we won't display a separate travel menu to save memory; showBattle directly
  217. }
  218.  
  219. void showBattle() {
  220.   lcd.clear();
  221.   lcd.setCursor(0,0);
  222.   lcd.print("Enemy: "); lcd.print(currentEnemy.name);
  223.   lcd.print(" HP"); lcd.print(currentEnemy.hp);
  224.   lcd.setCursor(0,1);
  225.   lcd.print("You: "); lcd.print(player.hp);
  226. }
  227.  
  228. /* END CODE */
  229.  
Advertisement
Add Comment
Please, Sign In to add comment