Thread: MSP432 + BoosterPack; paddle and ball game

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    1

    MSP432 + BoosterPack; paddle and ball game

    I need assistance on a part of a project in working on in which a ball bounces around a led screen and you have to hit it with a paddle and have it collide. There are two buttons that have been programmed to move the paddle up and down(since the paddle is on the right side of the screen), i need help on making the paddle a tangible thing. here is my code so far. any and all help is appreciated.
    Code:
    #include <ti/devices/msp432p4xx/driverlib/driverlib.h>#include<ti/grlib/grlib.h>
    #include"LcdDriver/Crystalfontz128x128_ST7735.h"
    #include <stdint.h>
    #include <stdbool.h>
    Graphics_Context g_sContext;
    volatile uint32_t radius;
    volatile uint32_t timerflag;
    volatile uint32_t timerflag2;
    volatile uint32_t timerflag3;
    volatile uint32_t x;
    volatile uint32_t y;
    volatile int32_t Vx;
    volatile int32_t Vy;
    volatile uint32_t window_height;
    volatile uint32_t window_width;
    volatile uint32_t paddleY;
    const int32_t paddle_width= 16;
    volatile uint32_t hits;
    void draw(int32_t x,int32_t y,int32_t radius)
    {
        Graphics_fillCircle(&g_sContext,x,y,radius);
    }
    void erase(int32_t x,int32_t y,int32_t radius)
    {
        Graphics_setForegroundColor(&g_sContext,GRAPHICS_COLOR_WHITE);
        Graphics_fillCircle(&g_sContext,x,y,radius);
        Graphics_setForegroundColor(&g_sContext,GRAPHICS_COLOR_RED);
    }
    void erase_paddle(int32_t x, int32_t y)
    {
        Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_WHITE);
        Graphics_drawLineV(&g_sContext, x, y, y + paddle_width -1);
        Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_RED);
    }
    void draw_paddle(int32_t x, int32_t y)
    {
        Graphics_drawLineV(&g_sContext, x, y, y + paddle_width -1);
    }
    int main(void)
    {
    
    
    SysTick_enableModule();
    
    
    GPIO_setAsInputPin(GPIO_PORT_P5, GPIO_PIN1);
    GPIO_setAsInputPin(GPIO_PORT_P3, GPIO_PIN5);
    GPIO_clearInterruptFlag(GPIO_PORT_P5, GPIO_PIN1);
    GPIO_clearInterruptFlag(GPIO_PORT_P3, GPIO_PIN5);
    GPIO_interruptEdgeSelect(GPIO_PORT_P5, GPIO_PIN1,GPIO_HIGH_TO_LOW_TRANSITION);
    GPIO_interruptEdgeSelect(GPIO_PORT_P3, GPIO_PIN5,GPIO_HIGH_TO_LOW_TRANSITION);
    GPIO_enableInterrupt(GPIO_PORT_P5, GPIO_PIN1);
    GPIO_enableInterrupt(GPIO_PORT_P3, GPIO_PIN5);
    Interrupt_enableInterrupt(INT_PORT5);
    Interrupt_enableInterrupt(INT_PORT3);
    Interrupt_enableMaster();
    
    
    
    
    Crystalfontz128x128_Init();
    Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP);
    Graphics_initContext(&g_sContext,&g_sCrystalfontz128x128,&g_sCrystalfontz128x128_funcs);
    Graphics_setForegroundColor(&g_sContext,GRAPHICS_COLOR_RED);
    Graphics_setBackgroundColor(&g_sContext,GRAPHICS_COLOR_WHITE);
    GrContextFontSet(&g_sContext,&g_sFontFixed6x8);
    Graphics_clearDisplay(&g_sContext);
    
    
    
    
    MAP_WDT_A_holdTimer();
    
    
    y = 64;
    x = 64;
    Vx = 1;
    Vy =-1;
    radius = 3;
    window_height = 127;
    window_width = 127;
    paddleY = 64;
     draw_paddle(120,paddleY);
    SysTick_setPeriod(90000);
    SysTick_enableInterrupt();
    while(1)
            {
              if(x <= 0 || x >= window_width-5)
                      Vx*= -1;
          if(y <= 0 || y >= window_height -5)
              Vy*= -1;
    
    
    if(timerflag==1)
    {
        draw(x,y,radius);
        erase(x,y,radius);
    
    
        timerflag = 0;
    }
    if(timerflag2==1)
    {
            erase_paddle(120,paddleY+paddle_width+1);
            draw_paddle(120,paddleY);
            timerflag2 = 0;
    }
    if(timerflag3==1)
    {
        erase_paddle(120,paddleY-paddle_width+1);
               draw_paddle(120,paddleY);
               timerflag3 = 0;
    }
    
    
    
    
    
    
            }
    
    
    
    
    
    
    
    
    }
    void SysTick_Handler(void)
    {
        timerflag = 1;
        if(x+radius <127 || x-radius>0)
        {
            if(y+radius <127 || y-radius>0)
            {
                x+=Vx;
                       y+=Vy;
            }
    
    
    }
        if(x+radius==127 || x-radius==0)
        {
            Vx*= -1;
        }
        if(y+radius ==127 || y-radius==0)
        {
            Vy*= -1;
        }
    }
    
    
    void PORT5_IRQHandler(void)
    {
        uint32_t status;
        status= GPIO_getEnabledInterruptStatus(GPIO_PORT_P5);
        GPIO_clearInterruptFlag(GPIO_PORT_P5, status);
        timerflag2=1;
        if(status & GPIO_PIN1)
        {
            timerflag2=1;
            paddleY-= paddle_width-1;
    
    
        }
    }
        void PORT3_IRQHandler(void)
        {
        uint32_t status;
           status= GPIO_getEnabledInterruptStatus(GPIO_PORT_P3);
           GPIO_clearInterruptFlag(GPIO_PORT_P3, status);
    
    
           if(status & GPIO_PIN5)
           {
               timerflag3=1;
                      paddleY+= paddle_width-1;
           }
         }
    Last edited by Solnoid; 06-05-2019 at 07:05 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Indentation style - Wikipedia
    It's too hard to follow the code as presented on this forum.

    Tip: Don't mix spaces and tabs in your code. Your editor might make sense of it, but most other tools will make a mess of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    214
    I'm with @Salem, most of us have trouble diving in as if by your side at the chair - we need something more obvious to deal with.

    That said, I focus on this question:

    i need help on making the paddle a tangible thing.
    So far as I can tell, the paddle is nothing more than two points (x and y), but they aren't associated with each other. I'd suggest creating a struct, put the x/y position there, and then anything else which is part of the concept of a paddle.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Indented, and some notes.
    Code:
    #include <ti/devices/msp432p4xx/driverlib/driverlib.h>#include<ti/grlib/grlib.h>
    #include"LcdDriver/Crystalfontz128x128_ST7735.h"
    #include <stdint.h>
    #include <stdbool.h>
    
    Graphics_Context g_sContext;
    volatile uint32_t radius;
    volatile uint32_t timerflag;
    volatile uint32_t timerflag2;
    volatile uint32_t timerflag3;
    volatile uint32_t x;
    volatile uint32_t y;
    volatile int32_t Vx;
    volatile int32_t Vy;
    volatile uint32_t window_height;
    volatile uint32_t window_width;
    volatile uint32_t paddleY;
    const int32_t paddle_width = 16;
    volatile uint32_t hits;
    
    // Make use of these
    struct ball_t {
      int     x;
      int     y;
      int     radius;
      int     vx;
      int     vy;
    } gBall;
    
    void draw_ball(int32_t x, int32_t y, int32_t radius)
    {
      Graphics_fillCircle(&g_sContext, x, y, radius);
    }
    void erase_ball(int32_t x, int32_t y, int32_t radius)
    {
      Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_WHITE);
      Graphics_fillCircle(&g_sContext, x, y, radius);
      Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_RED);
    }
    // This is called on timer interrupt
    void move_ball() {
      timerflag = 1;
      if (x + radius < 127 || x - radius > 0) {
        if (y + radius < 127 || y - radius > 0) {
          x += Vx;
          y += Vy;
        }
      }
      if (x + radius == 127 || x - radius == 0) {
        Vx *= -1;
      }
      if (y + radius == 127 || y - radius == 0) {
        Vy *= -1;
      }
    }
    
    // Make use of these
    struct paddle_t {
      int     x;
      int     y;
      int     width;
    } gPaddle;
    
    void draw_paddle(int32_t x, int32_t y)
    {
      Graphics_drawLineV(&g_sContext, x, y, y + paddle_width - 1);
    }
    void erase_paddle(int32_t x, int32_t y)
    {
      Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_WHITE);
      Graphics_drawLineV(&g_sContext, x, y, y + paddle_width - 1);
      Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_RED);
    }
    // This is called on pin interrupt
    void move_paddle_up() {
      uint32_t status;
      status = GPIO_getEnabledInterruptStatus(GPIO_PORT_P5);
      GPIO_clearInterruptFlag(GPIO_PORT_P5, status);
      timerflag2 = 1;
      if (status & GPIO_PIN1) {
        timerflag2 = 1;
        paddleY -= paddle_width - 1;
      }
    }
    // This is called on pin interrupt
    void move_paddle_down() {
      uint32_t status;
      status = GPIO_getEnabledInterruptStatus(GPIO_PORT_P3);
      GPIO_clearInterruptFlag(GPIO_PORT_P3, status);
    
      if (status & GPIO_PIN5) {
        timerflag3 = 1;
        paddleY += paddle_width - 1;
      }
    }
    
    
    int main(void)
    {
      SysTick_enableModule();
    
      GPIO_setAsInputPin(GPIO_PORT_P5, GPIO_PIN1);
      GPIO_setAsInputPin(GPIO_PORT_P3, GPIO_PIN5);
      GPIO_clearInterruptFlag(GPIO_PORT_P5, GPIO_PIN1);
      GPIO_clearInterruptFlag(GPIO_PORT_P3, GPIO_PIN5);
      GPIO_interruptEdgeSelect(GPIO_PORT_P5, GPIO_PIN1, GPIO_HIGH_TO_LOW_TRANSITION);
      GPIO_interruptEdgeSelect(GPIO_PORT_P3, GPIO_PIN5, GPIO_HIGH_TO_LOW_TRANSITION);
      GPIO_enableInterrupt(GPIO_PORT_P5, GPIO_PIN1);
      GPIO_enableInterrupt(GPIO_PORT_P3, GPIO_PIN5);
    
      Interrupt_enableInterrupt(INT_PORT5);
      Interrupt_enableInterrupt(INT_PORT3);
      Interrupt_enableMaster();
    
      Crystalfontz128x128_Init();
      Crystalfontz128x128_SetOrientation(LCD_ORIENTATION_UP);
      Graphics_initContext(&g_sContext, &g_sCrystalfontz128x128, &g_sCrystalfontz128x128_funcs);
      Graphics_setForegroundColor(&g_sContext, GRAPHICS_COLOR_RED);
      Graphics_setBackgroundColor(&g_sContext, GRAPHICS_COLOR_WHITE);
      GrContextFontSet(&g_sContext, &g_sFontFixed6x8);
      Graphics_clearDisplay(&g_sContext);
    
      MAP_WDT_A_holdTimer();
    
      y = 64;
      x = 64;
      Vx = 1;
      Vy = -1;
      radius = 3;
      window_height = 127;
      window_width = 127;
      paddleY = 64;
      draw_paddle(120, paddleY);
      SysTick_setPeriod(90000);
      SysTick_enableInterrupt();
    
      while (1) {
        if (x <= 0 || x >= window_width - 5)
          Vx *= -1;
        if (y <= 0 || y >= window_height - 5)
          Vy *= -1;
    
        if (timerflag == 1) {
          draw_ball(x, y, radius);
          erase_ball(x, y, radius);
          timerflag = 0;
        }
        if (timerflag2 == 1) {
          erase_paddle(120, paddleY + paddle_width + 1);
          draw_paddle(120, paddleY);
          timerflag2 = 0;
        }
        if (timerflag3 == 1) {
          erase_paddle(120, paddleY - paddle_width + 1);
          draw_paddle(120, paddleY);
          timerflag3 = 0;
        }
      }
    }
    
    void SysTick_Handler(void)
    {
      move_ball();
    }
    
    void PORT5_IRQHandler(void)
    {
      move_paddle_up();
    }
    
    void PORT3_IRQHandler(void)
    {
      move_paddle_down();
    }
    1. Things like SysTick_Handler and PORT3_IRQHandler might be what you have to call them, but they're not meaningful to what you want to do.
    So immediately call a function with a much better name.

    2. You have way too many global variables.
    x,y,radius are pretty meaningless.
    Having parameters of the same name in say draw_ball() just adds to that confusion.

    3. What is the relationship between the magic number 120 and the window size (127)

    4. What is the relationship between -5 and say the radius of the ball (3)

    5. The first part of your while(1) loop seems to duplicate the first part of move_ball(). Decide who is solely responsible for boundary checking.

    6. Equality tests for boundaries are not stable. Use either < for inbound, or >= for outbound.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ball keeps bouncing off paddle in same direction
    By arortell in forum C Programming
    Replies: 7
    Last Post: 04-30-2014, 02:51 PM
  2. Ball bouncing in Breakout-like game
    By redbull2020 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2011, 12:57 PM
  3. 3D ball problem
    By software tester in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2011, 03:54 PM
  4. The Old Bouncing Ball
    By bartybasher in forum Game Programming
    Replies: 3
    Last Post: 08-19-2003, 03:06 AM

Tags for this Thread