locked
Button Click and graphics window clear RRS feed

  • Question

  • Hi! I'm trying to start up using Small Basic to develop personal projects and hopefully make a career. I've been trying to develop
    a concessions application including products sold, promotional days, ect...

    Well this current application I'm having problems with because idk how to correctly use the buttonclick method...
    What I'm trying to do is create a game that asks what animal is shown, and allow the user to click the correct answer.
    I want to be able to create as many "questions" as possible and each have the operator variable recognize a new "correct answer"

    Is there a better way to set up this program?

    GraphicsWindow.Height = 600
    GraphicsWindow.Width = 800
    Controls.ButtonClicked = ButtonClick


    mainmenu()

    Sub mainmenu

      GraphicsWindow.Show()

      GraphicsWindow.DrawBoundText(50, 150, 10000, "Welcome to Guessing Game! What would you like to do?")


      newgame = Controls.AddButton("New Game", 50, 250)
      Controls.SetSize(newgame, 80, 75)
      New = Controls.GetButtonCaption(newgame)

      quitgame = Controls.AddButton("Quit", 200, 250)
      Controls.SetSize(quitgame, 80, 75)
      Quit = Controls.GetButtonCaption(quitgame)

    EndSub




    Sub ButtonClick

      operator = Controls.GetButtonCaption(Controls.LastClickedButton)



      If operator = new Then
        QuestionOne()


      ElseIf operator = dog then
        GraphicsWindow.ShowMessage("Correct!", "Right!")
        questiontwo()
        'operator = ""
      if operator = bird then
        GraphicsWindow.ShowMessage("Yes!", "X2")
      EndIf

    ' This is the issue I'm having here... This shows after I click the 2nd "correct" answer.

    Else
      GraphicsWindow.ShowMessage("Incorrect!", "Try Again!")

      EndIf










      If operator = Quit Then
        Program.End()
      EndIf





      choicetwo:
      If operator = bird Then
        GraphicsWindow.ShowMessage("Correct again!", "Correct!")
        GraphicsWindow.Clear()
        mainmenu()
      EndIf





    EndSub



    Sub QuestionOne

      GraphicsWindow.Clear()

      dogpic = "E:\Downloads\Temp\My Project\dog.jpg"

      dogimage = Shapes.AddImage(dogpic)


      Controls.SetSize(dogimage, 350, 350)
      Controls.Move(dogimage, 200, 10)

      GraphicsWindow.DrawBoundText(150, 400, 100, "What animal is this?")


      catbutton = Controls.AddButton("Cat", 300, 400)
      cat = Controls.GetButtonCaption(catbutton)
      Controls.SetSize(catbutton, 75, 75)

      horsebutton = Controls.AddButton("Horse", 400, 400)
      Controls.SetSize(horsebutton, 75, 75)
      horse = Controls.GetButtonCaption(horsebutton)

      dogbutton = Controls.AddButton("Dog", 500, 400)
      Controls.SetSize(dogbutton, 75, 75)
      dog = Controls.GetButtonCaption(dogbutton)




    EndSub

    Sub questiontwo
      GraphicsWindow.Clear()

      birdpic = "E:\Downloads\Temp\My Project\bird.jpg"

      birdimage = Shapes.AddImage(birdpic)

      Controls.SetSize(birdimage, 350, 350)
      Controls.Move(birdimage, 150, 10)

      GraphicsWindow.DrawBoundText(100, 400, 1000, "What animal is this?")

      snakebutton = Controls.AddButton("Snake", 250, 400)
      Controls.SetSize(snakebutton, 80, 80)
      snake = Controls.GetButtonCaption(dogbutton)



      catbutton = Controls.AddButton("Cat", 350, 400)
      Controls.SetSize(catbutton, 80, 80)
      cat = Controls.GetButtonCaption(catbutton)



      birdbutton = Controls.AddButton("Bird", 450, 400)
      Controls.SetSize(birdbutton, 80, 80)
      bird = Controls.GetButtonCaption(birdbutton)

    EndSub

                                                                                                                          
    Wednesday, April 18, 2012 4:46 AM

Answers

  • Heyya Poo90!

    Seems like you got a long road ahead!  O_o

    • My 1st advice is to transfer all code within Sub ButtonClick to separate subroutines to deal w/ those button verifications; leaving behind only this --> operator = Controls.GetButtonCaption(Controls.LastClickedButton).
    • "New Game" & "Quit" buttons should be checked apart from the normal question buttons. Which is, having its own subroutine.
    • Controls and Shapes are different types of objects in SB!

    Sorry I don't have time now to leave some examples for your code. But if you want, you can study some other codes dealing w/ buttons:

    This import code: MTG686-3 & this thread: Paint

    Wednesday, April 18, 2012 9:40 AM
    Answerer
  •  Try this. 

     GraphicsWindow.Height = 600
     GraphicsWindow.Width = 800
     photodata()
     Controls.ButtonClicked = ButtonClick

     mainmenu()
     
    Sub mainmenu
       GraphicsWindow.Show()
       GraphicsWindow.DrawBoundText(50, 150, 10000, "Welcome to Guessing Game! What would you like to do?")
       newgame = Controls.AddButton("New Game", 50, 250)
       Controls.SetSize(newgame, 80, 75)
       New = Controls.GetButtonCaption(newgame)
       quitgame = Controls.AddButton("Quit", 200, 250)
       Controls.SetSize(quitgame, 80, 75)
       Quit = Controls.GetButtonCaption(quitgame)
    EndSub

    Sub checkanswer
      If operator=pic[PicNMB] Then
        GraphicsWindow.ShowMessage("Yes!  Correct!", "Next challange!")
        Questions()
      Else
        GraphicsWindow.ShowMessage("Incorrect!", "Try Again!")
      EndIf
    EndSub   

    Sub ButtonClick
       operator = Controls.GetButtonCaption(Controls.LastClickedButton)
       If operator = new Then
         Questions()
       elseIf operator = Quit Then
         Program.End()
       Else
         checkanswer()
       EndIf
    EndSub
     
    Sub Questions
      GraphicsWindow.Clear()
      answers=""
      PicNMB=Math.GetRandomNumber(NN)        '  selct a photo  randomly  ...  This is correct answer photo name
      answers=answers+pic[PicNMB]+":"
      GraphicsWindow.DrawBoundText(150, 400, 100, "What animal is this?")
     
      For i=1 To 4
       otheranswer()                         '  These are wrong answer
       Controls.Remove(button[i])
       button[i] = Controls.AddButton(pic[ans], 300+(i-1)*100, 400)
       Controls.SetSize(button[i], 75, 75)
      EndFor
     
      correct= Math.GetRandomNumber(4)      '  to set  correct answer       
      Controls.SetButtonCaption(button[correct],pic[PicNMB])' caption is changed to correct name
      image = Shapes.AddImage(url+pic[PicNMB]+".jpg")
      Controls.SetSize(image, 350, 350)
      Controls.Move(image, 200, 10)
    EndSub

    Sub otheranswer
      retry:
      ans= Math.GetRandomNumber(NN)
      If text.IsSubText(answers,pic[ans])  Then '  check if  same name exists in answers
        Goto retry
      EndIf
      answers=answers+pic[ans]+":"  '  sum up names ...  like this  ...   dog:bird:horse:.....
    EndSub

    Sub photodata
     url="E:\Downloads\Temp\My Project\"
     pic[1] ="dog"          '  Example
     pic[2] ="bird"         '  Example
     pic[3] ="cat"          '  Example
     pic[4] ="horse"        '  Example
     pic[5] ="snake"        '  Example
     pic[6] ="butterfly"    '  Example
      '  
      '                                                 '  add  your favorite  photoname
      '
     NN= Array.GetItemCount(pic)
    endsub

    Wednesday, April 18, 2012 11:36 AM
    Answerer

All replies

  • Heyya Poo90!

    Seems like you got a long road ahead!  O_o

    • My 1st advice is to transfer all code within Sub ButtonClick to separate subroutines to deal w/ those button verifications; leaving behind only this --> operator = Controls.GetButtonCaption(Controls.LastClickedButton).
    • "New Game" & "Quit" buttons should be checked apart from the normal question buttons. Which is, having its own subroutine.
    • Controls and Shapes are different types of objects in SB!

    Sorry I don't have time now to leave some examples for your code. But if you want, you can study some other codes dealing w/ buttons:

    This import code: MTG686-3 & this thread: Paint

    Wednesday, April 18, 2012 9:40 AM
    Answerer
  •  Try this. 

     GraphicsWindow.Height = 600
     GraphicsWindow.Width = 800
     photodata()
     Controls.ButtonClicked = ButtonClick

     mainmenu()
     
    Sub mainmenu
       GraphicsWindow.Show()
       GraphicsWindow.DrawBoundText(50, 150, 10000, "Welcome to Guessing Game! What would you like to do?")
       newgame = Controls.AddButton("New Game", 50, 250)
       Controls.SetSize(newgame, 80, 75)
       New = Controls.GetButtonCaption(newgame)
       quitgame = Controls.AddButton("Quit", 200, 250)
       Controls.SetSize(quitgame, 80, 75)
       Quit = Controls.GetButtonCaption(quitgame)
    EndSub

    Sub checkanswer
      If operator=pic[PicNMB] Then
        GraphicsWindow.ShowMessage("Yes!  Correct!", "Next challange!")
        Questions()
      Else
        GraphicsWindow.ShowMessage("Incorrect!", "Try Again!")
      EndIf
    EndSub   

    Sub ButtonClick
       operator = Controls.GetButtonCaption(Controls.LastClickedButton)
       If operator = new Then
         Questions()
       elseIf operator = Quit Then
         Program.End()
       Else
         checkanswer()
       EndIf
    EndSub
     
    Sub Questions
      GraphicsWindow.Clear()
      answers=""
      PicNMB=Math.GetRandomNumber(NN)        '  selct a photo  randomly  ...  This is correct answer photo name
      answers=answers+pic[PicNMB]+":"
      GraphicsWindow.DrawBoundText(150, 400, 100, "What animal is this?")
     
      For i=1 To 4
       otheranswer()                         '  These are wrong answer
       Controls.Remove(button[i])
       button[i] = Controls.AddButton(pic[ans], 300+(i-1)*100, 400)
       Controls.SetSize(button[i], 75, 75)
      EndFor
     
      correct= Math.GetRandomNumber(4)      '  to set  correct answer       
      Controls.SetButtonCaption(button[correct],pic[PicNMB])' caption is changed to correct name
      image = Shapes.AddImage(url+pic[PicNMB]+".jpg")
      Controls.SetSize(image, 350, 350)
      Controls.Move(image, 200, 10)
    EndSub

    Sub otheranswer
      retry:
      ans= Math.GetRandomNumber(NN)
      If text.IsSubText(answers,pic[ans])  Then '  check if  same name exists in answers
        Goto retry
      EndIf
      answers=answers+pic[ans]+":"  '  sum up names ...  like this  ...   dog:bird:horse:.....
    EndSub

    Sub photodata
     url="E:\Downloads\Temp\My Project\"
     pic[1] ="dog"          '  Example
     pic[2] ="bird"         '  Example
     pic[3] ="cat"          '  Example
     pic[4] ="horse"        '  Example
     pic[5] ="snake"        '  Example
     pic[6] ="butterfly"    '  Example
      '  
      '                                                 '  add  your favorite  photoname
      '
     NN= Array.GetItemCount(pic)
    endsub

    Wednesday, April 18, 2012 11:36 AM
    Answerer
  • Thank you, I do have a VERY long road ahead of me. My only problem is that I've been trying to learn different programming languages all at once and it's causing my mind to explode... Once I found Small Basic, I felt that this was the best "beginning" for me. I don't want to purchase anything that I don't need in order to learn the language and get my foot in the door. After taking a linux class, my mind is set on programming.

    I've been trying to figure out how to get the button click events into different sub routines, guess I really need to learn that.

    I know controls and shapes are different objects, but I didn't know how else to resize the pictures I had.

    I greatly appreciate the reply and your help :D

    Thursday, April 19, 2012 12:18 AM
  • I like this set up somewhat... there's a lot of code in here I'm still trying to study and understand. Especially with the pictures... the url variable is used for my filepath, i understand... but in order to get the pic I would have to replace the pic[] variables with the last pathway?

    such as:

    url = "E\Downloads\Temp\My Project\"

    pic[1] = "\dog.jpg"
    pic[2] = "\cat.jpg"

    I really appreciate the addition to having it show up as random... that was the last thing I was worried about. I really like the fact that each button doesn't conflict with the program (before, I would click "new game" and it would show "incorrect" because I either didn't know what code block I should put it into...)

    i'm def going to study this code and try to improve it when I get a chance. Thank you again! :D

    Btw, I'm making this game for my niece :D

    Thursday, April 19, 2012 12:23 AM