Components and supplies
1
Arduino Nano
1
0.96" 128x64 OLED I2C Display
4
Puss Buttons 12x12x7.3 with capsule.
2
M3 screws
1
9v Battery
1
9v Battery Holder
2
Grove - Tilt Switch
Tools and machines
1
Mini drill (generic)
1
3D Printer (generic)
1
Super Glue
Apps and platforms
1
Cura
1
Arduino IDE
Project description
Code
Drone Proton Tetris Code
1// DRONE PROTON TETRİS GAME CODES 2 3#include <Wire.h> 4#include <Adafruit_GFX.h> 5#include <Adafruit_SSD1306.h> 6 7#define WIDTH 64 8#define HEIGHT 128 9 10Adafruit_SSD1306 display(128, 64, &Wire, -1); 11 12static const unsigned char PROGMEM logo [] = { 13 0x00, 0x00, 0x18, 0x06, 0x01, 0xc0, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 14 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 15 16 17 const char pieces_S_l[2][2][4] = {{ 18 {0, 0, 1, 1}, {0, 1, 1, 2} 19 }, 20 { 21 {0, 1, 1, 2}, {1, 1, 0, 0} 22 }}; 23 24 const char pieces_S_r[2][2][4]{{ 25 {1, 1, 0, 0}, {0, 1, 1, 2} 26 }, 27 { 28 {0, 1, 1, 2}, {0, 0, 1, 1} 29 }}; 30 31 const char pieces_L_l[4][2][4] = {{ 32 {0, 0, 0, 1}, {0, 1, 2, 2} 33 }, 34 { 35 {0, 1, 2, 2}, {1, 1, 1, 0} 36 }, 37 { 38 {0, 1, 1, 1}, {0, 0, 1, 2} 39 }, 40 { 41 {0, 0, 1, 2}, {1, 0, 0, 0} 42 }}; 43 44const char pieces_Sq[1][2][4] = {{ 45 {0, 1, 0, 1}, {0, 0, 1, 1} 46 }}; 47 48 const char pieces_T[4][2][4] = {{ 49 {0, 0, 1, 0},{0, 1, 1, 2} 50 }, 51 { 52 {0, 1, 1, 2},{1, 0, 1, 1} 53 }, 54 { 55 {1, 0, 1, 1},{0, 1, 1, 2} 56 }, 57 { 58 {0, 1, 1, 2},{0, 0, 1, 0} 59 }}; 60 61const char pieces_l[2][2][4] = {{ 62 {0, 1, 2, 3}, {0, 0, 0, 0} 63 }, 64 { 65 {0, 0, 0, 0}, {0, 1, 2, 3} 66 }}; 67 68 const short MARGIN_TOP = 19; //- declares a constant variable MARGIN_TOP with a value of 19 of type short. 69const short MARGIN_LEFT = 3;// - declares a constant variable MARGIN_LEFT with a value of 3 of type short. 70const short SIZE = 5;// - declares a constant variable SIZE with a value of 5 of type short. 71const short TYPES = 6; //- declares a constant variable TYPES with a value of 6 of type short. 72#define SPEAKER_PIN 3 //- creates a macro with the name SPEAKER_PIN and a value of 3. This allows the code to refer to SPEAKER_PIN throughout the rest of the code and substitute it with the value 3. 73const int MELODY_LENGTH = 10; //- declares a constant variable MELODY_LENGTH with a value of 10 of type int. 74const int MELODY_NOTES[MELODY_LENGTH] = {262, 294, 330, 262}; //- declares an array MELODY_NOTES of size MELODY_LENGTH (which is 10), and initializes it with four integer values. 75const int MELODY_DURATIONS[MELODY_LENGTH] = {500, 500, 500, 500}; //- declares an array MELODY_DURATIONS of size MELODY_LENGTH (which is 10), and initializes it with four integer values. 76int click[] = { 1047 }; //- declares an array click of size 1, and initializes it with one integer value. 77int click_duration[] = { 100 }; //- declares an array click_duration of size 1, and initializes it with one integer value. 78int erase[] = { 2093 }; //- declares an array erase of size 1, and initializes it with one integer value. 79int erase_duration[] = { 100 }; //- declares an array erase_duration of size 1, and initializes it with one integer value. 80word currentType, nextType, rotation; //- declares three variables of type word, named currentType, nextType, and rotation. 81short pieceX, pieceY; //- declares two variables of type short, named pieceX and pieceY. 82short piece[2][4]; //- declares a two-dimensional array piece of size 2x4 with elements of type short. 83int interval = 20, score; //- declares two variables, interval and score, of type int. interval is initialized with a value of 20. 84long timer, delayer; //- declares two variables, timer and delayer, of type long. 85boolean grid[10][18]; //- declares a two-dimensional array grid of size 10x18 with elements of type boolean. 86boolean b1, b2, b3; //- declares three variables of type boolean, named b1, b2, and b3. 87 int left=11; 88 int right=9; 89 int change=12; 90 int speed=10; 91 92 void checkLines(){ 93 boolean full; 94 for(short y = 17; y >= 0; y--){ 95 full = true; 96 for(short x = 0; x < 10; x++){ 97 full = full && grid[x][y]; 98 } 99 if(full){ 100 breakLine(y); 101 y++; 102 } 103 } 104 } 105 void breakLine(short line){ 106 tone(SPEAKER_PIN, erase[0], 1000 / erase_duration[0]); 107 delay(100); 108 noTone(SPEAKER_PIN); 109 for(short y = line; y >= 0; y--){ 110 for(short x = 0; x < 10; x++){ 111 grid[x][y] = grid[x][y-1]; 112 } 113 } 114 for(short x = 0; x < 10; x++){ 115 grid[x][0] = 0; 116 } 117 display.invertDisplay(true); 118 delay(50); 119 display.invertDisplay(false); 120 score += 10; 121 } 122 123 void refresh(){ 124 display.clearDisplay(); 125 drawLayout(); 126 drawGrid(); 127 drawPiece(currentType, 0, pieceX, pieceY); 128 display.display(); 129 } 130 void drawGrid(){ 131 for(short x = 0; x < 10; x++) 132 for(short y = 0; y < 18; y++) 133 if(grid[x][y]) 134 display.fillRect(MARGIN_LEFT + (SIZE + 1)*x, MARGIN_TOP + (SIZE + 1)*y, SIZE, SIZE, WHITE); 135 } 136 boolean nextHorizontalCollision(short piece[2][4], int amount){ 137 for(short i = 0; i < 4; i++){ 138 short newX = pieceX + piece[0][i] + amount; 139 if(newX > 9 || newX < 0 || grid[newX][pieceY + piece[1][i]]) 140 return true; 141 } 142 return false; 143 } 144 boolean nextCollision(){ 145 for(short i = 0; i < 4; i++){ 146 short y = pieceY + piece[1][i] + 1; 147 short x = pieceX + piece[0][i]; 148 if(y > 17 || grid[x][y]) 149 return true; 150 } 151 return false; 152 } 153 154 void generate(){ 155 currentType = nextType; 156 nextType = random(TYPES); 157 if(currentType != 5) 158 pieceX = random(9); 159 else 160 pieceX = random(7); 161 pieceY = 0; 162 rotation = 0; 163 copyPiece(piece, currentType, rotation); 164 } 165 void drawPiece(short type, short rotation, short x, short y){ 166 for(short i = 0; i < 4; i++) 167 display.fillRect(MARGIN_LEFT + (SIZE + 1)*(x + piece[0][i]), MARGIN_TOP + (SIZE + 1)*(y + piece[1][i]), SIZE, SIZE, WHITE); 168 } 169 void drawNextPiece(){ 170 short nPiece[2][4]; 171 copyPiece(nPiece, nextType, 0); 172 for(short i = 0; i < 4; i++) 173 display.fillRect(50 + 3*nPiece[0][i], 4 + 3*nPiece[1][i], 2, 2, WHITE); 174 } 175 176 void copyPiece(short piece[2][4], short type, short rotation){ 177 switch(type){ 178 case 0: //L_l 179 for(short i = 0; i < 4; i++){ 180 piece[0][i] = pieces_L_l[rotation][0][i]; 181 piece[1][i] = pieces_L_l[rotation][1][i]; 182 } 183 break; 184 case 1: //S_l 185 for(short i = 0; i < 4; i++){ 186 piece[0][i] = pieces_S_l[rotation][0][i]; 187 piece[1][i] = pieces_S_l[rotation][1][i]; 188 } 189 break; 190 case 2: //S_r 191 for(short i = 0; i < 4; i++){ 192 piece[0][i] = pieces_S_r[rotation][0][i]; 193 piece[1][i] = pieces_S_r[rotation][1][i]; 194 } 195 break; 196 case 3: //Sq 197 for(short i = 0; i < 4; i++){ 198 piece[0][i] = pieces_Sq[0][0][i]; 199 piece[1][i] = pieces_Sq[0][1][i]; 200 } 201 break; 202 case 4: //T 203 for(short i = 0; i < 4; i++){ 204 piece[0][i] = pieces_T[rotation][0][i]; 205 piece[1][i] = pieces_T[rotation][1][i]; 206 } 207 break; 208 case 5: //l 209 for(short i = 0; i < 4; i++){ 210 piece[0][i] = pieces_l[rotation][0][i]; 211 piece[1][i] = pieces_l[rotation][1][i]; 212 } 213 break; 214 } 215 } 216 short getMaxRotation(short type){ 217 if(type == 1 || type == 2 || type == 5) 218 return 2; 219 else if(type == 0 || type == 4) 220 return 4; 221 else if(type == 3) 222 return 1; 223 else 224 return 0; 225 } 226 boolean canRotate(short rotation){ 227 short piece[2][4]; 228 copyPiece(piece, currentType, rotation); 229 return !nextHorizontalCollision(piece, 0); 230 } 231 232 233 void drawLayout(){ 234 display.drawLine(0, 15, WIDTH, 15, WHITE); 235 display.drawRect(0, 0, WIDTH, HEIGHT, WHITE); 236 drawNextPiece(); 237 char text[6]; 238 itoa(score, text, 10); 239 drawText(text, getNumberLength(score), 7, 4); 240 } 241 short getNumberLength(int n){ 242 short counter = 1; 243 while(n >= 10){ 244 n /= 10; 245 counter++; 246 } 247 return counter; 248 } 249 void drawText(char text[], short length, int x, int y){ 250 display.setTextSize(1); // Normal 1:1 pixel scale 251 display.setTextColor(WHITE); // Draw white text 252 display.setCursor(x, y); // Start at top-left corner 253 display.cp437(true); // Use full 256 char 'Code Page 437' font 254 for(short i = 0; i < length; i++) 255 display.write(text[i]); 256 } 257 258void setup() { 259 pinMode(left, INPUT_PULLUP); 260 pinMode(right, INPUT_PULLUP); 261 pinMode(change, INPUT_PULLUP); 262 pinMode(speed, INPUT_PULLUP); 263 pinMode(SPEAKER_PIN, OUTPUT); 264 Serial.begin(9600); 265 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally 266 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 267 Serial.println(F("SSD1306 allocation failed")); 268 for(;;); // Don't proceed, loop forever 269 } 270 display.setRotation(1); 271 display.clearDisplay(); 272 display.drawBitmap(3, 23, logo, 64, 82, WHITE); 273 display.display(); 274 delay(2000); 275 display.clearDisplay(); 276 drawLayout(); 277 display.display(); 278 randomSeed(analogRead(0)); 279 nextType = random(TYPES); 280 generate(); 281 timer = millis(); 282 } 283 284 285 void loop() { 286 if(millis() - timer > interval){ 287 checkLines(); 288 refresh(); 289 if(nextCollision()){ 290 for(short i = 0; i < 4; i++) 291 grid[pieceX + piece[0][i]][pieceY + piece[1][i]] = 1; 292 generate(); 293 }else 294 pieceY++; 295 timer = millis(); 296 } 297 if(!digitalRead(left)){ 298 tone(SPEAKER_PIN, click[0], 1000 / click_duration[0]); 299 delay(100); 300 noTone(SPEAKER_PIN); 301 if(b1){ 302 if(!nextHorizontalCollision(piece, -1)){ 303 pieceX--; 304 refresh(); 305 } 306 b1 = false; 307 } 308 }else{ 309 b1 = true; 310 } 311 if(!digitalRead(right)){ 312 tone(SPEAKER_PIN, click[0], 1000 / click_duration[0]); 313 delay(100); 314 noTone(SPEAKER_PIN); 315 if(b2){ 316 if(!nextHorizontalCollision(piece, 1)){ 317 pieceX++; 318 refresh(); 319 } 320 b2 = false; 321 } 322 }else{ 323 b2 = true; 324 } 325 if(!digitalRead(speed)){ 326 interval = 20; 327 } else{ 328 interval = 400; 329 } 330 if(!digitalRead(change)){ 331 tone(SPEAKER_PIN, click[0], 1000 / click_duration[0]); 332 delay(100); 333 noTone(SPEAKER_PIN); 334 if(b3){ 335 if(rotation == getMaxRotation(currentType) - 1 && canRotate(0)){ 336 rotation = 0; 337 }else if(canRotate(rotation + 1)){ 338 rotation++; 339 } 340 copyPiece(piece, currentType, rotation); 341 refresh(); 342 b3 = false; 343 delayer = millis(); 344 } 345 }else if(millis() - delayer > 50){ 346 b3 = true; 347 } 348 }
Downloadable files
Body
body.stl
button 3d
button.stl
Lid 3d
Lid.stl
Documentation
Diagram
teetris şema.png

Comments
Only logged in users can leave comments