locked
how to make a section of code a variable? RRS feed

  • Question

  • Hello i want to make a section of my code, a variable, how would i do that?
    Tuesday, May 31, 2011 2:28 PM

Answers

  • Not quite sure what you mean - perhaps yo mean how to use subroutines which are blocks of code that can be called from somewhere else - they are used to keep the code logic clear and allow repeated code to be used without actual copies of the code.

    For example, your original paddle game could use subroutines initialise() and update().

    initialise()

    RunLoop:

    update()

    Program.Delay(5)

    If (y < gh) Then
      Goto RunLoop
    EndIf

    GraphicsWindow.ShowMessage("You Lose", "Paddle")

    Sub initialise
      GraphicsWindow.BackgroundColor = "DarkBlue"
      paddle = Shapes.AddRectangle(120, 12)
      ball = Shapes.AddEllipse(16, 16)
     
      GraphicsWindow.MouseMove = OnMouseMove
     
      x = 0
      y = 0
      deltaX = 1
      deltaY = 1
    EndSub

    Sub update
      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)
    EndSub

    Sub OnMouseMove
      paddleX = GraphicsWindow.MouseX
      Shapes.Move(paddle, paddleX - 60, GraphicsWindow.Height - 12)
    EndSub

    • Proposed as answer by litdev Wednesday, June 1, 2011 7:15 PM
    • Marked as answer by litdev Friday, June 10, 2011 7:39 PM
    Tuesday, May 31, 2011 6:21 PM