Hello everyone

In our last lesson we created our Pong table, the paddle and the ball. When the ball struck the paddle, it bounced only on the horizontal plane. Today, we are going to make the ball bounce at an angle. Watch the short video to see how it works and to get an overview of what we will discuss.

OK  now that we have seen the game in action lets look at the code. To start we need to know the edges of our table as well as the position of the paddle, the length of the paddle and the position of the ball on the vertical and horizontal plane at all times on the screen.

Paddle

  • Width = .3
  • Height = 1.0
  • Left = 1

Table

  • Left = 1
  • Top = .5
  • Right = 7.9
  • Bottom = 5.9

Ball

  • Width = .4
  • Height = .35

Each of the values above are the property values for the specific objects. We also need to know the angle the ball will move based on where it strikes the paddle and the previous angle when it hits one of the table edges. We set these values by making the vertical position change a portion of the horizontal position change. Each time the timer event fires, the ball moves horizontally by  .10 . Vertically we move the ball by

  • .15 –   Angle =   67.5 degrees
  • .05 –  Angle =   22.5 degrees
  • 0
  • -.05  – Angle = 337.5 degrees
  • -.15 –  Angle = 297.5 degrees

Here is the actual code;

if dl = "Bounce Ball" then 'test if timer should run
    xBPos = topparent:Ball1.Object.Left
    if xBPos <=1 then
        topparent:Ball1.Object.Visible  = .f.
        dl = ""
    end if    
    'Set Primary Ball and Paddle Positions based on previous position
    xBPos = topparent:Ball1.Object.Left
    xBTop = topparent:Ball1.Object.Top +.15
    xTxtTop = topparent:Text1.Object.Top
    xTxtBot = xTxtTop+topparent:Text1.Object.Height
    xLen = Text1.Object.Height
    if xDir = "Left" then 'Set Values at Paddle stirke
        if ( xBPos > 1.0 .and. xBPos <=1.3 ).and. ( xBTop >= xTxtTop ) .and. ( xBTop <= xTxtTop + (xLen*.25) ) then 
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top - .10
            xDir = "Right"
            xAng = .20
        else if ( xBPos > 1.0 .and. xBPos <=1.3 ).and. ( xBTop >= xTxtTop + (xlen*.26) ) .and. ( xBTop <= xTxtTop + (xLen*.47) ) then
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top - .05
            xDir = "Right"
            xAng = .10
        else if ( xBPos > 1.0 .and. xBPos <=1.3 ).and. ( xBTop >= xTxtTop + (xlen*.49) ) .and. ( xBTop < xTxtTop + (xLen*.53) ) then
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top + 0
            xDir = "Right"
            xAng = 0
        else if ( xBPos > 1.0 .and. xBPos <=1.3 ).and. ( xBTop >= xTxtTop + (xlen*.53) ) .and. ( xBTop <= xTxtTop + (xLen*.75) ) then
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top +.05
            xDir = "Right"
            xAng = -.10
        else if ( xBPos > 1.0 .and. xBPos <=1.3 ).and. ( xBTop > xTxtTop + (xlen*.75) ) .and. ( xBTop <= xTxtBot ) then
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top +.10
            xDir = "Right"
            xAng = -.20
        end if 
        goto dir_left

    else 'Set Values at Right Wall Strike.
        if ( xBPos >= 7.9) .and. xAng = 0.2 then 
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top - .10
            xDir = "Left"
            xAng = -.20
        else if ( xBPos >= 7.9) .and. xAng = 0.1 then 
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top - .05
            xDir = "Left"
            xAng = -.10
        else if ( xBPos >= 7.9) .and. xAng = 0 then 
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top
            xDir = "Left"
            xAng = 0
        else if ( xBPos >= 7.9) .and. xAng = -0.1 then 
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top + .05
            xDir = "Left"
            xAng = .10
        else if ( xBPos >= 7.9) .and. xAng = -0.2 then 
            topparent:BALL1.Object.Top = topparent:BALL1.Object.Top + .10
            xDir = "Left"
            xAng = .20
        end if 
        goto dir_right
    end if        
dir_right: ' Ball is moving Left
xBPos = xBPos+.10
if topparent:BALL1.Object.Top <= .5 .or. topparent:BALL1.Object.Top >= 5.9 then
    xAng = (xAng * -1)
end if
if xAng = 0.2 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top - .15
else if xAng = 0.1 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top - .05
else if xAng = 0 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top 
else if xAng = -0.1 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top + .05
else if xAng = -0.2 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top + .15
end if
goto done


dir_Left: 'Ball is moving right
xBPos = xBPos-.10
if topparent:BALL1.Object.Top <= .5 .or. topparent:BALL1.Object.Top >= 5.9 then
    xAng = (xAng * -1)
end if
if xAng = 0.2 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top + .15
else if xAng = 0.1 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top + .05
else if xAng = 0 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top 
else if xAng = -0.1 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top - .05
else if xAng = -0.2 then
    topparent:BALL1.Object.Top = topparent:BALL1.Object.Top - .15
end if
goto done

end if
done:
topparent:Ball1.Object.Left = xBPos

The trick is to make the angle change based on which surface it strikes and at which angle. This is done by identifing if the ball exceeds the table edge for the top, bottom or right side then multiplying the current value of xAng  times minus one. This makes the plus a negative and the negative a plus. (Seventh grade math finally came in handy.)

Now you have a pong game. If you like you can jazz it up with audible tones, exploding box objects, variable speed of the ball movement and anything else you can think of.

Well that’s it for today. I hope you found this lesson helpful and I hope you enjoy the rest of this series. As I mentioned in the video, our next lesson will be on creating a cad style design program in Alpha Software.

Remember, if you need help with an Alpha Software application or wish to inquire about a custom application for your business go to our website

www.cdc-takecharge.com

and inquire or contact

NLawson@cdc-TakeCharge.com

Have a great day.