Programming is supposed to be fun

Post Reply
User avatar
Posts: 104
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State

Programming is supposed to be fun

Post by Cedron » 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)
' Gambas class file

Private s As Float
Private vx As Float
Private vy As Float

Private b As Integer

Public Sub Form_Open()


        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
        Me.Maximized = True    

Public Sub ScrollBar1_Change()  ' Button Size

        Button1.H = ScrollBar1.Value     
        Button1.W = ScrollBar1.Value     

Public Sub ScrollBar2_Change()  ' Button Speed

        s = ScrollBar2.Value

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

        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 

        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 
        vx += 3.0 * (Rnd - 0.5)
        vy += 3.0 * (Rnd - 0.5)


Sub AdjustVelocityToSpeed()

        Dim d, f As Float
        d = Sqr(vx * vx + vy * vy)
        f = s / d
        vx *= f
        vy *= f
Public Sub Button1_MouseDown()

        b = 25
        Button1.Enabled = False

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.


** Embellished reality, it didn't happen exactly this way, but close enough. ;-)
(11.94 KiB) Downloaded 75 times
.... and carry a big stick!

User avatar
Posts: 104
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State

Re: Programming is supposed to be fun

Post by Cedron » Tuesday 12th March 2019 7:15pm

(14.15 KiB) Downloaded 71 times
Instructions: [Up], [Down], [Space], [Esc]

This is going to take a bit of 'splaining, so if the band could throw down a few notes for some background music, that would be appreciated.

Here is a next step towards learning how to program in Gambas, particularly aimed at the Gamer folks. Think of being a Coder as mastering the next level of gaming.

There's too much to explain, so I'm not going to explain anything. Let the code speak or be mute, depends on your effort level, and the readability/comprehensibility of the code.

For newbies to programming, Gambas uses what is called an Object Oriented Programming. aka OOP. Been famous for a while now, the novelty has sort of worn off. But to get you started on the right foot, using the right terminology, here are some guidelines.

* A Class is the definition of an Object

* An Object is an instance of a Class

So you see the terms "Class" and "Object" are not interchangeable. If you are talking about the thing, it's an object, has some data in memory somewhere, and an associated set of routines that work with that data. If you are talking about its description, it is the Class. It defines what kind of data is in that area, and the source code for the routines.

With that in mind, and for arrivals from other platforms............

Gambas implementation of OOP is not exactly like C++'s, but they are conceptually similar.

The attached program contains two objects. A ScrollbarThrower and a Coconut. The first throws coconuts, not scrollbars, but it is made using a scroll bar. Run the program, look at the overview below, study the code. You will learn.

The coconut is basically the button from the previous example, extracted from the form and made into its own thing.

The action is actually meant to be implemented as a monkey hurling a coconut. This requires an image frame sequence for the throw. I'm afraid the old fashioned simple graphics will not do in today's environment; you are going to want something more realistic. One idea is for you and your friends to take videos of each other throwing a football around. You can capture, edit, and use images from that. The football should make a good stand in for a coconut.

The next rendition will be more layered and rich yet. Multiplayer, scorekeeping, game timing, etc, will be included.

You are encouraged to copy and modify, extend, play with, the code etc. You are also strongly encouraged to study the coding style and emulate it.

Or not, but you can do a lot worse. I'm looking at you cryptic head Hungarian notation users now, that stuff belongs in C where they take pride in being obfuscated.

Here is the overview of the Coconut:

Code: Select all

Public Sub _new(ArgForm As Form)

Public Sub _done()

Public Sub Prepare()
   - Bail if not Flying
   - Change State to Prepping

Public Sub LetFly(ArgPx As Float, ArgPy As Float, ArgVx As Float, ArgVy As Float)
   - Bail if not Prepared or Ready
   - Store Initial Conditions
   - Enable Display
   - Change State to Flying

Public Sub TimeStep()
   - Bail if not Flying
   - Get Container for Size Parameters
   - Implement Velocity
   - Implement Acceleration Fields
   - Implement Sludge
   - Flying is done when too slow
   - Check for Horizontal Out of Bounds
   - Check for Vertical Out of Bounds

Public Sub RenderDisplay()
   - The Form will do the actual Drawing

And here is the overview of the Thrower:

Code: Select all

Public Sub _new(ArgForm As Form)

Public Sub _done()

Public Sub Resize(ArgX As Float, ArgY As Float, ArgW As Float, ArgH As Float)

Public Sub MoveVerticalSteps(ArgSteps As Float)

Public Sub Throw(ArgCoconut As CoconutClass)
   - Bail if already Throwing
   - Store and Prepare the Coconut
   - Set the State to Attack

Public Sub TimeStep()
   - Shrink the Width Parameter
   - Calculate the Advance Position
   - If End Reached, Change State to Decay and Fire the Coconut
   - Calculate the Flight Parameters
   - Let the Coconut Fly
   - Shrink the Width Parameter
   - When Recoil is Complete, Change State to Release
   - Grow the Width Parameter by Decaying the Recoil Length
   - Move Value Towards Zero
   - When Release is Complete, Change State to Ready

Public Sub RenderDisplay()

Remember, this is strictly for fun. Any useful application of the knowledge gained is merely a pleasant side effect.

Source archive attached, the code is too long to post.


P.S. I'm still somewhat of a newbie to Gambas as well, so if any of you more experienced fellow old farts have a better Gambas way of doing something, let's run it up the flag pole and see if ourSaluteFlag = True.
.... and carry a big stick!

Post Reply