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