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

Dependencies:   mbed ILI9341_STM32F4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Racket.cpp Source File

Racket.cpp

00001 #include "Racket.h"
00002 
00003 /**
00004  * @brief
00005  * @note
00006  * @param
00007  * @retval
00008  */
00009 Racket::Racket(int w /*= 20*/, int h /*= 40*/, uint16_t clr /*= TFT_WHITE*/ ) :
00010     width(w),
00011     height(h),
00012     xPos(320),
00013     yPos(240 / 2),
00014     yPosOld(240 / 2),
00015     velocity(4),
00016     xDir(0),
00017     yDir(0),
00018     color(clr)
00019 { }
00020 
00021 /**
00022  * @brief
00023  * @note
00024  * @param
00025  * @retval
00026  */
00027 void Racket::home()
00028 {
00029     xPos = 320;
00030     yPos = 240 / 2;
00031     yPosOld = yPos;
00032     xDir = 0;
00033     yDir = 0;
00034 }
00035 
00036 /**
00037  * @brief
00038  * @note
00039  * @param
00040  * @retval
00041  */
00042 void Racket::move(DigitalIn* btnUp, DigitalIn* btnDown)
00043 {
00044     yDir = 0;
00045     if (*btnUp == 0)
00046         yDir = -1;  // move up
00047     if (*btnDown == 0)
00048         yDir = 1;   // move down
00049     yPosOld = yPos;
00050     yPos += yDir * velocity;
00051     if (yPos < height / 2)
00052         yPos = height / 2;
00053     if (yPos > 240 - height / 2)
00054         yPos = 240 - height / 2;
00055 }
00056 
00057 /**
00058  * @brief
00059  * @note
00060  * @param
00061  * @retval
00062  */
00063 void Racket::paint()
00064 {
00065     if (yDir == 1) {
00066         tft_boxfill(xPos - width / 2, yPosOld - height / 2, xPos, yPos - height / 2, TFT_BLACK);    // hide racket at old position
00067         tft_boxfill(xPos - width / 2, yPos - height / 2, xPos, yPos + height / 2, TFT_WHITE);       // draw racket at new position
00068     }
00069     else {
00070         tft_boxfill(xPos - width / 2, yPos + height / 2, xPos, yPosOld + height / 2, TFT_BLACK);    // hide racket at old position
00071         tft_boxfill(xPos - width / 2, yPos - height / 2, xPos, yPos + height / 2, TFT_WHITE);       // draw racket at new position
00072     }
00073 }