LCD Display Pongball Game
A fun pong ball toggle game system.
Components and supplies
1
Solderless Breadboard Full Size
1
SPI TFT ST7735 LCD Display
1
Analog Joystick
5
Male/Female Jumper Wires
8
Jumper wires (generic)
1
Arduino UNO
Project description
Code
Pongball Game Code
arduino
1#include <Adafruit_GFX.h> 2#include <Adafruit_ST7735.h> 3#include <Fonts/FreeSerif18pt7b.h> 4#include <SPI.h> 5 6// Rotary Encoder 7#define TFT_CS 10 8#define TFT_RST 9 9#define TFT_DC 8 10#define DT 3 11#define CLK 2 12#define SW 4 13 14 15// Joy stick 16#define SW_pin 5 17#define X_pin A0 18#define Y_pin A1 19 20 21 22int counter = 0; 23int points = 0; 24int highScore = 0; 25int currentCount = 0; 26int currentStateCLK; 27int lastStateCLK; 28int height = 0; 29String currentDir = ""; 30int Variable; 31unsigned long lastButtonPress = 0; 32unsigned long lastToggle = 0; 33uint16_t color = 0xF800; 34 35uint16_t ballColor = ST7735_WHITE; 36 37int xPosition = 0; 38int yPosition = 0; 39int SW1_state = 0; 40int mapX = 0; 41int mapY = 0; 42 43bool endGame = false; 44 45int WIDTH = 128; 46int HEIGHT = 160; 47 48int ballX = WIDTH / 2; 49int ballY = HEIGHT / 2; 50 51int ballSpeedX = 5; 52int ballSpeedY = 7; 53 54int currentPos; 55int prevPos; 56 57int xPaddle; 58int yPaddle; 59 60 61Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); 62 63 uint16_t colors[9] = { 64 0x0000, 65 0xFFFF, 66 0xF800, 67 0x07E0, 68 0x001F, 69 0x07FF, 70 0xF81F, 71 0xFFE0, 72 0xFC00 73}; 74 75void setup() { 76 pinMode(CLK, INPUT); 77 pinMode(DT, INPUT); 78 pinMode(SW, INPUT_PULLUP); 79 80 tft.initR(INITR_BLACKTAB); 81 tft.fillScreen(0x6C3F); 82 tft.setTextWrap(false); 83 84 85 Serial.begin(9600); 86 87 pinMode(SW_pin, INPUT); 88 digitalWrite(SW_pin, HIGH); 89 90 lastStateCLK = digitalRead(CLK); 91 92 xPaddle = (WIDTH / 2) - 20; 93 yPaddle = 130; 94 95 prevPos = analogRead(X_pin); 96 97} 98 99void loop() { 100// rotaryScreen(); 101 int address=0; 102 tft.setTextSize(4); 103 tft.setCursor(0,0); 104 105 if(!endGame) { 106 107 tft.fillScreen(ST77XX_BLACK); 108 joyStickControl(); 109 collisions(); 110 scoreBoard(); 111 // delay(100); 112 } else { 113 delay(100); 114 // tft.invertDisplay(true); 115 textScreen(); 116 int btnState = digitalRead(SW_pin); 117 if(btnState == LOW) { 118 if(millis() - lastButtonPress > 50) { 119 ballX = WIDTH / 2; 120 ballY = HEIGHT / 2; 121 xPaddle = (WIDTH / 2) - 20; 122 yPaddle = 130; 123 endGame = false; 124 125 if(points > highScore) { 126 highScore = points; 127 } 128 points = 0; 129 } 130 lastButtonPress = millis(); 131 } 132 } 133} 134 135void scoreBoard() { 136 tft.setCursor(10, 10); 137 tft.setTextSize(1.5); 138 tft.setTextColor(ST77XX_YELLOW); 139 tft.println("Points: "); 140 tft.setCursor(60, 10); 141 tft.print(points); 142 143 tft.setCursor(10, 20); 144 tft.setTextSize(1.5); 145 tft.setTextColor(ST77XX_YELLOW); 146 tft.println("High Score: "); 147 tft.setCursor(80, 20); 148 tft.print(highScore); 149 delay(100); 150} 151 152void textScreen() { 153 tft.setCursor(30, 40); 154 tft.setTextSize(3); 155 tft.setTextColor(ST77XX_RED); 156 tft.println("GAME"); 157 tft.setCursor(30, 70); 158 tft.print("OVER"); 159 delay(500); 160 161 tft.setCursor(30, 40); 162 tft.setTextColor(ST77XX_GREEN); 163 tft.println("GAME"); 164 tft.setCursor(30, 70); 165 tft.print("OVER"); 166 delay(500); 167} 168 169void joyStickControl() { 170 int xPosition = analogRead(X_pin); 171 int yPosition = analogRead(Y_pin); 172 Serial.print("Switch: "); 173 Serial.print(digitalRead(SW_pin)); 174 Serial.print(" | "); 175 Serial.print("X-axis: "); 176 Serial.print(xPosition); 177 Serial.print(" | "); 178 Serial.print("Y-axis: "); 179 Serial.print(yPosition); 180 Serial.println(" | "); 181 182 tft.fillCircle(ballX, ballY, 5, ballColor); 183 184 185 186 if(millis() - lastToggle > 50) { 187 188 currentPos = xPosition; 189 190 if(currentPos > 512 && (currentPos != 512 || prevPos != 512)) { 191 xPaddle += 10; 192 } 193 194 if(currentPos < 512 && (currentPos != 512 || currentPos != 512)) { 195 xPaddle -= 10; 196 } 197 198 if(xPaddle < 0) { 199 xPaddle = 0; 200 } 201 202 if(xPaddle + 40 > WIDTH) { 203 xPaddle = WIDTH - 40; 204 } 205 206 prevPos = currentPos; 207 } 208 209 lastToggle = millis(); 210 211 212 int btnState = digitalRead(SW_pin); 213 214 if(btnState == LOW) { 215 if(millis() - lastButtonPress > 50) { 216 int rand = random(8); 217 color = colors[rand]; 218 } 219 220 lastButtonPress = millis(); 221 } 222 223 tft.fillRect(xPaddle, yPaddle, 40, 10, color); 224 225} 226 227void collisions() { 228 ballX += ballSpeedX; 229 ballY += ballSpeedY; 230 231 if(ballX+10 >= WIDTH || ballX-10 <= 0) { 232 ballSpeedX *= -1; 233 } 234 235 if(ballY >= HEIGHT) { 236 endGame = true; 237 } 238 239 if(ballY <= 0) { 240 ballSpeedY *= -1; 241 } 242 243 if(ballX >= xPaddle && ballX <= xPaddle+40) { 244 // ballColor = ST7735_RED; 245 if(ballY >= yPaddle-10) { 246 ballColor = ST7735_RED; 247 points++; 248 249 250 251 ballSpeedX *= -1; 252 ballSpeedY = -7; 253 254 ballSpeedX += 1; 255 ballSpeedY += 1; 256 } else { 257 ballColor = ST7735_WHITE; 258 } 259 } 260} 261 262void rotaryScreen() { 263 currentStateCLK = digitalRead(CLK); 264 265 if(currentStateCLK != lastStateCLK && currentStateCLK == 1) { 266 267 if(digitalRead(DT) != currentStateCLK) { 268 counter--; 269 currentDir = "CCW"; 270 } else { 271 counter++; 272 currentDir = "CW"; 273 } 274 275 if(counter < 0) { 276 counter = 0; 277 } 278 279 if(counter > 160) { 280 counter = 160; 281 } 282 283 Serial.print("Direction: "); 284 Serial.print(currentDir); 285 Serial.print(" | Counter: "); 286 Serial.println(counter); 287 } 288 289 lastStateCLK = currentStateCLK; 290 291 int btnState = digitalRead(SW); 292 293 if(btnState == LOW) { 294 if(millis() - lastButtonPress > 50) { 295 Serial.println("Button pressed!"); 296 int rand = random(8); 297 color = colors[rand]; 298 } 299 300 lastButtonPress = millis(); 301 } 302 303 tft.fillRect(0, 0, 128, (counter*10), color); 304 305 delay(1); 306} 307
Downloadable files
Schematic
Schematic

Schematic
Schematic

Comments
Only logged in users can leave comments