Pong by Letartre

Dependencies:   BSP_DISCO_F746NG

Files at this revision

API Documentation at this revision

Comitter:
quentlet
Date:
Tue Jun 22 12:26:47 2021 +0000
Parent:
3:9f66aabe7b3b
Commit message:
Pong

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Nov 20 11:49:15 2019 +0100
+++ b/main.cpp	Tue Jun 22 12:26:47 2021 +0000
@@ -1,74 +1,198 @@
 #include "mbed.h"
 #include "stm32746g_discovery_lcd.h"
 #include "stm32746g_discovery_ts.h"
+#include "stdlib.h"
 
-int main()
-{
+
+void Terrain(int xg, int yg, int xd, int yd, int x1, int y1);
+
+
+int main() {  
+
+    //Manette gauche
+    DigitalIn d0(D0);
+    DigitalIn d2(D2);
+    //Manette Droite
+    DigitalIn d5(D5);
+    DigitalIn d7(D7);
+    
     TS_StateTypeDef TS_State;
-    uint16_t x, y;
-    uint8_t text[30];
     uint8_t status;
-    uint8_t idx;
-    uint8_t cleared = 0;
-    uint8_t prev_nb_touches = 0;
+    int TouchScreen=1;
+    int x1=240, y1=136, depx=2, depy=2;
+    int xg=0, yg=96, xd=474, yd=96;
+    int direcx=1, direcy=1;
 
+    srand(time(NULL));
     BSP_LCD_Init();
     BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
     BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
-
-    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN DEMO", CENTER_MODE);
-    HAL_Delay(1000);
-
+    
     status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
-    if (status != TS_OK) {
-        BSP_LCD_Clear(LCD_COLOR_RED);
-        BSP_LCD_SetBackColor(LCD_COLOR_RED);
-        BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
-        BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
-    } else {
-        BSP_LCD_Clear(LCD_COLOR_GREEN);
-        BSP_LCD_SetBackColor(LCD_COLOR_GREEN);
-        BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
-        BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
-    }
-
-    HAL_Delay(1000);
-    BSP_LCD_SetFont(&Font12);
-    BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
+    
+    BSP_LCD_Clear(LCD_COLOR_WHITE);
+    BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+    BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"PONG!!", CENTER_MODE);
+    HAL_Delay(2000);
+    
+    BSP_LCD_Clear(LCD_COLOR_BLACK);
+    BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
     BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
-
-    while(1) {
-
+    BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"Start ?", CENTER_MODE);
+    HAL_Delay(500);
+    
+    //Attente avant de toucher l'écran
+    while(TouchScreen){
         BSP_TS_GetState(&TS_State);
         if (TS_State.touchDetected) {
-            // Clear lines corresponding to old touches coordinates
-            if (TS_State.touchDetected < prev_nb_touches) {
-                for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
-                    BSP_LCD_ClearStringLine(idx);
-                }
-            }
-            prev_nb_touches = TS_State.touchDetected;
-
-            cleared = 0;
-
-            sprintf((char*)text, "Touches: %d", TS_State.touchDetected);
-            BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
-
-            for (idx = 0; idx < TS_State.touchDetected; idx++) {
-                x = TS_State.touchX[idx];
-                y = TS_State.touchY[idx];
-                sprintf((char*)text, "Touch %d: x=%d y=%d    ", idx+1, x, y);
-                BSP_LCD_DisplayStringAt(0, LINE(idx+1), (uint8_t *)&text, LEFT_MODE);
-            }
-
-            BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
-        } else {
-            if (!cleared) {
-                BSP_LCD_Clear(LCD_COLOR_BLUE);
-                sprintf((char*)text, "Touches: 0");
-                BSP_LCD_DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
-                cleared = 1;
-            }
+            TouchScreen = 0;
         }
     }
+    TouchScreen=1;
+    
+    //départ alétoire de la balle
+    direcx = rand() % (50+1);
+    if (direcx ==0%2) {depx=-depx;}
+    direcy = rand() % (50+1);
+    if (direcy ==0%2) {depy=-depy;}
+    
+    Terrain(xg, yg, xd, yd, x1, y1);
+    HAL_Delay(500);
+    
+    while(1){
+        
+        //Déplacement haut de la raquette gauche
+        if(d0==1 && yg>=15) yg-=5;
+        //Déplacement bas de la raquette gauche
+        else if(d2==1 && yg<=200) yg+=5;
+        
+        //Déplacement haut de la raquette droite
+        if(d7==1 && yd>=15) yd-=5;
+        //Déplacement bas de la raquette droite
+        else if(d5==1 && yd<=200) yd+=5;
+        
+        //Limite basse
+        if(y1>=264) {
+            depy=-depy;
+        }
+        
+        //Limite haute
+        else if(y1<=6) {
+            depy=-depy;
+        }
+        
+        //Limite droite
+        else if(x1>=470) {
+            //Si on touche la raquette
+            if (BSP_LCD_ReadPixel(x1+7, y1)==0xFFFFFFFF) {
+                //Accélération de la balle
+                if (depx>0 && depx<5) depx+=1;
+                else if (depx<0 && depx>-5) depx-=1;
+                depx=-depx;
+                }
+                //Si on touche le bord
+            else if (BSP_LCD_ReadPixel(x1+7, y1)==0xFF000000){
+                BSP_LCD_Clear(LCD_COLOR_WHITE);
+                BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+                BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+                BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"Rejouer ?", CENTER_MODE);
+                HAL_Delay(1000);
+                
+                while(TouchScreen){
+                    BSP_TS_GetState(&TS_State);
+                    if (TS_State.touchDetected) {
+                        TouchScreen = 0;
+                        }
+                    }
+                    TouchScreen=1;
+                    
+                //Ré-initialisation
+                x1=240;
+                y1=136;
+                xg=0;
+                yg=96;
+                xd=474;
+                yd=96;
+                depx=2;
+                depy=2;
+                
+                //Départ aléatoire
+                direcx = rand() % (50+1);
+                if (direcx ==1%2) {depx=-depx;}
+                direcy = rand() % (50+1);
+                if (direcy ==1%2) {depy=-depy;}
+                
+                BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
+                BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+                Terrain(xg, yg, xd, yd, x1, y1);
+                HAL_Delay(500);
+                }
+        }
+        
+        //Limite gauche
+        else if(x1<=10) {
+            //Si on touche la raquette
+            if (BSP_LCD_ReadPixel(x1-7, y1)==0xFFFFFFFF) {
+                //Accélération de la balle
+                if (depx>0 && depx<5) depx+=1;
+                else if (depx<0 && depx>-5) depx-=1;
+                depx=-depx;
+                }
+            //Si on touche le bord
+            else if (BSP_LCD_ReadPixel(x1-7, y1)==0xFF000000) {
+                BSP_LCD_Clear(LCD_COLOR_WHITE);
+                BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
+                BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
+                BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)"Rejouer ?", CENTER_MODE);
+                HAL_Delay(1000);
+                
+                while(TouchScreen){
+                    BSP_TS_GetState(&TS_State);
+                    if (TS_State.touchDetected) {
+                    TouchScreen = 0;
+                        }
+                    }
+                TouchScreen=1;
+                
+                //Ré-initialisation 
+                x1=240;
+                y1=136;
+                xg=0;
+                yg=96;
+                xd=474;
+                yd=96;
+                depx=2;
+                depy=2;
+                
+                //Départ aléatoire
+                direcx = rand() % (50+1);
+                if (direcx ==1%2) {depx=-depx;}
+                direcy = rand() % (50+1);
+                if (direcy ==1%2) {depy=-depy;}
+                
+                BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
+                BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
+                Terrain(xg, yg, xd, yd, x1, y1);
+                HAL_Delay(500);     
+                }
+        }
+        x1+=depx;
+        y1+=depy;
+        Terrain(xg, yg, xd, yd, x1, y1);
+    }
 }
+
+void Terrain(int xg, int yg, int xd, int yd, int x1, int y1) {
+    //Affichage de...
+    BSP_LCD_Clear(LCD_COLOR_BLACK);
+    //Raquettes
+    BSP_LCD_FillRect(xg, yg, 6, 80);
+    BSP_LCD_FillRect(xd, yd, 6, 80);
+    //Filet
+    BSP_LCD_FillRect(239, 0, 3, 271);
+    //Balle
+    BSP_LCD_FillCircle(x1, y1, 6);
+    HAL_Delay(15);
+    }
+    
\ No newline at end of file