acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Pong_v3.ino (4912B)
1 /* 2 * Pong 3 * Original Code from https://github.com/rparrett/pongclock 4 * 5 */ 6 7 #define BLACK 0x0000 8 #define WHITE 0xFFFF 9 #define GREY 0x5AEB 10 11 #include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip 12 #include <SPI.h> 13 14 TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h 15 16 int16_t h = 128; 17 int16_t w = 160; 18 19 int dly = 10; 20 21 int16_t paddle_h = 25; 22 int16_t paddle_w = 2; 23 24 int16_t lpaddle_x = 0; 25 int16_t rpaddle_x = w - paddle_w; 26 27 int16_t lpaddle_y = 0; 28 int16_t rpaddle_y = h - paddle_h; 29 30 int16_t lpaddle_d = 1; 31 int16_t rpaddle_d = -1; 32 33 int16_t lpaddle_ball_t = w - w / 4; 34 int16_t rpaddle_ball_t = w / 4; 35 36 int16_t target_y = 0; 37 38 int16_t ball_x = 2; 39 int16_t ball_y = 2; 40 int16_t oldball_x = 2; 41 int16_t oldball_y = 2; 42 43 int16_t ball_dx = 1; 44 int16_t ball_dy = 1; 45 46 int16_t ball_w = 4; 47 int16_t ball_h = 4; 48 49 int16_t dashline_h = 4; 50 int16_t dashline_w = 2; 51 int16_t dashline_n = h / dashline_h; 52 int16_t dashline_x = w / 2 - 1; 53 int16_t dashline_y = dashline_h / 2; 54 55 int16_t lscore = 12; 56 int16_t rscore = 4; 57 58 void setup(void) { 59 60 randomSeed(analogRead(0)*analogRead(1)); 61 62 tft.init(); 63 64 tft.setRotation(1); 65 66 tft.fillScreen(BLACK); 67 68 initgame(); 69 70 tft.setTextColor(WHITE, BLACK); 71 72 delay(2000); 73 74 } 75 76 void loop() { 77 delay(dly); 78 79 lpaddle(); 80 rpaddle(); 81 82 midline(); 83 84 ball(); 85 } 86 87 void initgame() { 88 lpaddle_y = random(0, h - paddle_h); 89 rpaddle_y = random(0, h - paddle_h); 90 91 // ball is placed on the center of the left paddle 92 ball_y = lpaddle_y + (paddle_h / 2); 93 94 calc_target_y(); 95 96 midline(); 97 98 tft.fillRect(0,h-26,w,h-1,BLACK); 99 100 tft.setTextDatum(TC_DATUM); 101 tft.setTextColor(WHITE); 102 tft.drawString("TFT_eSPI example", w/2, h-26 , 2); 103 } 104 105 void midline() { 106 107 // If the ball is not on the line then don't redraw the line 108 if ((ball_x<dashline_x-ball_w) && (ball_x > dashline_x+dashline_w)) return; 109 110 tft.startWrite(); 111 112 // Quick way to draw a dashed line 113 tft.setAddrWindow(dashline_x, 0, dashline_w, h); 114 115 for(int16_t i = 0; i < dashline_n; i+=2) { 116 tft.pushColor(WHITE, dashline_w*dashline_h); // push dash pixels 117 tft.pushColor(BLACK, dashline_w*dashline_h); // push gap pixels 118 } 119 120 tft.endWrite(); 121 } 122 123 void lpaddle() { 124 125 if (lpaddle_d == 1) { 126 tft.fillRect(lpaddle_x, lpaddle_y, paddle_w, 1, BLACK); 127 } 128 else if (lpaddle_d == -1) { 129 tft.fillRect(lpaddle_x, lpaddle_y + paddle_h - 1, paddle_w, 1, BLACK); 130 } 131 132 lpaddle_y = lpaddle_y + lpaddle_d; 133 134 if (ball_dx == 1) lpaddle_d = 0; 135 else { 136 if (lpaddle_y + paddle_h / 2 == target_y) lpaddle_d = 0; 137 else if (lpaddle_y + paddle_h / 2 > target_y) lpaddle_d = -1; 138 else lpaddle_d = 1; 139 } 140 141 if (lpaddle_y + paddle_h >= h && lpaddle_d == 1) lpaddle_d = 0; 142 else if (lpaddle_y <= 0 && lpaddle_d == -1) lpaddle_d = 0; 143 144 tft.fillRect(lpaddle_x, lpaddle_y, paddle_w, paddle_h, WHITE); 145 } 146 147 void rpaddle() { 148 149 if (rpaddle_d == 1) { 150 tft.fillRect(rpaddle_x, rpaddle_y, paddle_w, 1, BLACK); 151 } 152 else if (rpaddle_d == -1) { 153 tft.fillRect(rpaddle_x, rpaddle_y + paddle_h - 1, paddle_w, 1, BLACK); 154 } 155 156 rpaddle_y = rpaddle_y + rpaddle_d; 157 158 if (ball_dx == -1) rpaddle_d = 0; 159 else { 160 if (rpaddle_y + paddle_h / 2 == target_y) rpaddle_d = 0; 161 else if (rpaddle_y + paddle_h / 2 > target_y) rpaddle_d = -1; 162 else rpaddle_d = 1; 163 } 164 165 if (rpaddle_y + paddle_h >= h && rpaddle_d == 1) rpaddle_d = 0; 166 else if (rpaddle_y <= 0 && rpaddle_d == -1) rpaddle_d = 0; 167 168 tft.fillRect(rpaddle_x, rpaddle_y, paddle_w, paddle_h, WHITE); 169 } 170 171 void calc_target_y() { 172 int16_t target_x; 173 int16_t reflections; 174 int16_t y; 175 176 if (ball_dx == 1) { 177 target_x = w - ball_w; 178 } 179 else { 180 target_x = -1 * (w - ball_w); 181 } 182 183 y = abs(target_x * (ball_dy / ball_dx) + ball_y); 184 185 reflections = floor(y / h); 186 187 if (reflections % 2 == 0) { 188 target_y = y % h; 189 } 190 else { 191 target_y = h - (y % h); 192 } 193 } 194 195 void ball() { 196 ball_x = ball_x + ball_dx; 197 ball_y = ball_y + ball_dy; 198 199 if (ball_dx == -1 && ball_x == paddle_w && ball_y + ball_h >= lpaddle_y && ball_y <= lpaddle_y + paddle_h) { 200 ball_dx = ball_dx * -1; 201 dly = random(5); // change speed of ball after paddle contact 202 calc_target_y(); 203 } else if (ball_dx == 1 && ball_x + ball_w == w - paddle_w && ball_y + ball_h >= rpaddle_y && ball_y <= rpaddle_y + paddle_h) { 204 ball_dx = ball_dx * -1; 205 dly = random(5); // change speed of ball after paddle contact 206 calc_target_y(); 207 } else if ((ball_dx == 1 && ball_x >= w) || (ball_dx == -1 && ball_x + ball_w < 0)) { 208 dly = 5; 209 } 210 211 if (ball_y > h - ball_w || ball_y < 0) { 212 ball_dy = ball_dy * -1; 213 ball_y += ball_dy; // Keep in bounds 214 } 215 216 //tft.fillRect(oldball_x, oldball_y, ball_w, ball_h, BLACK); 217 tft.drawRect(oldball_x, oldball_y, ball_w, ball_h, BLACK); // Less TFT refresh aliasing than line above for large balls 218 tft.fillRect( ball_x, ball_y, ball_w, ball_h, WHITE); 219 oldball_x = ball_x; 220 oldball_y = ball_y; 221 } 222