Beginner Tutorial

In this beginner tutorial you will learn beginner programming and the basics of using the PlayBasic software.

If you have never programmed a game before, you should read this tutorial first.

Part 1 : Introduction to PlayBasic

PlayBasic is one of the fastest game programming languages. It excels at creating 2D games and it's capability to help you make 3D games is rapidly increasing.

Get PlayBasic

To get the most out of this beginner tutorial, you will need to get the PlayBasic software. If you don't already have PlayBasic you can get the FREE PlayBasic : Learning Edition at the official PlayBasic website.

A Simple Example

This is a very simple example of PlayBasic code.

Print "Hello World"
Sync
WaitKey

Copy and paste the code into the PlayBasic Code editor and Press F5.

Once you're done, press the Escape Key and return to this tutorial.

Here is a short explanation of what each line of code in the above example does :

  • Print displays Hello World to the game screen.
  • Sync makes it visible to the player.
  • WaitKey waits until the user presses a key.

Part 2 : Game Code Structure

The source code of all games are structured in three phases :

Game Code Structure
Name Does
Initialize Set up the game to run.
Game Loop Repeat code to keep game running.
Terminate Perform cleanup and shut down the game.

This is your basic code structure, including the game loop you will see in most PlayBasic tutorials and examples :

SetFPS 60

Do
    Cls 0

    Sync
Loop

This is the core structure.

To illustrate all the very basic parts of code structure, we're going to flesh it out a bit.

SetFPS 60

Constant green    = $00FF00
Constant yellow   = $FFFF00
Constant white    = $FFFFFF

Do
    Ink white
    If LeftKey()  Then Ink yellow
    If RightKey() Then Ink green

    Cls 0
        Print "Hello"
    Sync
Loop

End

Copy and paste the code into the PlayBasic Code editor and Press F5.

Use the Arrow Keys to change the color of the word "Hello."

Press the Escape Key to exit the example and return

Initialize

Initialize essentially means "prepare".

In the Initialize phase of the game code you are preparing your game to run.

You prepare a game to run by setting options and creating things your game will need to run.

This is the entire initialization section of the example code :

SetFPS 60

Constant green    = $00FF00
Constant yellow   = $FFFF00
Constant white    = $FFFFFF

The first part line in the Initialize phase of our example is :

SetFPS 60

This limits the frame rate to 60 frames per second.

The second part we create a few named colors.

Constant green    = $00FF00
Constant yellow   = $FFFF00
Constant white    = $FFFFFF

These colors are exactly the same as HTML colors.

Game Loop

The game loop repeats the main game code over and over to keep the game running.

All this code is part of the main game loop :

Do
    Ink white
    If LeftKey()  Then Ink yellow
    If RightKey() Then Ink green

    Cls 0
        Print "Hello"
    Sync
Loop

The game loop starts at the Do and ends at the Loop.

Do

Loop

The code that is between these two commands is repeated until the user presses the Escape key.

This simple game loop has two parts :

  • Game Logic
  • Draw a Frame

Game Logic

The game logic in this example makes the text one of three colors : white / yellow / or green.

Ink white
If LeftKey()  Then Ink yellow
If RightKey() Then Ink green

Ink is used to change the text color.

Ink white

In the game object, first the Ink color is set to white. That way, if the player doesn't press any key, the text will be white.

If LeftKey() Then Ink yellow

This does exactly what you think it does: If the player presses the Left Key, then the Ink color is set to yellow.

If RightKey() Then Ink green

It's so easy, isn't it? Let's move on.

Drawing a Frame

Cls 0 and Sync work together to draw a frame.

Cls 0

Sync

Cls 0 clears the screen.

Sync makes the frame visible to the player.

Each time the game loop is repeated, one frame is drawn.

Important: Make sure you put all your code that draws to the screen between Cls 0 and Sync. Otherwise it will not be seen by the player.

Because the Ink color has been set by the game logic, the only thing to do is display the word "Hello."

Cls 0
    Print "Hello"
Sync

Termination

When the player presses the Escape Key, the game shuts down.

In some game programming languages, this is where you would do clean up.

PlayBasic doesn't have this problem. However, there may be cases in the future when the termination phase will come in useful, so we stuck the optional End command after the game just to let you know about the termination phase.

End

Part 3 : Making Things Move

Let's make something move. Here's the code for this part.

; INITIALIZATION ----------------------------------------

; set the frame rate to 60 frames per second
SetFPS 60

; game object starting position
x = 400
y = 300

; GAME LOOP ----------------------------------------------

; start of game loop
Do

    ; GAME LOGIC ------------------------------------------

    ; player control
    If UpKey()    Then y = y + -2
    If DownKey()  Then y = y +  2
    If LeftKey()  Then x = x + -2
    If RightKey() Then x = x +  2

    ; draw a frame
    Cls 0

            ; draw current position
            Dot x, y

    Sync
Loop

Use the Arrow Keys to control the dot.

Comments

Everything after a semicolon ; is ignored by PlayBasic.

; INITIALIZATION ----------------------------------------

; set the frame rate to 60 frames per second
SetFPS 60

Comments like these give programmers like us a way to mark off sections of our code and leave notes in it for tutorials like this one. :)

Starting Position

; game object starting position
x = 400
y = 300

Game Controls

; player control
If UpKey()    Then y = y + -2
If DownKey()  Then y = y +  2
If LeftKey()  Then x = x + -2
If RightKey() Then x = x +  2

Drawing the Dot

Dot x, y
Categories: Tutorials
page tags: beginner tutorial