Gambas Examples

From Pigasoftware

Jump to: navigation, search

"Gambas Examples" is a Piga Software project to help newcomers learn the Gambas language, particularly in the realm of game development. You may use the following example codes any way you want.

Contents

Source Code Examples

How To Add Comments

Comments are invaluable ways to keep code organized and help other programmers. To add them to Gambas code you need to start them with an "'":

 'This is a comment
 PUBLIC SUB Form_Open()
 'The above and bellow lines are actual code.
 END

Hello World

To create Hello World add this code to the start up form's .class file:

 PUBLIC SUB Form_Open()
 
   Message("Hello World!")
 
 END

Linking Two Forms

To make one form ("FOne") link to another form ("FTwo") with a button ("BLink") add this to "FOne"'s .class file:

 PUBLIC SUB BLink_MouseDown()
 
   FTwo.Show
 
 END

To close a form, try this:

   FTwo.Close

Label Text Manipulation

To make a label ("LText") on a form ("FOne") change it's text with a click of a button ("BChange") add this to "FOne"'s .class file:

 PUBLIC SUB BLink_MouseDown()
 
   LText.text = "The Label now has different text"
 
 END

Object Picture Manipulation

Make a picture box ("PicBox") on a form as well as a button ("BChange"), and have a picture ("NewImage.png") in your project's directory.

 PUBLIC SUB BChange_MouseDown()
 
   PicBox.Picture = Picture["NewImage.png"]
 
 END

Form Colour Manipulation

To change the background colour of a form, create a button called "BChange" and add this code:

 PUBLIC SUB BChange_MouseDown()
 
   Form.BackColor = &H000000&
 
 END

This changes the from's background colour to black. This is works to change any objects hexadecimal colour setting.

Common Hexadecimal Colours
  Black: &H000000&
  White: &HFFFFFF&
  Red: &HFF0000&
  Blue: &H0000FF&
  Purple: &H55007F&
  Green: &H00AA00&
  Yellow: &HFFFF00&
  Orange: &HFF5500&
  Pink: &HFF557F&
  Grey: &H787878&
  Light Blue: &H00AAFF&
  Light Green: &H55FF00&
  Light Grey: &HACACAC&

Probability Example

You can use probability to make Gambas randomly display a message box. If you add this code to a form's .class file, it will make it so that if you click a button ("BDice") it will randomly pick a message box:

 PUBLIC Dice AS Integer
 
 PUBLIC SUB BDice_Click()
 
   RANDOMIZE 
 
 Dice = Int(Rnd(0, 2))
 
 IF Dice = "1" THEN
 
   Message("You rolled a One")
 
 END IF
 
 IF Dice = "2" THEN
 
   Message("You rolled a Two")
 
 END IF
 
 END

Adding to a Numeric String

On a form ("FOne") have a label or other object ("LNumber") that has a .text variable and set it to "1" and have a button ("BAdd").

 PUBLIC SUB BAdd_MouseDown()
 
   LNumber.text = LNumber.text + "1"
 
 END

Moving an Object

On a form ("FOne") create an object (in this case a picture box: "PBPlayer") and four buttons: "BLeft", "BRight", "BUp", "BDown".

 PUBLIC SUB BLeft_MouseDown()
 
   PBPlayer.X = PBPlayer.X - "32"
 
 END
 
 PUBLIC SUB BRight_MouseDown()
 
   PBPlayer.X = PBPlayer.X + "32"
 
 END
 
 PUBLIC SUB BUp_MouseDown()
 
   PBPlayer.Y = PBPlayer.Y - "32"
 
 END
 
 PUBLIC SUB BDown_MouseDown()
 
   PBPlayer.Y = PBPlayer.Y + "32"
 
 END

Alternately, you can move an object with the keyboard's arrow keys:

PUBLIC SUB Form_Keypress()

SELECT Key.Code

 CASE Key.Left

  PBPlayer.X = PBPlayer.X - "32"

 CASE Key.Right

  PBPlayer.X = PBPlayer.X + "32"

 CASE Key.Up

  PBPlayer.Y = PBPlayer.Y - "32"

 CASE Key.Down

  PBPlayer.Y = PBPlayer.Y + "32"

END SELECT

END

Collecting an Object

Expand on Moving an Object and add another object ("PBCollectible"). Both MUST fit an even grid (32x32 or 48x48 recommended) and as such their dimensions should fit these parameters as well as their placement must be even to grid. Create a timer ("TmrCollisions") and add this code, set it to be enabled and a delay of "1".

 PUBLIC SUB TmrCollisions_Timer()
 
 IF PBCollectible.x = PBPlayer.x and PBCollectible.y = PBPlayer.y THEN
 
   PBCollectible.x = "-9000"
 
   Message("You have collected this object!")
 
 END IF
 
 END

Align to Grid

This code aligns an object ("PBObject") to a given grid, (in this case 48x48). This is needed to allow collisions to work.

PBObject.x = (Round (PBObject.x/"48"))*"48"

PBObject.y = (Round (PBObject.y/"48"))*"48"

This can also be used to move an object to the grid point nearest your cursor.

PBObject.X = (Round (Mouse.x/"48"))*"48"

PBObject.y = (Round (Mouse.y/"48"))*"48"

Changing a Border

You can change a given object's borders (in this case between plain and none) by using this code.

First put this on the very top of your class file:

CONST None AS Integer = 0
CONST Plain AS Integer = 1

Then you can use this to change your object's ("PBObject") border:

PBObject.Border = "1"

Or:

PBObject.Border = "0"

Using Tags

Tags are a useful object property in that they have no function in rendering it. They are used to tag in a new function. For example if you are building a strategy game you would want a building to be labeled "Not-Built" (eg, a worker needs to finish it) or "Built" (once the building is fully functional). To do this simply write this out:

Building.Tag = "Not-Built"

This can then be used as a property for this object for uses such as:

IF Building.Tag = "Not-Built" THEN

It is also useful for direction tracking, by using a tag to say "Left" or "Right" it can make it easier to adapt conditions to an object's direction by using this property to signify it.

Playing Sounds

The Simple DirectMedia Layer (SDL) can be used to add sounds to your project. First you first must make sure it is set to use the Gambas SDL sound component. You can do this by clicking the "Project" menu, then clicking on "Properties". A window will appear showing various properties of your Gambas project. To add components, click the "Components" tab. Scroll through the list that appears until you find one called "gb.sdl.sound". Click the check-box beside the text and click the "OK" button. Now save a sound file ("AudioFile.ogg") into your project's directory. Finally, add the code below to the top of your start-up forms class file. When you start your project you should hear a sound.

 PUBLIC AudioFile AS Sound
 
 PUBLIC SUB Form_Open()
 
   AudioFile = NEW Sound("audiofile.ogg")
 
   AudioFile.Play   
 
 END

Gambas Project Examples

Gambler

Gambler Screenshot
Enlarge
Gambler Screenshot

Gambler is a probability example built with Gambas 2.7.0, and tested on Fedora 9 GNU/Linux. It allows you to gamble with virtual money (represented by a variable) using probability to simulate the throw of a dice. Aspiring Gambas programmers should find this example useful in learning how to use probability and variable manipulation. The example is released Free Software under version three of the GNU General Public License.

Walk About

Walk About Screenshot
Enlarge
Walk About Screenshot

Walk About is a movement example built with Gambas 2.7.0, and tested on Fedora 9 GNU/Linux. It allows you to move a player (represented by picture box) around, as well as being blocked by walls. Aspiring Gambas programmers should find this example useful in learning how to use coordinates for object movement and control. The example is released Free Software under version three of the GNU General Public License.

String Fun

String Fun is a command line example built with Gambas 2.7.0, and tested on Fedora 9 GNU/Linux. It allows you to manipulate Strings to create a humorous paragraph. Aspiring Gambas programmers should find this example useful in learning how to manipulate strings and create simple command line programs. The example is released Free Software under version three of the GNU General Public License.

Mouse Movement

PPC Screenshot
Enlarge
PPC Screenshot

A tech demo created for the Gambas Genie engine project tested in Fedora 8 GNU/Linux. It shows you how to make a selectable object, and how to make it so you can drop a movement point with your mouse which the object will move too. The example is released Free Software under version three of the GNU General Public License.

Piga's Pumpkin Carving

Although not exactly a technical demo, it is still a fairly small program, though expanded in its latest version. It will teach you the basics about using drawing areas, which support a wide variety of graphics features, including transparency! The example is released Free Software under the GNU General Public License except for the "Spooky Pumpkin Head" track which is used under permission, and can be freely used and redistributed as long as the permission notice is on the project's website and it is non-commercial.


Personal tools

sl
דומיין בעברית  דומיין  דומין  תוכנה לניהול  קשרי לקוחות  CRM, ניהול קשרי לקוחות  דומין בעברית  פורומים  ספרדית  גיבוי