shivamisone

atomtendo

Apr 20th, 2025
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.43 KB | Gaming | 0 0
  1. #include <FastLED.h>
  2. #include <pitches.h>
  3. #define Score_Address 0
  4. int starPositions[10][2];  // Track up to 10 stars
  5. int numStars = 3;
  6. const int NUM_STAR_COLORS = 2;
  7.  
  8. #define scorePin 15
  9. #define LED_PIN 2
  10. #define NUM_LEDS 200
  11. #define ROWS 10
  12. #define COLS 20
  13. #define sright 12
  14. #define shoots 14
  15. #define sleft 13
  16. #define max_bullets 12  // Maximum number of bullets on screen at once
  17. #define max_enemies 12
  18. bool gameStarted = false;
  19. const int motorPin = 4;
  20.  
  21.  
  22. const int binPin[10] = { 19, 20, 59, 60, 99, 100, 139, 140, 179, 180 };
  23. int ssleft = 0;
  24. int ssright = 2;
  25. const int melody[] = {
  26.   // The Godfather theme
  27.   REST, 4, REST, 8, REST, 8, REST, 8, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8,  //1
  28.   NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, NOTE_G4, 8,
  29.   NOTE_E4, 2, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8,
  30.   NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_E4, 8, NOTE_DS4, 8,
  31.  
  32.   NOTE_D4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8,  //5
  33.   NOTE_B4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8,
  34.   NOTE_A4, 2, NOTE_C4, 8, NOTE_C4, 8, NOTE_G4, 8,
  35.   NOTE_F4, 8, NOTE_E4, 8, NOTE_G4, 8, NOTE_F4, 8, NOTE_F4, 8, NOTE_E4, 8, NOTE_E4, 8, NOTE_GS4, 8,
  36.  
  37.   NOTE_A4, 2, REST, 8, NOTE_A4, 8, NOTE_A4, 8, NOTE_GS4, 8,  //9
  38.   NOTE_G4, 2, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8,
  39.   NOTE_E4, 2, NOTE_E4, 8, NOTE_G4, 8, NOTE_E4, 8,
  40.   NOTE_D4, 2, NOTE_D4, 8, NOTE_D4, 8, NOTE_F4, 8, NOTE_DS4, 8,
  41.  
  42.   NOTE_E4, 2, REST, 8, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8,  //13
  43.  
  44.   //repeats from 2
  45.   NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8, NOTE_G4, 8,  //2
  46.   NOTE_E4, 2, NOTE_E4, 8, NOTE_A4, 8, NOTE_C5, 8,
  47.   NOTE_B4, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_C5, 8, NOTE_A4, 8, NOTE_E4, 8, NOTE_DS4, 8,
  48.  
  49.   NOTE_D4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8,  //5
  50.   NOTE_B4, 2, NOTE_D4, 8, NOTE_F4, 8, NOTE_GS4, 8,
  51.   NOTE_A4, 2, NOTE_C4, 8, NOTE_C4, 8, NOTE_G4, 8,
  52.   NOTE_F4, 8, NOTE_E4, 8, NOTE_G4, 8, NOTE_F4, 8, NOTE_F4, 8, NOTE_E4, 8, NOTE_E4, 8, NOTE_GS4, 8,
  53.  
  54.   NOTE_A4, 2, REST, 8, NOTE_A4, 8, NOTE_A4, 8, NOTE_GS4, 8,  //9
  55.   NOTE_G4, 2, NOTE_B4, 8, NOTE_A4, 8, NOTE_F4, 8,
  56.   NOTE_E4, 2, NOTE_E4, 8, NOTE_G4, 8, NOTE_E4, 8,
  57.   NOTE_D4, 2, NOTE_D4, 8, NOTE_D4, 8, NOTE_F4, 8, NOTE_DS4, 8,
  58.  
  59.   NOTE_E4, 2  //13
  60. };
  61. const int buzzerPin = 26;
  62. const int screen[ROWS][COLS] = {
  63.   { 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },
  64.   { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 },
  65.   { 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40 },
  66.   { 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79 },
  67.   { 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80 },
  68.   { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119 },
  69.   { 139, 138, 137, 136, 135, 134, 133, 132, 131, 130, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120 },
  70.   { 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159 },
  71.   { 179, 178, 177, 176, 175, 174, 173, 172, 171, 170, 169, 168, 167, 166, 165, 164, 163, 162, 161, 160 },
  72.   { 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199 }
  73. };
  74. CRGB colors[ROWS][COLS];
  75. CRGB leds[NUM_LEDS];
  76.  
  77. // Structure to track bullets
  78. struct Bullet {
  79.   int row;
  80.   int col;
  81.   bool active;
  82.   unsigned long lastMoveTime;
  83. };
  84. struct Enemy {
  85.   int type;
  86.   int health;
  87.   int row;
  88.   int col;
  89.   bool active;
  90.   unsigned long lastMoveTime;
  91. };
  92. // array to store enemies
  93. Enemy enemies[max_enemies];
  94. // Array to store bullets
  95. Bullet bullets[max_bullets];
  96. //Timing variables Enemy
  97. int difficultyFactor = 3;
  98. int difficultyFactor1 = 18;
  99. unsigned long EnemyMoveInterval = 600;  // Time between enemy movements in ms
  100. unsigned long lastEnemySpawnTime = 0;
  101. unsigned long enemyCooldown = 2500;  // Minimum time between spawning enemies
  102.  
  103. // Timing variables Bullet
  104. const unsigned long bulletMoveInterval = 100;  // Time between bullet movements in ms
  105. unsigned long lastBulletFiredTime = 0;         // Time when last bullet was fired
  106. const unsigned long bulletCooldown = 10;       // Minimum time between firing bullets
  107. int score = 0;
  108. int maxhealth = 3;
  109. void setLEDColors(CRGB colors[ROWS][COLS]) {
  110.   for (int i = 0; i < ROWS; i++) {
  111.     for (int j = 0; j < COLS; j++) {
  112.       int led = pgm_read_word(&(screen[i][j]));
  113.       leds[led] = colors[i][j];
  114.     }
  115.   }
  116. }
  117.  
  118. // Button debouncing variables
  119. unsigned long lastDebounceTimeLeft = 0;
  120. unsigned long lastDebounceTimeRight = 0;
  121. unsigned long lastDebounceTimeShoot = 0;
  122. unsigned long debounceDelay = 50;
  123. bool leftButtonActive = false;
  124. bool rightButtonActive = false;
  125.  
  126. int lastLeftState = HIGH;
  127. int lastRightState = HIGH;
  128. int lastShootState = HIGH;
  129. bool shootButtonPressed = false;  // Track if shoot button is being held down
  130.  
  131. void setup() {
  132.   pinMode(scorePin, INPUT_PULLUP);
  133. initStarfield();
  134.  
  135.   pinMode(motorPin, OUTPUT);
  136.   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  137.   FastLED.clear();
  138.   setupMusic();  // Initialize the music player
  139.   startMusic();  // Start playing the music
  140.   FastLED.show();
  141.   pinMode(shoots, INPUT_PULLUP);
  142.   pinMode(sleft, INPUT_PULLUP);
  143.   pinMode(sright, INPUT_PULLUP);
  144.   Serial.begin(9600);
  145.   int x = random(1, 2343);
  146.   int total = 0;
  147.   for (int i = 15; i <= 23; i++)  // or up to A7 for many Arduino boards.
  148.     total += (analogRead(i) + x);
  149.   randomSeed(total);
  150.  
  151.   // Initialize all bullets as inactive
  152.   for (int i = 0; i < max_bullets; i++) {
  153.     bullets[i].active = false;
  154.     enemies[i].active = false;
  155.   }
  156.   while (!gameStarted) {
  157.     mainscreen();
  158.     checkStartButton();
  159.   }
  160. }
  161.  
  162. void displayBinary(int num) {
  163.   for (int i = 0; i < 10; i++) {
  164.     if ((num >> i) & 1) {
  165.       colors[i][19] = CRGB(255, 255, 255);
  166.     }
  167.   }
  168. }
  169.  
  170. void mainscreen() {  //chamakta hua A.T.O.M
  171.   static unsigned long lastChange = 0;
  172.   if (millis() - lastChange > 700) {
  173.     for (int i = 0; i < 10; i++) {
  174.       int c1 = random(255);
  175.       int c2 = random(255);
  176.       int c3 = random(255);
  177.       colors[2][0] = CRGB(c1, c2, c3);
  178.       colors[3][0] = CRGB(c1, c2, c3);
  179.       colors[4][0] = CRGB(c1, c2, c3);
  180.       colors[5][0] = CRGB(c1, c2, c3);
  181.       colors[6][0] = CRGB(c1, c2, c3);
  182.  
  183.       colors[2][2] = CRGB(c1, c2, c3);
  184.       colors[3][2] = CRGB(c1, c2, c3);
  185.       colors[4][2] = CRGB(c1, c2, c3);
  186.       colors[5][2] = CRGB(c1, c2, c3);
  187.       colors[6][2] = CRGB(c1, c2, c3);
  188.  
  189.       colors[2][6] = CRGB(c1, c2, c3);
  190.       colors[3][6] = CRGB(c1, c2, c3);
  191.       colors[4][6] = CRGB(c1, c2, c3);
  192.       colors[5][6] = CRGB(c1, c2, c3);
  193.       colors[6][6] = CRGB(c1, c2, c3);
  194.  
  195.       colors[2][10] = CRGB(c1, c2, c3);
  196.       colors[3][10] = CRGB(c1, c2, c3);
  197.       colors[4][10] = CRGB(c1, c2, c3);
  198.       colors[5][10] = CRGB(c1, c2, c3);
  199.       colors[6][10] = CRGB(c1, c2, c3);
  200.  
  201.       colors[2][13] = CRGB(c1, c2, c3);
  202.       colors[3][13] = CRGB(c1, c2, c3);
  203.       colors[4][13] = CRGB(c1, c2, c3);
  204.       colors[5][13] = CRGB(c1, c2, c3);
  205.       colors[6][13] = CRGB(c1, c2, c3);
  206.  
  207.       colors[2][15] = CRGB(c1, c2, c3);
  208.       colors[3][15] = CRGB(c1, c2, c3);
  209.       colors[4][15] = CRGB(c1, c2, c3);
  210.       colors[5][15] = CRGB(c1, c2, c3);
  211.       colors[6][15] = CRGB(c1, c2, c3);
  212.  
  213.       colors[2][19] = CRGB(c1, c2, c3);
  214.       colors[3][19] = CRGB(c1, c2, c3);
  215.       colors[4][19] = CRGB(c1, c2, c3);
  216.       colors[5][19] = CRGB(c1, c2, c3);
  217.       colors[6][19] = CRGB(c1, c2, c3);
  218.  
  219.       colors[2][1] = CRGB(c1, c2, c3);
  220.       colors[4][1] = CRGB(c1, c2, c3);
  221.       colors[2][4] = CRGB(c1, c2, c3);
  222.       colors[2][5] = CRGB(c1, c2, c3);
  223.       colors[2][7] = CRGB(c1, c2, c3);
  224.       colors[2][8] = CRGB(c1, c2, c3);
  225.       colors[2][11] = CRGB(c1, c2, c3);
  226.       colors[2][12] = CRGB(c1, c2, c3);
  227.       colors[6][11] = CRGB(c1, c2, c3);
  228.       colors[6][12] = CRGB(c1, c2, c3);
  229.       colors[4][16] = CRGB(c1, c2, c3);
  230.       colors[4][17] = CRGB(c1, c2, c3);
  231.       colors[4][18] = CRGB(c1, c2, c3);
  232.       colors[3][16] = CRGB(c1, c2, c3);
  233.       colors[3][18] = CRGB(c1, c2, c3);
  234.       colors[5][17] = CRGB(c1, c2, c3);
  235.       setLEDColors(colors);
  236.       FastLED.show();
  237.       lastChange = millis();
  238.       updateMusic();
  239.     }
  240.     FastLED.show();
  241.   }
  242. }
  243. //MOOOSIC
  244. int tempo = 80;
  245. int currentNote = 0;
  246. int notesCount;
  247. unsigned long previousNoteTime = 0;
  248. int noteDuration = 0;
  249. int pauseBetweenNotes = 0;
  250. bool isPlaying = false;
  251. bool isTonePlaying = false;
  252.  
  253. // Calculate the number of notes in the melody
  254. const int noteCount = sizeof(melody) / sizeof(melody[0]) / 2;
  255.  
  256. // Configure whole note duration in milliseconds
  257. const int wholenote = (60000 * 4) / tempo;
  258.  
  259. // Initialize the music player
  260. void setupMusic() {
  261.   notesCount = noteCount;
  262.   isPlaying = true;
  263. }
  264.  
  265. // Start or restart the music
  266. void startMusic() {
  267.   currentNote = 0;
  268.   isPlaying = true;
  269.   isTonePlaying = false;
  270.   previousNoteTime = millis();
  271. }
  272.  
  273. // Pause the music
  274. void pauseMusic() {
  275.   isPlaying = false;
  276.   noTone(buzzerPin);
  277. }
  278.  
  279. // Resume the music from where it left off
  280. void resumeMusic() {
  281.   isPlaying = true;
  282. }
  283.  
  284. // Stop the music and reset to beginning
  285. void stopMusic() {
  286.   isPlaying = false;
  287.   currentNote = 0;
  288.   noTone(buzzerPin);
  289. }
  290.  
  291. // Update music player - call this in your loop() function
  292. void updateMusic() {
  293.   if (!isPlaying) {
  294.     return;
  295.   }
  296.  
  297.   unsigned long currentTime = millis();
  298.  
  299.   // If we're playing a tone and it's time to stop it
  300.   if (isTonePlaying && currentTime - previousNoteTime >= noteDuration) {
  301.     noTone(buzzerPin);
  302.     isTonePlaying = false;
  303.     pauseBetweenNotes = noteDuration * 0.1;  // 10% pause between notes
  304.     previousNoteTime = currentTime;
  305.     return;
  306.   }
  307.  
  308.   // If we're in the pause between notes
  309.   if (!isTonePlaying && currentTime - previousNoteTime >= pauseBetweenNotes) {
  310.     // Move to the next note
  311.     if (currentNote >= notesCount * 2) {
  312.       // Reached the end of the melody, restart
  313.       currentNote = 0;
  314.     }
  315.  
  316.     // Calculate note duration
  317.     int divider = melody[currentNote + 1];
  318.     if (divider > 0) {
  319.       // Regular note
  320.       noteDuration = wholenote / divider;
  321.     } else if (divider < 0) {
  322.       // Dotted note
  323.       noteDuration = (wholenote / abs(divider)) * 1.5;
  324.     }
  325.  
  326.     // Play the note (only if it's not a REST)
  327.     if (melody[currentNote] != REST) {
  328.       tone(buzzerPin, melody[currentNote], noteDuration * 0.9);
  329.       isTonePlaying = true;
  330.     } else {
  331.       // For REST notes, just wait the duration
  332.       isTonePlaying = true;
  333.     }
  334.  
  335.     previousNoteTime = currentTime;
  336.     currentNote += 2;  // Move to the next note-duration pair
  337.   }
  338. }
  339.  
  340. void drawspaceship() {
  341.   CRGB colo;
  342.   switch (maxhealth) {
  343.     case 2:
  344.       colo = CRGB(255, 248, 23);
  345.       break;
  346.     case 3:
  347.       colo = CRGB(36,82,40);
  348.       break;
  349.     case 1:
  350.       colo = CRGB(255, 0, 0);
  351.       break;
  352.     default:
  353.       shipPhatna();
  354.       delay(3000);
  355.       for (int row = 0; row < ROWS; row++) {
  356.         for (int col = 0; col < COLS; col++) {
  357.           colors[row][col] = CRGB::Black;
  358.         }
  359.       }
  360.       setLEDColors(colors);
  361.       FastLED.show();
  362.       gameover();
  363.       break;
  364.   }
  365.   for (int i = ssleft; i <= ssright; i++) {
  366.     colors[i][0] = colo;
  367.   }
  368.   colors[ssleft + 1][1] = colo;
  369. }
  370. void shipPhatna() {
  371.   for (int i = ssleft; i <= ssright; i++) {
  372.     colors[i][0] = CRGB(255, 0, 0);
  373.   }
  374.   colors[ssleft + 1][1] = CRGB(255, 0, 0);
  375.   colors[ssleft - 1][1] = CRGB(255, 64, 0);
  376.   colors[ssleft - 2][2] = CRGB(255, 128, 0);
  377.   colors[ssleft][1] = CRGB(255, 64, 0);
  378.   colors[ssleft][2] = CRGB(255, 128, 0);
  379.   colors[ssleft - 1][3] = CRGB(255, 64, 0);
  380.   colors[ssleft - 2][4] = CRGB(255, 128, 0);
  381.   colors[ssleft + 1][2] = CRGB(255, 64, 0);
  382.   colors[ssleft + 1][3] = CRGB(255, 64, 0);
  383.   colors[ssleft + 2][2] = CRGB(255, 128, 0);
  384.   colors[ssleft + 2][1] = CRGB(255, 64, 0);
  385.   colors[ssleft + 3][1] = CRGB(255, 128, 0);
  386.   colors[ssleft + 3][2] = CRGB(255, 128, 0);
  387.   setLEDColors(colors);
  388.   FastLED.show();
  389. }
  390. void spawnEnemies() {
  391.   unsigned long currentTime = millis();
  392.   if (currentTime - lastEnemySpawnTime < enemyCooldown) {
  393.     return;
  394.   }
  395.  
  396.   int spawnCount = (random(1, 100) < 35) ? 2 : 1;
  397.   for (int i = 0; i < max_enemies && spawnCount > 0; i++) {
  398.     if (!enemies[i].active) {
  399.       int rs = random(1, 100);
  400.       if (rs > 4 && rs < 32) {
  401.         enemies[i].type = 2;
  402.         enemies[i].health = 2;
  403.       } else if (rs <= 4) {
  404.         enemies[i].type = 3;
  405.         enemies[i].health = 10;
  406.         spawnCount = 1;
  407.       } else {
  408.         enemies[i].type = 1;
  409.         enemies[i].health = 1;
  410.       }
  411.       if (enemies[i].type == 3) {
  412.         enemies[i].row = 3;
  413.         enemies[i].col = 18;
  414.       } else {
  415.         enemies[i].row = random(1, 8);
  416.         enemies[i].col = 18;
  417.       }
  418.       enemies[i].active = true;
  419.       enemies[i].lastMoveTime = currentTime;
  420.       lastEnemySpawnTime = currentTime;
  421.       spawnCount--;
  422.     }
  423.   }
  424. }
  425.  
  426.  
  427. void fireBullet() {
  428.   unsigned long currentTime = millis();
  429.  
  430.   // Add cooldown to prevent firing too rapidly
  431.   if (currentTime - lastBulletFiredTime < bulletCooldown) {
  432.     return;
  433.   }
  434.  
  435.   // Find first inactive bullet
  436.   for (int i = 0; i < max_bullets; i++) {
  437.     if (!bullets[i].active) {
  438.       bullets[i].row = ssleft + 1;  // Middle of ship
  439.       bullets[i].col = 2;           // Just in front of ship
  440.       bullets[i].active = true;
  441.       bullets[i].lastMoveTime = currentTime;
  442.       lastBulletFiredTime = currentTime;
  443.  
  444.       return;
  445.     }
  446.   }
  447. }
  448. void updateEnemies() {
  449.   unsigned long currentTime = millis();
  450.   int moveBoss = EnemyMoveInterval;
  451.   for (int i = 0; i < max_enemies; i++) {
  452.     if (enemies[i].active) {
  453.       if (enemies[i].type == 1)
  454.         colors[enemies[i].row][enemies[i].col] = CRGB(0, 255, 0);
  455.       else if (enemies[i].type == 2) colors[enemies[i].row][enemies[i].col] = CRGB(255, 45, 241);
  456.       else if (enemies[i].type == 3) {
  457.         colors[enemies[i].row][enemies[i].col] = CRGB(150, 150, random(0, 255));
  458.         colors[enemies[i].row + 1][enemies[i].col] = CRGB(150, 150, random(0, 255));
  459.         colors[enemies[i].row + 2][enemies[i].col] = CRGB(150, 150, random(0, 255));
  460.         colors[enemies[i].row + 1][enemies[i].col - 1] = CRGB(150, 150, random(0, 255));
  461.         colors[enemies[i].row][enemies[i].col - 2] = CRGB(150, 150, random(0, 255));
  462.         colors[enemies[i].row + 1][enemies[i].col - 2] = CRGB(150, 150, random(0, 255));
  463.         colors[enemies[i].row + 2][enemies[i].col - 2] = CRGB(150, 150, random(0, 255));
  464.       }
  465.     }
  466.  
  467.     if (enemies[i].type == 3) {
  468.       EnemyMoveInterval = 2000;
  469.     }
  470.     if (currentTime - enemies[i].lastMoveTime > EnemyMoveInterval) {
  471.       enemies[i].col--;
  472.       if (enemies[i].type != 3) {
  473.         if (random(1, 100) < 25 && enemies[i].row > 2) {
  474.           int new_row = enemies[i].row + random(-1, 2);
  475.           if (new_row != 0 && new_row != ROWS - 1) {
  476.             enemies[i].row = new_row;
  477.           }
  478.         }
  479.         if (random(1, 100) < 20 && enemies[i].row > 2) {
  480.           int new_row = enemies[i].row + random(-1, 2);
  481.           if (new_row != 0 && new_row != ROWS - 1) {
  482.             enemies[i].row = new_row;
  483.           }
  484.         }
  485.       }
  486.  
  487.       enemies[i].lastMoveTime = currentTime;
  488.  
  489.       int er = enemies[i].row;
  490.       int ec = enemies[i].col;
  491.  
  492.       bool shipCollision = false;
  493.       if (enemies[i].type == 3) {
  494.         if ((ssleft == er && 0 == enemies[i].col - 2) || (ssleft == er + 1 && 0 == enemies[i].col - 2) || (ssleft == er + 2 && 0 == enemies[i].col - 2))
  495.           shipCollision = true;
  496.       } else {
  497.         if (ec == 0) {
  498.           if (er == ssleft && enemies[i].active == true) {
  499.             shipCollision = true;
  500.           } else if (er == ssright && enemies[i].active == true) {
  501.             shipCollision = true;
  502.           }
  503.         } else if (ec == 1 && er == ssleft + 1 && enemies[i].active == true) {
  504.           shipCollision = true;
  505.         }
  506.       }
  507.  
  508.       if (shipCollision) {
  509.         maxhealth--;
  510.         enemies[i].active = false;
  511.       }
  512.  
  513.       for (int j = 0; j < max_bullets; j++) {
  514.         if (enemies[i].type == 3) {
  515.           int br = bullets[j].row;
  516.           int bc = bullets[j].col;
  517.           if (bullets[j].active) {
  518.             if ((br == er && bc == enemies[i].col - 2) || (br == er + 1 && bc == enemies[i].col - 2) || (br == er + 2 && bc == enemies[i].col - 2)) {
  519.               enemies[i].health--;
  520.               bullets[j].active = false;
  521.               if (enemies[i].health == 0) {
  522.                 enemies[i].active = false;
  523.                 score++;
  524.               }
  525.               break;
  526.             }
  527.           }
  528.         }
  529.  
  530.         else if (bullets[j].active && checkCollision(j, i)) {
  531.           enemies[i].type--;
  532.           enemies[i].health--;
  533.           bullets[j].active = false;
  534.           if (enemies[i].health == 0) {
  535.             enemies[i].active = false;
  536.             score++;
  537.           }
  538.           break;
  539.         }
  540.       }
  541.     }
  542.     if (enemies[i].type == 3 && enemies[i].col < 0 && enemies[i].active == true) {
  543.       gameover();
  544.     }
  545.     if (enemies[i].col < 0 && enemies[i].active == true) {
  546.       enemies[i].active = false;
  547.       if (score > 0) score--;
  548.     }
  549.   }
  550.   EnemyMoveInterval = moveBoss;
  551. }
  552.  
  553. void updateBullets() {
  554.   unsigned long currentTime = millis();
  555.  
  556.   for (int i = 0; i < max_bullets; i++) {
  557.     if (bullets[i].active) {
  558.       // Draw the bullet
  559.       colors[bullets[i].row][bullets[i].col] = CRGB(255, 0, 0);
  560.  
  561.       // Check if it's time to move the bullet
  562.       if (currentTime - bullets[i].lastMoveTime > bulletMoveInterval) {
  563.         // Move bullet forward
  564.         bullets[i].col++;
  565.         bullets[i].lastMoveTime = currentTime;
  566.  
  567.         for (int j = 0; j < max_enemies; j++) {
  568.  
  569.           if (enemies[j].active && enemies[j].type == 3) {
  570.             // Check all 7 parts of type 3 enemy
  571.             if ((bullets[i].row == enemies[j].row && bullets[i].col == enemies[j].col) || (bullets[i].row == enemies[j].row + 1 && bullets[i].col == enemies[j].col) || (bullets[i].row == enemies[j].row + 2 && bullets[i].col == enemies[j].col) || (bullets[i].row == enemies[j].row + 1 && bullets[i].col == enemies[j].col - 1) || (bullets[i].row == enemies[j].row && bullets[i].col == enemies[j].col - 2) || (bullets[i].row == enemies[j].row + 1 && bullets[i].col == enemies[j].col - 2) || (bullets[i].row == enemies[j].row + 2 && bullets[i].col == enemies[j].col - 2)) {
  572.               enemies[j].health--;
  573.               bullets[i].active = false;
  574.               if (enemies[j].health <= 0) {
  575.                 enemies[j].active = false;
  576.                 score++;
  577.               }
  578.               break;
  579.             }
  580.           } else if (enemies[j].active && checkCollision(i, j)) {
  581.             if (enemies[j].type == 2) enemies[j].type--;
  582.             enemies[j].health--;
  583.             bullets[i].active = false;
  584.             if (enemies[j].health == 0) {
  585.               enemies[j].active = false;
  586.               score++;
  587.             }
  588.             break;
  589.           }
  590.         }
  591.         // Check if bullet reached the end of the screen
  592.         if (bullets[i].col >= COLS) {
  593.           bullets[i].active = false;
  594.         }
  595.       }
  596.     }
  597.   }
  598. }
  599.  
  600. void checkStartButton() {
  601.   int buttonState = digitalRead(shoots);
  602.   if (buttonState == LOW) {
  603.     delay(50);  // Simple debounce
  604.     if (digitalRead(shoots) == LOW) {
  605.       gameStarted = true;
  606.       FastLED.clear();
  607.       FastLED.show();
  608.     }
  609.   }
  610. }
  611.  
  612. bool checkCollision(int i, int j) {
  613.   if (!bullets[i].active || !enemies[j].active) {
  614.     return false;
  615.   }
  616.   int er = enemies[j].row;
  617.   int ec = enemies[j].col;
  618.   int br = bullets[i].row;
  619.   int bc = bullets[i].col;
  620.  
  621.   return (er == br && ec == bc);
  622. }
  623.  
  624.  
  625. void gameover() {
  626.   digitalWrite(motorPin, HIGH);  // Turn motor ON
  627.  
  628.  
  629.   colors[2][6] = CRGB(255, 0, 0);
  630.   colors[3][6] = CRGB(255, 0, 0);
  631.   colors[2][7] = CRGB(255, 0, 0);
  632.   colors[3][7] = CRGB(255, 0, 0);
  633.   colors[2][12] = CRGB(255, 0, 0);
  634.   colors[3][12] = CRGB(255, 0, 0);
  635.   colors[3][13] = CRGB(255, 0, 0);
  636.   colors[2][13] = CRGB(255, 0, 0);
  637.  
  638.   //white face
  639.   colors[0][4] = CRGB(255, 255, 255);
  640.   colors[1][4] = CRGB(255, 255, 255);
  641.   colors[2][4] = CRGB(255, 255, 255);
  642.   colors[3][4] = CRGB(255, 255, 255);
  643.   colors[4][4] = CRGB(255, 255, 255);
  644.   colors[5][4] = CRGB(255, 255, 255);
  645.   colors[6][4] = CRGB(255, 255, 255);
  646.   colors[0][5] = CRGB(255, 255, 255);
  647.   colors[0][6] = CRGB(255, 255, 255);
  648.   colors[0][7] = CRGB(255, 255, 255);
  649.   colors[0][8] = CRGB(255, 255, 255);
  650.   colors[0][9] = CRGB(255, 255, 255);
  651.   colors[0][10] = CRGB(255, 255, 255);
  652.   colors[0][11] = CRGB(255, 255, 255);
  653.   colors[0][12] = CRGB(255, 255, 255);
  654.   colors[0][13] = CRGB(255, 255, 255);
  655.   colors[0][14] = CRGB(255, 255, 255);
  656.   colors[0][15] = CRGB(255, 255, 255);
  657.   colors[1][5] = CRGB(255, 255, 255);
  658.   colors[1][6] = CRGB(255, 255, 255);
  659.   colors[1][7] = CRGB(255, 255, 255);
  660.   colors[1][8] = CRGB(255, 255, 255);
  661.   colors[1][9] = CRGB(255, 255, 255);
  662.   colors[1][10] = CRGB(255, 255, 255);
  663.   colors[1][11] = CRGB(255, 255, 255);
  664.   colors[1][12] = CRGB(255, 255, 255);
  665.   colors[1][13] = CRGB(255, 255, 255);
  666.   colors[1][14] = CRGB(255, 255, 255);
  667.   colors[1][15] = CRGB(255, 255, 255);
  668.   colors[2][5] = CRGB(255, 255, 255);
  669.   colors[3][5] = CRGB(255, 255, 255);
  670.   colors[2][8] = CRGB(255, 255, 255);
  671.   colors[2][9] = CRGB(255, 255, 255);
  672.   colors[2][10] = CRGB(255, 255, 255);
  673.   colors[2][11] = CRGB(255, 255, 255);
  674.   colors[3][9] = CRGB(255, 255, 255);
  675.   colors[3][10] = CRGB(255, 255, 255);
  676.   colors[3][11] = CRGB(255, 255, 255);
  677.   colors[3][8] = CRGB(255, 255, 255);
  678.   colors[3][14] = CRGB(255, 255, 255);
  679.   colors[3][15] = CRGB(255, 255, 255);
  680.   colors[2][14] = CRGB(255, 255, 255);
  681.   colors[2][15] = CRGB(255, 255, 255);
  682.   colors[4][5] = CRGB(255, 255, 255);
  683.   colors[4][6] = CRGB(255, 255, 255);
  684.   colors[4][7] = CRGB(255, 255, 255);
  685.   colors[4][8] = CRGB(255, 255, 255);
  686.   colors[4][11] = CRGB(255, 255, 255);
  687.   colors[4][12] = CRGB(255, 255, 255);
  688.   colors[4][13] = CRGB(255, 255, 255);
  689.   colors[4][14] = CRGB(255, 255, 255);
  690.   colors[4][15] = CRGB(255, 255, 255);
  691.   colors[5][5] = CRGB(255, 255, 255);
  692.   colors[5][6] = CRGB(255, 255, 255);
  693.   colors[5][6] = CRGB(255, 255, 255);
  694.   colors[5][8] = CRGB(255, 255, 255);
  695.   colors[5][9] = CRGB(255, 255, 255);
  696.   colors[5][10] = CRGB(255, 255, 255);
  697.   colors[5][11] = CRGB(255, 255, 255);
  698.   colors[5][12] = CRGB(255, 255, 255);
  699.   colors[5][13] = CRGB(255, 255, 255);
  700.   colors[5][14] = CRGB(255, 255, 255);
  701.   colors[5][15] = CRGB(255, 255, 255);
  702.   colors[6][5] = CRGB(255, 255, 255);
  703.   colors[6][6] = CRGB(255, 255, 255);
  704.   colors[6][7] = CRGB(255, 255, 255);
  705.   colors[6][12] = CRGB(255, 255, 255);
  706.   colors[6][13] = CRGB(255, 255, 255);
  707.   colors[6][14] = CRGB(255, 255, 255);
  708.   colors[6][15] = CRGB(255, 255, 255);
  709.   colors[7][7] = CRGB(255, 255, 255);
  710.   colors[7][12] = CRGB(255, 255, 255);
  711.   colors[8][10] = CRGB(255, 255, 255);
  712.   colors[8][11] = CRGB(255, 255, 255);
  713.   colors[8][7] = CRGB(255, 255, 255);
  714.   colors[8][8] = CRGB(255, 255, 255);
  715.   colors[8][9] = CRGB(255, 255, 255);
  716.   colors[8][12] = CRGB(255, 255, 255);
  717.   colors[9][7] = CRGB(255, 255, 255);
  718.   colors[9][8] = CRGB(255, 255, 255);
  719.   colors[9][9] = CRGB(255, 255, 255);
  720.   colors[9][10] = CRGB(255, 255, 255);
  721.   colors[9][11] = CRGB(255, 255, 255);
  722.   colors[9][12] = CRGB(255, 255, 255);
  723.   colors[5][7] = CRGB(255, 255, 255);
  724.  
  725.  
  726.   //nose
  727.   colors[4][9] = CRGB(4, 69, 217);
  728.   colors[4][10] = CRGB(4, 69, 217);
  729.  
  730.   //teeth
  731.   colors[6][8] = CRGB(25, 118, 3);
  732.   colors[6][9] = CRGB(217, 181, 5);
  733.   colors[6][10] = CRGB(25, 118, 3);
  734.   colors[6][11] = CRGB(25, 118, 3);
  735.   colors[7][8] = CRGB(25, 118, 3);
  736.   colors[7][9] = CRGB(25, 118, 3);
  737.   colors[7][10] = CRGB(25, 118, 3);
  738.   colors[7][11] = CRGB(25, 118, 3);
  739.   setLEDColors(colors);
  740.   FastLED.show();
  741.   delay(9500);
  742.   esp_restart();
  743. }
  744. CRGB ar[] = {
  745.   CRGB(255, 255, 255),  
  746.   CRGB(220, 160, 109),  
  747.   CRGB(215, 108, 130)  
  748. };
  749.  
  750. // Corrected color assignments
  751. CRGB backgroundDarkBlue = CRGB(0, 0, 4);  // Deep dark blue
  752. CRGB starColors[] = {ar[1], ar[2]};  // Use only the two defined colors after white
  753. void updateStarfieldNonBlocking() {
  754.   static unsigned long lastStarUpdateTime = 0;
  755.   unsigned long currentTime = millis();
  756.  
  757.   if (currentTime - lastStarUpdateTime >= 100) {
  758.     // Move stars
  759.     for (int i = 0; i < numStars; i++) {
  760.       starPositions[i][0]++;
  761.      
  762.       // Wrap around if star goes off the matrix
  763.       if (starPositions[i][0] >= ROWS) {
  764.         starPositions[i][0] = 0;
  765.         starPositions[i][1] = random(COLS);
  766.       }
  767.     }
  768.  
  769.     // Redraw starfield
  770.     for (int row = 0; row < ROWS; row++) {
  771.       for (int col = 0; col < COLS; col++) {
  772.         colors[row][col] = backgroundDarkBlue;
  773.       }
  774.     }
  775.  
  776.     // Draw stars
  777.     for (int i = 0; i < numStars; i++) {
  778.       CRGB starColor = starColors[i % NUM_STAR_COLORS];
  779.       colors[starPositions[i][0]][starPositions[i][1]] = starColor;
  780.     }
  781.  
  782.     lastStarUpdateTime = currentTime;
  783.   }
  784. }
  785.  
  786.  
  787. void initStarfield() {
  788.   // Initialize random star positions
  789.   for (int i = 0; i < numStars; i++) {
  790.     starPositions[i][0] = random(ROWS);
  791.     starPositions[i][1] = random(COLS);
  792.   }
  793. }
  794.  
  795. int reduceTime = 10;
  796. void loop() {
  797.  
  798.  
  799.   // Clear the display
  800.   Serial.println(score);
  801.  
  802.   updateStarfieldNonBlocking();
  803.  
  804.   // Read the current button states
  805.   int currentstate_left = digitalRead(sleft);
  806.   int currentstate_right = digitalRead(sright);
  807.   int currentstate_shoot = digitalRead(shoots);
  808.   int currentstate_score = digitalRead(score);
  809.  
  810.   // Left button with debouncing
  811.   if (currentstate_left != lastLeftState) {
  812.     lastDebounceTimeLeft = millis();
  813.   }
  814.  
  815.   if ((millis() - lastDebounceTimeLeft) > debounceDelay) {
  816.     if (currentstate_left == LOW && !leftButtonActive && ssleft > 0) {
  817.       // Button is newly pressed
  818.       ssright--;
  819.       ssleft--;
  820.       leftButtonActive = true;
  821.     } else if (currentstate_left == HIGH) {
  822.       // Button is released
  823.       leftButtonActive = false;
  824.     }
  825.   }
  826.   lastLeftState = currentstate_left;
  827.  
  828.   // Right button with debouncing
  829.   if (currentstate_right != lastRightState) {
  830.     lastDebounceTimeRight = millis();
  831.   }
  832.  
  833.   if ((millis() - lastDebounceTimeRight) > debounceDelay) {
  834.     if (currentstate_right == LOW && !rightButtonActive && ssright < 9) {
  835.       // Button is newly pressed
  836.       ssright++;
  837.       ssleft++;
  838.       rightButtonActive = true;
  839.     } else if (currentstate_right == HIGH) {
  840.       // Button is released
  841.       rightButtonActive = false;
  842.     }
  843.   }
  844.   lastRightState = currentstate_right;
  845.  
  846.   // Shoot button with improved handling
  847.   if (currentstate_shoot != lastShootState) {
  848.     lastDebounceTimeShoot = millis();
  849.   }
  850.  
  851.   if ((millis() - lastDebounceTimeShoot) > debounceDelay) {
  852.     // Only fire when button is first pressed, not while held
  853.     if (currentstate_shoot == LOW && !shootButtonPressed) {
  854.       fireBullet();
  855.       shootButtonPressed = true;
  856.  
  857.     } else if (currentstate_shoot == HIGH) {
  858.       shootButtonPressed = false;
  859.     }
  860.   }
  861.   lastShootState = currentstate_shoot;
  862.  
  863.   if (score % reduceTime == 0) {
  864.     EnemyMoveInterval -= difficultyFactor * score;
  865.     enemyCooldown -= difficultyFactor1 * score;
  866.     reduceTime += 10;
  867.   }
  868.  
  869.   // Update and draw active bullets
  870.   drawspaceship();
  871.  
  872.   updateEnemies();
  873.  
  874.   // Draw spaceship
  875.   spawnEnemies();
  876.   updateBullets();
  877.   displayBinary(score);
  878.  
  879.   // Update LED matrix
  880.   setLEDColors(colors);
  881.   FastLED.show();
  882.   updateMusic();
  883.   delay(20);  // Faster refresh rate for smoother movement
  884. }
Advertisement
Add Comment
Please, Sign In to add comment