Tutorial: Graphics

This tutorial will introduce you to displaying graphics in PlayBasic

The term graphics includes anything on the screen seen by your player.

Drawing a Frame

All graphics must be drawn between Cls and Sync or they won't be seen.

SetFPS 60

; define colors
Constant RED     = $FF0000
Constant GREEN = $00FF00 
Constant BLUE  = $0000FF

Do
    CircleC 10, 5, 5, True, RED

    Cls 0
        CircleC 10, 10, 5, True, GREEN
    Sync

    CircleC 10, 29, 5, True, BLUE
Loop

Cls clears the screen.

Anything drawn before it is cleared and therefore not seen.

Sync updates the screen.

Anything drawn after it doesn't appear visible to the player.

Color

Ink

Drawing Primitive Shapes

Images

Sprites

Load and Display a Sprite

Simple Animation

Locking Buffers

Doing a lot of drawing to the screen all at once can slow the game down.

SetFPS 60
LoadFont "Arial", 1, 20, 0

sw = GetScreenWidth()
sh = GetScreenHeight()

Do
    Cls 0
        For i = 1 to 5000
            x  = RndRange(0,sw)
            y  = RndRange(20,sh)
            r  = RndRange(0,10)
            rd = RndRange(0,255)
               gr = RndRange(0,255)
               bl = RndRange(0,255)

              CircleC x, y, r, True, RGB(rd, gr, bl)
          Next
      Print "Game Speed : " + Str$(FPS()) + " out of 60"
      Sync
Loop

To speed the game up, you can lock the buffer while you are drawing chunks of graphics.

SetFPS 60
LoadFont "Arial", 1, 20, 0

sw = GetScreenWidth()
sh = GetScreenHeight()

Do
    Cls 0
        LockBuffer
            For i = 1 to 5000
                x  = RndRange(0,sw)
                y  = RndRange(20,sh)
                r  = RndRange(0,10)
                rd = RndRange(0,255)
               gr = RndRange(0,255)
               bl = RndRange(0,255)

                  CircleC x, y, r, True, RGB(rd, gr, bl)
              Next
          UnLockBuffer
          Print "Game Speed : " + Str$(FPS()) + " out of 60"
      Sync
Loop
Categories: Graphics : Graphics Tutorials : Tutorials
page tags: _wip