Programming is supposed to be fun
Posted: Sunday 10th March 2019 2:30pm
**So, I was out going for a walk, always a good thing to do, and a neigbor invited me in. He said, my boy just doesn't get programming.
I said, hmmmm, has he tried Gambas?
He said no, so I said let's try this. We went on the computer and installed Gambas. Created a project. I had him put a button on the form, gave it some text, then a text box, and then double clicked the button, and I had him enter "Message.Info(Text1.Text)", and we ran the program. I said type something into the text box, now press the button. The pop up popped up, and I said wasn't the neat? He said "Yeah" in that particular teenage way.
Hmmmmm, okay not impressed. So I said, put a second button on the form. He did. Then I had him put "Button1.X += 10", and we ran the program. I had him press the second button, and of course, the first button moved. His eyes went wide. Then the light bulb came on, and he clicked it again. We built upon that.
When I got home, I decided this would be a fun program for everyone, so I stripped it down, then embellished it a little bit. This is the result.
Step 1. Create a new project, choose Qt type
Step 2. Add two ScrollBars onto the form from the [Control/Form] panel
Step 3. Add a Button onto the form from the [Control/Form] panel
Step 4. Add a Timer onto the form from the [Control/Special] panel
Step 5. Double-click the form
Step 6. Replace the code that's there with the following
(You can also download the attached Source archive - Thanks Jussi)
The purpose to the game is to click on it with your mouse. It will pause for a second then take off in a random direction.
Suggestions for further embellishment:
* Add scoring
* Make it multiple Button with varying scores
* (Bonus) Make the buttons bounce off each other
Hint: For horizontal bounces exchange horizontal velocity components, likewise for vertical.
If you resize so the button is off the form, be patient, it will come back.
Ced
** Embellished reality, it didn't happen exactly this way, but close enough.
I said, hmmmm, has he tried Gambas?
He said no, so I said let's try this. We went on the computer and installed Gambas. Created a project. I had him put a button on the form, gave it some text, then a text box, and then double clicked the button, and I had him enter "Message.Info(Text1.Text)", and we ran the program. I said type something into the text box, now press the button. The pop up popped up, and I said wasn't the neat? He said "Yeah" in that particular teenage way.
Hmmmmm, okay not impressed. So I said, put a second button on the form. He did. Then I had him put "Button1.X += 10", and we ran the program. I had him press the second button, and of course, the first button moved. His eyes went wide. Then the light bulb came on, and he clicked it again. We built upon that.
When I got home, I decided this would be a fun program for everyone, so I stripped it down, then embellished it a little bit. This is the result.
Step 1. Create a new project, choose Qt type
Step 2. Add two ScrollBars onto the form from the [Control/Form] panel
Step 3. Add a Button onto the form from the [Control/Form] panel
Step 4. Add a Timer onto the form from the [Control/Special] panel
Step 5. Double-click the form
Step 6. Replace the code that's there with the following
(You can also download the attached Source archive - Thanks Jussi)
' Gambas class file
Private s As Float
Private vx As Float
Private vy As Float
Private b As Integer
'=============================================================================
Public Sub Form_Open()
Randomize
vx = Rnd - 0.5
vy = Rnd - 0.5
b = 0
Button1.Text = "O"
ScrollBar1.MaxValue = 200
ScrollBar1.MinValue = 30
ScrollBar1.Value = 100
ScrollBar2.MaxValue = 50
ScrollBar2.MinValue = 10
ScrollBar2.Value = 20
Timer1.delay = 40
Timer1.Start()
Me.Maximized = True
End
'=============================================================================
Public Sub ScrollBar1_Change() ' Button Size
Button1.H = ScrollBar1.Value
Button1.W = ScrollBar1.Value
End
'=============================================================================
Public Sub ScrollBar2_Change() ' Button Speed
s = ScrollBar2.Value
AdjustVelocityToSpeed()
End
'=============================================================================
Public Sub Timer1_Timer()
If Not Button1.Enabled Then
b = b - 1
If b = 0 Then
Button1.Enabled = True
vx = Rnd - 0.5
vy = Rnd - 0.5
AdjustVelocityToSpeed()
Endif
Return
Endif
Button1.X += vx
If Button1.X + Button1.W > Me.w Then
If vx > 0 Then vx = - vx
Else If Button1.X < 0 Then
If vx < 0 Then vx = - vx
Endif
Button1.Y += vy
If Button1.Y + Button1.H > Me.H Then
If vy > 0 Then vy = - vy
Else If Button1.Y < 0 Then
If vy < 0 Then vy = - vy
Endif
vx += 3.0 * (Rnd - 0.5)
vy += 3.0 * (Rnd - 0.5)
AdjustVelocityToSpeed()
End
'=============================================================================
Sub AdjustVelocityToSpeed()
Dim d, f As Float
d = Sqr(vx * vx + vy * vy)
f = s / d
vx *= f
vy *= f
End
'=============================================================================
Public Sub Button1_MouseDown()
b = 25
Button1.Enabled = False
End
'=============================================================================
Step 7. Have fun.The purpose to the game is to click on it with your mouse. It will pause for a second then take off in a random direction.
Suggestions for further embellishment:
* Add scoring
* Make it multiple Button with varying scores
* (Bonus) Make the buttons bounce off each other
Hint: For horizontal bounces exchange horizontal velocity components, likewise for vertical.
If you resize so the button is off the form, be patient, it will come back.
Ced
** Embellished reality, it didn't happen exactly this way, but close enough.