We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenge 4 - Colour buttons
#1
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!
Reply
#2
(translated by Google translator)

Hi all.
For those who are gambling people, this challenge can be made more difficult.  Shy
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? Rolleyes
[-] The following 1 user Likes AbsoluteBeginner's post:
  • litdev
Reply
#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
[-] The following 1 user Likes jrmrhrb00's post:
  • litdev
Reply
#4
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
[-] The following 2 users Like Scout's post:
  • AbsoluteBeginner, litdev
Reply
#5
ZJLF704.000
[-] The following 4 users Like litdev's post:
  • AbsoluteBeginner, jrmrhrb00, Scout, z-s
Reply
#6
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.

   
Reply
#7
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
[-] The following 1 user Likes WhTurner's post:
  • litdev
Reply
#8
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?
Reply
#9
(translated by Google translator)

Hello.  Shy

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.  Blush
Reply
#10
Scout,

Just like AB I don't understand what your asking. Could you give an example?

JR
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)