none
Explaning what each section of the code does?

    Question

  • Hey everyone, i know its a simple program, but im not very experienced with programming, and i need to make a game program for school, all i want is for someone to explain what each section this code does in relation to the program. i get what some parts do, but during the runloop section, i get confused. if you can help that would be great! thanks!

    GraphicsWindow.BackgroundColor = "DarkBlue"
    paddle = Shapes.AddRectangle(120, 12)
    ball = Shapes.AddEllipse(16, 16)

    GraphicsWindow.MouseMove = OnMouseMove
     
    x = 0
    y = 0
    deltaX = 1
    deltaY = 1
     
    RunLoop:
      x = x + deltaX
      y = y + deltaY
      
      gw = GraphicsWindow.Width
      gh = GraphicsWindow.Height
      If (x >= gw - 16 or x <= 0) Then
        deltaX = -deltaX
      EndIf
      If (y <= 0) Then
        deltaY = -deltaY
      EndIf
      
      padX = Shapes.GetLeft (paddle)
      If (y = gh - 28 and x >= padX and x <= padX + 120) Then
        deltaY = -deltaY
      EndIf
      
      Shapes.Move(ball, x, y)
      Program.Delay(5)
      
      If (y < gh) Then
        Goto RunLoop
      EndIf
     
    GraphicsWindow.ShowMessage("You Lose", "Paddle")
     
    Sub OnMouseMove
      paddleX = GraphicsWindow.MouseX
      Shapes.Move(paddle, paddleX - 60, GraphicsWindow.Height - 12)
    EndSub

    Friday, May 27, 2011 2:28 PM

Answers

  • 'Set background colour and create paddle and ball shapes
    GraphicsWindow.BackgroundColor = "DarkBlue"
    paddle = Shapes.AddRectangle(120, 12)
    ball = Shapes.AddEllipse(16, 16)

    'Register mouse movement event - the subroutine OnMouseMove is called whenever the mouse moves
    GraphicsWindow.MouseMove = OnMouseMove
     
    'Initialise ball position (x,y) and its itial velocity (deltaX,deltaY)
    'Note that x increases from left to right and y increases from top to bottom so x=0,y=0 is the top left and the velocity direction is 45 degrees down and right
    x = 0
    y = 0
    deltaX = 1
    deltaY = 1
     
    'Runloop is a point where the code will return each timestep - each time all the positions are updated
    RunLoop:
    'Update the ball position variables by adding its velocity in x and y directions
      x = x + deltaX
      y = y + deltaY
     
    'Get the size of the GraphicsWindow (this could have been done before the RunLoop since it is constant and doesn't change unless the window is resized during the game!)
      gw = GraphicsWindow.Width
      gh = GraphicsWindow.Height
    'Check if the ball is beyond the left or right sides of the GraphicsWindow - if it is reverse the X direction velocity - bounce the ball off the sides
      If (x >= gw - 16 or x <= 0) Then
        deltaX = -deltaX
      EndIf
    'Do the same for the top - reverse Y direction velocity
      If (y <= 0) Then
        deltaY = -deltaY
      EndIf

    'Get the left position of the paddle and check if the ball has hit it (below it and within its X position - if it has then bounce the ball up by reversing Y velocity component)
      padX = Shapes.GetLeft (paddle)
      If (y = gh - 28 and x >= padX and x <= padX + 120) Then
        deltaY = -deltaY
      EndIf
     
    'Update the ball position - redraw or move the ball image
      Shapes.Move(ball, x, y)

    'A delay of 5 ms to keep the game running at a reasonable speed
      Program.Delay(5)
     
    'If the ball is in play then do the game loop again - if not, the ball has missed the paddle and is below it and the game ends
      If (y < gh) Then
        Goto RunLoop
      EndIf
     
    'The game has ended and show a message
    GraphicsWindow.ShowMessage("You Lose", "Paddle")
     
    'Subroutine called only when the mouse moves - it just re-positions or moves the paddle centred on the X position of the mouse - this happens independently of the main game loop, whenever the mouse moves
    Sub OnMouseMove
      paddleX = GraphicsWindow.MouseX
      Shapes.Move(paddle, paddleX - 60, GraphicsWindow.Height - 12)
    EndSub






    Friday, May 27, 2011 6:03 PM
    Moderator

All replies

  • just click on the colored words and read the text on the sidebar.

     


    Check out my website: http://k-dev.webs.com/
    Friday, May 27, 2011 4:58 PM
  • 'Set background colour and create paddle and ball shapes
    GraphicsWindow.BackgroundColor = "DarkBlue"
    paddle = Shapes.AddRectangle(120, 12)
    ball = Shapes.AddEllipse(16, 16)

    'Register mouse movement event - the subroutine OnMouseMove is called whenever the mouse moves
    GraphicsWindow.MouseMove = OnMouseMove
     
    'Initialise ball position (x,y) and its itial velocity (deltaX,deltaY)
    'Note that x increases from left to right and y increases from top to bottom so x=0,y=0 is the top left and the velocity direction is 45 degrees down and right
    x = 0
    y = 0
    deltaX = 1
    deltaY = 1
     
    'Runloop is a point where the code will return each timestep - each time all the positions are updated
    RunLoop:
    'Update the ball position variables by adding its velocity in x and y directions
      x = x + deltaX
      y = y + deltaY
     
    'Get the size of the GraphicsWindow (this could have been done before the RunLoop since it is constant and doesn't change unless the window is resized during the game!)
      gw = GraphicsWindow.Width
      gh = GraphicsWindow.Height
    'Check if the ball is beyond the left or right sides of the GraphicsWindow - if it is reverse the X direction velocity - bounce the ball off the sides
      If (x >= gw - 16 or x <= 0) Then
        deltaX = -deltaX
      EndIf
    'Do the same for the top - reverse Y direction velocity
      If (y <= 0) Then
        deltaY = -deltaY
      EndIf

    'Get the left position of the paddle and check if the ball has hit it (below it and within its X position - if it has then bounce the ball up by reversing Y velocity component)
      padX = Shapes.GetLeft (paddle)
      If (y = gh - 28 and x >= padX and x <= padX + 120) Then
        deltaY = -deltaY
      EndIf
     
    'Update the ball position - redraw or move the ball image
      Shapes.Move(ball, x, y)

    'A delay of 5 ms to keep the game running at a reasonable speed
      Program.Delay(5)
     
    'If the ball is in play then do the game loop again - if not, the ball has missed the paddle and is below it and the game ends
      If (y < gh) Then
        Goto RunLoop
      EndIf
     
    'The game has ended and show a message
    GraphicsWindow.ShowMessage("You Lose", "Paddle")
     
    'Subroutine called only when the mouse moves - it just re-positions or moves the paddle centred on the X position of the mouse - this happens independently of the main game loop, whenever the mouse moves
    Sub OnMouseMove
      paddleX = GraphicsWindow.MouseX
      Shapes.Move(paddle, paddleX - 60, GraphicsWindow.Height - 12)
    EndSub






    Friday, May 27, 2011 6:03 PM
    Moderator
  • thank you for that answer litdev! im also mixing this program and the paint program, but im trying to substitute the paddle, with the line that you draw. but i don't know how to get that to work. any suggestions?
    Tuesday, May 31, 2011 2:33 PM
  • I'm not sure what you mean 'substitute the paddle, with the line that you draw'.
    Tuesday, May 31, 2011 6:23 PM
    Moderator
  • well in the drawing program, you draw line with your mouse, and im combing the paddle game and the paint program! but instead of the ball bouncing off of the "paddle" it would bounce off of the "line that you draw" Does that clarify anything any better?
    Wednesday, June 01, 2011 2:12 AM
  • In your collision detection code, where you check for the ball going off screen, you could add additional checks for where the lines were drawn.

     You could also try using one color for the background and a second color for the line, then check what color is under the ball.  If it's the second color, the ball hit the line and should be "bounced".

    Friday, June 03, 2011 4:20 PM