GameBASIC Reference

Complete command and function reference for GameBASIC.

Overview

Statements

Base64 Data Management

Base64 Data Panel

Use the Base64 Data panel to manage large Base64 strings separately from your program code. Each entry has a name and a Base64 string.

// Example usage
LET SPRITE = "MY_SPRITE"
BANKLOADSPRITE(1, SPRITE, 16, 16)
// 'MY_SPRITE' is a Base64 entry managed in the panel
GETBASE64(name)

Returns the Base64 string for the entry named name from the Base64 Data panel.

// Example usage
  LET SPRITE = GETBASE64("MY_SPRITE")
  BANKLOADSPRITE(1, SPRITE, 16, 16)
  // 'MY_SPRITE' is a Base64 entry managed in the panel
REM comment

Single-line comment. Ignored during execution.

REM This is a comment
// Double-slash also works
LET name = expr or name = expr

name: Variable name | expr: Any expression

Assign a value to a variable.

Scope rules:

X = 100 // Global variable

FUNC TEST()
  X = X + 10 // Modifies global X (now 110)
  LET Y = 20 // Creates local Y
END

TEST()
PRINT(X) // 110
PRINT(Y) // Error: Y is local to TEST
DIM name(size)

name: Array name | size: Maximum index (0-indexed)

Create a one-dimensional array.

Scope rules:

DIM GLOBAL(10) // Global array

FUNC TEST()
  DIM LOCAL(5) // Local to TEST
  GLOBAL(0) = 100 // Can access global
  LOCAL(0) = 50 // Local only
END

GLOBAL(0) = 1 // OK
LOCAL(0) = 1 // Error: LOCAL not defined here
DATA value, value, ...

Define data entries. Use READ to retrieve them sequentially.

DATA 10, 20, 30, 40
READ A, B
PRINT(A, B)  // Outputs: 10 20
READ var, var, ...

Read next values from DATA into variables.

DATA 100, 200
READ X, Y
PRINT(X, Y)
RESTORE [id]

id: Optional DATA label

Reset DATA pointer to start (or specific label).

DATA VALUES, 1, 2, 3
RESTORE VALUES
READ A, B, C
IF condition THEN ... ENDIF

Conditional block. ELSEIF and ELSE are optional.

// Multi-line form
IF X > 100 THEN
  PRINT("HIGH")
ELSEIF X > 50 THEN
  PRINT("MEDIUM")
ELSE
  PRINT("LOW")
ENDIF

// Single-line form (no ENDIF required when THEN is followed by a single statement)
IF X == 2 THEN PRINT("HELLO")
FOR var = start TO end [STEP step]

var: Loop variable | start, end: Range | step: Increment (default 1)

Counted loop. End with NEXT var.

FOR I = 0 TO 10
  PRINT(I)
NEXT

FOR J = 10 TO 0 STEP -1
  PRINT(J)
NEXT
WHILE condition ... ENDWHILE

Loop while condition is true.

X = 0
WHILE X < 100
  X = X + 1
ENDWHILE
FUNC name(param1, param2, ...) ... END

Define a user function. Call with name(args) or FUNC name(args).

Variable scope in functions:

SCORE = 0 // Global

FUNC ADDSCORE(POINTS)
  SCORE = SCORE + POINTS // Modifies global
  LET BONUS = POINTS * 2 // Local only
END

ADDSCORE(10)
PRINT(SCORE) // 10
RETURN [expr]

Return from function with optional value.

END

Stop program execution immediately.

Text Output

PRINT(value, value, ...)

value: Any expression or string

Print values to screen output (wraps long text). Multiple values separated by spaces.

PRINT("Score:", 1000)
PRINT("Multiple values", "with", "spaces")
TEXT(x, y, text, [color])

x, y: Pixel coordinates | text: String to draw | color: Optional CSS color

Draw text at a specific position.

TEXT(10, 20, "Hello World", "#ffff00")
TEXT(50, 100, "Default color")
TEXTCENTER(text, [y], [color])

text: String to draw | y: Optional Y position (default: centered vertically) | color: Optional CSS color

Draw text centered horizontally.

TEXTCENTER("TITLE")
TEXTCENTER("Score: 1000", 50, "#00ff00")
SETTEXTCOLOR(color)

color: CSS color string

Set the default text and drawing color.

SETTEXTCOLOR("#ffcc00")
PRINT("This is golden")
SETTEXTOUTLINE(color)

color: CSS color string, or null/empty to disable

Add an outline/stroke around text for better visibility.

SETTEXTCOLOR("#ffffff")
SETTEXTOUTLINE("#000000")
TEXT(10, 10, "Outlined text")
SETTEXTSCALE(x, y)

x, y: Scale factors (e.g., 0.5, 1, 2)

Set text scaling for width and height.

SETTEXTSCALE(0.5, 1)  // 4x8 text
SETTEXTSCALE(1, 2)    // 8x16 text
SETTEXTSCALEX(x)

x: Width scale factor

Set text width scale only.

SETTEXTSCALEX(0.5)
PRINT("Half-width text")
SETTEXTSCALEY(y)

y: Height scale factor

Set text height scale only.

SETTEXTSCALEY(1.5)
TEXT(10, 10, "Taller text")
SETPENCOLOR(color)

Alias for SETTEXTCOLOR().

Screen Control

CLEARSCREEN()

Clear all draw operations and reset display.

CLEARSCREEN()
PRINT("Fresh start")
CLS()

Clear text output lines only.

FLIP()

Render all queued drawing operations to the screen. Typically called once per frame.

FOR I = 1 TO 10
  CLEARSCREEN()
  PRINT("Frame", I)
  FLIP()
  WAIT(0.1)
NEXT
SCREENBG(color)

color: CSS color string

Set background color for the screen (or active window).

SCREENBG("#1a1a2e")
FLIP()
BORDER(color)

color: CSS color string

Set the border color outside the 320×200 canvas.

BORDER("#ff0000")
SETFPS(fps)

fps: Frames per second (numeric)

Set target frame rate.

SETFPS(30)  // 30 FPS
WHILE 1
  FLIP()
ENDWHILE
PIXELMODE(mode)

mode: 1 = pixel-perfect, 0 = smooth

Set rendering mode for shapes. Default is 1.

PIXELMODE(1)
FELLIPSE(160, 100, 50, 30)

Windows (Clipping Regions)

Windows define rectangular drawing areas. All drawing within a window is clipped to its bounds.

WINDOW(id, x, y, w, h, [radius])

id: Window identifier | x, y, w, h: Rectangle bounds | radius: Optional corner radius

Define a new window.

WINDOW("HUD", 0, 0, 320, 30)
WINDOW("GAME", 0, 30, 320, 170, 4)
USEWINDOW(id)

id: Window identifier

Activate a window for drawing.

USEWINDOW("HUD")
TEXT(5, 5, "Score: 1000")
USEWINDOW("GAME")
LINE(10, 10, 100, 100)
NOWINDOW()

Disable window clipping (draw to full screen).

WINDOWX(), WINDOWY(), WINDOWW(), WINDOWH()

Get active window bounds (x, y, width, height).

W = WINDOWW()
H = WINDOWH()
PRINT("Window size:", W, "x", H)

Shapes & Drawing

LINE(x1, y1, x2, y2, [color])

x1, y1, x2, y2: Start and end points | color: Optional CSS color

Draw a line between two points.

LINE(10, 10, 100, 100, "#ff0000")
LINE(50, 50, 200, 50)
RECT(x, y, w, h, [color])

x, y: Top-left corner | w, h: Width and height | color: Optional CSS color

Draw a rectangle outline.

RECT(10, 10, 100, 50, "#00ff00")
FRECT(x, y, w, h, [color])

Draw a filled rectangle.

FRECT(50, 50, 50, 50, "#ffff00")
ELLIPSE(cx, cy, rx, ry, [color])

cx, cy: Center | rx, ry: Horizontal and vertical radii | color: Optional CSS color

Draw an ellipse outline.

ELLIPSE(160, 100, 80, 50, "#0088ff")
FELLIPSE(cx, cy, rx, ry, [color])

Draw a filled ellipse.

FELLIPSE(160, 100, 50, 30, "#ff00ff")
ARC(cx, cy, r, startDeg, endDeg, [color])

cx, cy: Center | r: Radius | startDeg, endDeg: Angles in degrees

Draw an arc outline.

ARC(160, 100, 50, 0, 180, "#ffaa00")
FARC(cx, cy, r, startDeg, endDeg, [color])

Draw a filled arc (pie slice).

POLY(x1, y1, x2, y2, ..., [color])

x1, y1, x2, y2, ...: Polygon vertices

Draw a polygon outline.

POLY(100, 50, 150, 100, 50, 100, "#00aa00")
FPOLY(x1, y1, x2, y2, ..., [color])

Draw a filled polygon.

Sprites

SPRITE(name, w, h, color)

name: Sprite identifier | w, h: Width and height | color: CSS color

Create a solid-color sprite.

SPRITE("PLAYER", 16, 16, "#ff0000")
SPRITE("ENEMY", 8, 8, "#00ff00")
SPRITE(name, imageUrl)

Create a sprite from an image URL.

SPRITE("HERO", "https://example.com/hero.png")
DRAWSPRITE(name, x, y)

name: Sprite identifier | x, y: Draw position

Draw sprite at the specified position.

DRAWSPRITE("PLAYER", 100, 100)
MOVESPRITE(name, x, y)

Set sprite position (compatibility alias for DRAWSPRITE).

SPRITEX(name), SPRITEY(name)

Get current sprite position (x or y).

X = SPRITEX("PLAYER")
Y = SPRITEY("PLAYER")
SETSPRITEFRAME(name, frameIndex)

frameIndex: Frame number (0-indexed)

Change the current frame of a sprite bank.

SETSPRITEFRAME("WALK", 0)
WAIT(0.1)
SETSPRITEFRAME("WALK", 1)
SPRITEBANK(name, bankId, frameIndex)

name: Sprite identifier | bankId: Bank with sprite sheet | frameIndex: Starting frame

Create a sprite from a sprite bank.

SETSPRITETRANSFORM(name, scaleX, scaleY, rotationDeg, flipX, flipY)

scaleX, scaleY: Scale factors | rotationDeg: Rotation in degrees | flipX, flipY: Flip flags (0 or 1)

Transform a sprite.

SETSPRITETRANSFORM("PLAYER", 2, 2, 45, 0, 0)
SETHOTSPOT(name, hotX, hotY)

hotX, hotY: Offset from sprite origin

Set the sprite rotation/positioning origin point.

Collision Detection

COLLIDES(nameA, ax, ay, nameB, bx, by)

nameA, nameB: Sprite names | ax, ay, bx, by: Positions

Check AABB collision between two sprites. Returns 1 if colliding, 0 otherwise.

IF COLLIDES("PLAYER", PX, PY, "ENEMY", EX, EY) THEN
  PRINT("HIT!")
ENDIF
RECTHIT(x1, y1, w1, h1, x2, y2, w2, h2)

Check if two rectangles overlap.

IF RECTHIT(10, 10, 50, 50, 40, 40, 50, 50) THEN
  PRINT("Rectangles overlap")
ENDIF
ELLIPSEHIT(cx, cy, rx, ry, px, py)

cx, cy, rx, ry: Ellipse center and radii | px, py: Point to test

Check if a point is inside an ellipse.

Banks & Memory

BANKCREATE(id, size)

id: Bank identifier (number or string) | size: Number of bytes

Create a byte bank (Uint8Array).

BANKCREATE("BUFFER", 256000)
BANKCREATE(1, 1024)
BANKPEEK(id, index)

id: Bank identifier | index: Byte offset

Read a byte from bank (0-255). Returns 0 if out of bounds.

VALUE = BANKPEEK("BUFFER", 10)
BANKPOKE(id, index, value)

value: Byte to write (clamped to 0-255)

Write a byte to bank.

BANKPOKE("BUFFER", 10, 255)
BANKLOAD(id, data)

data: Hex string (space-separated) or BASE64: prefix

Load pre-formatted data into a bank.

BANKLOAD(1, "00 FF 1A 2B")
BANKLOAD(2, "BASE64:AAECAwQ=")
BANKCOPY(srcId, dstId)

Copy one bank to another.

BANKFREE(id)

Delete a bank and free its memory.

Screen Capture & Playback

SCREENCAPTURE(bankId)

bankId: Bank identifier (requires 256,000 bytes for full screen)

Save the current screen to a bank (320×200 RGBA = 256,000 bytes).

BANKCREATE("SCREEN", 256000)
CLEARSCREEN()
LINE(0, 0, 319, 199, "#ffffff")
FLIP()
SCREENCAPTURE("SCREEN")
SCREENDRAW(bankId)

Restore a full screen from a bank.

SCREENDRAW("SCREEN")
SCREENDRAWRECT(bankId, x, y, w, h, [flipH], [flipV])

flipH, flipV: 1 to flip horizontally/vertically, 0 for normal

Draw a captured screen as a scaled rectangle, optionally flipped.

SCREENDRAWRECT("SCREEN", 0, 0, 160, 100)  // Half-size
SCREENDRAWRECT("SCREEN", 160, 0, 160, 100, 1)  // Flipped horizontally
SCREENDRAWRECT("SCREEN", 0, 100, 160, 100, 0, 1)  // Flipped vertically

Input & Control

INPUT([prompt], [cursor], [maxLen])

prompt: Optional message to display | cursor: Single character to show (default "_") | maxLen: Optional maximum characters

Display a prompt and wait for text input. Returns the entered string.

NAME = INPUT("What is your name?")
AGE = INPUT("Age:", ">", 3)
PRINT("Hello,", NAME)
KEYDOWN(code)

code: Key code string (e.g., "Space", "ArrowLeft", "a")

Check if a key is currently pressed. Returns 1 if down, 0 if up.

IF KEYDOWN("ArrowUp") THEN Y = Y - 1 ENDIF
IF KEYDOWN("ArrowUp") THEN Y = Y - 1
IF KEYDOWN("Space") THEN PRINT("JUMP")
LASTKEY()

Get the last key that was pressed (as a string).

KEY = LASTKEY()
IF KEY = "Escape" THEN END
WAIT(seconds)

seconds: Duration to pause

Pause execution for a number of seconds (fractional).

PRINT("Starting...")
WAIT(1)
PRINT("Done!")

Math & Utilities

Tilemaps

TILEMAP_CREATE(id, cols, rows)

Create a named tilemap grid with given columns and rows.

TILEMAP_CREATE("ROOM1", 20, 15)
TILEMAP_SET(id, x, y, value)

Set the tile value at coordinates (x,y) in the named map.

TILEMAP_SET("ROOM1", 3, 2, 5)
TILEMAP_GET(id, x, y)

Get the numeric tile value at (x,y).

t = TILEMAP_GET("ROOM1", 3, 2)
PRINT(t)
TILEMAP_FILL(id, value)

Fill the entire map with a value.

TILEMAP_FILL("ROOM1", 0)
TILEMAP_DRAW(id, x, y, bankId, [scale])

Draw the tilemap using frames from a sprite bank loaded with BANKLOADSPRITE. Each tile value is taken as a frame index into the bank. Optional scale scales tiles on display.

// Assume bank 1 contains a tileset with frames sized 16x16
TILEMAP_DRAW("ROOM1", 0, 0, 1, 2) // draws ROOM1 at (0,0) scaled 2x
TILEMAP_COLLIDE(id, x, y, value)

Return 1 if the tile at (x,y) equals value, otherwise 0. Coordinates are in tile units.

IF TILEMAP_COLLIDE("ROOM1", PX, PY, 2) THEN PRINT("Hit wall")
TILEMAP_COLLIDE_RECT(id, x, y, w, h, value)

Return 1 if any tile inside the rectangle (x,y,w,h) equals value, otherwise 0.

IF TILEMAP_COLLIDE_RECT("ROOM1", PX-1, PY-1, 3, 3, 5) THEN PRINT("Overlap tile 5")
TILEMAP_COLLIDE_PIXEL(id, px, py, value, [tileSize])

Pixel-aware collision: returns 1 if the tile under pixel (px,py) equals value. Optional tileSize (default 8) specifies tile width/height in pixels.

// Check pixel 8,8 using 8px tiles
IF TILEMAP_COLLIDE_PIXEL("ROOM1", 8, 8, 1, 8) THEN PRINT("Hit")
TILEMAP_COLLIDE_RECT_PIXEL(id, px, py, pw, ph, value, [tileSize])

Pixel-aware rectangle collision: returns 1 if any tile overlapped by the pixel rectangle equals value.

IF TILEMAP_COLLIDE_RECT_PIXEL("ROOM1", 8, 8, 8, 8, 1) THEN PRINT("Overlap")
SEED(value)

value: Number used to seed the RNG

Set a deterministic seed for RND(). If not called, RND uses the browser's RNG.

SEED(1234)
A = RND()
B = RND(10)
RND([max])

max: Upper limit (exclusive)

Generate a random float in the range [0, max). Default max=1.

X = RND(320)
Y = RND(200)
DICE = INT(RND(6)) + 1
INT(value)

Floor to integer (truncate decimal part).

A = INT(3.7)  // Returns 3
B = INT(-2.5)  // Returns -3
HEX(value)

value: Number 0-255

Convert a byte value to a 2-character uppercase hex string.

R = 255
G = 0
B = 127
COLOR = "#" & HEX(R) & HEX(G) & HEX(B)  // "#FF007F"
COS(radians), SIN(radians)

Trigonometric functions. Input in radians.

X = 160 + COS(ANGLE) * 50
Y = 100 + SIN(ANGLE) * 30
ANGLE = ANGLE + 0.05
STRLEN(text)

Get the length of a string.

LEN = STRLEN("Hello")  // Returns 5

Sound

SOUND(wave, note, seconds)

wave: 0=sine, 1=sawtooth, 2=square, 4=noise | note: MIDI note number (0=middle C) | seconds: Duration

Play a sound. For noise, the note parameter controls filter pitch.

SOUND(0, 0, 0.2)    // Sine wave, middle C
SOUND(2, 12, 0.1)   // Square wave, one octave up
SOUND(4, 5, 0.3)    // White noise

Font

SETFONT(bankId)

bankId: Bank containing 1024 bytes (128 chars × 8 bytes per char)

Set a custom font from a bank.

String Concatenation

Use the ampersand operator & to join strings:

MESSAGE = "Score: " & SCORE & " points"
COLOR = "#" & HEX(R) & HEX(G) & HEX(B)

Tips & Tricks