locked
Help with any issues i have in my code? RRS feed

  • Question

  • This code is The paddle game, and the paint program from the small basic tutorial combined , and what i'm trying to accomplish is instead of using the paddle to hit the ball, and guide it, i want to use the line that you draw with the paint program to guide the ball. i got the ball to respond exactly where i wanted it to and to go straight down, but when i hold down the left mouse button to draw a line, it won't draw. any suggestions on how to fix this? And if there is something i should add to the program, please let me know :) Thank you!

    - Jasper Heins

     

    GraphicsWindow.BackgroundColor = "#00FFFF"
    GraphicsWindow.Title = "My game"
    GraphicsWindow.Width = 1250
    GraphicsWindow.Height = 800


    ball = Shapes.AddEllipse(25, 25)

    GraphicsWindow.DrawLine = line  Yes i do realize that this is not right.
    x = 625
    y = 200
    deltaX = 0
    deltaY = 2



    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(line)
      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
     

    guider:
    GraphicsWindow.PenColor = "#8AFB17"

    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseMove = OnMouseMove

    Sub OnMouseDown
      prevX = GraphicsWindow.MouseX
      prevY = GraphicsWindow.MouseY
    EndSub
     
    Sub OnMouseMove
      x = GraphicsWindow.MouseX
      y = GraphicsWindow.MouseY
      If (Mouse.IsLeftButtonDown) Then
        GraphicsWindow.DrawLine(prevX, prevY, x, y)
      EndIf
      prevX = x
      prevY = y
    EndSub


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

    Wednesday, June 1, 2011 2:22 PM

Answers

  • Suggestions - things to consider.

    1] You should register the mouse events somewhere near the start - as it is it doesn't happen until after the game is lost.

    Move GraphicsWindow.MouseDown = OnMouseDown etc near the top before the main loop

    2] Be careful using the variables x and y for different things in different places (are they the ball or line position).

    3] Sort out variable names for the start and end points of the line - these will be used to check for a collision.

    Consider if the line is erased after each bounce (how will you do this) - is the line drawn on the background or is it a Shape - does the line have a maximum length - is it horizontal - is it straight - does it have to be near the bottom.

    Have a think about these and try some stuff out and post back if you have problems - try to keep the program short and well structured (and use plenty of COMMENTS) and just doing the functional bits until this is sorted, then add details.

    About structure - consider breaking the program into 4 sections (INITIALISATION, MAIN LOOP, SUBROUTINES, EVENT SUBROUTINES) with big comments like below separating them - this helps keep the right bits in the right places and will make the program easier to develop and debug.

    '=========================================================

    '                                                                  INITIALISATION

    '=========================================================


    • Marked as answer by litdev Friday, June 10, 2011 7:42 PM
    Wednesday, June 1, 2011 5:45 PM

All replies

  • Suggestions - things to consider.

    1] You should register the mouse events somewhere near the start - as it is it doesn't happen until after the game is lost.

    Move GraphicsWindow.MouseDown = OnMouseDown etc near the top before the main loop

    2] Be careful using the variables x and y for different things in different places (are they the ball or line position).

    3] Sort out variable names for the start and end points of the line - these will be used to check for a collision.

    Consider if the line is erased after each bounce (how will you do this) - is the line drawn on the background or is it a Shape - does the line have a maximum length - is it horizontal - is it straight - does it have to be near the bottom.

    Have a think about these and try some stuff out and post back if you have problems - try to keep the program short and well structured (and use plenty of COMMENTS) and just doing the functional bits until this is sorted, then add details.

    About structure - consider breaking the program into 4 sections (INITIALISATION, MAIN LOOP, SUBROUTINES, EVENT SUBROUTINES) with big comments like below separating them - this helps keep the right bits in the right places and will make the program easier to develop and debug.

    '=========================================================

    '                                                                  INITIALISATION

    '=========================================================


    • Marked as answer by litdev Friday, June 10, 2011 7:42 PM
    Wednesday, June 1, 2011 5:45 PM
  • Wow! Thank you so much for all your help! i really appreciate it! :)
    Thursday, June 2, 2011 2:03 PM
  • Now this is what i have so far. it all works perfectly, accept for the fact that the ball won't bounce off of the line that i draw, please give any suggestions, or any fixations i could do. Thank you!

    INITIALISE:
    GraphicsWindow.BackgroundColor = "yellow"

    GraphicsWindow.Title = "My game"
    GraphicsWindow.Width = 1250
    GraphicsWindow.Height = 800

    GraphicsWindow.MouseMove = OnMouseMove
    GraphicsWindow.MouseDown = OnMouseDown

    ball = Shapes.AddEllipse(25, 25)
     
    line = Shapes.AddLine(prevW, prevZ, w, z)
    x = 625
    y = 200
    deltaX = 0
    deltaY = 2



    MAINLOOP:
    x = x + deltaX
    y = y + deltaY

    gw = GraphicsWindow.Width
    gh = GraphicsWindow.Height


     
      padX = Shapes.GetLeft(line)
      If (y = gh - 24 and x >= padX and x <= padX + 120) Then
        deltaY = -deltaY
      EndIf
     
      Shapes.Move(ball, x, y)
      Program.Delay(5)
     
      If (y < gh) Then
      Goto MAINLOOP
    endif

    If (y > gh) then
    Goto MAINLOOP
    endif

      If (x < gw) then
      Goto MAINLOOP
    EndIf

    If (x > gw) then
    Goto MAINLOOP
    endif
        
     

    GUIDER:
    GraphicsWindow.PenColor = "#8AFB17"



    Sub OnMouseDown
      prevW = GraphicsWindow.MouseX
      prevZ = GraphicsWindow.MouseY
    EndSub
     
    Sub OnMouseMove
      w = GraphicsWindow.MouseX
      z = GraphicsWindow.MouseY
      If (Mouse.IsLeftButtonDown) Then
        GraphicsWindow.DrawLine(prevW, prevZ, w, z)
      EndIf
      prevW = w
      prevZ = z
    EndSub


    Thursday, June 2, 2011 2:45 PM
  • Good progress.  I added variables for the start and end of your line (startX,startY) and (endX,endY).  We can use these to check for a collision.  You need to put the top bottom bounces back in etc.

    INITIALISE:
    GraphicsWindow.BackgroundColor = "yellow"

    GraphicsWindow.Title = "My game"
    GraphicsWindow.Width = 1250
    GraphicsWindow.Height = 800

    GraphicsWindow.MouseMove = OnMouseMove
    GraphicsWindow.MouseDown = OnMouseDown

    ball = Shapes.AddEllipse(25, 25)

    line = Shapes.AddLine(prevW, prevZ, w, z)
    x = 625
    y = 200
    deltaX = 0
    deltaY = 2



    MAINLOOP:
    x = x + deltaX
    y = y + deltaY

    gw = GraphicsWindow.Width
    gh = GraphicsWindow.Height



    'padX = Shapes.GetLeft(line)
    'If (y = gh - 24 and x >= padX and x <= padX + 120) Then
    '  deltaY = -deltaY
    'EndIf

    'Check for bounce on line drawn
    lineY = (startY+endY)/2 ' average height
    minX = Math.Min(startX,endX) 'Smallest (left) side of line
    maxX = Math.Max(startX,endX) 'Right side of line

    If (x >= minX And x <= maxX And y-deltaY <= y And y >= lineY) Then ' Cross in line from above
      deltaY = -deltaY
    EndIf

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

    If (y < gh) Then
      Goto MAINLOOP
    endif

    If (y > gh) then
      Goto MAINLOOP
    endif

    If (x < gw) then
      Goto MAINLOOP
    EndIf

    If (x > gw) then
      Goto MAINLOOP
    endif



    GUIDER:
    GraphicsWindow.PenColor = "#8AFB17"



    Sub OnMouseDown
      prevW = GraphicsWindow.MouseX
      prevZ = GraphicsWindow.MouseY
      startX = prevW
      startY = prevZ
    EndSub

    Sub OnMouseMove
      w = GraphicsWindow.MouseX
      z = GraphicsWindow.MouseY
      If (Mouse.IsLeftButtonDown) Then
        GraphicsWindow.DrawLine(prevW, prevZ, w, z)
        endX = w
        endY = z
      EndIf
      prevW = w
      prevZ = z
    EndSub



    Thursday, June 2, 2011 6:11 PM