Posts: 451
Threads: 34
Likes Received: 359 in 246 posts
Likes Given: 178
Joined: Aug 2023
Reputation:
17
09-04-2024, 06:06 PM
(This post was last modified: 09-04-2024, 06:06 PM by litdev.)
Create some buttons that when pressed change the background colour of the GraphicsWindow.
As an extra challenge, try to make it so that the buttons are suitably coloured - no extensions!
Posts: 394
Threads: 15
Likes Received: 123 in 106 posts
Likes Given: 199
Joined: Sep 2023
Reputation:
6
(translated by Google translator)
Hi all.
For those who are gambling people, this challenge can be made more difficult.
First, let the program itself choose one of the buttons at random.
And then let the person choose the button he wants.
If you count the number of coincidences between the choice of a program and a person, you can check whether your intuition differs from the statistical probability of coincidences.
What if it turns out that you have paranormal abilities?
What if you can guess or be wrong much more often than expected by probability theory?
Posts: 136
Threads: 10
Likes Received: 37 in 31 posts
Likes Given: 29
Joined: Oct 2023
Reputation:
3
Button1=Controls.AddButton("My Button1",0,0)
Button2 = Controls.AddButton("My Button2",0,30)
Controls.ButtonClicked=OnButtonClicked
Sub OnButtonClicked
If Controls.LastClickedButton = "Button1" then
GraphicsWindow.BackgroundColor="Blue"
Elseif Controls.LastClickedButton = "Button2" then
GraphicsWindow.BackgroundColor= "Green"
EndIf
endsub
JR
Posts: 63
Threads: 9
Likes Received: 41 in 28 posts
Likes Given: 26
Joined: Sep 2023
Reputation:
9
09-04-2024, 09:23 PM
(This post was last modified: 09-09-2024, 08:10 AM by Scout.)
Code: Red = 0
Green = 0
Blue = 0
BG = GraphicsWindow.GetColorFromRGB(Red,Green,Blue)
GraphicsWindow.BackgroundColor = BG
RedColor = GraphicsWindow.GetColorFromRGB(Red,0,0)
GraphicsWindow.BrushColor = RedColor
ButtonRed = Controls.AddButton("",10,10)
Controls.SetButtonCaption(ButtonRed, Red)
GreenColor = GraphicsWindow.GetColorFromRGB(0,Green,0)
GraphicsWindow.BrushColor = GreenColor
ButtonGreen = Controls.AddButton("",110,10)
Controls.SetButtonCaption(ButtonGreen, Green)
BlueColor = GraphicsWindow.GetColorFromRGB(0,0,Blue)
GraphicsWindow.BrushColor = BlueColor
ButtonBlue = Controls.AddButton("",210,10)
Controls.SetButtonCaption(ButtonBlue, Blue)
Controls.ButtonClicked = OnButtonClicked
Sub OnButtonClicked
If Controls.LastClickedButton = ButtonRed then
Controls.Remove(ButtonRed)
Red = Math.GetRandomNumber(255)
RedColor = GraphicsWindow.GetColorFromRGB(Red,0,0)
GraphicsWindow.BrushColor = RedColor
ButtonRed = Controls.AddButton("",10,10)
Controls.SetButtonCaption(ButtonRed, Red)
Elseif Controls.LastClickedButton = ButtonGreen then
Controls.Remove(ButtonGreen)
Green = Math.GetRandomNumber(255)
GreenColor = GraphicsWindow.GetColorFromRGB(0,Green,0)
GraphicsWindow.BrushColor = GreenColor
ButtonGreen = Controls.AddButton("",110,10)
Controls.SetButtonCaption(ButtonGreen, Green)
Elseif Controls.LastClickedButton = ButtonBlue then
Controls.Remove(ButtonBlue)
Blue = Math.GetRandomNumber(255)
BlueColor = GraphicsWindow.GetColorFromRGB(0,0,Blue)
GraphicsWindow.BrushColor = BlueColor
ButtonBlue = Controls.AddButton("",210,10)
Controls.SetButtonCaption(ButtonBlue, Blue)
EndIf
BG = GraphicsWindow.GetColorFromRGB(Red,Green,Blue)
GraphicsWindow.BackgroundColor = BG
TBox = Controls.AddTextBox(200,200)
Controls.SetTextBoxText(TBox, TBox)
TextWindow.WriteLine(ButtonRed + " " + ButtonGreen + " " + ButtonBlue)
EndSub
Posts: 451
Threads: 34
Likes Received: 359 in 246 posts
Likes Given: 178
Joined: Aug 2023
Reputation:
17
09-05-2024, 09:11 AM
(This post was last modified: 09-05-2024, 09:29 AM by litdev.)
ZJLF704.000
Posts: 63
Threads: 9
Likes Received: 41 in 28 posts
Likes Given: 26
Joined: Sep 2023
Reputation:
9
09-05-2024, 11:16 AM
(This post was last modified: 09-05-2024, 04:31 PM by Scout.)
Great! This means you can create buttons with other shapes and not just rectangular ones.
For example, use the triangle-shaped arrow keys to change values or scrolling.
Posts: 13
Threads: 0
Likes Received: 16 in 11 posts
Likes Given: 10
Joined: Nov 2023
Reputation:
5
Here is my solution for a colour button program:
Code: GraphicsWindow.Show()
gw=GraphicsWindow.Width
gh=GraphicsWindow.Height
colors="1=red;2=green;3=blue"
For bb=1 To 3
but[bb]=Controls.AddButton("",50*bb,gh-40)
Controls.SetSize(but[bb],40,30)
Shapes.SetOpacity(but[bb],1)
GraphicsWindow.BrushColor=colors[bb]
GraphicsWindow.FillRectangle(50*bb,gh-40,40,30)
EndFor
GraphicsWindow.BackgroundColor="white"
GraphicsWindow.PenWidth=3
GraphicsWindow.FontSize=30
GraphicsWindow.BrushColor="Black"
GraphicsWindow.DrawText(100,200,"click a button")
Controls.ButtonClicked=Click
While 0=0
If draw=1 Then
GraphicsWindow.PenColor=col
For x=1 To 250
Program.Delay(2)
GraphicsWindow.DrawLine(2*x,0,2*x,gh*0.9)
EndFor
GraphicsWindow.DrawText(100,200,"click a button")
draw=0
EndIf
EndWhile
Sub Click
lastbut=Text.GetSubTextToEnd(Controls.LastClickedButton,7)
col=colors[lastbut]
draw=1
EndSub
Posts: 63
Threads: 9
Likes Received: 41 in 28 posts
Likes Given: 26
Joined: Sep 2023
Reputation:
9
Dear SmallBasic friends,
shouldn't we all just copy the button name into a variable and work with it?
The use of the internal name is very confusing for beginners because it assumes knowledge of the internals.
In my opinion, using it in variables is also simpler and more elegant and is more appropriate for a high-level language.
What do you think?
Posts: 394
Threads: 15
Likes Received: 123 in 106 posts
Likes Given: 199
Joined: Sep 2023
Reputation:
6
(translated by Google translator)
Hello.
I can't quite understand your words.
When creating a button, we assign its internal name to a variable.
The developer assigns the name of this variable himself. He can use the name that is most convenient and understandable to him.
I don't understand what problem you're talking about.
Posts: 136
Threads: 10
Likes Received: 37 in 31 posts
Likes Given: 29
Joined: Oct 2023
Reputation:
3
Scout,
Just like AB I don't understand what your asking. Could you give an example?
JR
|