ferrybig

Arduino Stacker Game

Jan 27th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. // https://www.reddit.com/user/ferrybig
  2. /*  OLED SSD1306 <-> Arduino (nano/uno)
  3.      D0--------------10
  4.      D1--------------9
  5.      RST-------------13
  6.      DC--------------11
  7.      VCC-------------5V
  8.      GND-------------GND
  9.      CS--------------12
  10.     Others:
  11.      Button active low: 2
  12.      > Used for placing the blocks
  13.      Led active high: A5 (Optional)
  14.      > Used for death indication
  15.      Buzzer: 3 (Optional)
  16.      > Feedback for button press
  17.  */
  18. #include <SPI.h>
  19. #include <Wire.h>
  20. #include <Adafruit_GFX.h>
  21. #include <Adafruit_SSD1306.h>
  22.  
  23. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  24. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  25.  
  26. // Declaration for SSD1306 display connected using software SPI (default case):
  27. #define OLED_MOSI   9
  28. #define OLED_CLK   10
  29. #define OLED_DC    11
  30. #define OLED_CS    12
  31. #define OLED_RESET 13
  32. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  33.   OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
  34.  
  35. const int pin_button = 2;
  36. const int pin_led = A5;
  37. const int pin_buzzer = 3;
  38.  
  39. const int initial_delay_timer = 32;
  40. const int initial_x = 32;
  41. const int initial_width = 8;
  42. const int height = 4;
  43. const int initial_y = 128 - height;
  44. const int speed = 2;
  45.  
  46. int valid_x = 0;
  47. int x = initial_x;
  48. int width = initial_width;
  49. int y = initial_y;
  50. bool direction_inverse = false;
  51. bool has_released = false;
  52. bool first_block = true;
  53. int delayTimer = initial_delay_timer;
  54. bool game_over = false;
  55.  
  56.  
  57. void setup() {
  58.     // put your setup code here, to run once:
  59.  
  60.     Serial.begin(9600);
  61.     pinMode(pin_button, INPUT_PULLUP);
  62.     pinMode(pin_led, OUTPUT);
  63.     pinMode(pin_buzzer, OUTPUT);
  64.  
  65.     ADCSRA = 0; // We don't need analog read functionality in this project
  66.    
  67.     // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  68.     if(!display.begin(SSD1306_SWITCHCAPVCC)) {
  69.         Serial.println(F("SSD1306 allocation failed"));
  70.         for(;;); // Don't proceed, loop forever
  71.     }
  72.     display.clearDisplay();
  73.     //display.invertDisplay(true);
  74.     draw();
  75. }
  76.  
  77. void restart() {
  78.     //digitalWrite(pin_reset, LOW); // Reset arduino
  79.     //pinMode(pin_reset, OUTPUT);
  80.     y = initial_y;
  81.     x = initial_x;
  82.     width = initial_width;
  83.     delayTimer = initial_delay_timer;
  84.     first_block = true;
  85.     display.clearDisplay();
  86.     //display.invertDisplay(true);
  87. }
  88.  
  89. void gameOver() {
  90.     game_over = true;
  91.     digitalWrite(pin_led, HIGH);
  92.     // Wait till button is released
  93.     while(digitalRead(pin_button) == LOW);
  94.    
  95.     while(true) {
  96.         fillRect(x, y, width, height, SSD1306_INVERSE);
  97.         display.display();
  98.         delay(100);
  99.         if(digitalRead(pin_button) == LOW) {
  100.             restart();
  101.             break;
  102.         }
  103.     }
  104.     digitalWrite(pin_led, LOW);
  105. }
  106.  
  107. void fillRect(int x, int y, int width, int height, int color) {
  108.     // Perform screen rotation here
  109.     display.fillRect(128 - y - height, x, height, width, color);
  110. }
  111.  
  112. void input() {
  113.     if (digitalRead(pin_button) == LOW) {
  114.         if(has_released) {
  115.             if(first_block) {
  116.                 valid_x = x;
  117.                 first_block = false;
  118.             } else {
  119.                 const int old_width = width;
  120.                 if(x + width < valid_x) {
  121.                     // ##
  122.                     //   ##
  123.                     gameOver();
  124.                 } else if (x + width < valid_x + width) {
  125.                     // ##
  126.                     //  ##
  127.                     width = width - (valid_x - x);
  128.                 } else if (x < valid_x + width) {
  129.                     //   ##
  130.                     //  ##
  131.                     width = valid_x + width - x;
  132.                     valid_x = x;
  133.                 } else {
  134.                     //   ##
  135.                     // ##
  136.                     gameOver();
  137.                 }
  138.                 if(width == 0) {
  139.                     // strange bug
  140.                     width = old_width;
  141.                     gameOver();
  142.                 }
  143.             }
  144.             has_released = false;
  145.             if(!game_over) {
  146.                 fillRect(valid_x, y, width, height, SSD1306_INVERSE);
  147.                 if(y == 0) {
  148.                     // win
  149.                 }
  150.                 y -= height;
  151.                 delayTimer--;
  152.             } else {
  153.                 game_over = false;
  154.             }
  155.             tone(pin_buzzer, 3000, 10);
  156.         }
  157.     } else {
  158.         has_released = true;
  159.     }
  160. }
  161.  
  162. void update() {
  163.     if(direction_inverse) {
  164.         if(x == 0) {
  165.             direction_inverse = false;
  166.         } else {
  167.             x -= speed;
  168.         }
  169.     } else {
  170.         if(x == 64 - width) {
  171.             direction_inverse = true;
  172.         } else {
  173.             x += speed;
  174.         }
  175.     }
  176. }
  177.  
  178. void draw() {
  179.     fillRect(x, y, width, height, SSD1306_INVERSE);
  180. }
  181.  
  182. void clearDraw() {
  183.     fillRect(x, y, width, height, SSD1306_INVERSE);
  184. }
  185.  
  186. void loop() {
  187.     // put your main code here, to run repeatedly:
  188.     clearDraw();
  189.     input();
  190.     update();
  191.     draw();
  192.     display.display();
  193.     if(delayTimer > 1) {
  194.         delay(delayTimer);
  195.     }
  196. }
Add Comment
Please, Sign In to add comment