Programming is supposed to be fun
Posted
#1
(In Topic #237)
Regular

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)
Code (gambas)
- ' Gambas class file
- '=============================================================================
- 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()
- '=============================================================================
- Button1.H = ScrollBar1.Value
- Button1.W = ScrollBar1.Value
- '=============================================================================
- s = ScrollBar2.Value
- AdjustVelocityToSpeed()
- '=============================================================================
- b = b - 1
- AdjustVelocityToSpeed()
- Button1.X += vx
- Button1.Y += vy
- AdjustVelocityToSpeed()
- '=============================================================================
- f = s / d
- vx *= f
- vy *= f
- '=============================================================================
- b = 25
- '=============================================================================
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. ;-)
.... and carry a big stick!
Posted
Regular

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
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
DoneFlying:
Public Sub RenderDisplay()
- The Form will do the actual Drawing
And here is the overview of the Thrower:
Code
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()
WhileReady:
WhileAttack:
- Shrink the Width Parameter
- Calculate the Advance Position
- If End Reached, Change State to Decay and Fire the Coconut
ThrowTheCoconut:
- Calculate the Flight Parameters
- Let the Coconut Fly
WhileDecay:
- Shrink the Width Parameter
- When Recoil is Complete, Change State to Release
WhileSustain:
WhileRelease:
- 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.
Ced
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!
1 guest and 0 members have just viewed this.



