EkBass commited on
Commit
fb74fd1
·
verified ·
1 Parent(s): 1af859e

Update BazzBasic-AI-guide-19032026.md

Browse files
Files changed (1) hide show
  1. BazzBasic-AI-guide-19032026.md +763 -758
BazzBasic-AI-guide-19032026.md CHANGED
@@ -1,759 +1,764 @@
1
- > METADATA:
2
- > Name: BazzBasic
3
-
4
- > Description: BazzBasic BASIC interpreter language reference. Use when writing, debugging, or explaining BazzBasic code (.bas files). Triggers on BazzBasic syntax, BASIC programming with $ and # suffixes, SDL2 graphics in BASIC, or references to BazzBasic interpreter features
5
-
6
- > About: BazzBasic is built around one simple idea: starting programming should feel nice and even fun. Ease of learning, comfort of exploration and small but important moments of success. Just like the classic BASICs of decades past, but with a fresh and modern feel.
7
-
8
- > Purpose: This guide has been provided with the idea that it would be easy and efficient for a modern AI to use this guide and through this either guide a new programmer to the secrets of BazzBasic or, if necessary, generate code himself.
9
-
10
- > Version: This guide is written for BazzBasic version 1.0 and is updated 19.03.2026 09:51 Finnish time.
11
-
12
- ---
13
-
14
- # BazzBasic Language Reference
15
-
16
- BazzBasic is a BASIC interpreter for .NET 10 with SDL2 graphics and SDL2_mixer sound support. It is not a clone of any previous BASIC — it aims to be easy, fun, and modern. Released under MIT license.
17
-
18
- **Version:** 1.0 (Released February 22, 2026)
19
- **Author:** Kristian Virtanen (krisu.virtanen@gmail.com)
20
- **Platform:** Windows (x64 primary); Linux/macOS possible with effort
21
- **Dependencies:** SDL2.dll, SDL2_mixer (bundled).
22
- **Github:** https://github.com/EkBass/BazzBasic
23
- **Homepage:** https://ekbass.github.io/BazzBasic/
24
- **Manual:** "https://ekbass.github.io/BazzBasic/manual/#/"
25
- **Examples:** "https://github.com/EkBass/BazzBasic/tree/main/Examples"
26
- **Github_repo:** "https://github.com/EkBass/BazzBasic"
27
- **Github_discussions:** "https://github.com/EkBass/BazzBasic/discussions"
28
- **Discord_channel:** "https://discord.com/channels/682603735515529216/1464283741919907932"
29
- **Thinbasic subforum:** "https://www.thinbasic.com/community/forumdisplay.php?401-BazzBasic"
30
-
31
- ## Few examples:
32
- **raycaster_3d:** https://raw.githubusercontent.com/EkBass/BazzBasic/refs/heads/main/Examples/raycaster_3d_optimized.bas
33
- **voxel_terrain:** https://raw.githubusercontent.com/EkBass/BazzBasic/refs/heads/main/Examples/voxel_terrain.bas
34
- **sprite load:** https://github.com/EkBass/BazzBasic/blob/main/Examples/countdown_demo.bas
35
- **Eliza:** https://github.com/EkBass/BazzBasic/blob/main/Examples/Eliza.bas
36
-
37
- ## CLI Usage
38
-
39
- | Command | Action |
40
- |---------|--------|
41
- | `bazzbasic.exe` | Launch IDE |
42
- | `bazzbasic.exe file.bas` | Run program |
43
- | `bazzbasic.exe -exe file.bas` | Create standalone .exe |
44
- | `bazzbasic.exe -lib file.bas` | Create tokenized library (.bb) |
45
-
46
- IDE shortcuts: F5 run, Ctrl+N/O/S new/open/save, Ctrl+Shift+S save as, Ctrl+W close tab, Ctrl+F find, Ctrl+H replace.
47
-
48
- ### External syntax colors.
49
- BazzBasic IDE is developed just so double-clicking *bazzbasic.exe* would bring something in the screen of a newbie. A person can use it, but it stands no chance for more advanced editors.
50
-
51
- There are syntax color files for *Notepad++*, *Geany* and *Visual Studio Code* available at https://github.com/EkBass/BazzBasic/tree/main/extras
52
-
53
- ---
54
-
55
- ## Syntax Fundamentals
56
-
57
- ### Variables and Constants
58
- Forget traditional BASIC types. Suffixes in BazzBasic define mutability, not data type.
59
-
60
- Variables and arrays in BazzBasic are typeless: hold numbers or strings interchangeably
61
-
62
- - $ = Mutable Variable
63
- - # = Immutable Constant
64
-
65
-
66
- ```basic
67
- LET a$ ' Declare without value
68
- LET name$ = "Alice" ' Variable, string (mutable, $ suffix)
69
- LET age$ = 19 ' Variable, integer (mutable, $ suffix)
70
- LET price$ = 1.99 ' Variable, decimal (mutable, $ suffix)
71
- LET PI# = 3.14159 ' Constant (immutable, # suffix)
72
- LET x$, y$, z$ = 10 ' Multiple declaration
73
- ```
74
- - All variables require `$` suffix, constants require `#`
75
- - Typeless: hold numbers or strings interchangeably
76
- - Must declare with `LET` before use (except FOR/INPUT which auto-declare)
77
- - Assignment after declaration: `x$ = x$ + 1` (no LET needed)
78
- - **Case-insensitive**: `PRINT`, `print`, `Print` all work
79
- - Naming: letters, numbers, underscores; cannot start with number
80
-
81
- ### Comparison Behavior
82
- `"123" = 123` is TRUE (cross-type comparison), but slower keep types consistent.
83
-
84
- ### Scope
85
- - Main code shares one scope (even inside IF blocks)
86
- - `DEF FN` functions have completely isolated scope
87
- - Only global constants (`#`) are accessible inside functions
88
-
89
- ### Multiple Statements Per Line
90
- ```basic
91
- COLOR 14, 0 : CLS : PRINT "Hello"
92
- ```
93
-
94
- ### Comments
95
- ```basic
96
- REM This is a comment
97
- ' This is also a comment
98
- PRINT "Hello" ' Inline comment
99
- ```
100
-
101
- ### Escape Sequences in Strings
102
- | Sequence | Result |
103
- |----------|--------|
104
- | `\"` | Quote |
105
- | `\n` | Newline |
106
- | `\t` | Tab |
107
- | `\\` | Backslash |
108
-
109
- ---
110
-
111
- ## Arrays
112
- ```basic
113
- DIM scores$ ' Declare (must use DIM)
114
- DIM a$, b$, c$ ' Multiple declaration
115
- scores$(0) = 95 ' Numeric index (0-based)
116
- scores$("name") = "Alice" ' String key (associative)
117
- matrix$(0, 1) = "A2" ' Multi-dimensional
118
- data$(1, "header") = "Name" ' Mixed indexing
119
- ```
120
- - Array names must end with `$`
121
- - Fully dynamic: numeric, string, multi-dimensional, mixed indexing
122
- - Arrays **cannot** be passed directly to functions — pass values of individual elements
123
- - Accessing uninitialized elements is an error — check with `HASKEY` first
124
-
125
- ### Array Functions
126
- | Function | Description |
127
- |----------|-------------|
128
- | `LEN(arr$())` | Element count (note: empty parens) |
129
- | `HASKEY(arr$(key))` | 1 if exists, 0 if not |
130
- | `DELKEY arr$(key)` | Remove element |
131
- | `DELARRAY arr$` | Remove entire array (can re-DIM after) |
132
-
133
- ---
134
-
135
- ## Operators
136
-
137
- ### Arithmetic
138
- `+` (add/concatenate), `-`, `*`, `/` (returns float)
139
-
140
- ### Comparison
141
- `=` or `==`, `<>` or `!=`, `<`, `>`, `<=`, `>=`
142
-
143
- ### Logical
144
- `AND`, `OR`, `NOT`
145
-
146
- ### Precedence (high to low)
147
- `()` → `NOT` → `*`, `/` → `+`, `-` → comparisons → `AND` → `OR`
148
-
149
- ---
150
-
151
- ## User-Defined Functions
152
- ```basic
153
- DEF FN add$(a$, b$)
154
- RETURN a$ + b$
155
- END DEF
156
- PRINT FN add$(3, 4) ' Call with FN prefix
157
- ```
158
- - Function name **must** end with `$`, called with `FN` prefix
159
- - **Must be defined before called** (top of file or via INCLUDE)
160
- - Parameters passed **by value**
161
- - Completely isolated scope: no access to global variables, only global constants (`#`)
162
- - Labels inside functions are local — GOTO/GOSUB **cannot** jump outside (error)
163
- - Supports recursion
164
- - Tip: put functions in separate files, `INCLUDE` at program start
165
-
166
- ---
167
-
168
- ## Control Flow
169
-
170
- ### IF Statements
171
- ```basic
172
- ' Block IF (END IF and ENDIF both work)
173
- IF x$ > 10 THEN
174
- PRINT "big"
175
- ELSEIF x$ > 5 THEN
176
- PRINT "medium"
177
- ELSE
178
- PRINT "small"
179
- ENDIF
180
-
181
- ' One-line IF (GOTO/GOSUB only)
182
- IF lives$ = 0 THEN GOTO [game_over]
183
- IF key$ = KEY_ESC# THEN GOTO [menu] ELSE GOTO [continue]
184
- IF ready$ = 1 THEN GOSUB [start_game]
185
- ```
186
-
187
- ### FOR Loops
188
- ```basic
189
- FOR i$ = 1 TO 10 STEP 2 ' Auto-declares variable, STEP optional
190
- PRINT i$
191
- NEXT
192
-
193
- FOR i$ = 10 TO 1 STEP -1 ' Count down
194
- PRINT i$
195
- NEXT
196
- ```
197
-
198
- ### WHILE Loops
199
- ```basic
200
- WHILE x$ < 100
201
- x$ = x$ * 2
202
- WEND
203
- ```
204
-
205
- ### Labels, GOTO, GOSUB
206
- ```basic
207
- [start]
208
- GOSUB [subroutine]
209
- GOTO [start]
210
- END
211
-
212
- [subroutine]
213
- PRINT "Hello"
214
- RETURN
215
- ```
216
-
217
- ### Dynamic Jumps (Labels as Variables)
218
- ```basic
219
- LET target$ = "[menu]"
220
- GOTO target$ ' Variable holds label string
221
- LET dest# = "[jump]"
222
- GOSUB dest# ' Constants work too
223
- ```
224
-
225
- If you want to make a dynamic jump, use the "[" and "]" characters to indicate to BazzBasic that it is specifically a label.
226
-
227
- ```basic
228
- LET target$ = "[menu]" ' Correct way
229
- LET target$ = "menu" ' Incorrect way
230
- ```
231
-
232
- ### Other
233
- | Command | Description |
234
- |---------|-------------|
235
- | `SLEEP ms` | Pause execution |
236
- | `END` | Terminate program |
237
-
238
- ---
239
-
240
- ## I/O Commands
241
-
242
- | Command | Description |
243
- |---------|-------------|
244
- | `PRINT expr; expr` | Output (`;` = no space, `,` = tab) |
245
- | `PRINT "text";` | Trailing `;` suppresses newline |
246
- | `INPUT "prompt", var$` | Read input (splits on whitespace/comma) |
247
- | `INPUT "prompt", a$, b$` | Read multiple values |
248
- | `INPUT var$` | Default prompt `"? "` |
249
- | `LINE INPUT "prompt", var$` | Read entire line including spaces |
250
- | `CLS` | Clear screen |
251
- | `LOCATE row, col` | Move cursor (1-based) |
252
- | `COLOR fg, bg` | Set colors (0-15 palette) |
253
- | `GETCONSOLE(row, col, type)` | Console read: 0=char(ASCII), 1=fg, 2=bg |
254
- | `INKEY` | Non-blocking key check (0 if none, >256 for special keys) |
255
- | `KEYDOWN(key#)` | Returns TRUE if specified key is currently held down |
256
-
257
- ### INPUT vs LINE INPUT
258
- | Feature | INPUT | LINE INPUT |
259
- |---------|-------|------------|
260
- | Reads spaces | No (splits) | Yes |
261
- | Multiple variables | Yes | No |
262
- | Default prompt | `"? "` | None |
263
-
264
- ### INKEY vs KEYDOWN
265
- | Feature | INKEY | KEYDOWN |
266
- |---------|-------|---------|
267
- | Returns | Key value or 0 | TRUE/FALSE |
268
- | Key held | Reports once | Reports while held |
269
- | Use case | Menu navigation | Game movement, held keys |
270
-
271
- ```basic
272
- ' KEYDOWN example smooth movement
273
- IF KEYDOWN(KEY_LEFT#) THEN x$ = x$ - 5
274
- IF KEYDOWN(KEY_RIGHT#) THEN x$ = x$ + 5
275
- ```
276
-
277
- ---
278
-
279
- ## Math Functions
280
-
281
- | Function | Description |
282
- |----------|-------------|
283
- | `ABS(n)` | Absolute value |
284
- | `ATAN(n)` | Returns the arc tangent of n |
285
- | `BETWEEN(n, min, max)` | TRUE if n is in range |
286
- | `CEIL(n)` | Rounds up |
287
- | `CINT(n)` | Convert to integer (rounds) |
288
- | `COS(n)` | Returns the cosine of an angle |
289
- | `CLAMP(n, min, max)` | Constrain value to range |
290
- | `DEG(radians)` | Radians to degrees |
291
- | `DISTANCE(x1,y1,x2,y2)` | 2D Euclidean distance |
292
- | `DISTANCE(x1,y1,z1,x2,y2,z2)` | 3D Euclidean distance |
293
- | `EXP(n)` | Exponential (e^n) |
294
- | `FLOOR(n)` | Rounds down |
295
- | `INT(n)` | Truncate toward zero |
296
- | `LERP(start, end, t)` | Linear interpolation (t = 0.0-1.0) |
297
- | `LOG(n)` | Natural log |
298
- | `MAX(a, b)` | Returns higher from a & b |
299
- | `MIN(a, b)` | Returns smaller from a & b |
300
- | `MOD(a, b)` | Remainder |
301
- | `POW(base, exp)` | Power |
302
- | `RAD(degrees)` | Degrees to radians |
303
- | `RND(n)` | Random 0 to n-1 |
304
- | `ROUND(n)` | Standard rounding |
305
- | `SGN(n)` | Sign (-1, 0, 1) |
306
- | `SIN(n)` | Returns the sine of n |
307
- | `SQR(n)` | Square root |
308
- | `TAN(n)` | Returns the tangent of n |
309
-
310
-
311
- ### Math Constants
312
- | Constant | Value | Notes |
313
- |----------|-------|-------|
314
- | `PI` | 3.14159265358979 | 180° |
315
- | `HPI` | 1.5707963267929895 | 90° (PI/2) |
316
- | `QPI` | 0.7853981633974483 | 45° (PI/4) |
317
- | `TAU` | 6.283185307179586 | 360° (PI*2) |
318
- | `EULER` | 2.718281828459045 | e |
319
-
320
- All are raw-coded values no math at runtime, maximum performance.
321
-
322
- ### Fast Trigonometry (Lookup Tables)
323
- For graphics-intensive applications ~20x faster than SIN/COS, 1-degree precision.
324
-
325
- ```basic
326
- FastTrig(TRUE) ' Enable lookup tables (~5.6 KB)
327
-
328
- LET x$ = FastCos(45) ' Degrees, auto-normalized to 0-359
329
- LET y$ = FastSin(90)
330
- LET r$ = FastRad(180) ' Degrees to radians (doesn't need FastTrig)
331
-
332
- FastTrig(FALSE) ' Free memory when done
333
- ```
334
-
335
- **Use FastTrig when:** raycasting, rotating sprites, particle systems, any loop calling trig hundreds of times per frame.
336
- **Use regular SIN/COS when:** high precision needed, one-time calculations.
337
-
338
- ---
339
-
340
- ## String Functions
341
-
342
- | Function | Description |
343
- |----------|-------------|
344
- | `ASC(s$)` | Character to ASCII code |
345
- | `CHR(n)` | ASCII code to character |
346
- | `INSTR(s$, search$)` | Find substring position (0 = not found) |
347
- | `INSTR(start$, s$, search$)` | Find substring starting from position start$ (0 = not found) |
348
- | `INVERT(s$)` | Inverts a string |
349
- | `LCASE(s$)` | Converts to lowercase |
350
- | `LEFT(s$, n)` | First n characters |
351
- | `LEN(s$)` | String length |
352
- | `LTRIM(s$)` | Left trim |
353
- | `MID(s$, start, len)` | Substring (1-based) len optional |
354
- | `REPEAT(s$, n)` | Repeat s$ for n times |
355
- | `REPLACE(s$, old$, new$)` | Replace all occurrences |
356
- | `RIGHT(s$, n)` | Last n characters |
357
- | `RTRIM(s$)` | Right trim |
358
- | `SPLIT(s$, delim$, arr$)` | Split into array |
359
- | `SRAND(n)` | Returns random string length of n from allowed chars (letters, numbers and "_") |
360
- | `STR(n)` | Number to string |
361
- | `TRIM(s$)` | Remove leading/trailing spaces |
362
- | `UCASE(s$)` | Converts to uppercase |
363
- | `VAL(s$)` | String to number |
364
-
365
- ---
366
-
367
- ## File I/O
368
-
369
- ```basic
370
- ' Simple text file
371
- FileWrite "save.txt", data$
372
- LET data$ = FileRead("save.txt")
373
-
374
- ' Array read/write (key=value format)
375
- DIM a$
376
- a$("name") = "player1"
377
- a$("score") = 9999
378
- FileWrite "scores.txt", a$
379
-
380
- DIM b$
381
- LET b$ = FileRead("scores.txt")
382
- PRINT b$("name") ' Output: player1
383
- ```
384
-
385
- **Array file format:**
386
- ```
387
- name=player1
388
- score=9999
389
- multi,dim,key=value
390
- ```
391
-
392
- | Function/Command | Description |
393
- |---------|-------------|
394
- | `FileRead(path)` | Read file; returns string or populates array |
395
- | `FileWrite path, data` | Write string or array to file |
396
- | `FileExists(path)` | Returns 1 if file exists, 0 if not |
397
- | `FileDelete path` | Delete a file |
398
- | `FileList(path$, arr$)` | List files in directory into array |
399
-
400
- ---
401
-
402
- ## Network (HTTP)
403
-
404
- ```basic
405
- DIM response$
406
- LET response$ = HTTPGET("https://api.example.com/data")
407
- PRINT response$
408
-
409
- DIM result$
410
- LET result$ = HTTPPOST("https://api.example.com/post", "{""key"":""value""}")
411
- PRINT result$
412
- ```
413
-
414
- - Returns response body as string
415
- - Supports HTTPS
416
- - Use `""` inside strings to escape quotes in JSON bodies
417
- - Timeout handled gracefully — returns error message string on failure
418
-
419
- ---
420
-
421
- ## Sound (SDL2_mixer)
422
-
423
- ```basic
424
- DIM bgm$
425
- LET bgm$ = LOADSOUND("music.wav")
426
- LET sfx$ = LOADSOUND("jump.wav")
427
-
428
- SOUNDREPEAT(bgm$) ' Loop continuously
429
- SOUNDONCE(sfx$) ' Play once
430
- SOUNDSTOP(bgm$) ' Stop specific sound
431
- SOUNDSTOPALL ' Stop all sounds
432
- ```
433
-
434
- | Command | Description |
435
- |---------|-------------|
436
- | `LOADSOUND(path)` | Load sound file, returns ID |
437
- | `SOUNDONCE(id$)` | Play once |
438
- | `SOUNDREPEAT(id$)` | Loop continuously |
439
- | `SOUNDSTOP(id$)` | Stop specific sound |
440
- | `SOUNDSTOPALL` | Stop all sounds |
441
-
442
- - Formats: WAV (recommended), MP3, others via SDL2_mixer
443
- - Thread-safe, multiple simultaneous sounds supported
444
- - Load sounds once at startup for performance
445
-
446
- ---
447
-
448
- ## Graphics (SDL2)
449
-
450
- ### Screen Setup
451
- ```basic
452
- SCREEN 12 ' 640x480 VGA mode
453
- SCREEN 0, 800, 600, "Title" ' Custom size with title
454
- ```
455
- Modes: 0=640x400, 1=320x200, 2=640x350, 7=320x200, 9=640x350, 12=640x480, 13=320x200
456
-
457
- ### Fullscreen
458
- ```basic
459
- FULLSCREEN TRUE ' Borderless fullscreen
460
- FULLSCREEN FALSE ' Windowed mode
461
- ```
462
- Call after `SCREEN`.
463
-
464
- ### VSync
465
- ```basic
466
- VSYNC(TRUE) ' Enable (default) — caps to monitor refresh
467
- VSYNC(FALSE) ' Disable — unlimited FPS, may tear
468
- ```
469
-
470
- ### Double Buffering (Required for Animation)
471
- ```basic
472
- SCREENLOCK ON ' Start buffering
473
- ' ... draw commands ...
474
- SCREENLOCK OFF ' Display frame
475
- SLEEP 16 ' ~60 FPS
476
- ```
477
- `SCREENLOCK` without argument = `SCREENLOCK ON`. Do math/logic outside SCREENLOCK block.
478
-
479
- ### Drawing Primitives
480
- | Command | Description |
481
- |---------|-------------|
482
- | `PSET (x, y), color` | Draw pixel |
483
- | `POINT(x, y)` | Read pixel color (returns RGB integer) |
484
- | `LINE (x1,y1)-(x2,y2), color` | Draw line |
485
- | `LINE (x1,y1)-(x2,y2), color, B` | Box outline |
486
- | `LINE (x1,y1)-(x2,y2), color, BF` | Filled box (faster than CLS) |
487
- | `CIRCLE (cx, cy), r, color` | Circle outline |
488
- | `CIRCLE (cx, cy), r, color, 1` | Filled circle |
489
- | `PAINT (x, y), fill, border` | Flood fill |
490
- | `RGB(r, g, b)` | Create color (0-255 each) |
491
-
492
- ### Shape/Sprite System
493
- ```basic
494
- DIM sprite$
495
- sprite$ = LOADSHAPE("RECTANGLE", 50, 50, RGB(255,0,0)) ' Types: RECTANGLE, CIRCLE, TRIANGLE
496
- sprite$ = LOADIMAGE("player.png") ' PNG (with alpha) or BMP
497
-
498
- MOVESHAPE sprite$, x, y ' Position (top-left point)
499
- ROTATESHAPE sprite$, angle ' Absolute degrees
500
- SCALESHAPE sprite$, 1.5 ' 1.0 = original
501
- SHOWSHAPE sprite$
502
- HIDESHAPE sprite$
503
- DRAWSHAPE sprite$ ' Render
504
- REMOVESHAPE sprite$ ' Free memory
505
- ```
506
- - PNG recommended (full alpha transparency 0-255), BMP for legacy
507
- - Images positioned by their **top-left point**
508
- - Rotation is absolute, not cumulative
509
- - Always REMOVESHAPE when done to free memory
510
-
511
- ### Sprite Sheets (LOADSHEET)
512
- ```basic
513
- DIM sprites$
514
- LOADSHEET sprites$, spriteW, spriteH, "sheet.png"
515
-
516
- ' Access sprites by 1-based index
517
- MOVESHAPE sprites$(index$), x#, y#
518
- DRAWSHAPE sprites$(index$)
519
- ```
520
- - Sprites indexed left-to-right, top-to-bottom starting at 1
521
- - All sprites must be same size (spriteW x spriteH)
522
-
523
- ### Mouse Input (Graphics Mode Only)
524
- | Function | Description |
525
- |----------|-------------|
526
- | `MOUSEX` / `MOUSEY` | Cursor position |
527
- | `MOUSEB` | Button state (bitmask, use `AND`) |
528
-
529
- Button constants: `MOUSE_LEFT#`=1, `MOUSE_RIGHT#`=2, `MOUSE_MIDDLE#`=4
530
-
531
- ### Color Palette (0-15)
532
- | 0 Black | 4 Red | 8 Dark Gray | 12 Light Red |
533
- |---------|-------|-------------|--------------|
534
- | 1 Blue | 5 Magenta | 9 Light Blue | 13 Light Magenta |
535
- | 2 Green | 6 Brown | 10 Light Green | 14 Yellow |
536
- | 3 Cyan | 7 Light Gray | 11 Light Cyan | 15 White |
537
-
538
- ### Performance Tips
539
- 1. Use `SCREENLOCK ON/OFF` for all animation
540
- 2. `LINE...BF` is faster than `CLS` for clearing
541
- 3. Store `RGB()` values in constants
542
- 4. REMOVESHAPE unused shapes
543
- 5. SLEEP 16 for ~60 FPS
544
- 6. Do math/logic outside SCREENLOCK block
545
-
546
- ---
547
-
548
- ## Built-in Constants
549
-
550
- ### Arrow Keys
551
- | | | |
552
- |---|---|---|
553
- | `KEY_UP#` | `KEY_DOWN#` | `KEY_LEFT#` |
554
- | `KEY_RIGHT#` | | |
555
-
556
- ### Special Keys
557
- | | | |
558
- |---|---|---|
559
- | `KEY_ESC#` | `KEY_TAB#` | `KEY_BACKSPACE#` |
560
- | `KEY_ENTER#` | `KEY_SPACE#` | `KEY_INSERT#` |
561
- | `KEY_DELETE#` | `KEY_HOME#` | `KEY_END#` |
562
- | `KEY_PGUP#` | `KEY_PGDN#` | |
563
-
564
- ### Modifier Keys
565
- | | | |
566
- |---|---|---|
567
- | `KEY_LSHIFT#` | `KEY_RSHIFT#` | `KEY_LCTRL#` |
568
- | `KEY_RCTRL#` | `KEY_LALT#` | `KEY_RALT#` |
569
- | `KEY_LWIN#` | `KEY_RWIN#` | |
570
-
571
- ### Function Keys
572
- | | | |
573
- |---|---|---|
574
- | `KEY_F1#` | `KEY_F2#` | `KEY_F3#` |
575
- | `KEY_F4#` | `KEY_F5#` | `KEY_F6#` |
576
- | `KEY_F7#` | `KEY_F8#` | `KEY_F9#` |
577
- | `KEY_F10#` | `KEY_F11#` | `KEY_F12#` |
578
-
579
- ### Numpad Keys
580
- | | | |
581
- |---|---|---|
582
- | `KEY_NUMPAD0#` | `KEY_NUMPAD1#` | `KEY_NUMPAD2#` |
583
- | `KEY_NUMPAD3#` | `KEY_NUMPAD4#` | `KEY_NUMPAD5#` |
584
- | `KEY_NUMPAD6#` | `KEY_NUMPAD7#` | `KEY_NUMPAD8#` |
585
- | `KEY_NUMPAD9#` | | |
586
-
587
- ### Punctuation Keys
588
- | | | |
589
- |---|---|---|
590
- | `KEY_COMMA#` | `KEY_DOT#` | `KEY_MINUS#` |
591
- | `KEY_EQUALS#` | `KEY_SLASH#` | `KEY_BACKSLASH#` |
592
- | `KEY_SEP#` | `KEY_GRAVE#` | `KEY_LBRACKET#` |
593
- | `KEY_RBRACKET#` | | |
594
-
595
- ### Alphabet Keys
596
- | | | |
597
- |---|---|---|
598
- | `KEY_A#` | `KEY_B#` | `KEY_C#` |
599
- | `KEY_D#` | `KEY_E#` | `KEY_F#` |
600
- | `KEY_G#` | `KEY_H#` | `KEY_I#` |
601
- | `KEY_J#` | `KEY_K#` | `KEY_L#` |
602
- | `KEY_M#` | `KEY_N#` | `KEY_O#` |
603
- | `KEY_P#` | `KEY_Q#` | `KEY_R#` |
604
- | `KEY_S#` | `KEY_T#` | `KEY_U#` |
605
- | `KEY_V#` | `KEY_W#` | `KEY_X#` |
606
- | `KEY_Y#` | `KEY_Z#` | |
607
-
608
- ### Number Keys
609
- | | | |
610
- |---|---|---|
611
- | `KEY_0#` | `KEY_1#` | `KEY_2#` |
612
- | `KEY_3#` | `KEY_4#` | `KEY_5#` |
613
- | `KEY_6#` | `KEY_7#` | `KEY_8#` |
614
- | `KEY_9#` | | |
615
-
616
- ### Mouse
617
- ```basic
618
- MOUSE_LEFT# = 1, MOUSE_RIGHT# = 2, MOUSE_MIDDLE# = 4
619
- ```
620
-
621
- ### Logical
622
- `TRUE` = 1, `FALSE` = 0
623
-
624
- ---
625
-
626
- ## Source Control
627
-
628
- ### INCLUDE
629
- ```basic
630
- INCLUDE "other_file.bas" ' Insert source at this point
631
- INCLUDE "MathLib.bb" ' Include compiled library
632
- ```
633
-
634
- ### Libraries (.bb files)
635
- ```basic
636
- ' MathLib.bas can ONLY contain DEF FN functions
637
- DEF FN add$(x$, y$)
638
- RETURN x$ + y$
639
- END DEF
640
- ```
641
- Compile: `bazzbasic.exe -lib MathLib.bas` `MathLib.bb`
642
-
643
- ```basic
644
- INCLUDE "MathLib.bb"
645
- PRINT FN MATHLIB_add$(5, 3) ' Auto-prefix: FILENAME_ + functionName
646
- ```
647
- - Libraries can only contain `DEF FN` functions
648
- - Library functions can access main program constants (`#`)
649
- - Version-locked: .bb may not work across BazzBasic versions
650
-
651
- ---
652
-
653
- ## Common Patterns
654
-
655
- ### Game Loop
656
- ```basic
657
- SCREEN 12
658
- LET running$ = TRUE
659
-
660
- WHILE running$
661
- LET key$ = INKEY
662
- IF key$ = KEY_ESC# THEN running$ = FALSE
663
-
664
- ' Math and logic here
665
-
666
- SCREENLOCK ON
667
- LINE (0,0)-(640,480), 0, BF ' Fast clear
668
- ' Draw game state
669
- SCREENLOCK OFF
670
- SLEEP 16
671
- WEND
672
- END
673
- ```
674
-
675
- ### HTTP + Data
676
- ```basic
677
- DIM response$
678
- LET response$ = HTTPGET("https://api.example.com/scores")
679
- PRINT response$
680
-
681
- DIM payload$
682
- LET payload$ = HTTPPOST("https://api.example.com/submit", "{""score"":9999}")
683
- PRINT payload$
684
- ```
685
-
686
- ### Save/Load with Arrays
687
- ```basic
688
- DIM save$
689
- save$("level") = 3
690
- save$("hp") = 80
691
- FileWrite "save.txt", save$
692
-
693
- DIM load$
694
- LET load$ = FileRead("save.txt")
695
- PRINT load$("level") ' Output: 3
696
- ```
697
-
698
- ### Sound with Graphics
699
- ```basic
700
- SCREEN 12
701
- LET bgm$ = LOADSOUND("music.wav")
702
- LET sfx$ = LOADSOUND("jump.wav")
703
- SOUNDREPEAT(bgm$)
704
-
705
- WHILE INKEY <> KEY_ESC#
706
- IF INKEY = KEY_SPACE# THEN SOUNDONCE(sfx$)
707
- SLEEP 16
708
- WEND
709
- SOUNDSTOPALL
710
- END
711
- ```
712
-
713
- ---
714
-
715
- ## Code Style Conventions
716
-
717
- **Variables** — `camelCase$`
718
- ```basic
719
- LET playerName$ = "Hero"
720
- LET score$ = 0
721
- ```
722
-
723
- **Constants** — `UPPER_SNAKE_CASE#`
724
- ```basic
725
- LET MAX_HEALTH# = 100
726
- LET SCREEN_W# = 640
727
- ```
728
-
729
- **Arrays** — `camelCase$` (like variables, declared with DIM)
730
- ```basic
731
- DIM scores$
732
- DIM playerData$
733
- ```
734
-
735
- **User-defined functions** — `PascalCase$`
736
- ```basic
737
- DEF FN CalculateDamage$(attack$, defence$)
738
- DEF FN IsColliding$(x$, y$)
739
- ```
740
-
741
- **Labels** — descriptive names, `[sub:]` prefix recommended for subroutines
742
- ```basic
743
- [sub:DrawPlayer] ' Subroutine
744
- [gameLoop] ' Jump target
745
- ```
746
-
747
- ### Program Structure
748
- 1. Constants first
749
- 2. User-defined functions (or INCLUDE them)
750
- 3. Main program loop
751
- 4. Subroutines (labels) last
752
-
753
- ### Subroutine Indentation
754
- ```basic
755
- [sub:DrawPlayer]
756
- MOVESHAPE player$, x$, y$
757
- DRAWSHAPE player$
758
- RETURN
 
 
 
 
 
759
  ```
 
1
+ > METADATA:
2
+ > Name: BazzBasic
3
+
4
+ > Description: BazzBasic BASIC interpreter language reference. Use when writing, debugging, or explaining BazzBasic code (.bas files). Triggers on BazzBasic syntax, BASIC programming with $ and # suffixes, SDL2 graphics in BASIC, or references to BazzBasic interpreter features
5
+
6
+ > About: BazzBasic is built around one simple idea: starting programming should feel nice and even fun. Ease of learning, comfort of exploration and small but important moments of success. Just like the classic BASICs of decades past, but with a fresh and modern feel.
7
+
8
+ > Purpose: This guide has been provided with the idea that it would be easy and efficient for a modern AI to use this guide and through this either guide a new programmer to the secrets of BazzBasic or, if necessary, generate code himself.
9
+
10
+ > Version: This guide is written for BazzBasic version 1.0 and is updated 19.03.2026 09:51 Finnish time.
11
+
12
+ ---
13
+
14
+ # BazzBasic Language Reference
15
+
16
+ BazzBasic is a BASIC interpreter for .NET 10 with SDL2 graphics and SDL2_mixer sound support. It is not a clone of any previous BASIC — it aims to be easy, fun, and modern. Released under MIT license.
17
+
18
+ **Version:** 1.0 (Released February 22, 2026)
19
+ **Author:** Kristian Virtanen (krisu.virtanen@gmail.com)
20
+ **Platform:** Windows (x64 primary); Linux/macOS possible with effort
21
+ **Dependencies:** SDL2.dll, SDL2_mixer (bundled).
22
+ **Github:** https://github.com/EkBass/BazzBasic
23
+ **Homepage:** https://ekbass.github.io/BazzBasic/
24
+ **Manual:** "https://ekbass.github.io/BazzBasic/manual/#/"
25
+ **Examples:** "https://github.com/EkBass/BazzBasic/tree/main/Examples"
26
+ **Github_repo:** "https://github.com/EkBass/BazzBasic"
27
+ **Github_discussions:** "https://github.com/EkBass/BazzBasic/discussions"
28
+ **Discord_channel:** "https://discord.com/channels/682603735515529216/1464283741919907932"
29
+ **Thinbasic subforum:** "https://www.thinbasic.com/community/forumdisplay.php?401-BazzBasic"
30
+
31
+ ## Usage:
32
+ This file is created as a guide specifically for the AI. With this, the AI can not only code itself with BazzBasic, but also guide the user with it.
33
+
34
+ Feed the file as it is to the AI, as an attachment, as a project file, etc.
35
+
36
+ ## Few examples:
37
+ **raycaster_3d:** https://raw.githubusercontent.com/EkBass/BazzBasic/refs/heads/main/Examples/raycaster_3d_optimized.bas
38
+ **voxel_terrain:** https://raw.githubusercontent.com/EkBass/BazzBasic/refs/heads/main/Examples/voxel_terrain.bas
39
+ **sprite load:** https://github.com/EkBass/BazzBasic/blob/main/Examples/countdown_demo.bas
40
+ **Eliza:** https://github.com/EkBass/BazzBasic/blob/main/Examples/Eliza.bas
41
+
42
+ ## CLI Usage
43
+
44
+ | Command | Action |
45
+ |---------|--------|
46
+ | `bazzbasic.exe` | Launch IDE |
47
+ | `bazzbasic.exe file.bas` | Run program |
48
+ | `bazzbasic.exe -exe file.bas` | Create standalone .exe |
49
+ | `bazzbasic.exe -lib file.bas` | Create tokenized library (.bb) |
50
+
51
+ IDE shortcuts: F5 run, Ctrl+N/O/S new/open/save, Ctrl+Shift+S save as, Ctrl+W close tab, Ctrl+F find, Ctrl+H replace.
52
+
53
+ ### External syntax colors.
54
+ BazzBasic IDE is developed just so double-clicking *bazzbasic.exe* would bring something in the screen of a newbie. A person can use it, but it stands no chance for more advanced editors.
55
+
56
+ There are syntax color files for *Notepad++*, *Geany* and *Visual Studio Code* available at https://github.com/EkBass/BazzBasic/tree/main/extras
57
+
58
+ ---
59
+
60
+ ## Syntax Fundamentals
61
+
62
+ ### Variables and Constants
63
+ Forget traditional BASIC types. Suffixes in BazzBasic define mutability, not data type.
64
+
65
+ Variables and arrays in BazzBasic are typeless: hold numbers or strings interchangeably
66
+
67
+ - $ = Mutable Variable
68
+ - # = Immutable Constant
69
+
70
+
71
+ ```basic
72
+ LET a$ ' Declare without value
73
+ LET name$ = "Alice" ' Variable, string (mutable, $ suffix)
74
+ LET age$ = 19 ' Variable, integer (mutable, $ suffix)
75
+ LET price$ = 1.99 ' Variable, decimal (mutable, $ suffix)
76
+ LET PI# = 3.14159 ' Constant (immutable, # suffix)
77
+ LET x$, y$, z$ = 10 ' Multiple declaration
78
+ ```
79
+ - All variables require `$` suffix, constants require `#`
80
+ - Typeless: hold numbers or strings interchangeably
81
+ - Must declare with `LET` before use (except FOR/INPUT which auto-declare)
82
+ - Assignment after declaration: `x$ = x$ + 1` (no LET needed)
83
+ - **Case-insensitive**: `PRINT`, `print`, `Print` all work
84
+ - Naming: letters, numbers, underscores; cannot start with number
85
+
86
+ ### Comparison Behavior
87
+ `"123" = 123` is TRUE (cross-type comparison), but slower keep types consistent.
88
+
89
+ ### Scope
90
+ - Main code shares one scope (even inside IF blocks)
91
+ - `DEF FN` functions have completely isolated scope
92
+ - Only global constants (`#`) are accessible inside functions
93
+
94
+ ### Multiple Statements Per Line
95
+ ```basic
96
+ COLOR 14, 0 : CLS : PRINT "Hello"
97
+ ```
98
+
99
+ ### Comments
100
+ ```basic
101
+ REM This is a comment
102
+ ' This is also a comment
103
+ PRINT "Hello" ' Inline comment
104
+ ```
105
+
106
+ ### Escape Sequences in Strings
107
+ | Sequence | Result |
108
+ |----------|--------|
109
+ | `\"` | Quote |
110
+ | `\n` | Newline |
111
+ | `\t` | Tab |
112
+ | `\\` | Backslash |
113
+
114
+ ---
115
+
116
+ ## Arrays
117
+ ```basic
118
+ DIM scores$ ' Declare (must use DIM)
119
+ DIM a$, b$, c$ ' Multiple declaration
120
+ scores$(0) = 95 ' Numeric index (0-based)
121
+ scores$("name") = "Alice" ' String key (associative)
122
+ matrix$(0, 1) = "A2" ' Multi-dimensional
123
+ data$(1, "header") = "Name" ' Mixed indexing
124
+ ```
125
+ - Array names must end with `$`
126
+ - Fully dynamic: numeric, string, multi-dimensional, mixed indexing
127
+ - Arrays **cannot** be passed directly to functions — pass values of individual elements
128
+ - Accessing uninitialized elements is an error check with `HASKEY` first
129
+
130
+ ### Array Functions
131
+ | Function | Description |
132
+ |----------|-------------|
133
+ | `LEN(arr$())` | Element count (note: empty parens) |
134
+ | `HASKEY(arr$(key))` | 1 if exists, 0 if not |
135
+ | `DELKEY arr$(key)` | Remove element |
136
+ | `DELARRAY arr$` | Remove entire array (can re-DIM after) |
137
+
138
+ ---
139
+
140
+ ## Operators
141
+
142
+ ### Arithmetic
143
+ `+` (add/concatenate), `-`, `*`, `/` (returns float)
144
+
145
+ ### Comparison
146
+ `=` or `==`, `<>` or `!=`, `<`, `>`, `<=`, `>=`
147
+
148
+ ### Logical
149
+ `AND`, `OR`, `NOT`
150
+
151
+ ### Precedence (high to low)
152
+ `()``NOT` → `*`, `/` → `+`, `-` → comparisons → `AND` → `OR`
153
+
154
+ ---
155
+
156
+ ## User-Defined Functions
157
+ ```basic
158
+ DEF FN add$(a$, b$)
159
+ RETURN a$ + b$
160
+ END DEF
161
+ PRINT FN add$(3, 4) ' Call with FN prefix
162
+ ```
163
+ - Function name **must** end with `$`, called with `FN` prefix
164
+ - **Must be defined before called** (top of file or via INCLUDE)
165
+ - Parameters passed **by value**
166
+ - Completely isolated scope: no access to global variables, only global constants (`#`)
167
+ - Labels inside functions are local — GOTO/GOSUB **cannot** jump outside (error)
168
+ - Supports recursion
169
+ - Tip: put functions in separate files, `INCLUDE` at program start
170
+
171
+ ---
172
+
173
+ ## Control Flow
174
+
175
+ ### IF Statements
176
+ ```basic
177
+ ' Block IF (END IF and ENDIF both work)
178
+ IF x$ > 10 THEN
179
+ PRINT "big"
180
+ ELSEIF x$ > 5 THEN
181
+ PRINT "medium"
182
+ ELSE
183
+ PRINT "small"
184
+ ENDIF
185
+
186
+ ' One-line IF (GOTO/GOSUB only)
187
+ IF lives$ = 0 THEN GOTO [game_over]
188
+ IF key$ = KEY_ESC# THEN GOTO [menu] ELSE GOTO [continue]
189
+ IF ready$ = 1 THEN GOSUB [start_game]
190
+ ```
191
+
192
+ ### FOR Loops
193
+ ```basic
194
+ FOR i$ = 1 TO 10 STEP 2 ' Auto-declares variable, STEP optional
195
+ PRINT i$
196
+ NEXT
197
+
198
+ FOR i$ = 10 TO 1 STEP -1 ' Count down
199
+ PRINT i$
200
+ NEXT
201
+ ```
202
+
203
+ ### WHILE Loops
204
+ ```basic
205
+ WHILE x$ < 100
206
+ x$ = x$ * 2
207
+ WEND
208
+ ```
209
+
210
+ ### Labels, GOTO, GOSUB
211
+ ```basic
212
+ [start]
213
+ GOSUB [subroutine]
214
+ GOTO [start]
215
+ END
216
+
217
+ [subroutine]
218
+ PRINT "Hello"
219
+ RETURN
220
+ ```
221
+
222
+ ### Dynamic Jumps (Labels as Variables)
223
+ ```basic
224
+ LET target$ = "[menu]"
225
+ GOTO target$ ' Variable holds label string
226
+ LET dest# = "[jump]"
227
+ GOSUB dest# ' Constants work too
228
+ ```
229
+
230
+ If you want to make a dynamic jump, use the "[" and "]" characters to indicate to BazzBasic that it is specifically a label.
231
+
232
+ ```basic
233
+ LET target$ = "[menu]" ' Correct way
234
+ LET target$ = "menu" ' Incorrect way
235
+ ```
236
+
237
+ ### Other
238
+ | Command | Description |
239
+ |---------|-------------|
240
+ | `SLEEP ms` | Pause execution |
241
+ | `END` | Terminate program |
242
+
243
+ ---
244
+
245
+ ## I/O Commands
246
+
247
+ | Command | Description |
248
+ |---------|-------------|
249
+ | `PRINT expr; expr` | Output (`;` = no space, `,` = tab) |
250
+ | `PRINT "text";` | Trailing `;` suppresses newline |
251
+ | `INPUT "prompt", var$` | Read input (splits on whitespace/comma) |
252
+ | `INPUT "prompt", a$, b$` | Read multiple values |
253
+ | `INPUT var$` | Default prompt `"? "` |
254
+ | `LINE INPUT "prompt", var$` | Read entire line including spaces |
255
+ | `CLS` | Clear screen |
256
+ | `LOCATE row, col` | Move cursor (1-based) |
257
+ | `COLOR fg, bg` | Set colors (0-15 palette) |
258
+ | `GETCONSOLE(row, col, type)` | Console read: 0=char(ASCII), 1=fg, 2=bg |
259
+ | `INKEY` | Non-blocking key check (0 if none, >256 for special keys) |
260
+ | `KEYDOWN(key#)` | Returns TRUE if specified key is currently held down |
261
+
262
+ ### INPUT vs LINE INPUT
263
+ | Feature | INPUT | LINE INPUT |
264
+ |---------|-------|------------|
265
+ | Reads spaces | No (splits) | Yes |
266
+ | Multiple variables | Yes | No |
267
+ | Default prompt | `"? "` | None |
268
+
269
+ ### INKEY vs KEYDOWN
270
+ | Feature | INKEY | KEYDOWN |
271
+ |---------|-------|---------|
272
+ | Returns | Key value or 0 | TRUE/FALSE |
273
+ | Key held | Reports once | Reports while held |
274
+ | Use case | Menu navigation | Game movement, held keys |
275
+
276
+ ```basic
277
+ ' KEYDOWN example — smooth movement
278
+ IF KEYDOWN(KEY_LEFT#) THEN x$ = x$ - 5
279
+ IF KEYDOWN(KEY_RIGHT#) THEN x$ = x$ + 5
280
+ ```
281
+
282
+ ---
283
+
284
+ ## Math Functions
285
+
286
+ | Function | Description |
287
+ |----------|-------------|
288
+ | `ABS(n)` | Absolute value |
289
+ | `ATAN(n)` | Returns the arc tangent of n |
290
+ | `BETWEEN(n, min, max)` | TRUE if n is in range |
291
+ | `CEIL(n)` | Rounds up |
292
+ | `CINT(n)` | Convert to integer (rounds) |
293
+ | `COS(n)` | Returns the cosine of an angle |
294
+ | `CLAMP(n, min, max)` | Constrain value to range |
295
+ | `DEG(radians)` | Radians to degrees |
296
+ | `DISTANCE(x1,y1,x2,y2)` | 2D Euclidean distance |
297
+ | `DISTANCE(x1,y1,z1,x2,y2,z2)` | 3D Euclidean distance |
298
+ | `EXP(n)` | Exponential (e^n) |
299
+ | `FLOOR(n)` | Rounds down |
300
+ | `INT(n)` | Truncate toward zero |
301
+ | `LERP(start, end, t)` | Linear interpolation (t = 0.0-1.0) |
302
+ | `LOG(n)` | Natural log |
303
+ | `MAX(a, b)` | Returns higher from a & b |
304
+ | `MIN(a, b)` | Returns smaller from a & b |
305
+ | `MOD(a, b)` | Remainder |
306
+ | `POW(base, exp)` | Power |
307
+ | `RAD(degrees)` | Degrees to radians |
308
+ | `RND(n)` | Random 0 to n-1 |
309
+ | `ROUND(n)` | Standard rounding |
310
+ | `SGN(n)` | Sign (-1, 0, 1) |
311
+ | `SIN(n)` | Returns the sine of n |
312
+ | `SQR(n)` | Square root |
313
+ | `TAN(n)` | Returns the tangent of n |
314
+
315
+
316
+ ### Math Constants
317
+ | Constant | Value | Notes |
318
+ |----------|-------|-------|
319
+ | `PI` | 3.14159265358979 | 180° |
320
+ | `HPI` | 1.5707963267929895 | 90° (PI/2) |
321
+ | `QPI` | 0.7853981633974483 | 45° (PI/4) |
322
+ | `TAU` | 6.283185307179586 | 360° (PI*2) |
323
+ | `EULER` | 2.718281828459045 | e |
324
+
325
+ All are raw-coded values — no math at runtime, maximum performance.
326
+
327
+ ### Fast Trigonometry (Lookup Tables)
328
+ For graphics-intensive applications ~20x faster than SIN/COS, 1-degree precision.
329
+
330
+ ```basic
331
+ FastTrig(TRUE) ' Enable lookup tables (~5.6 KB)
332
+
333
+ LET x$ = FastCos(45) ' Degrees, auto-normalized to 0-359
334
+ LET y$ = FastSin(90)
335
+ LET r$ = FastRad(180) ' Degrees to radians (doesn't need FastTrig)
336
+
337
+ FastTrig(FALSE) ' Free memory when done
338
+ ```
339
+
340
+ **Use FastTrig when:** raycasting, rotating sprites, particle systems, any loop calling trig hundreds of times per frame.
341
+ **Use regular SIN/COS when:** high precision needed, one-time calculations.
342
+
343
+ ---
344
+
345
+ ## String Functions
346
+
347
+ | Function | Description |
348
+ |----------|-------------|
349
+ | `ASC(s$)` | Character to ASCII code |
350
+ | `CHR(n)` | ASCII code to character |
351
+ | `INSTR(s$, search$)` | Find substring position (0 = not found) |
352
+ | `INSTR(start$, s$, search$)` | Find substring starting from position start$ (0 = not found) |
353
+ | `INVERT(s$)` | Inverts a string |
354
+ | `LCASE(s$)` | Converts to lowercase |
355
+ | `LEFT(s$, n)` | First n characters |
356
+ | `LEN(s$)` | String length |
357
+ | `LTRIM(s$)` | Left trim |
358
+ | `MID(s$, start, len)` | Substring (1-based) len optional |
359
+ | `REPEAT(s$, n)` | Repeat s$ for n times |
360
+ | `REPLACE(s$, old$, new$)` | Replace all occurrences |
361
+ | `RIGHT(s$, n)` | Last n characters |
362
+ | `RTRIM(s$)` | Right trim |
363
+ | `SPLIT(s$, delim$, arr$)` | Split into array |
364
+ | `SRAND(n)` | Returns random string length of n from allowed chars (letters, numbers and "_") |
365
+ | `STR(n)` | Number to string |
366
+ | `TRIM(s$)` | Remove leading/trailing spaces |
367
+ | `UCASE(s$)` | Converts to uppercase |
368
+ | `VAL(s$)` | String to number |
369
+
370
+ ---
371
+
372
+ ## File I/O
373
+
374
+ ```basic
375
+ ' Simple text file
376
+ FileWrite "save.txt", data$
377
+ LET data$ = FileRead("save.txt")
378
+
379
+ ' Array read/write (key=value format)
380
+ DIM a$
381
+ a$("name") = "player1"
382
+ a$("score") = 9999
383
+ FileWrite "scores.txt", a$
384
+
385
+ DIM b$
386
+ LET b$ = FileRead("scores.txt")
387
+ PRINT b$("name") ' Output: player1
388
+ ```
389
+
390
+ **Array file format:**
391
+ ```
392
+ name=player1
393
+ score=9999
394
+ multi,dim,key=value
395
+ ```
396
+
397
+ | Function/Command | Description |
398
+ |---------|-------------|
399
+ | `FileRead(path)` | Read file; returns string or populates array |
400
+ | `FileWrite path, data` | Write string or array to file |
401
+ | `FileExists(path)` | Returns 1 if file exists, 0 if not |
402
+ | `FileDelete path` | Delete a file |
403
+ | `FileList(path$, arr$)` | List files in directory into array |
404
+
405
+ ---
406
+
407
+ ## Network (HTTP)
408
+
409
+ ```basic
410
+ DIM response$
411
+ LET response$ = HTTPGET("https://api.example.com/data")
412
+ PRINT response$
413
+
414
+ DIM result$
415
+ LET result$ = HTTPPOST("https://api.example.com/post", "{""key"":""value""}")
416
+ PRINT result$
417
+ ```
418
+
419
+ - Returns response body as string
420
+ - Supports HTTPS
421
+ - Use `""` inside strings to escape quotes in JSON bodies
422
+ - Timeout handled gracefully — returns error message string on failure
423
+
424
+ ---
425
+
426
+ ## Sound (SDL2_mixer)
427
+
428
+ ```basic
429
+ DIM bgm$
430
+ LET bgm$ = LOADSOUND("music.wav")
431
+ LET sfx$ = LOADSOUND("jump.wav")
432
+
433
+ SOUNDREPEAT(bgm$) ' Loop continuously
434
+ SOUNDONCE(sfx$) ' Play once
435
+ SOUNDSTOP(bgm$) ' Stop specific sound
436
+ SOUNDSTOPALL ' Stop all sounds
437
+ ```
438
+
439
+ | Command | Description |
440
+ |---------|-------------|
441
+ | `LOADSOUND(path)` | Load sound file, returns ID |
442
+ | `SOUNDONCE(id$)` | Play once |
443
+ | `SOUNDREPEAT(id$)` | Loop continuously |
444
+ | `SOUNDSTOP(id$)` | Stop specific sound |
445
+ | `SOUNDSTOPALL` | Stop all sounds |
446
+
447
+ - Formats: WAV (recommended), MP3, others via SDL2_mixer
448
+ - Thread-safe, multiple simultaneous sounds supported
449
+ - Load sounds once at startup for performance
450
+
451
+ ---
452
+
453
+ ## Graphics (SDL2)
454
+
455
+ ### Screen Setup
456
+ ```basic
457
+ SCREEN 12 ' 640x480 VGA mode
458
+ SCREEN 0, 800, 600, "Title" ' Custom size with title
459
+ ```
460
+ Modes: 0=640x400, 1=320x200, 2=640x350, 7=320x200, 9=640x350, 12=640x480, 13=320x200
461
+
462
+ ### Fullscreen
463
+ ```basic
464
+ FULLSCREEN TRUE ' Borderless fullscreen
465
+ FULLSCREEN FALSE ' Windowed mode
466
+ ```
467
+ Call after `SCREEN`.
468
+
469
+ ### VSync
470
+ ```basic
471
+ VSYNC(TRUE) ' Enable (default) — caps to monitor refresh
472
+ VSYNC(FALSE) ' Disable — unlimited FPS, may tear
473
+ ```
474
+
475
+ ### Double Buffering (Required for Animation)
476
+ ```basic
477
+ SCREENLOCK ON ' Start buffering
478
+ ' ... draw commands ...
479
+ SCREENLOCK OFF ' Display frame
480
+ SLEEP 16 ' ~60 FPS
481
+ ```
482
+ `SCREENLOCK` without argument = `SCREENLOCK ON`. Do math/logic outside SCREENLOCK block.
483
+
484
+ ### Drawing Primitives
485
+ | Command | Description |
486
+ |---------|-------------|
487
+ | `PSET (x, y), color` | Draw pixel |
488
+ | `POINT(x, y)` | Read pixel color (returns RGB integer) |
489
+ | `LINE (x1,y1)-(x2,y2), color` | Draw line |
490
+ | `LINE (x1,y1)-(x2,y2), color, B` | Box outline |
491
+ | `LINE (x1,y1)-(x2,y2), color, BF` | Filled box (faster than CLS) |
492
+ | `CIRCLE (cx, cy), r, color` | Circle outline |
493
+ | `CIRCLE (cx, cy), r, color, 1` | Filled circle |
494
+ | `PAINT (x, y), fill, border` | Flood fill |
495
+ | `RGB(r, g, b)` | Create color (0-255 each) |
496
+
497
+ ### Shape/Sprite System
498
+ ```basic
499
+ DIM sprite$
500
+ sprite$ = LOADSHAPE("RECTANGLE", 50, 50, RGB(255,0,0)) ' Types: RECTANGLE, CIRCLE, TRIANGLE
501
+ sprite$ = LOADIMAGE("player.png") ' PNG (with alpha) or BMP
502
+
503
+ MOVESHAPE sprite$, x, y ' Position (top-left point)
504
+ ROTATESHAPE sprite$, angle ' Absolute degrees
505
+ SCALESHAPE sprite$, 1.5 ' 1.0 = original
506
+ SHOWSHAPE sprite$
507
+ HIDESHAPE sprite$
508
+ DRAWSHAPE sprite$ ' Render
509
+ REMOVESHAPE sprite$ ' Free memory
510
+ ```
511
+ - PNG recommended (full alpha transparency 0-255), BMP for legacy
512
+ - Images positioned by their **top-left point**
513
+ - Rotation is absolute, not cumulative
514
+ - Always REMOVESHAPE when done to free memory
515
+
516
+ ### Sprite Sheets (LOADSHEET)
517
+ ```basic
518
+ DIM sprites$
519
+ LOADSHEET sprites$, spriteW, spriteH, "sheet.png"
520
+
521
+ ' Access sprites by 1-based index
522
+ MOVESHAPE sprites$(index$), x#, y#
523
+ DRAWSHAPE sprites$(index$)
524
+ ```
525
+ - Sprites indexed left-to-right, top-to-bottom starting at 1
526
+ - All sprites must be same size (spriteW x spriteH)
527
+
528
+ ### Mouse Input (Graphics Mode Only)
529
+ | Function | Description |
530
+ |----------|-------------|
531
+ | `MOUSEX` / `MOUSEY` | Cursor position |
532
+ | `MOUSEB` | Button state (bitmask, use `AND`) |
533
+
534
+ Button constants: `MOUSE_LEFT#`=1, `MOUSE_RIGHT#`=2, `MOUSE_MIDDLE#`=4
535
+
536
+ ### Color Palette (0-15)
537
+ | 0 Black | 4 Red | 8 Dark Gray | 12 Light Red |
538
+ |---------|-------|-------------|--------------|
539
+ | 1 Blue | 5 Magenta | 9 Light Blue | 13 Light Magenta |
540
+ | 2 Green | 6 Brown | 10 Light Green | 14 Yellow |
541
+ | 3 Cyan | 7 Light Gray | 11 Light Cyan | 15 White |
542
+
543
+ ### Performance Tips
544
+ 1. Use `SCREENLOCK ON/OFF` for all animation
545
+ 2. `LINE...BF` is faster than `CLS` for clearing
546
+ 3. Store `RGB()` values in constants
547
+ 4. REMOVESHAPE unused shapes
548
+ 5. SLEEP 16 for ~60 FPS
549
+ 6. Do math/logic outside SCREENLOCK block
550
+
551
+ ---
552
+
553
+ ## Built-in Constants
554
+
555
+ ### Arrow Keys
556
+ | | | |
557
+ |---|---|---|
558
+ | `KEY_UP#` | `KEY_DOWN#` | `KEY_LEFT#` |
559
+ | `KEY_RIGHT#` | | |
560
+
561
+ ### Special Keys
562
+ | | | |
563
+ |---|---|---|
564
+ | `KEY_ESC#` | `KEY_TAB#` | `KEY_BACKSPACE#` |
565
+ | `KEY_ENTER#` | `KEY_SPACE#` | `KEY_INSERT#` |
566
+ | `KEY_DELETE#` | `KEY_HOME#` | `KEY_END#` |
567
+ | `KEY_PGUP#` | `KEY_PGDN#` | |
568
+
569
+ ### Modifier Keys
570
+ | | | |
571
+ |---|---|---|
572
+ | `KEY_LSHIFT#` | `KEY_RSHIFT#` | `KEY_LCTRL#` |
573
+ | `KEY_RCTRL#` | `KEY_LALT#` | `KEY_RALT#` |
574
+ | `KEY_LWIN#` | `KEY_RWIN#` | |
575
+
576
+ ### Function Keys
577
+ | | | |
578
+ |---|---|---|
579
+ | `KEY_F1#` | `KEY_F2#` | `KEY_F3#` |
580
+ | `KEY_F4#` | `KEY_F5#` | `KEY_F6#` |
581
+ | `KEY_F7#` | `KEY_F8#` | `KEY_F9#` |
582
+ | `KEY_F10#` | `KEY_F11#` | `KEY_F12#` |
583
+
584
+ ### Numpad Keys
585
+ | | | |
586
+ |---|---|---|
587
+ | `KEY_NUMPAD0#` | `KEY_NUMPAD1#` | `KEY_NUMPAD2#` |
588
+ | `KEY_NUMPAD3#` | `KEY_NUMPAD4#` | `KEY_NUMPAD5#` |
589
+ | `KEY_NUMPAD6#` | `KEY_NUMPAD7#` | `KEY_NUMPAD8#` |
590
+ | `KEY_NUMPAD9#` | | |
591
+
592
+ ### Punctuation Keys
593
+ | | | |
594
+ |---|---|---|
595
+ | `KEY_COMMA#` | `KEY_DOT#` | `KEY_MINUS#` |
596
+ | `KEY_EQUALS#` | `KEY_SLASH#` | `KEY_BACKSLASH#` |
597
+ | `KEY_SEP#` | `KEY_GRAVE#` | `KEY_LBRACKET#` |
598
+ | `KEY_RBRACKET#` | | |
599
+
600
+ ### Alphabet Keys
601
+ | | | |
602
+ |---|---|---|
603
+ | `KEY_A#` | `KEY_B#` | `KEY_C#` |
604
+ | `KEY_D#` | `KEY_E#` | `KEY_F#` |
605
+ | `KEY_G#` | `KEY_H#` | `KEY_I#` |
606
+ | `KEY_J#` | `KEY_K#` | `KEY_L#` |
607
+ | `KEY_M#` | `KEY_N#` | `KEY_O#` |
608
+ | `KEY_P#` | `KEY_Q#` | `KEY_R#` |
609
+ | `KEY_S#` | `KEY_T#` | `KEY_U#` |
610
+ | `KEY_V#` | `KEY_W#` | `KEY_X#` |
611
+ | `KEY_Y#` | `KEY_Z#` | |
612
+
613
+ ### Number Keys
614
+ | | | |
615
+ |---|---|---|
616
+ | `KEY_0#` | `KEY_1#` | `KEY_2#` |
617
+ | `KEY_3#` | `KEY_4#` | `KEY_5#` |
618
+ | `KEY_6#` | `KEY_7#` | `KEY_8#` |
619
+ | `KEY_9#` | | |
620
+
621
+ ### Mouse
622
+ ```basic
623
+ MOUSE_LEFT# = 1, MOUSE_RIGHT# = 2, MOUSE_MIDDLE# = 4
624
+ ```
625
+
626
+ ### Logical
627
+ `TRUE` = 1, `FALSE` = 0
628
+
629
+ ---
630
+
631
+ ## Source Control
632
+
633
+ ### INCLUDE
634
+ ```basic
635
+ INCLUDE "other_file.bas" ' Insert source at this point
636
+ INCLUDE "MathLib.bb" ' Include compiled library
637
+ ```
638
+
639
+ ### Libraries (.bb files)
640
+ ```basic
641
+ ' MathLib.bas can ONLY contain DEF FN functions
642
+ DEF FN add$(x$, y$)
643
+ RETURN x$ + y$
644
+ END DEF
645
+ ```
646
+ Compile: `bazzbasic.exe -lib MathLib.bas``MathLib.bb`
647
+
648
+ ```basic
649
+ INCLUDE "MathLib.bb"
650
+ PRINT FN MATHLIB_add$(5, 3) ' Auto-prefix: FILENAME_ + functionName
651
+ ```
652
+ - Libraries can only contain `DEF FN` functions
653
+ - Library functions can access main program constants (`#`)
654
+ - Version-locked: .bb may not work across BazzBasic versions
655
+
656
+ ---
657
+
658
+ ## Common Patterns
659
+
660
+ ### Game Loop
661
+ ```basic
662
+ SCREEN 12
663
+ LET running$ = TRUE
664
+
665
+ WHILE running$
666
+ LET key$ = INKEY
667
+ IF key$ = KEY_ESC# THEN running$ = FALSE
668
+
669
+ ' Math and logic here
670
+
671
+ SCREENLOCK ON
672
+ LINE (0,0)-(640,480), 0, BF ' Fast clear
673
+ ' Draw game state
674
+ SCREENLOCK OFF
675
+ SLEEP 16
676
+ WEND
677
+ END
678
+ ```
679
+
680
+ ### HTTP + Data
681
+ ```basic
682
+ DIM response$
683
+ LET response$ = HTTPGET("https://api.example.com/scores")
684
+ PRINT response$
685
+
686
+ DIM payload$
687
+ LET payload$ = HTTPPOST("https://api.example.com/submit", "{""score"":9999}")
688
+ PRINT payload$
689
+ ```
690
+
691
+ ### Save/Load with Arrays
692
+ ```basic
693
+ DIM save$
694
+ save$("level") = 3
695
+ save$("hp") = 80
696
+ FileWrite "save.txt", save$
697
+
698
+ DIM load$
699
+ LET load$ = FileRead("save.txt")
700
+ PRINT load$("level") ' Output: 3
701
+ ```
702
+
703
+ ### Sound with Graphics
704
+ ```basic
705
+ SCREEN 12
706
+ LET bgm$ = LOADSOUND("music.wav")
707
+ LET sfx$ = LOADSOUND("jump.wav")
708
+ SOUNDREPEAT(bgm$)
709
+
710
+ WHILE INKEY <> KEY_ESC#
711
+ IF INKEY = KEY_SPACE# THEN SOUNDONCE(sfx$)
712
+ SLEEP 16
713
+ WEND
714
+ SOUNDSTOPALL
715
+ END
716
+ ```
717
+
718
+ ---
719
+
720
+ ## Code Style Conventions
721
+
722
+ **Variables** — `camelCase$`
723
+ ```basic
724
+ LET playerName$ = "Hero"
725
+ LET score$ = 0
726
+ ```
727
+
728
+ **Constants** — `UPPER_SNAKE_CASE#`
729
+ ```basic
730
+ LET MAX_HEALTH# = 100
731
+ LET SCREEN_W# = 640
732
+ ```
733
+
734
+ **Arrays** — `camelCase$` (like variables, declared with DIM)
735
+ ```basic
736
+ DIM scores$
737
+ DIM playerData$
738
+ ```
739
+
740
+ **User-defined functions** — `PascalCase$`
741
+ ```basic
742
+ DEF FN CalculateDamage$(attack$, defence$)
743
+ DEF FN IsColliding$(x$, y$)
744
+ ```
745
+
746
+ **Labels** — descriptive names, `[sub:]` prefix recommended for subroutines
747
+ ```basic
748
+ [sub:DrawPlayer] ' Subroutine
749
+ [gameLoop] ' Jump target
750
+ ```
751
+
752
+ ### Program Structure
753
+ 1. Constants first
754
+ 2. User-defined functions (or INCLUDE them)
755
+ 3. Main program loop
756
+ 4. Subroutines (labels) last
757
+
758
+ ### Subroutine Indentation
759
+ ```basic
760
+ [sub:DrawPlayer]
761
+ MOVESHAPE player$, x$, y$
762
+ DRAWSHAPE player$
763
+ RETURN
764
  ```