SHARE
    TWEET
    ferrybig

    Arduino Stacker Game

    Jan 27th, 2020
    650
    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. #define SCREEN_WIDTH 128 // OLED display width, in pixels
    23. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
    24. // Declaration for SSD1306 display connected using software SPI (default case):
    25. #define OLED_MOSI 9
    26. #define OLED_CLK 10
    27. #define OLED_DC 11
    28. #define OLED_CS 12
    29. #define OLED_RESET 13
    30. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
    31. OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
    32. const int pin_button = 2;
    33. const int pin_led = A5;
    34. const int pin_buzzer = 3;
    35. const int initial_delay_timer = 32;
    36. const int initial_x = 32;
    37. const int initial_width = 8;
    38. const int height = 4;
    39. const int initial_y = 128 - height;
    40. const int speed = 2;
    41. int valid_x = 0;
    42. int x = initial_x;
    43. int width = initial_width;
    44. int y = initial_y;
    45. bool direction_inverse = false;
    46. bool has_released = false;
    47. bool first_block = true;
    48. int delayTimer = initial_delay_timer;
    49. bool game_over = false;
    50. void setup() {
    51. // put your setup code here, to run once:
    52. Serial.begin(9600);
    53. pinMode(pin_button, INPUT_PULLUP);
    54. pinMode(pin_led, OUTPUT);
    55. pinMode(pin_buzzer, OUTPUT);
    56. ADCSRA = 0; // We don't need analog read functionality in this project
    57. // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    58. if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    59. Serial.println(F("SSD1306 allocation failed"));
    60. for(;;); // Don't proceed, loop forever
    61. }
    62. display.clearDisplay();
    63. //display.invertDisplay(true);
    64. draw();
    65. }
    66. void restart() {
    67. //digitalWrite(pin_reset, LOW); // Reset arduino
    68. //pinMode(pin_reset, OUTPUT);
    69. y = initial_y;
    70. x = initial_x;
    71. width = initial_width;
    72. delayTimer = initial_delay_timer;
    73. first_block = true;
    74. display.clearDisplay();
    75. //display.invertDisplay(true);
    76. }
    77. void gameOver() {
    78. game_over = true;
    79. digitalWrite(pin_led, HIGH);
    80. // Wait till button is released
    81. while(digitalRead(pin_button) == LOW);
    82. while(true) {
    83. fillRect(x, y, width, height, SSD1306_INVERSE);
    84. display.display();
    85. delay(100);
    86. if(digitalRead(pin_button) == LOW) {
    87. restart();
    88. break;
    89. }
    90. }
    91. digitalWrite(pin_led, LOW);
    92. }
    93. void fillRect(int x, int y, int width, int height, int color) {
    94. // Perform screen rotation here
    95. display.fillRect(128 - y - height, x, height, width, color);
    96. }
    97. void input() {
    98. if (digitalRead(pin_button) == LOW) {
    99. if(has_released) {
    100. if(first_block) {
    101. valid_x = x;
    102. first_block = false;
    103. } else {
    104. const int old_width = width;
    105. if(x + width < valid_x) {
    106. // ##
    107. // ##
    108. gameOver();
    109. } else if (x + width < valid_x + width) {
    110. // ##
    111. // ##
    112. width = width - (valid_x - x);
    113. } else if (x < valid_x + width) {
    114. // ##
    115. // ##
    116. width = valid_x + width - x;
    117. valid_x = x;
    118. } else {
    119. // ##
    120. // ##
    121. gameOver();
    122. }
    123. if(width == 0) {
    124. // strange bug
    125. width = old_width;
    126. gameOver();
    127. }
    128. }
    129. has_released = false;
    130. if(!game_over) {
    131. fillRect(valid_x, y, width, height, SSD1306_INVERSE);
    132. if(y == 0) {
    133. // win
    134. }
    135. y -= height;
    136. delayTimer--;
    137. } else {
    138. game_over = false;
    139. }
    140. tone(pin_buzzer, 3000, 10);
    141. }
    142. } else {
    143. has_released = true;
    144. }
    145. }
    146. void update() {
    147. if(direction_inverse) {
    148. if(x == 0) {
    149. direction_inverse = false;
    150. } else {
    151. x -= speed;
    152. }
    153. } else {
    154. if(x == 64 - width) {
    155. direction_inverse = true;
    156. } else {
    157. x += speed;
    158. }
    159. }
    160. }
    161. void draw() {
    162. fillRect(x, y, width, height, SSD1306_INVERSE);
    163. }
    164. void clearDraw() {
    165. fillRect(x, y, width, height, SSD1306_INVERSE);
    166. }
    167. void loop() {
    168. // put your main code here, to run repeatedly:
    169. clearDraw();
    170. input();
    171. update();
    172. draw();
    173. display.display();
    174. if(delayTimer > 1) {
    175. delay(delayTimer);
    176. }
    177. }
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /