EkBass commited on
Commit
a27552f
·
verified ·
1 Parent(s): 074a8e9

Delete BazzBasic-AI-guide.txt

Browse files
Files changed (1) hide show
  1. BazzBasic-AI-guide.txt +0 -1137
BazzBasic-AI-guide.txt DELETED
@@ -1,1137 +0,0 @@
1
- # METADATA:
2
-
3
- Name: BazzBasic
4
-
5
- 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
6
-
7
- 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.
8
-
9
- 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.
10
-
11
- Version: This guide is written for BazzBasic version 1.1b and is updated 23.03.2026 Finnish time.
12
-
13
- # END METADATA
14
-
15
- ---
16
-
17
- # BazzBasic Language Reference
18
-
19
- 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.
20
-
21
- **Version:** 1.1b (Released March, 2026)
22
- **Author:** Kristian Virtanen (krisu.virtanen@gmail.com)
23
- **Platform:** Windows (x64 primary); Linux/macOS possible with effort
24
- **Dependencies:** SDL2.dll, SDL2_mixer (bundled).
25
- **Github:** https://github.com/EkBass/BazzBasic
26
- **Homepage:** https://ekbass.github.io/BazzBasic/
27
- **Manual:** "https://ekbass.github.io/BazzBasic/manual/#/"
28
- **Examples:** "https://github.com/EkBass/BazzBasic/tree/main/Examples"
29
- **Github_repo:** "https://github.com/EkBass/BazzBasic"
30
- **Github_discussions:** "https://github.com/EkBass/BazzBasic/discussions"
31
- **Discord_channel:** "https://discord.com/channels/682603735515529216/1464283741919907932"
32
- **Thinbasic subforum:** "https://www.thinbasic.com/community/forumdisplay.php?401-BazzBasic"
33
-
34
- ## Few examples:
35
- **raycaster_3d:** https://raw.githubusercontent.com/EkBass/BazzBasic/refs/heads/main/Examples/raycaster_3d_optimized.bas
36
- **voxel_terrain:** https://raw.githubusercontent.com/EkBass/BazzBasic/refs/heads/main/Examples/voxel_terrain.bas
37
- **sprite load:** https://github.com/EkBass/BazzBasic/blob/main/Examples/countdown_demo.bas
38
- **Eliza:** https://github.com/EkBass/BazzBasic/blob/main/Examples/Eliza.bas
39
-
40
- ## This guide:
41
- **BazzBasic AI-guide:** https://huggingface.co/datasets/EkBass/BazzBasic_AI_Guide/tree/main
42
- Just download the latest *BazzBasic-AI-guide-DDMMYYYY.md* where *DDMMYYYY* marks the date.
43
-
44
-
45
- ## CLI Usage
46
-
47
- | Command | Action |
48
- |---------|--------|
49
- | `bazzbasic.exe` | Launch IDE |
50
- | `bazzbasic.exe file.bas` | Run program |
51
- | `bazzbasic.exe -exe file.bas` | Create standalone .exe |
52
- | `bazzbasic.exe -lib file.bas` | Create tokenized library (.bb) |
53
-
54
- 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.
55
-
56
- In-built IDE is just a small very basic IDE which fires up when double-clicking *bazzbasic.exe*. Usage of Notepad++ etc. recommended.
57
-
58
- ### External syntax colors.
59
- 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.
60
-
61
- There are syntax color files for *Notepad++*, *Geany* and *Visual Studio Code* available at https://github.com/EkBass/BazzBasic/tree/main/extras
62
-
63
- ---
64
-
65
- ## Syntax Fundamentals
66
-
67
- ### Variables and Constants
68
- Forget traditional BASIC types. Suffixes in BazzBasic define mutability, not data type.
69
-
70
- Variables and arrays in BazzBasic are typeless: hold numbers or strings interchangeably
71
-
72
- - $ = Mutable Variable
73
- - # = Immutable Constant
74
-
75
-
76
- ```basic
77
- LET a$ ' Declare without value
78
- LET name$ = "Alice" ' Variable, string (mutable, $ suffix)
79
- LET age$ = 19 ' Variable, integer (mutable, $ suffix)
80
- LET price$ = 1.99 ' Variable, decimal (mutable, $ suffix)
81
- LET PI# = 3.14159 ' Constant (immutable, # suffix)
82
- LET x$, y$, z$ = 10 ' Multiple declaration
83
- ```
84
- - All variables require `$` suffix, constants require `#`
85
- - Typeless: hold numbers or strings interchangeably
86
- - Must declare with `LET` before use (except FOR/INPUT which auto-declare)
87
- - Assignment after declaration: `x$ = x$ + 1` (no LET needed)
88
- - **Case-insensitive**: `PRINT`, `print`, `Print` all work
89
- - Naming: letters, numbers, underscores; cannot start with number
90
-
91
- ### Comparison Behavior
92
- `"123" = 123` is TRUE (cross-type comparison), but slower - keep types consistent.
93
-
94
- ### Scope
95
- - Main code shares one scope (even inside IF blocks)
96
- - `DEF FN` functions have completely isolated scope
97
- - Only global constants (`#`) are accessible inside functions
98
-
99
- ### Multiple Statements Per Line
100
- ```basic
101
- COLOR 14, 0 : CLS : PRINT "Hello"
102
- ```
103
-
104
- ### Comments
105
- ```basic
106
- REM This is a comment
107
- ' This is also a comment
108
- PRINT "Hello" ' Inline comment
109
- ```
110
-
111
- ### Escape Sequences in Strings
112
- | Sequence | Result |
113
- |----------|--------|
114
- | `\"` | Quote |
115
- | `\n` | Newline |
116
- | `\t` | Tab |
117
- | `\\` | Backslash |
118
-
119
- ---
120
-
121
- ## Arrays
122
- ```basic
123
- DIM scores$ ' Declare (must use DIM)
124
- DIM a$, b$, c$ ' Multiple declaration
125
- scores$(0) = 95 ' Numeric index (0-based)
126
- scores$("name") = "Alice" ' String key (associative)
127
- matrix$(0, 1) = "A2" ' Multi-dimensional
128
- data$(1, "header") = "Name" ' Mixed indexing
129
- ```
130
- - Array names must end with `$`
131
- - Fully dynamic: numeric, string, multi-dimensional, mixed indexing
132
- - Arrays **cannot** be passed directly to functions - pass values of individual elements
133
- - Accessing uninitialized elements is an error - check with `HASKEY` first
134
-
135
- ### Array Functions
136
- | Function | Description |
137
- |----------|-------------|
138
- | `LEN(arr$())` | Element count (note: empty parens) |
139
- | `HASKEY(arr$(key))` | 1 if exists, 0 if not |
140
- | `DELKEY arr$(key)` | Remove element |
141
- | `DELARRAY arr$` | Remove entire array (can re-DIM after) |
142
-
143
- ---
144
-
145
- ## Operators
146
-
147
- ### Arithmetic
148
- `+` (add/concatenate), `-`, `*`, `/` (returns float)
149
-
150
- ### Comparison
151
- `=` or `==`, `<>` or `!=`, `<`, `>`, `<=`, `>=`
152
-
153
- ### Logical
154
- `AND`, `OR`, `NOT`
155
-
156
- ### Precedence (high to low)
157
- `()` ? `NOT` ? `*`, `/` ? `+`, `-` ? comparisons ? `AND` ? `OR`
158
-
159
- ---
160
-
161
- ## User-Defined Functions
162
- ```basic
163
- DEF FN add$(a$, b$)
164
- RETURN a$ + b$
165
- END DEF
166
- PRINT FN add$(3, 4) ' Call with FN prefix
167
- ```
168
- - Function name **must** end with `$`, called with `FN` prefix
169
- - **Must be defined before called** (top of file or via INCLUDE)
170
- - Parameters passed **by value**
171
- - Completely isolated scope: no access to global variables, only global constants (`#`)
172
- - Labels inside functions are local - GOTO/GOSUB **cannot** jump outside (error)
173
- - Supports recursion
174
- - Tip: put functions in separate files, `INCLUDE` at program start
175
-
176
- ---
177
-
178
- ## Control Flow
179
-
180
- ### IF Statements
181
- ```basic
182
- ' Block IF (END IF and ENDIF both work)
183
- IF x$ > 10 THEN
184
- PRINT "big"
185
- ELSEIF x$ > 5 THEN
186
- PRINT "medium"
187
- ELSE
188
- PRINT "small"
189
- ENDIF
190
-
191
- ' One-line IF (GOTO/GOSUB only)
192
- IF lives$ = 0 THEN GOTO [game_over]
193
- IF key$ = KEY_ESC# THEN GOTO [menu] ELSE GOTO [continue]
194
- IF ready$ = 1 THEN GOSUB [start_game]
195
- ```
196
-
197
- ### FOR Loops
198
- ```basic
199
- FOR i$ = 1 TO 10 STEP 2 ' Auto-declares variable, STEP optional
200
- PRINT i$
201
- NEXT
202
-
203
- FOR i$ = 10 TO 1 STEP -1 ' Count down
204
- PRINT i$
205
- NEXT
206
- ```
207
-
208
- ### WHILE Loops
209
- ```basic
210
- WHILE x$ < 100
211
- x$ = x$ * 2
212
- WEND
213
- ```
214
-
215
- ### Labels, GOTO, GOSUB
216
- ```basic
217
- [start]
218
- GOSUB [subroutine]
219
- GOTO [start]
220
- END
221
-
222
- [subroutine]
223
- PRINT "Hello"
224
- RETURN
225
- ```
226
-
227
- ### Dynamic Jumps (Labels as Variables)
228
- ```basic
229
- LET target$ = "[menu]"
230
- GOTO target$ ' Variable holds label string
231
- LET dest# = "[jump]"
232
- GOSUB dest# ' Constants work too
233
- ```
234
-
235
- If you want to make a dynamic jump, use the "[" and "]" characters to indicate to BazzBasic that it is specifically a label.
236
-
237
- ```basic
238
- LET target$ = "[menu]" ' Correct way
239
- LET target$ = "menu" ' Incorrect way
240
- ```
241
-
242
- ### Other
243
- | Command | Description |
244
- |---------|-------------|
245
- | `SLEEP ms` | Pause execution |
246
- | `END` | Terminate program |
247
-
248
- ---
249
-
250
- ## I/O Commands
251
-
252
- | Command | Description |
253
- |---------|-------------|
254
- | `PRINT expr; expr` | Output (`;` = no space, `,` = tab) |
255
- | `PRINT "text";` | Trailing `;` suppresses newline |
256
- | `INPUT "prompt", var$` | Read input (splits on whitespace/comma) |
257
- | `INPUT "prompt", a$, b$` | Read multiple values |
258
- | `INPUT var$` | Default prompt `"? "` |
259
- | `LINE INPUT "prompt", var$` | Read entire line including spaces |
260
- | `CLS` | Clear screen |
261
- | `LOCATE row, col` | Move cursor (1-based) |
262
- | `COLOR fg, bg` | Set colors (0-15 palette) |
263
- | `GETCONSOLE(row, col, type)` | Console read: 0=char(ASCII), 1=fg, 2=bg |
264
- | `INKEY` | Non-blocking key check (0 if none, >256 for special keys) |
265
- | `KEYDOWN(key#)` | Returns TRUE if specified key is currently held down |
266
- | `WAITKEY(key#, key2#...)` | Halts program until requested key pressed |
267
-
268
- ### INPUT vs LINE INPUT
269
- | Feature | INPUT | LINE INPUT |
270
- |---------|-------|------------|
271
- | Reads spaces | No (splits) | Yes |
272
- | Multiple variables | Yes | No |
273
- | Default prompt | `"? "` | None |
274
-
275
- ### INKEY vs KEYDOWN
276
- | Feature | INKEY | KEYDOWN |
277
- |---------|-------|---------|
278
- | Returns | Key value or 0 | TRUE/FALSE |
279
- | Key held | Reports once | Reports while held |
280
- | Use case | Menu navigation | Game movement, held keys |
281
-
282
- ```basic
283
- ' KEYDOWN example - smooth movement
284
- IF KEYDOWN(KEY_LEFT#) THEN x$ = x$ - 5
285
- IF KEYDOWN(KEY_RIGHT#) THEN x$ = x$ + 5
286
- ```
287
- ### WAITKEY
288
- Halts the execution until requested key is pressed.
289
- ```vb
290
- ' Wait just the ENTER key
291
- PRINT "ENTER"
292
- PRINT WAITKEY(KEY_ENTER#) ' output: 13
293
-
294
- ' Wait any of "a", "b" or "esc" keys
295
- PRINT "A, B or ESC"
296
- PRINT WAITKEY(KEY_A#, KEY_B#, KEY_ESC#) ' output: depends of your choice of key
297
-
298
- PRINT "Press any key..."
299
- LET a$ = WAITKEY()
300
- PRINT a$ ' output: val of key you pressed
301
- ```
302
- ---
303
-
304
- ## Math Functions
305
-
306
- | Function | Description |
307
- |----------|-------------|
308
- | `ABS(n)` | Absolute value |
309
- | `ATAN(n)` | Returns the arc tangent of n |
310
- | `BETWEEN(n, min, max)` | TRUE if n is in range |
311
- | `CEIL(n)` | Rounds up |
312
- | `CINT(n)` | Convert to integer (rounds) |
313
- | `COS(n)` | Returns the cosine of an angle |
314
- | `CLAMP(n, min, max)` | Constrain value to range |
315
- | `DEG(radians)` | Radians to degrees |
316
- | `DISTANCE(x1,y1,x2,y2)` | 2D Euclidean distance |
317
- | `DISTANCE(x1,y1,z1,x2,y2,z2)` | 3D Euclidean distance |
318
- | `EXP(n)` | Exponential (e^n) |
319
- | `FLOOR(n)` | Rounds down |
320
- | `INT(n)` | Truncate toward zero |
321
- | `LERP(start, end, t)` | Linear interpolation (t = 0.0-1.0) |
322
- | `LOG(n)` | Natural log |
323
- | `MAX(a, b)` | Returns higher from a & b |
324
- | `MIN(a, b)` | Returns smaller from a & b |
325
- | `MOD(a, b)` | Remainder |
326
- | `POW(base, exp)` | Power |
327
- | `RAD(degrees)` | Degrees to radians |
328
- | `RND(n)` | Random 0 to n-1 |
329
- | `ROUND(n)` | Standard rounding |
330
- | `SGN(n)` | Sign (-1, 0, 1) |
331
- | `SIN(n)` | Returns the sine of n |
332
- | `SQR(n)` | Square root |
333
- | `TAN(n)` | Returns the tangent of n |
334
-
335
-
336
- ### Math Constants
337
- | Constant | Value | Notes |
338
- |----------|-------|-------|
339
- | `PI` | 3.14159265358979 | 180 |
340
- | `HPI` | 1.5707963267929895 | 90 (PI/2) |
341
- | `QPI` | 0.7853981633974483 | 45 (PI/4) |
342
- | `TAU` | 6.283185307179586 | 360 (PI*2) |
343
- | `EULER` | 2.718281828459045 | e |
344
-
345
- All are raw-coded values - no math at runtime, maximum performance.
346
-
347
- ### Fast Trigonometry (Lookup Tables)
348
- For graphics-intensive applications - ~20x faster than SIN/COS, 1-degree precision.
349
-
350
- ```basic
351
- FastTrig(TRUE) ' Enable lookup tables (~5.6 KB)
352
-
353
- LET x$ = FastCos(45) ' Degrees, auto-normalized to 0-359
354
- LET y$ = FastSin(90)
355
- LET r$ = FastRad(180) ' Degrees to radians (doesn't need FastTrig)
356
-
357
- FastTrig(FALSE) ' Free memory when done
358
- ```
359
-
360
- **Use FastTrig when:** raycasting, rotating sprites, particle systems, any loop calling trig hundreds of times per frame.
361
- **Use regular SIN/COS when:** high precision needed, one-time calculations.
362
-
363
- ---
364
-
365
- ## String Functions
366
-
367
- | Function | Description |
368
- |----------|-------------|
369
- | `ASC(s$)` | Character to ASCII code |
370
- | `CHR(n)` | ASCII code to character |
371
- | `INSTR(s$, search$)` | Find substring position (0 = not found) |
372
- | `INSTR(start$, s$, search$)` | Find substring starting from position start$ (0 = not found) |
373
- | `INVERT(s$)` | Inverts a string |
374
- | `LCASE(s$)` | Converts to lowercase |
375
- | `LEFT(s$, n)` | First n characters |
376
- | `LEN(s$)` | String length |
377
- | `LTRIM(s$)` | Left trim |
378
- | `MID(s$, start, len)` | Substring (1-based) len optional |
379
- | `REPEAT(s$, n)` | Repeat s$ for n times |
380
- | `REPLACE(s$, old$, new$)` | Replace all occurrences |
381
- | `RIGHT(s$, n)` | Last n characters |
382
- | `RTRIM(s$)` | Right trim |
383
- | `SPLIT(arr$, s$, delim$)` | Split into array |
384
- | `SRAND(n)` | Returns random string length of n from allowed chars (letters, numbers and "_") |
385
- | `STR(n)` | Number to string |
386
- | `TRIM(s$)` | Remove leading/trailing spaces |
387
- | `UCASE(s$)` | Converts to uppercase |
388
- | `VAL(s$)` | String to number |
389
-
390
- ### SPLIT example
391
- ```vb
392
- DIM parts$
393
- REM Split with ","
394
- LET count$ = SPLIT(parts$, "apple,banana,orange", ",")
395
- PRINT "Parts: "; count$
396
- PRINT parts$(0) ' "apple"
397
- PRINT parts$(1) ' "banana"
398
- PRINT parts$(2) ' "orange"
399
- ```
400
- ---
401
-
402
- ## File I/O
403
-
404
- ```basic
405
- ' Simple text file
406
- FileWrite "save.txt", data$
407
- LET data$ = FileRead("save.txt")
408
-
409
- ' Array read/write (key=value format)
410
- DIM a$
411
- a$("name") = "player1"
412
- a$("score") = 9999
413
- FileWrite "scores.txt", a$
414
-
415
- DIM b$
416
- LET b$ = FileRead("scores.txt")
417
- PRINT b$("name") ' Output: player1
418
- ```
419
-
420
- **Array file format:**
421
- ```
422
- name=player1
423
- score=9999
424
- multi,dim,key=value
425
- ```
426
-
427
- | Function/Command | Description |
428
- |---------|-------------|
429
- | `FileRead(path)` | Read file; returns string or populates array |
430
- | `FileWrite path, data` | Write string or array to file |
431
- | `FileExists(path)` | Returns 1 if file exists, 0 if not |
432
- | `FileDelete path` | Delete a file |
433
- | `FileList(path$, arr$)` | List files in directory into array |
434
- | `SHELL(cmd$)` | Run system command, returns output. Default 5000ms timeout |
435
- | `SHELL(cmd$, timeout$)` | Run system command with custom timeout in milliseconds |
436
-
437
- ### FileRead into array: key=value and .env support
438
-
439
- When `FileRead` assigns to a `DIM`'d array, BazzBasic automatically parses `key=value` lines into array elements. Lines starting with `#` are treated as comments and ignored.
440
-
441
- ```vb
442
- ' settings.txt contains:
443
- ' # Game config
444
- ' width=800
445
- ' height=600
446
-
447
- DIM config$
448
- LET config$ = FileRead("settings.txt")
449
- PRINT config$("width") ' Output: 800
450
- PRINT config$("height") ' Output: 600
451
- ```
452
-
453
- This makes `.env` files work directly for storing API keys outside source code:
454
-
455
- ```vb
456
- ' .env contains:
457
- ' OPENAI_API_KEY=sk-proj-...
458
-
459
- IF FileExists(".env") = 0 THEN
460
- PRINT "Error: .env not found"
461
- END
462
- END IF
463
-
464
- DIM env$
465
- LET env$ = FileRead(".env")
466
- LET API_KEY# = env$("OPENAI_API_KEY")
467
- ```
468
-
469
- **Note:** Assigning `FileRead` to a regular variable (`LET data$`) always returns raw file contents as a plain string. Array parsing only happens when the target is a `DIM`'d array.
470
-
471
- ---
472
-
473
- ## Network (HTTP)
474
-
475
- ```basic
476
- ' Simple GET
477
- LET response$ = HTTPGET("https://api.example.com/data")
478
-
479
- ' GET with custom headers
480
- DIM headers$
481
- headers$("Authorization") = "Bearer mytoken"
482
- LET response$ = HTTPGET("https://api.example.com/data", headers$)
483
-
484
- ' Simple POST
485
- LET result$ = HTTPPOST("https://api.example.com/post", "{""key"":""value""}")
486
-
487
- ' POST with custom headers
488
- DIM headers$
489
- headers$("Authorization") = "Bearer mytoken"
490
- headers$("Content-Type") = "application/json"
491
- LET result$ = HTTPPOST("https://api.example.com/post", body$, headers$)
492
- ```
493
-
494
- - Returns response body as string
495
- - Supports HTTPS
496
- - Use `""` inside strings to escape quotes in JSON bodies
497
- - Timeout handled gracefully - returns error message string on failure
498
- - Optional `headers$` array parameter: pass any HTTP headers including Authorization, Content-Type, etc.
499
-
500
- ### HTTP + JSON API pattern
501
- ```vb
502
- DIM headers$
503
- headers$("Authorization") = "Bearer " + API_KEY#
504
- headers$("Content-Type") = "application/json"
505
-
506
- DIM body$
507
- body$("model") = "gpt-4.1-mini"
508
- body$("messages,0,role") = "user"
509
- body$("messages,0,content") = "Hello"
510
- body$("max_tokens") = 100
511
-
512
- LET jsonBody$ = ASJSON(body$)
513
- LET raw$ = HTTPPOST("https://api.openai.com/v1/chat/completions", jsonBody$, headers$)
514
-
515
- DIM result$
516
- LET count$ = ASARRAY(result$, raw$)
517
- PRINT result$("choices,0,message,content") ' Output: the AI response text
518
- ```
519
-
520
- ### Encoding & Hashing
521
-
522
- | Function | Description |
523
- |----------|-------------|
524
- | `BASE64ENCODE(s$)` | Encode string to Base64 |
525
- | `BASE64DECODE(s$)` | Decode Base64 string |
526
- | `SHA256(s$)` | Returns lowercase hex SHA256 hash |
527
-
528
- ```vb
529
- LET encoded$ = BASE64ENCODE("Hello, World!")
530
- PRINT encoded$ ' SGVsbG8sIFdvcmxkIQ==
531
-
532
- LET decoded$ = BASE64DECODE(encoded$)
533
- PRINT decoded$ ' Hello, World!
534
-
535
- LET hash$ = SHA256("password123")
536
- PRINT hash$ ' ef92b778... (64-char lowercase hex)
537
- ```
538
-
539
- ### JSON
540
- BazzBasic arrays map naturally to JSON. Nested objects become comma-separated keys.
541
-
542
- ```vb
543
- DIM data$
544
- data$("name") = "Alice"
545
- data$("score") = 9999
546
- data$("address,city") = "New York"
547
- data$("skills,0") = "JavaScript"
548
- data$("skills,1") = "Python"
549
-
550
- LET json$ = ASJSON(data$)
551
- ' Output: {"name":"Alice","score":9999,"address":{"city":"New York"},"skills":["JavaScript","Python"]}
552
-
553
- DIM back$
554
- LET count$ = ASARRAY(back$, json$)
555
- PRINT back$("name") ' Output: Alice
556
- PRINT back$("address,city") ' Output: New York
557
- PRINT back$("skills,0") ' Output: JavaScript
558
-
559
- SAVEJSON data$, "scores.json"
560
- DIM loaded$
561
- LOADJSON loaded$, "scores.json"
562
- ```
563
-
564
- | Function | Description |
565
- |----------|-------------|
566
- | `ASJSON(arr$)` | Convert array to JSON string |
567
- | `ASARRAY(arr$, json$)` | Fill array from JSON string, returns element count |
568
- | `LOADJSON arr$, path$` | Load JSON file into array |
569
- | `SAVEJSON arr$, path$` | Save array as formatted JSON file |
570
-
571
- **Nested JSON key convention:** `object,key` and `array,index` (0-based)
572
-
573
- ---
574
-
575
- ## Sound (SDL2_mixer)
576
-
577
- ```basic
578
- DIM bgm$
579
- LET bgm$ = LOADSOUND("music.wav")
580
- LET sfx$ = LOADSOUND("jump.wav")
581
-
582
- SOUNDREPEAT(bgm$) ' Loop continuously
583
- SOUNDONCE(sfx$) ' Play once
584
- SOUNDSTOP(bgm$) ' Stop specific sound
585
- SOUNDSTOPALL ' Stop all sounds
586
- ```
587
-
588
- | Command | Description |
589
- |---------|-------------|
590
- | `LOADSOUND(path)` | Load sound file, returns ID |
591
- | `SOUNDONCE(id$)` | Play once |
592
- | `SOUNDREPEAT(id$)` | Loop continuously |
593
- | `SOUNDSTOP(id$)` | Stop specific sound |
594
- | `SOUNDSTOPALL` | Stop all sounds |
595
-
596
- - Formats: WAV (recommended), MP3, others via SDL2_mixer
597
- - Thread-safe, multiple simultaneous sounds supported
598
- - Load sounds once at startup for performance
599
-
600
- ---
601
-
602
- ## Graphics (SDL2)
603
-
604
- ### Screen Setup
605
- ```basic
606
- SCREEN 12 ' 640x480 VGA mode
607
- SCREEN 0, 800, 600, "Title" ' Custom size with title
608
- ```
609
- Modes: 0=640x400, 1=320x200, 2=640x350, 7=320x200, 9=640x350, 12=640x480, 13=320x200
610
-
611
- ### Fullscreen
612
- ```basic
613
- FULLSCREEN TRUE ' Borderless fullscreen
614
- FULLSCREEN FALSE ' Windowed mode
615
- ```
616
- Call after `SCREEN`.
617
-
618
- ### VSync
619
- ```basic
620
- VSYNC(TRUE) ' Enable (default) - caps to monitor refresh
621
- VSYNC(FALSE) ' Disable - unlimited FPS, may tear
622
- ```
623
-
624
- ### Double Buffering (Required for Animation)
625
- ```basic
626
- SCREENLOCK ON ' Start buffering
627
- ' ... draw commands ...
628
- SCREENLOCK OFF ' Display frame
629
- SLEEP 16 ' ~60 FPS
630
- ```
631
- `SCREENLOCK` without argument = `SCREENLOCK ON`. Do math/logic outside SCREENLOCK block.
632
-
633
- ### Drawing Primitives
634
- | Command | Description |
635
- |---------|-------------|
636
- | `PSET (x, y), color` | Draw pixel |
637
- | `POINT(x, y)` | Read pixel color (returns RGB integer) |
638
- | `LINE (x1,y1)-(x2,y2), color` | Draw line |
639
- | `LINE (x1,y1)-(x2,y2), color, B` | Box outline |
640
- | `LINE (x1,y1)-(x2,y2), color, BF` | Filled box (faster than CLS) |
641
- | `CIRCLE (cx, cy), r, color` | Circle outline |
642
- | `CIRCLE (cx, cy), r, color, 1` | Filled circle |
643
- | `PAINT (x, y), fill, border` | Flood fill |
644
- | `RGB(r, g, b)` | Create color (0-255 each) |
645
-
646
- ### Shape/Sprite System
647
- ```basic
648
- DIM sprite$
649
- sprite$ = LOADSHAPE("RECTANGLE", 50, 50, RGB(255,0,0)) ' Types: RECTANGLE, CIRCLE, TRIANGLE
650
- sprite$ = LOADIMAGE("player.png") ' PNG (with alpha) or BMP
651
-
652
- MOVESHAPE sprite$, x, y ' Position (top-left point)
653
- ROTATESHAPE sprite$, angle ' Absolute degrees
654
- SCALESHAPE sprite$, 1.5 ' 1.0 = original
655
- SHOWSHAPE sprite$
656
- HIDESHAPE sprite$
657
- DRAWSHAPE sprite$ ' Render
658
- REMOVESHAPE sprite$ ' Free memory
659
- ```
660
- - PNG recommended (full alpha transparency 0-255), BMP for legacy
661
- - Images positioned by their **top-left point**
662
- - Rotation is absolute, not cumulative
663
- - Always REMOVESHAPE when done to free memory
664
-
665
- ### LOADIMAGE with url
666
- ```vb
667
- LET sprite$ = LOADIMAGE("https://example.com/sprite.png")
668
- ' ? downloads "sprite.png" to root of your program
669
- ' ? sprite$ works just as with normal file load
670
-
671
- ' to remove or move the downloaded file
672
- SHELL("move sprite.png images\sprite.png") ' move to subfolder
673
- ' or
674
- FileDelete "sprite.png" ' just delete it
675
- ```
676
-
677
- ### Sprite Sheets (LOADSHEET)
678
- ```basic
679
- DIM sprites$
680
- LOADSHEET sprites$, spriteW, spriteH, "sheet.png"
681
-
682
- ' Access sprites by 1-based index
683
- MOVESHAPE sprites$(index$), x#, y#
684
- DRAWSHAPE sprites$(index$)
685
- ```
686
- - Sprites indexed left-to-right, top-to-bottom starting at 1
687
- - All sprites must be same size (spriteW x spriteH)
688
-
689
- ### Mouse Input (Graphics Mode Only)
690
- | Function | Description |
691
- |----------|-------------|
692
- | `MOUSEX` / `MOUSEY` | Cursor position |
693
- | `MOUSEB` | Button state (bitmask, use `AND`) |
694
-
695
- Button constants: `MOUSE_LEFT#`=1, `MOUSE_RIGHT#`=2, `MOUSE_MIDDLE#`=4
696
-
697
- ### Color Palette (0-15)
698
- | 0 Black | 4 Red | 8 Dark Gray | 12 Light Red |
699
- |---------|-------|-------------|--------------|
700
- | 1 Blue | 5 Magenta | 9 Light Blue | 13 Light Magenta |
701
- | 2 Green | 6 Brown | 10 Light Green | 14 Yellow |
702
- | 3 Cyan | 7 Light Gray | 11 Light Cyan | 15 White |
703
-
704
- ### Performance Tips
705
- 1. Use `SCREENLOCK ON/OFF` for all animation
706
- 2. `LINE...BF` is faster than `CLS` for clearing
707
- 3. Store `RGB()` values in constants
708
- 4. REMOVESHAPE unused shapes
709
- 5. SLEEP 16 for ~60 FPS
710
- 6. Do math/logic outside SCREENLOCK block
711
-
712
- ---
713
-
714
- ## Built-in Constants
715
-
716
- ### Arrow Keys
717
- | | | |
718
- |---|---|---|
719
- | `KEY_UP#` | `KEY_DOWN#` | `KEY_LEFT#` |
720
- | `KEY_RIGHT#` | | |
721
-
722
- ### Special Keys
723
- | | | |
724
- |---|---|---|
725
- | `KEY_ESC#` | `KEY_TAB#` | `KEY_BACKSPACE#` |
726
- | `KEY_ENTER#` | `KEY_SPACE#` | `KEY_INSERT#` |
727
- | `KEY_DELETE#` | `KEY_HOME#` | `KEY_END#` |
728
- | `KEY_PGUP#` | `KEY_PGDN#` | |
729
-
730
- ### Modifier Keys
731
- | | | |
732
- |---|---|---|
733
- | `KEY_LSHIFT#` | `KEY_RSHIFT#` | `KEY_LCTRL#` |
734
- | `KEY_RCTRL#` | `KEY_LALT#` | `KEY_RALT#` |
735
- | `KEY_LWIN#` | `KEY_RWIN#` | |
736
-
737
- ### Function Keys
738
- | | | |
739
- |---|---|---|
740
- | `KEY_F1#` | `KEY_F2#` | `KEY_F3#` |
741
- | `KEY_F4#` | `KEY_F5#` | `KEY_F6#` |
742
- | `KEY_F7#` | `KEY_F8#` | `KEY_F9#` |
743
- | `KEY_F10#` | `KEY_F11#` | `KEY_F12#` |
744
-
745
- ### Numpad Keys
746
- | | | |
747
- |---|---|---|
748
- | `KEY_NUMPAD0#` | `KEY_NUMPAD1#` | `KEY_NUMPAD2#` |
749
- | `KEY_NUMPAD3#` | `KEY_NUMPAD4#` | `KEY_NUMPAD5#` |
750
- | `KEY_NUMPAD6#` | `KEY_NUMPAD7#` | `KEY_NUMPAD8#` |
751
- | `KEY_NUMPAD9#` | | |
752
-
753
- ### Punctuation Keys
754
- | | | |
755
- |---|---|---|
756
- | `KEY_COMMA#` | `KEY_DOT#` | `KEY_MINUS#` |
757
- | `KEY_EQUALS#` | `KEY_SLASH#` | `KEY_BACKSLASH#` |
758
- | `KEY_SEP#` | `KEY_GRAVE#` | `KEY_LBRACKET#` |
759
- | `KEY_RBRACKET#` | | |
760
-
761
- ### Alphabet Keys
762
- | | | |
763
- |---|---|---|
764
- | `KEY_A#` | `KEY_B#` | `KEY_C#` |
765
- | `KEY_D#` | `KEY_E#` | `KEY_F#` |
766
- | `KEY_G#` | `KEY_H#` | `KEY_I#` |
767
- | `KEY_J#` | `KEY_K#` | `KEY_L#` |
768
- | `KEY_M#` | `KEY_N#` | `KEY_O#` |
769
- | `KEY_P#` | `KEY_Q#` | `KEY_R#` |
770
- | `KEY_S#` | `KEY_T#` | `KEY_U#` |
771
- | `KEY_V#` | `KEY_W#` | `KEY_X#` |
772
- | `KEY_Y#` | `KEY_Z#` | |
773
-
774
- ### Number Keys
775
- | | | |
776
- |---|---|---|
777
- | `KEY_0#` | `KEY_1#` | `KEY_2#` |
778
- | `KEY_3#` | `KEY_4#` | `KEY_5#` |
779
- | `KEY_6#` | `KEY_7#` | `KEY_8#` |
780
- | `KEY_9#` | | |
781
-
782
- ### Mouse
783
- ```basic
784
- MOUSE_LEFT# = 1, MOUSE_RIGHT# = 2, MOUSE_MIDDLE# = 4
785
- ```
786
-
787
- ### Logical
788
- `TRUE` = 1, `FALSE` = 0
789
-
790
- ---
791
-
792
- ## TIME & TICKS
793
-
794
- ### TIME(format$)
795
- Returns current date/time as a formatted string. Uses .NET DateTime format strings.
796
- ```vb
797
- PRINT TIME() ' Default: "16:30:45"
798
- PRINT TIME("HH:mm:ss") ' "16:30:45"
799
- PRINT TIME("dd.MM.yyyy") ' "09.01.2026"
800
- PRINT TIME("yyyy-MM-dd") ' "2026-01-09"
801
- PRINT TIME("dddd") ' "Friday"
802
- PRINT TIME("MMMM") ' "January"
803
- PRINT TIME("dd MMMM yyyy") ' "09 January 2026"
804
- PRINT TIME("HH:mm") ' "16:30"
805
- ```
806
-
807
- **Common format codes:**
808
- | Code | Description | Example |
809
- |------|-------------|--------|
810
- | HH | Hour (00-23) | 16 |
811
- | mm | Minutes | 30 |
812
- | ss | Seconds | 45 |
813
- | dd | Day | 09 |
814
- | MM | Month (number) | 01 |
815
- | MMM | Month (short) | Jan |
816
- | MMMM | Month (full) | January |
817
- | yy | Year (2 digits) | 26 |
818
- | yyyy | Year (4 digits) | 2026 |
819
- | ddd | Weekday (short) | Fri |
820
- | dddd | Weekday (full) | Friday |
821
-
822
-
823
- ### TICKS
824
- Returns milliseconds elapsed since program started. Useful for timing, animations, and game loops.
825
- ```vb
826
- LET start$ = TICKS
827
-
828
- ' Do some work
829
- FOR i$ = 1 TO 10000
830
- LET x$ = x$ + 1
831
- NEXT
832
-
833
- LET elapsed$ = TICKS - start$
834
- PRINT "Time taken: "; elapsed$; " ms"
835
- ```
836
- ## Source Control
837
-
838
- ### INCLUDE
839
- ```basic
840
- INCLUDE "other_file.bas" ' Insert source at this point
841
- INCLUDE "MathLib.bb" ' Include compiled library
842
- ```
843
-
844
- ### Libraries (.bb files)
845
- ```basic
846
- ' MathLib.bas - can ONLY contain DEF FN functions
847
- DEF FN add$(x$, y$)
848
- RETURN x$ + y$
849
- END DEF
850
- ```
851
- Compile: `bazzbasic.exe -lib MathLib.bas` ? `MathLib.bb`
852
-
853
- ```basic
854
- INCLUDE "MathLib.bb"
855
- PRINT FN MATHLIB_add$(5, 3) ' Auto-prefix: FILENAME_ + functionName
856
- ```
857
- - Libraries can only contain `DEF FN` functions
858
- - Library functions can access main program constants (`#`)
859
- - Version-locked: .bb may not work across BazzBasic versions
860
-
861
- ---
862
-
863
- ## Common Patterns
864
-
865
- ### Game Loop
866
- ```basic
867
- SCREEN 12
868
- LET running$ = TRUE
869
-
870
- WHILE running$
871
- LET key$ = INKEY
872
- IF key$ = KEY_ESC# THEN running$ = FALSE
873
-
874
- ' Math and logic here
875
-
876
- SCREENLOCK ON
877
- LINE (0,0)-(640,480), 0, BF ' Fast clear
878
- ' Draw game state
879
- SCREENLOCK OFF
880
- SLEEP 16
881
- WEND
882
- END
883
- ```
884
-
885
- ### HTTP + Data
886
- ```basic
887
- DIM response$
888
- LET response$ = HTTPGET("https://api.example.com/scores")
889
- PRINT response$
890
-
891
- DIM payload$
892
- LET payload$ = HTTPPOST("https://api.example.com/submit", "{""score"":9999}")
893
- PRINT payload$
894
- ```
895
-
896
- ### Save/Load with Arrays
897
- ```basic
898
- DIM save$
899
- save$("level") = 3
900
- save$("hp") = 80
901
- FileWrite "save.txt", save$
902
-
903
- DIM load$
904
- LET load$ = FileRead("save.txt")
905
- PRINT load$("level") ' Output: 3
906
- ```
907
-
908
- ## Arrays and JSON
909
-
910
- BazzBasic arrays map naturally to JSON. Nested JSON objects and arrays become multi-dimensional keys using comma-separated indices.
911
-
912
- ### ASJSON
913
- Converts a BazzBasic array to a JSON string:
914
- ```vb
915
- DIM player$
916
- player$("name") = "Alice"
917
- player$("score") = 9999
918
- player$("address,city") = "New York"
919
- player$("skills,0") = "JavaScript"
920
- player$("skills,1") = "Python"
921
-
922
- LET json$ = ASJSON(player$)
923
- PRINT json$
924
- ' Output: {"name":"Alice","score":9999,"address":{"city":"New York"},"skills":["JavaScript","Python"]}
925
- ```
926
-
927
- ### ASARRAY
928
- Converts a JSON string into a BazzBasic array. Returns number of elements loaded:
929
- ```vb
930
- DIM data$
931
- LET count$ = ASARRAY(data$, "{""name"":""Bob"",""score"":42}")
932
-
933
- PRINT data$("name") ' Output: Bob
934
- PRINT data$("score") ' Output: 42
935
- PRINT count$ ' Output: 2
936
- ```
937
-
938
- Nested JSON becomes comma-separated keys:
939
- ```vb
940
- DIM data$
941
- LET json$ = "{""player"":{""name"":""Alice"",""hp"":100},""skills"":[""fire"",""ice""]}"
942
- ASARRAY data$, json$
943
-
944
- PRINT data$("player,name") ' Output: Alice
945
- PRINT data$("player,hp") ' Output: 100
946
- PRINT data$("skills,0") ' Output: fire
947
- PRINT data$("skills,1") ' Output: ice
948
- ```
949
-
950
- ### LOADJSON
951
- Loads a JSON file directly into an array:
952
- ```vb
953
- DIM scores$
954
- LOADJSON scores$, "highscores.json"
955
-
956
- PRINT scores$("first,name") ' Output: depends on file contents
957
- ```
958
-
959
- ### SAVEJSON
960
- Saves an array as a formatted JSON file:
961
- ```vb
962
- DIM save$
963
- save$("level") = 3
964
- save$("hp") = 80
965
- save$("position,x") = 100
966
- save$("position,y") = 200
967
-
968
- SAVEJSON save$, "savegame.json"
969
- ```
970
-
971
- The resulting `savegame.json`:
972
- ```json
973
- {
974
- "level": 3,
975
- "hp": 80,
976
- "position": {
977
- "x": 100,
978
- "y": 200
979
- }
980
- }
981
- ```
982
-
983
- ### Practical example: HTTP API + JSON
984
- ```vb
985
- DIM response$
986
- LET raw$ = HTTPGET("https://api.example.com/user/1")
987
- ASARRAY response$, raw$
988
-
989
- PRINT "Name: "; response$("name")
990
- PRINT "Email: "; response$("email")
991
- ```
992
-
993
- ### Sound with Graphics
994
- ```basic
995
- SCREEN 12
996
- LET bgm$ = LOADSOUND("music.wav")
997
- LET sfx$ = LOADSOUND("jump.wav")
998
- SOUNDREPEAT(bgm$)
999
-
1000
- WHILE INKEY <> KEY_ESC#
1001
- IF INKEY = KEY_SPACE# THEN SOUNDONCE(sfx$)
1002
- SLEEP 16
1003
- WEND
1004
- SOUNDSTOPALL
1005
- END
1006
- ```
1007
-
1008
- ---
1009
-
1010
- ## Code Style Conventions
1011
-
1012
- **Variables** - `camelCase$`
1013
- ```basic
1014
- LET playerName$ = "Hero"
1015
- LET score$ = 0
1016
- ```
1017
-
1018
- **Constants** - `UPPER_SNAKE_CASE#`
1019
- ```basic
1020
- LET MAX_HEALTH# = 100
1021
- LET SCREEN_W# = 640
1022
- ```
1023
-
1024
- **Arrays** - `camelCase$` (like variables, declared with DIM)
1025
- ```basic
1026
- DIM scores$
1027
- DIM playerData$
1028
- ```
1029
-
1030
- **User-defined functions** - `PascalCase$`
1031
- ```basic
1032
- DEF FN CalculateDamage$(attack$, defence$)
1033
- DEF FN IsColliding$(x$, y$)
1034
- ```
1035
-
1036
- **Labels** - descriptive names, `[sub:]` prefix recommended for subroutines
1037
- ```basic
1038
- [sub:DrawPlayer] ' Subroutine
1039
- [gameLoop] ' Jump target
1040
- ```
1041
-
1042
- ## Program Structure
1043
-
1044
- 1. Constants first
1045
- 2. User-defined functions
1046
- 3. Init
1047
- 4. Main program loop
1048
- 5. Subroutines (labels) last
1049
-
1050
- ```basic
1051
- ' -- 1. CONSTANTS ----------------------------
1052
- ' or INCLUDE as "constants.bas" etc
1053
-
1054
- LET SCREEN_W# = 640
1055
- LET SCREEN_H# = 480
1056
- LET MAX_SPEED# = 5
1057
-
1058
- ' -- 2. FUNCTIONS ----------------------------
1059
- ' or INCLUDE as "functions.bas" etc
1060
- DEF FN Clamp$(val$, lo$, hi$)
1061
- IF val$ < lo$ THEN RETURN lo$
1062
- IF val$ > hi$ THEN RETURN hi$
1063
- RETURN val$
1064
- END DEF
1065
-
1066
- ' -- 3. INIT ---------------------------------
1067
- ' or INCLUDE as "inits.bas" etc
1068
- ' IMPORTANT:
1069
- ' Avoid declaring variables outside INIT if speed matters.
1070
- ' If done inside [main] or [subs], Bazzbasic has to check the existence of the variable in each call.
1071
-
1072
- [inits]
1073
- SCREEN 0, SCREEN_W#, SCREEN_H#, "My Game"
1074
- LET x$ = 100
1075
- LET y$ = 100
1076
- LET running$ = TRUE
1077
-
1078
- ' -- 4. MAIN LOOP ----------------------------
1079
- [main]
1080
- WHILE running$
1081
- IF INKEY = KEY_ESC# THEN running$ = FALSE
1082
- GOSUB [sub:update]
1083
- GOSUB [sub:draw]
1084
- SLEEP 16
1085
- WEND
1086
- END
1087
-
1088
- ' -- 5. SUBROUTINES --------------------------
1089
- ' or INCLUDE as "subs.bas" etc
1090
-
1091
- [sub:update]
1092
- IF KEYDOWN(KEY_LEFT#) THEN x$ = x$ - MAX_SPEED#
1093
- IF KEYDOWN(KEY_RIGHT#) THEN x$ = x$ + MAX_SPEED#
1094
- RETURN
1095
-
1096
- [sub:draw]
1097
- SCREENLOCK ON
1098
- LINE (0,0)-(SCREEN_W#, SCREEN_H#), 0, BF
1099
- CIRCLE (x$, y$), 10, RGB(0,255,0), 1
1100
- SCREENLOCK OFF
1101
- RETURN
1102
- ```
1103
-
1104
- ### Images and sounds
1105
- When ever there is no reason to change the ID reference of image/sound, we should use constants.
1106
-
1107
- ```basic
1108
- LET MY_IMAGE# = LOADIMAGE("temp.bmp") ' Correct
1109
- LET myImage$# = LOADIMAGE("temp.bmp") ' Wrong
1110
- ```
1111
-
1112
- To change image of enemy during the game
1113
- ```basic
1114
- LET ENEMY1# = LOADIMAGE("enemy1.bmp")
1115
- LET ENEMY2# = LOADIMAGE("enemy2.bmp")
1116
-
1117
- ' begin of game
1118
- LET curEnemy$ = ENEMY1#
1119
-
1120
- ' after Enemy1 has died
1121
- curEnemy$ = ENEMY2#
1122
- ```
1123
-
1124
- ### Collect
1125
- Collect data of same types, characters etc. inside of array if amount of variables starts to pile up.
1126
-
1127
- ```basic
1128
- ' while this breaks the idea of making images constants, it helps by packing all images to same array
1129
- ' notice also how I intent even DIM
1130
- DIM enemyImages$
1131
- enemyImages$("enemy1") = LOADIMAGE("enemy1.bmp")
1132
- enemyImages$("enemy2") = LOADIMAGE("enemy2.bmp")
1133
-
1134
- DIM levelSprites$
1135
- levelSprites$("floor") = LOADIMAGE("floor.png")
1136
- levelSprites$("table") = LOADIMAGE("table.png")
1137
- ```