A simple Pong game with STM32F407VET6 black board (Seeed Arch Max) and ILI9341 320x240 TFT display.

Dependencies:   mbed ILI9341_STM32F4

A simple Pong game with STM32F407VET6 black board (compatible with Seed Arch Max) and ILI9341 320x240 TFT display.

Connect the SPI interface of an ILI9341 320x240 TFT display to the STM32F407VET6 board (Seeed Arch Max) as follows:

ILI9341 TFTSPI interfaceSTM32F407VET6
VCC+5V
GNDGND
CSPB_7
RESETPB_8
D/CPB_6
SDI(MOSI)PB_5
SCKPB_3
LEDover a 56 ohm resistor+5V
SDO(MISO)PB_4
Revision:
1:971e721f6ef2
Parent:
0:887dd664eca0
--- a/Ball.cpp	Thu Apr 11 20:18:15 2019 +0000
+++ b/Ball.cpp	Mon Apr 15 06:02:55 2019 +0000
@@ -8,9 +8,31 @@
  * @retval
  */
 Ball::Ball(int s /*= 8*/, float v /*= 8*/, uint16_t clr /*= TFT_WHITE*/ ) :
-    size(s), velocity(v), gameOver(true), color(clr), xPos(320 / 2), yPos(240 / 2), xDir(-1), yDir(1)
+    size(s),
+    velocity(v),
+    color(clr),
+    xPos(320 / 2),
+    yPos(240 / 2),
+    xPosOld(320 / 2),
+    yPosOld(240 / 2),
+    xDir(-1),
+    yDir(1)
+{ }
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+void Ball::home()
 {
-    timeout.attach(callback(this, &Ball::newGame), 2);
+    xPos = 320 / 2;
+    yPos = 240 / 2;
+    xPosOld = xPos;
+    yPosOld = yPos;
+    xDir = -1;
+    yDir = 1;
 }
 
 /**
@@ -21,9 +43,8 @@
  */
 bool Ball::move(Racket* rkt)
 {
-    if (gameOver)
-        return true;
-
+    xPosOld = xPos;
+    yPosOld = yPos;
     xPos += xDir * velocity;
     yPos += yDir * velocity;
 
@@ -45,9 +66,7 @@
         yPos = 240 / 2;
         xDir = -1;
         yDir = 1;
-        gameOver = true;
-        tft_text(320/2 - 35, 240 / 2, "GAME OVER", TFT_WHITE, TFT_BLACK);
-        timeout.attach(callback(this, &Ball::newGame), 2);
+        return true;
     }
 
     // hit left wall?
@@ -71,10 +90,7 @@
     // make sure that length of dir stays at 1
     vec2_norm(xDir, yDir);
 
-    // paint
-    //tft_boxfill(xPos - size / 2, yPos - size / 2, xPos - size / 2 + size, yPos - size / 2 + size, TFT_WHITE);
-    
-    return gameOver;
+    return false;
 }
 
 /**
@@ -83,21 +99,10 @@
  * @param
  * @retval
  */
-void Ball::newGame()
+void Ball::paint()
 {
-    gameOver = false;
-    tft_text(320/2 - 35, 240 / 2, "GAME OVER", TFT_BLACK, TFT_BLACK);   // hide the text 
-}
-
-/**
- * @brief
- * @note
- * @param
- * @retval
- */
-void Ball::paint(uint16_t clr)
-{
-    tft_boxfill(xPos - size / 2, yPos - size / 2, xPos + size / 2, yPos + size / 2, clr);
+    tft_boxfill(xPosOld - size / 2, yPosOld - size / 2, xPosOld + size / 2, yPosOld + size / 2, TFT_BLACK); // hide ball at old position
+    tft_boxfill(xPos - size / 2, yPos - size / 2, xPos + size / 2, yPos + size / 2, TFT_WHITE); // draw ball at new position
 }
 
 /**
@@ -108,7 +113,7 @@
  */
 void Ball::vec2_norm(float& x, float& y)
 {
-    // sets a vectors length to 1 (which means that x + y == 1)
+    // sets vector's length to 1 (which means that x + y = 1)
     float   length = sqrt((x * x) + (y * y));
     if (length != 0.0f) {
         length = 1.0f / length;