Variables

 

There are three types of variables in CoolBasic:

 

Integers

Use these every time you can. All variables are integers by default.

Example:

lives = 1

lives = lives + 5

Print lives

 

Floats

Use these when you want to do calculations with the decimal point. You need to express the variable with the tailing slash # the first time you use it.

Example:

speed# = 0

speed = speed – friction#

 

 

Strings

The strings are the only way to store characters and text. You must specify a dollar $ at the end of the variable name (this goes for the first time similarly to Float variables)

Example:

name$ = "James Bond"

Print name

 

Also notice, that you can't perform regular calculations to strings. The only legal operation between two strings is the union, the + -operator. You can merge two strings like this:

 

FirstName$ = "James"

LastName$ = "Bond"

FullName$ = FirstName + " " + LastName

 

 

You can't change a variable's type once it's defined i.e. You can't first express 'lives' and then 'lives#' a variable. Variable names are case insensitive, so "NaMe" is the same as "namE". A variable name can contain characters and digits. However, you can't have a digit as the first character. Also _ is supported.

 

Introducing variables

You can do this via the Dim-command:

Dim lives As Integer

Dim speed As Float

Dim name As String

 

After which you can use them without the postfix, ex. Print lives

 

Global Variables

If you are to write Functions to your program, you might want to introduce certain variables global. This means the functions can 'see' them as well as modify them. Typically, it's good to make image/object/sound etc. variables global.

 

 

Comments

 

Comments can be written in three ways:

 

'This is comment

 

// This is comment

 

REMSTART

    all this

    is

    a comment

REMEND

 

 

Operators

 

(,)                               Brackets. Everything in these will be executed first 2*(5+6)=22

+, -                             Absolute value and unary minus -7     

^                                Rise to power of 2^5.     

*, /, Mod                      Multiply, divide and remainder

Shl, Shr, Sar                  Bitwise shift operators. More info in the command reference       

+,-                              Addition and subtraction. Addition works as union with strings

=,<>,<,>,<=,>=              Comparison operators     

And, Or, Xor                  Logical And, Or and exclusive Or        

Not                              Logical unry Not

 

 

Jumps

 

You can jump to a label with the Goto –command.

 

If you plan to return there from the original location, use GosubReturn instead.

1    Print "Program start"

2

3    Goto skip

4

5    Print "This text never gets printed"

6

7    skip:

9

10   Print "End of proram"

11

12   WaitKey

 

 

Constants

 

Use the Const-keyword to declare a constant. Constants are similar to variables, but their value can't be changed. They are also a bit faster in performance.

 

Const GRAVITY = 0.02

 

Constants don't take postfix # or $.

 

CoolBasic has some in-built constants including:

 

True           1

False           0

ON              1

OFF            0

PI               3.141592653

NULL           nullPointer for types

 

Key Constants

 

cbkeyesc = 1

cbkeyf1 = 59

cbkeyf2 = 60

cbkeyf3 = 61

cbkeyf4 = 62

cbkeyf5 = 63

cbkeyf6 = 64

cbkeyf7 = 65

cbkeyf8 = 66

cbkeyf9 = 67

cbkeyf10 = 68

cbkeyf11 = 87

cbkeyf12 = 88

cbkeyprint = 183

cbkeyscroll = 70

cbkeypause = 197

cbkeyoem102 = 86

cbkey1 = 2

cbkey2 = 3

cbkey3 = 4

cbkey4 = 5

cbkey5 = 6

cbkey6 = 7

cbkey7 = 8

cbkey8 = 9

cbkey9 = 10

cbkey0 = 11

cbkeyminus = 12

cbkeyequals = 13

cbkeybackspace = 14

cbkeyinsert = 210

cbkeyhome = 199

cbkeypgup = 201

cbkeynumlock = 69

cbkeydivide = 181

cbkeymultiply = 55

cbkeysubtract = 74

cbkeytab = 15

cbkeyq = 16

cbkeyw = 17

cbkeye = 18

cbkeyr = 19

cbkeyt = 20

cbkeyy = 21

cbkeyu = 22

cbkeyi = 23

cbkeyo = 24

cbkeyp = 25

cbkey] = 26

cbkey[ = 27

cbkeyreturn = 28

cbkeydel = 211

cbkeyend = 207

cbkeypgdown = 209

cbkeynum7 = 71

cbkeynum8 = 72

cbkeynum9 = 73

cbkeyadd = 78

cbkeycapslock = 58

cbkeya = 30

cbkeys = 31

cbkeyd = 32

cbkeyf = 33

cbkeyg = 34

cbkeyh = 35

cbkeyj = 36

cbkeyk = 37

cbkeyl = 38

cbkeysemicolon = 39

cbkeyapostrophe = 40

cbkeygrave = 41

cbkeynum4 = 75

cbkeynum5 = 76

cbkeynum6 = 77

cbkeylshift = 42

cbkeybackslash = 43

cbkeyz = 44

cbkeyx = 45

cbkeyc = 46

cbkeyv = 47

cbkeyb = 48

cbkeyn = 49

cbkeym = 50

cbkeycomma = 51

cbkeyperiod = 52

cbkeyslash = 53

cbkeyrshift = 54

cbkeynum1 = 79

cbkeynum2 = 80

cbkeynum3 = 81

cbkeyenter = 156

cbkeylcontrol = 29

cbkeylwin = 219

cbkeylalt = 56

cbkeyspace = 57

cbkeyralt = 184

cbkeyrwin = 220

cbkeyapps = 221

cbkeyrcontrol = 157

cbkeynum0 = 82

cbkeydecimal = 83

cbkeyup = 200

cbkeydown = 208

cbkeyleft = 203

cbkeyright = 205

 

Colour constants

 

Use like this:

 

Color cbRed

 

cbRed                    255,0,0               

cbOrange                 255,172,0             

cbYellow                 255,255,0             

cbGreen                  128,168,4             

cbBlue                   0,164,255             

cbPurple                 168,16,224            

cbLightRed               255,128,128           

cbPink                   253,167,171           

cbLightYellow                        255,255,192           

cbLightGreen             228,254,166           

cbLightBlue              123,222,251           

cbLightPurple                        207,185,237           

cbDarkRed                192,0,0               

cbDarkYellow             208,176,0             

cbDarkGreen             96,127,13             

cbDarkBlue               56,62,184             

cbDarkPurple             132,11,177            

cbCyan                   0,255,255             

cbMagenta                255,0,255             

cbBlack                  0,0,0                 

cbWhite                  255,255,255           

cbSilver                 192,192,192           

cbGold                   190,172,86            

cbDark                   64,64,64              

cbWhiteSkin              255,212,129           

cbBlackSkin              123,90,0

 

Include files

 

You can merge other sources to the main file with the Include-command:

 

Include "GameFunctions.cb"

 

Basic code structures

 

Conditions

 

If ... Then

 

If {condition} Then {statements1} Else {statements2}

 

Check if the condition is True (non-zero), CoolBasic executes statements1. If the result was False (zero), CoolBasic will jump the Else-part and executes it. The Else-part is optional in which case False results in no statements executed. {statements} can include more than one statements, but they must be separated with the colon (:)

 

If {condition1}

    {statements1}

ElseIf {condition2}

    {statements2}

ElseIf {condition3}

    {statements3}

Else

    {statements4}

EndIf

 

 

This kind of If enables to execute multiple statements in block. The condition iterates all its If's and ElseIf's until the condition is True. if none of the conditions matched, the Else is executed (if it's provided).

Loops

 

While ... Wend

 

While {expression}

    {statements}

Wend

 

Generates a loop that is repeatedly executed as long as the condition in While-statement is True. When the condition evaluates False, the loop is broken.

 

Repeat ... Until

 

Repeat

    {statements}

Until {expression}

 

Generates a loop that is repeatedly executed until the condition in Until-statement is True. Notice, that the While-loop does not necessarily execute at all, while Repeat...Until always executes once or more.

 

For ... Next

 

For {variable}={start_value} To {end_value} Step {increment}

    {statements}

Next {variable}

 

First assigns start_value to the variable, then repeat For…Next for n times.

Step is optional. You can also provide negative step value, like:

 

For i = 10 To 1 Step -1

    Print i

Next

 

For ... Each

 

For {type_variable}=Each {type_name}

    {statements}

Next {type_variable}

 

This is more unconventional an iterator. It iterates all TYPE elements. Notice that the variable must be introduced as TYPE. More information later in this document.

 

Loops can be broken with the Exit-command.

 

Select ... Case

 

Select {expression}

    Case {values1}

        {statements1}

    Case {values2}

        {statements2}

    Default

        {statements3}

End Select

 

 

First evaluate expression. Then compare its result to each Case. If match -> execute that Case-block. This is very similar to If...ElseIf...Else...Endif.

 

You can provide more than one Case-value separated with commas.

 

 

Loading objects, images, sounds etc.

 

In CoolBasic, you load images and other media to variables. That variable becomes a handle of that media. It contains a pointer where the media is saved in computer memory.

 

sndBlaster = LoadSound ("Blaster.wav")

 

If Not sndBlaster Then MakeError "Can't load sound."

 

PlaySound sndBlaster

 

Same thing goes for images, objects, fonts, tilemaps etc.

 

If you accidentally try to draw an image with an invalid variable, you'll get "Memory Access Violation". The same goes for other types of Media as well.

 

Media should always be deleted from memory when you don't need them anymore.

 

DeleteObject car

 

CoolBasic, however, automatically frees all used memory and media on exit.

 

 

Arrays

 

Arrays act like variables, but can have multiple dimensions. The same name policy applies to them. The same data types apply to them. The same limits apply to them.

 

You can declare 5 dimensions maximum.

 

You provide the type identifier # or $ only in the Dim-statement. This postfix must be left off when you later use the array.

 

Arrays are always global, so any function can access them.

 

An array always has a set of brackets at the end of its name. This region has the parameters that point to right cell according to dimensions.

 

Examples:

 

1    Dim ENEMIES (100, 2)

2

3    For i = 1 to 100

4

5        ENEMIES (i, 1) = 400

6        ENEMIES (i, 2) = 200

7

8    Next i

 

DIM Names$ (20)

DIM Names (20) As String

 

Print Names(5)

 

 

An array can later be resized with the Redim-command as long as its number of dimensions doesn't change.

 

 

User-defined types

 

Types are good. Use Types.

With Types you can make collections of enemies, bullets, bots, power-ups – anything. This is even more flexible way to handle large quantities of objects than arrays that hold their X, Y etc.

 

You should declare all types at the top of your source code. This is done in the Type...EndType –block that has Fields inside. Fields are variables only accessible through a type variable – a member.

 

You can then create instances of this type. A type works as if it was a template that can be made clones from. Each member has its own copies of those variables.

 

All members made out of the same type form a collection. In programming these are called linked lists. You can freely delete members from the middle or any other location in a linked list. You can also add an element (member) to any location in the linked list. This is something you can't do with arrays so easily.

 

Example:

 

First you have elements                    1,2,3,4,5

Then you delete element 3                 1,2,4,5

Then you add two elements               1,2,4,5,6,7

Then you clear the list                     

Then you add one element                 8

 

 

What a...

Let's take a little example. Your game is a top-down shooter, and needs a bullet management system. When the player chooses to fire, a new bullet is created. Bullets move at their own and they can hit walls.

 

First declare a type for it

 

Type BULLET

    Field obj

EndType

 

When the player want's to shoot

 

bul.BULLET = New(BULLET)

bul\obj = CloneObject(objBullet) // objBullet is an object that was loaded at game start

CloneObjectPosition bul\obj, hero

CloneObjectOrientation bul\obj, hero

ResetObjectCollision bul\obj

 

But all the bullets must be updated somewhere in the main loop

 

For bul.BULLET = Each BULLET

    MoveObject bul\obj

 

    // If it hit something, delete it

    If CountCollisions(bul\obj)

        DeleteObject bul\obj

        Delete bul

    EndIf

Next bul

 

This example demonstrates the creation, updating and destroying a bullet type. Easy, huh.

Also take a look at First(), Last(), Before() and After() in the command Reference.