• ### Question

• Recently, I have been programming small games. One of the things I do to learn techniques and get ideas is look at samples. One of the samples I am currently studying is a paddle game. Below is the original code to the game. I understand everything here, except when it comes to changing the value of "deltaX". I understand that the game runs a loop and reads the conditions constantly. When the conditions are met the values of "deltaX" change. For example, "x" starts with the value of zero and "deltaX" starts with the value of one. Inside the loop, deltaX is add on to "x" so it creates the illusion that the ball is moving. When "x" is greater than or equal to gw-16 then it will change the value of "deltaX" to "-deltaX" which is -1. Now, the part I don't understand is how does the value of "deltaX" changes back to 1. I know that it returns to the value of positive one because the ball moves in the opposite direction. Also, I inputted several TextWindow.WriteLine() to see the value of deltaX and noticed that the value of one starts at positive one then negative. From there, it switches back and forth between  positive and negative.

```Dark blue background, paddle, & ball
GraphicsWindow.BackgroundColor = "DarkBlue"

GraphicsWindow.MouseMove = OnMouseMove

x = 0
y = 0
deltaX = 1
deltaY = 1

RunLoop:
x = x + deltaX
y = y + deltaY

gw = GraphicsWindow.Width '624
gh = GraphicsWindow.Height '441

If (x >= gw - 16 Or x <= 0) Then '16 rep. size of ball
deltaX = -deltaX
EndIf

If ( y <= 0) Then
deltaY = -deltaY
EndIf

If (y = gh - 28 And x >= padX And x <= padX + 120) Then '120 size of the paddle & 28 is distance between the paddle and bottom of screen
deltaY = -deltaY
EndIf

'Movement of ball
Shapes.Move(ball,x,y)
Program.Delay(5)

If (y < gh) Then
Goto RunLoop
EndIf

'Message: if game is over

'control: Moves paddle with mouse (on x axis)
Sub OnMouseMove
EndSub```

Monday, August 25, 2014 6:36 AM

• Ezra,

The answer lies in the following part:

```If (x >= gw - 16 Or x <= 0) Then '16 rep. size of ball
deltaX = -deltaX
EndIfIf  this If statement is true, deltaX changes sign. So deltaX=-1 becomes deltaX= - (-1) , which is +1.```

Jan [ WhTurner ] The Netherlands

• Edited by Monday, August 25, 2014 9:30 AM
• Marked as answer by Tuesday, August 26, 2014 12:57 AM
Monday, August 25, 2014 9:28 AM
• A negative times a negative equals a positive

-x * -x = x

So if Deltax = -1 and you do a Deltax = -Deltax Then Deltax = 1

It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

• Marked as answer by Tuesday, August 26, 2014 12:57 AM
Monday, August 25, 2014 10:26 PM

### All replies

• Ezra,

The answer lies in the following part:

```If (x >= gw - 16 Or x <= 0) Then '16 rep. size of ball
deltaX = -deltaX
EndIfIf  this If statement is true, deltaX changes sign. So deltaX=-1 becomes deltaX= - (-1) , which is +1.```

Jan [ WhTurner ] The Netherlands

• Edited by Monday, August 25, 2014 9:30 AM
• Marked as answer by Tuesday, August 26, 2014 12:57 AM
Monday, August 25, 2014 9:28 AM
• A negative times a negative equals a positive

-x * -x = x

So if Deltax = -1 and you do a Deltax = -Deltax Then Deltax = 1

It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

• Marked as answer by Tuesday, August 26, 2014 12:57 AM
Monday, August 25, 2014 10:26 PM
• I see. . . . so the program does this by multiplying the previous value of deltaX and the new deltaX?
Monday, August 25, 2014 11:15 PM
• Ezra, it is not multiplication. It is taking the negative of a negative number.  So  - ( -18 )  equals + 18.

If you want to see it as multiplication, it is not  -deltaX*-deltaX  but it is  -1 * -deltaX which is + deltaX

Jan [ WhTurner ] The Netherlands

Tuesday, August 26, 2014 10:29 AM
• Okay, I've given it more thought. I look at your first response and I think I get it now. The initial value of deltaX is 1 so ball moves at an increment of 1. If x coordinate of the ball is greater than or equal to gw-16 then deltaX = -deltaX, which looks like this deltaX = -1(deltaX) or -1(1):  -1.  Do I have the format right?
Wednesday, August 27, 2014 1:03 AM