# i need help with graphics window (small basic)

• ### Question

• Start:
GraphicsWindow.BackgroundColor =  "black"
GraphicsWindow.DrawText(200, 200" Hello today we are going to play computer D&D")
GraphicsWindow.Show()
skill = Controls.AddButton("Skill " 150, 150)
strength = Controls.AddButton("Strength " 350, 150)

If Controls.ButtonClicked = skill  Then
Goto skill
ElseIf Controls.ButtonClicked = strength then
Goto strength
EndIf

skill:
Program.Delay(5000)
GraphicsWindow.Clear()
GraphicsWindow.DrawText(150, 240 "First we are going to do skill")
Program.Delay(2000)
GraphicsWindow.Clear()
dice12 = Math.GetRandomNumber(12)
dice4 = Math.GetRandomNumber(4)
dice42 = Math.GetRandomNumber(12)
dice47 = Math.GetRandomNumber(4)

rawscoreskill = dice12/dice4
finalscoreskill = rawscoreskill
GraphicsWindow.DrawText(150, 200"Your first number for skill is " + dice12)
GraphicsWindow.DrawText(150, 250"Your second number for skill is " + dice4)
GraphicsWindow.DrawText(150, 300"Your number not rounded is " + dice12/dice4)
GraphicsWindow.DrawText(150, 350"Your score for skill is " + finalscoreskill)
Program.Delay(4000)
GraphicsWindow.Clear()
Goto start

strength:
rawscorestrength = dice42/dice47
finalscorestrength = rawscorestrength
GraphicsWindow.DrawText(150, 150" Now we are going to do strength")
GraphicsWindow.DrawText(150, 200"Your first number for strength is " + dice47)
GraphicsWindow.DrawText(150, 250"Your second number for strength is " + dice42)
GraphicsWindow.DrawText(150, 300"Your number not rounded is " + dice42/dice47)
GraphicsWindow.DrawText(150, 350"Your score for strength is " + finalscorestrength)
Program.Delay(4000)
GraphicsWindow.Clear()
Goto start

GraphicsWindow.DrawText(150, 200 "Your score for skill is" + finalscoreskill)
GraphicsWindow.DrawText(150, 250 "Your score for strength is" + finalscorestrength)

If i click on the strength button it goes to skill can anyone fix it or tell me how to
Friday, December 20, 2013 9:54 PM

• Controls.ButtonClicked is an event - what you need to do is set this to the name of a subroutine that is called when the button is clicked.

Also, I suggest:

1] trying to use subroutines rather than GoTo if you can.

2] If you do a GraphicsWindow.Clear() then everything on GW is lost including buttons etc - try to use it sparingly.  I suggest just drawing a full GW filled rectangle to cover all past drawings - I made a subroutine for this below.

3] I don't recommend using Program.Delay inside events or subroutines called from events - there are ways 'game loop' to handle this, but maybe this is too much in one go.  If interested, then see this recent thread where the issue turns out to be exactly this.

The following is not a final solution at all, but a pointer to how this might work to move you forward.  You will have to tackle point 3 at some point with your game though.

Nice work so far.

`GraphicsWindow.BackgroundColor =  "black"GraphicsWindow.DrawText(200, 200" Hello today we are going to play computer D&D")skillButton = Controls.AddButton("Skill " 150, 100)strengthButton = Controls.AddButton("Strength " 350, 100)Controls.ButtonClicked = OnButtonClickedSub OnButtonClicked  button = Controls.LastClickedButton  If (button = skillButton) Then    skill()  ElseIf (button = strengthButton) Then    strength()  EndIfEndSubSub skill  Clear()  GraphicsWindow.DrawText(150, 150 "First we are going to do skill")  dice12 = Math.GetRandomNumber(12)  dice4 = Math.GetRandomNumber(4)  dice42 = Math.GetRandomNumber(12)  dice47 = Math.GetRandomNumber(4)    rawscoreskill = dice12/dice4  finalscoreskill = rawscoreskill  GraphicsWindow.DrawText(150, 200"Your first number for skill is " + dice12)  GraphicsWindow.DrawText(150, 250"Your second number for skill is " + dice4)  GraphicsWindow.DrawText(150, 300"Your number not rounded is " + dice12/dice4)  GraphicsWindow.DrawText(150, 350"Your score for skill is " + finalscoreskill)EndSubSub strength  Clear()  rawscorestrength = dice42/dice47  finalscorestrength = rawscorestrength  GraphicsWindow.DrawText(150, 150"Now we are going to do strength")  GraphicsWindow.DrawText(150, 200"Your first number for strength is " + dice47)  GraphicsWindow.DrawText(150, 250"Your second number for strength is " + dice42)  GraphicsWindow.DrawText(150, 300"Your number not rounded is " + dice42/dice47)  GraphicsWindow.DrawText(150, 350"Your score for strength is " + finalscorestrength)EndSubSub finalScore  GraphicsWindow.DrawText(150, 200 "Your score for skill is" + finalscoreskill)  GraphicsWindow.DrawText(150, 250 "Your score for strength is" + finalscorestrength)EndSubSub Clear  oldBrush = GraphicsWindow.BrushColor  GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor  GraphicsWindow.FillRectangle(0,0,GraphicsWindow.Width, GraphicsWindow.Height)  GraphicsWindow.BrushColor = oldBrushEndSub`

• Edited by Friday, December 20, 2013 10:50 PM
• Proposed as answer by Monday, December 23, 2013 7:21 AM
• Marked as answer by Sunday, December 29, 2013 1:42 PM
Friday, December 20, 2013 10:32 PM
• Also see here for some info on SB events and game loop to handle point 3 raised in previous post.  And this one one on GoTo and other programming tips.
Friday, December 20, 2013 11:53 PM

### All replies

• Controls.ButtonClicked is an event - what you need to do is set this to the name of a subroutine that is called when the button is clicked.

Also, I suggest:

1] trying to use subroutines rather than GoTo if you can.

2] If you do a GraphicsWindow.Clear() then everything on GW is lost including buttons etc - try to use it sparingly.  I suggest just drawing a full GW filled rectangle to cover all past drawings - I made a subroutine for this below.

3] I don't recommend using Program.Delay inside events or subroutines called from events - there are ways 'game loop' to handle this, but maybe this is too much in one go.  If interested, then see this recent thread where the issue turns out to be exactly this.

The following is not a final solution at all, but a pointer to how this might work to move you forward.  You will have to tackle point 3 at some point with your game though.

Nice work so far.

`GraphicsWindow.BackgroundColor =  "black"GraphicsWindow.DrawText(200, 200" Hello today we are going to play computer D&D")skillButton = Controls.AddButton("Skill " 150, 100)strengthButton = Controls.AddButton("Strength " 350, 100)Controls.ButtonClicked = OnButtonClickedSub OnButtonClicked  button = Controls.LastClickedButton  If (button = skillButton) Then    skill()  ElseIf (button = strengthButton) Then    strength()  EndIfEndSubSub skill  Clear()  GraphicsWindow.DrawText(150, 150 "First we are going to do skill")  dice12 = Math.GetRandomNumber(12)  dice4 = Math.GetRandomNumber(4)  dice42 = Math.GetRandomNumber(12)  dice47 = Math.GetRandomNumber(4)    rawscoreskill = dice12/dice4  finalscoreskill = rawscoreskill  GraphicsWindow.DrawText(150, 200"Your first number for skill is " + dice12)  GraphicsWindow.DrawText(150, 250"Your second number for skill is " + dice4)  GraphicsWindow.DrawText(150, 300"Your number not rounded is " + dice12/dice4)  GraphicsWindow.DrawText(150, 350"Your score for skill is " + finalscoreskill)EndSubSub strength  Clear()  rawscorestrength = dice42/dice47  finalscorestrength = rawscorestrength  GraphicsWindow.DrawText(150, 150"Now we are going to do strength")  GraphicsWindow.DrawText(150, 200"Your first number for strength is " + dice47)  GraphicsWindow.DrawText(150, 250"Your second number for strength is " + dice42)  GraphicsWindow.DrawText(150, 300"Your number not rounded is " + dice42/dice47)  GraphicsWindow.DrawText(150, 350"Your score for strength is " + finalscorestrength)EndSubSub finalScore  GraphicsWindow.DrawText(150, 200 "Your score for skill is" + finalscoreskill)  GraphicsWindow.DrawText(150, 250 "Your score for strength is" + finalscorestrength)EndSubSub Clear  oldBrush = GraphicsWindow.BrushColor  GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor  GraphicsWindow.FillRectangle(0,0,GraphicsWindow.Width, GraphicsWindow.Height)  GraphicsWindow.BrushColor = oldBrushEndSub`

• Edited by Friday, December 20, 2013 10:50 PM
• Proposed as answer by Monday, December 23, 2013 7:21 AM
• Marked as answer by Sunday, December 29, 2013 1:42 PM
Friday, December 20, 2013 10:32 PM
• Thanks alot been stuck on this forever

Friday, December 20, 2013 11:11 PM
• Also see here for some info on SB events and game loop to handle point 3 raised in previous post.  And this one one on GoTo and other programming tips.
Friday, December 20, 2013 11:53 PM