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

Ball.cpp

Committer:
hudakz
Date:
2019-04-11
Revision:
0:887dd664eca0
Child:
1:971e721f6ef2

File content as of revision 0:887dd664eca0:

#include "Ball.h"
#include "Racket.h"

/**
 * @brief
 * @note
 * @param
 * @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)
{
    timeout.attach(callback(this, &Ball::newGame), 2);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
bool Ball::move(Racket* rkt)
{
    if (gameOver)
        return true;

    xPos += xDir * velocity;
    yPos += yDir * velocity;

    // hit by racket?
    if
    (
        rkt->xPos - rkt->width / 2 < xPos + size / 2 &&
        xPos + size / 2 < rkt->xPos &&
        rkt->yPos - rkt->height / 2 < yPos + size / 2 &&
        yPos - size / 2 < rkt->yPos + rkt->height / 2
    ) {
        xPos = rkt->xPos - rkt->width / 2 - size / 2;
        xDir = -fabs(xDir); // force it to be negative
    }

    // missed racket?
    if (xPos > 320) {
        xPos = 320 / 2;
        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);
    }

    // hit left wall?
    if (xPos - size / 2 < 0) {
        xPos = size / 2;
        xDir = fabs(xDir);  // force it to be positive
    }

    // hit bottom wall?
    if (yPos + size / 2 > 240) {
        yPos = 240 - size / 2;
        yDir = -fabs(yDir); // force it to be negative
    }

    // hit top wall?
    if (yPos - size / 2 < 0) {
        yPos = size / 2;
        yDir = fabs(yDir);  // force it to be positive
    }

    // 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;
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
void Ball::newGame()
{
    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);
}

/**
 * @brief
 * @note
 * @param
 * @retval
 */
void Ball::vec2_norm(float& x, float& y)
{
    // sets a vectors length to 1 (which means that x + y == 1)
    float   length = sqrt((x * x) + (y * y));
    if (length != 0.0f) {
        length = 1.0f / length;
        x *= length;
        y *= length;
    }
}