none
Make the program wait for my click? RRS feed

  • Question

  • Ok look at this:

    For p = 1 To 2
      For Turn = 1 To 3
        For Put = 1 To Turn
          SetDown()
        EndFor
      EndFor
    EndFor

    I am doing that game Battleship. That little thing up there must do this:

    - Progressively player 1 and 2 (p) will be asked where they put their ships.

    - Both put, 1 time ship type1. 2 times ships type2 and at the last 3 times ships type3 on their boards.

     

    Ok that SetDown is a Subroutine. In the other parts of the program i created buttons in each spot.

    So i am getting a bit of trouble making this sub which has to do something like this...

    - Controls.LastClickedButton , if that value in the button is right, then it will say "You hit it" or if not then "Missed". whatever.

    Now I don't know how SmallBasic works (still noob). i want the program to wait for the Click to set down Ship1, then after the Click pass to the next waiting for the next ship. You get what i mean.

     

    ^^ Can anyone who is more experienced give me a little help?

    A bit first I did this to put X value everywhere:

    For p = 1 To 2
      For i = 1 To 10
        For j = 1 To 10
          Boxes[p][i][j] = Controls.AddButton("X",i*width + Positionx[p],j*height)
        EndFor
      EndFor
    EndFor

     

    So i would like it if u give me an answer using arrays and so, that's the idea i had to use, but am blocked here... xD how the heck can i make the program wait for my Clicks on the buttons?

    anyway i can use the Turn variable to modify also the name of the boat that will be inserted. ( Ship[True] ) using that stupid loop there. xD

     

    Thanks to everyone helping. :)

     

     

     

    Thursday, February 10, 2011 5:38 PM

Answers

  • The SetDown routine can wait for a click, then update the box:

    Sub SetDown
      clicked = 0
      While (clicked = 0) 'Wait for box to be found
        Program.Delay(100)
      EndWhile
      If (player = p And Controls.GetButtonCaption(box) = " ") Then
        Controls.SetButtonCaption(box,Turn+"_"+Put) 'Valid Go
      Else
        SetDown() 'Invalid go - try again
      EndIf
    EndSub


    This is checking whether a box is clicked (clicked ) and which box it is (box ) and what player that correspnds to (p ) - this is set in the event:

    Sub OnButtonClicked
      box = Controls.LastClickedButton 'box clicked
      clicked = 1 'Flag to show we have a box
      'Find which p,i,j the box was
      For p = 1 To 2
        For i = 1 To 10
          For j = 1 To 10
            If (box = Boxes[p][i][j]) Then
              Goto Found
            EndIf
          EndFor
        EndFor
      EndFor
      Found: 'box found
    EndSub 

    Now all we need to initialise the boxes, and start the event, then cycle through the setup.  Here it is together, easier with an example rather than trying to describe.

    'Create all the positions
    width = 40
    height = 30
    GraphicsWindow.Width = 10*2*width+width
    GraphicsWindow.Height = 10*height
    For p = 1 To 2
      Positionx[p] = (p-1)*11*width
      For i = 1 To 10
        For j = 1 To 10
          Boxes[p][i][j] = Controls.AddButton(" ",(i-1)*width+Positionx[p],(j-1)*height) 'Blank box at first
        EndFor
      EndFor
    EndFor

    'Start event to check for buttons clicked
    Controls.ButtonClicked = OnButtonClicked

    'Now put the position's in
    For Player = 1 To 2
      For Turn = 1 To 3
        For Put = 1 To Turn
          GraphicsWindow.Title = "Player "+Player+" Turn "+Turn+" Put "+Put
          SetDown()
        EndFor
      EndFor
    EndFor

    Sub SetDown
      clicked = 0
      While (clicked = 0) 'Wait for box to be found
        Program.Delay(100)
      EndWhile
      If (player = p And Controls.GetButtonCaption(box) = " ") Then
        Controls.SetButtonCaption(box,Turn+"_"+Put) 'Valid Go
      Else
        SetDown() 'Invalid go - try again
      EndIf
    EndSub

    Sub OnButtonClicked
      box = Controls.LastClickedButton 'box clicked
      clicked = 1 'Flag to show we have a box
      'Find which p,i,j the box was
      For p = 1 To 2
        For i = 1 To 10
          For j = 1 To 10
            If (box = Boxes[p][i][j]) Then
              Goto Found
            EndIf
          EndFor
        EndFor
      EndFor
      Found: 'box found
    EndSub

    Thursday, February 10, 2011 8:53 PM
    Moderator

All replies

  • You can use that code in a subroutine:

    GraphicsWindow.MouseDown = MouseDown 'Use the event to wait for the mouseclick
    Sub MouseDown
        For p = 1 To 2
            For i = 1 To 10
                For j = 1 To 10
                    Boxes[p][i][j] = Controls.AddButton("X",i*width + Positionx[p],j*height)
                EndFor
            EndFor
        EndFor
    EndSub

    ~~AirWaves!!~~
    Thursday, February 10, 2011 5:58 PM
  • Hmm, well yea.. thx but i think does not help me at all, it seems that when i click down the mouse button, it's like the asking starts, but i need after the Mouse clicking to choose where too shot when the game will proceed. I already know most of the stuff i need to do, i got the idea, but my question would be this, Can you give me an example of Sub to put in my For there?

    I mean i was going on with this game coding... and then the dilemma... ok how the heck can i make the program stop for a moment in that moment in the inner of For and wait for my click (When clicking i will think about how to make something, memorize the position of the clicked button) but for now i am still blocked at this point... xD sry, noob me. But ty anyway.

    HEELP! :\

    Thursday, February 10, 2011 7:35 PM
  • The SetDown routine can wait for a click, then update the box:

    Sub SetDown
      clicked = 0
      While (clicked = 0) 'Wait for box to be found
        Program.Delay(100)
      EndWhile
      If (player = p And Controls.GetButtonCaption(box) = " ") Then
        Controls.SetButtonCaption(box,Turn+"_"+Put) 'Valid Go
      Else
        SetDown() 'Invalid go - try again
      EndIf
    EndSub


    This is checking whether a box is clicked (clicked ) and which box it is (box ) and what player that correspnds to (p ) - this is set in the event:

    Sub OnButtonClicked
      box = Controls.LastClickedButton 'box clicked
      clicked = 1 'Flag to show we have a box
      'Find which p,i,j the box was
      For p = 1 To 2
        For i = 1 To 10
          For j = 1 To 10
            If (box = Boxes[p][i][j]) Then
              Goto Found
            EndIf
          EndFor
        EndFor
      EndFor
      Found: 'box found
    EndSub 

    Now all we need to initialise the boxes, and start the event, then cycle through the setup.  Here it is together, easier with an example rather than trying to describe.

    'Create all the positions
    width = 40
    height = 30
    GraphicsWindow.Width = 10*2*width+width
    GraphicsWindow.Height = 10*height
    For p = 1 To 2
      Positionx[p] = (p-1)*11*width
      For i = 1 To 10
        For j = 1 To 10
          Boxes[p][i][j] = Controls.AddButton(" ",(i-1)*width+Positionx[p],(j-1)*height) 'Blank box at first
        EndFor
      EndFor
    EndFor

    'Start event to check for buttons clicked
    Controls.ButtonClicked = OnButtonClicked

    'Now put the position's in
    For Player = 1 To 2
      For Turn = 1 To 3
        For Put = 1 To Turn
          GraphicsWindow.Title = "Player "+Player+" Turn "+Turn+" Put "+Put
          SetDown()
        EndFor
      EndFor
    EndFor

    Sub SetDown
      clicked = 0
      While (clicked = 0) 'Wait for box to be found
        Program.Delay(100)
      EndWhile
      If (player = p And Controls.GetButtonCaption(box) = " ") Then
        Controls.SetButtonCaption(box,Turn+"_"+Put) 'Valid Go
      Else
        SetDown() 'Invalid go - try again
      EndIf
    EndSub

    Sub OnButtonClicked
      box = Controls.LastClickedButton 'box clicked
      clicked = 1 'Flag to show we have a box
      'Find which p,i,j the box was
      For p = 1 To 2
        For i = 1 To 10
          For j = 1 To 10
            If (box = Boxes[p][i][j]) Then
              Goto Found
            EndIf
          EndFor
        EndFor
      EndFor
      Found: 'box found
    EndSub

    Thursday, February 10, 2011 8:53 PM
    Moderator