EkBass commited on
Commit
0b51466
·
verified ·
1 Parent(s): 954b913

Delete BazzBasic-AI-guide.txt

Browse files
Files changed (1) hide show
  1. BazzBasic-AI-guide.txt +0 -739
BazzBasic-AI-guide.txt DELETED
@@ -1,739 +0,0 @@
1
- ---
2
- name: bazzbasic
3
- description: "BazzBasic BASIC interpreter language reference. Use when writing, debugging, or explaining BazzBasic code (.bas files). Triggers on: BazzBasic syntax, $ and # variable suffixes, SDL2 graphics in BASIC, SCREEN/DRAWSHAPE/LOADIMAGE commands, DEF FN functions, BazzBasic file I/O, sound commands, or any question about BazzBasic features. Always use this skill before writing any BazzBasic code."
4
- ---
5
-
6
- # BazzBasic Language Reference
7
- **Version:** 1.3b | **Author:** Kristian Virtanen (EkBass) | **Platform:** Windows x64
8
- **Homepage:** https://ekbass.github.io/BazzBasic/
9
- **GitHub:** https://github.com/EkBass/BazzBasic
10
- **Manual:** https://ekbass.github.io/BazzBasic/manual/#/
11
- **Examples:** https://github.com/EkBass/BazzBasic/tree/main/Examples
12
- **Rosetta Code:** https://rosettacode.org/wiki/Category:BazzBasic
13
- **Rosetta Code solutions:** https://github.com/EkBass/BazzBasic/tree/main/Examples/rosetta-code
14
- **BazzBasic-AI-Guide:** https://huggingface.co/datasets/EkBass/BazzBasic_AI_Guide
15
- **BazzBasic Beginner's Guide:** https://github.com/EkBass/BazzBasic-Beginners-Guide/releases
16
- **Communities:** https://ekbass.github.io/BazzBasic/communities.html
17
-
18
- ---
19
-
20
- ## ⚠️ Critical Rules — Read First
21
-
22
- | Rule | Detail |
23
- |------|--------|
24
- | Variables end with `$` | `name$`, `score$`, `x$` |
25
- | Constants end with `#` | `MAX#`, `PI#`, `TITLE#` |
26
- | Arrays declared with `DIM`, end with `$` | `DIM items$` |
27
- | First use of variable requires `LET` | `LET x$ = 0` — after that `x$ = x$ + 1` |
28
- | FOR and INPUT auto-declare, no LET needed | `FOR i$ = 1 TO 10` |
29
- | Functions defined **before** they are called | Put at top or INCLUDE |
30
- | Function name ends with `$`, called with `FN` | `FN MyFunc$(a$, b$)` |
31
- | Function return value **must** be used | `PRINT FN f$()` or `LET v$ = FN f$()` |
32
- | Arrays **cannot** be passed to functions directly | Pass individual elements, or serialize to JSON string — see *Passing Arrays to Functions* section |
33
- | Case-insensitive | `PRINT`, `print`, `Print` all work |
34
- | `+` operator does both add and concatenate | `"Hi" + " " + name$` |
35
- | Division always returns float | `10 / 3` → `3.333...` |
36
- | Division by zero returns `0` (no error) | Guard with `IF b$ = 0 THEN ...` if needed |
37
- | No integer-division operator | Use `INT(a / b)`, `FLOOR(a / b)`, or `CINT(a / b)` |
38
- | Errors halt the program | No `TRY`/`CATCH` or `ON ERROR` — line-numbered message printed |
39
-
40
- ---
41
-
42
- ## 🚫 Common AI Mistakes (avoid these)
43
-
44
- These are the patterns LLMs most often produce when extrapolating from QBASIC, FreeBASIC, or VB. None of them are valid BazzBasic.
45
-
46
- ```basic
47
- ' ❌ WRONG ' ✓ CORRECT
48
- LET x = 5 LET x$ = 5
49
- LET MAX = 10 LET MAX# = 10
50
- score$ = 0 ' first use LET score$ = 0
51
- DEF FN doStuff(a, b) DEF FN DoStuff$(a$, b$)
52
- FN MyFunc$(5) ' return ignored LET v$ = FN MyFunc$(5)
53
- LET handle$ = LOADIMAGE("x.png") LET HANDLE# = LOADIMAGE("x.png")
54
- CLS ' in graphics mode LINE (0,0)-(W,H), 0, BF
55
- PRINT "x:", x$ ' in graphics mode DRAWSTRING "x:" + STR(x$), 10, 10, RGB(255,255,255)
56
- ```
57
-
58
- **Two more traps that don't fit the table:**
59
-
60
- - Inside `DEF FN` you can read `#` constants from the outer scope but **not** `$` variables. Pass them as parameters.
61
- - Arrays cannot be passed to a function. Serialize with `ASJSON()` and rebuild inside the function with `ASARRAY()` — see *Passing Arrays to Functions* below.
62
-
63
- ---
64
-
65
- ## Minimal Valid Program
66
-
67
- ```basic
68
- [inits]
69
- LET name$ = "World"
70
-
71
- [main]
72
- PRINT "Hello, " + name$
73
- END
74
- ```
75
-
76
- Programs flow top to bottom. `[inits]` and `[main]` are organizational labels, not required syntax — the file would also run without them. `END` halts execution; place it at the end of the main flow so control doesn't fall through into subroutines or function definitions.
77
-
78
- ---
79
-
80
- ## When Generating BazzBasic Code
81
-
82
- - Always declare variables with `LET` on first use; subsequent uses don't need it
83
- - Always include the `$` (variable) or `#` (constant) suffix
84
- - Place `DEF FN` definitions at the top of the file or via `INCLUDE`
85
- - Store every `LOADIMAGE` / `LOADSOUND` / `LOADSHAPE` handle as a `#` constant
86
- - In graphics mode, prefer `LINE (0,0)-(W,H), 0, BF` over `CLS`, and `DRAWSTRING` over `PRINT`
87
- - Wrap each frame's drawing in `SCREENLOCK ON` / `SCREENLOCK OFF`
88
- - Always use a function's return value — assign with `LET` or consume with `PRINT`
89
- - If a feature isn't documented in this guide, say so rather than inventing one — BazzBasic does not silently inherit QBASIC, FreeBASIC, or VB conventions
90
-
91
- ---
92
-
93
-
94
- ## ABOUT
95
- 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.
96
-
97
- ### What people have built with it
98
-
99
- BazzBasic is built for beginners, but it isn't a toy. It can carry real 2D games and even 2.5D graphics work.
100
-
101
- - https://github.com/EkBass/BazzBasic/blob/main/Examples/Voxel_terrain.bas
102
- A Comanche-style heightmap renderer with procedurally generated terrain. Eight smoothing passes turn random noise into hills, valleys, water, beaches, forests, rock, and snow — each assigned a color by height band.
103
- - https://github.com/EkBass/BazzBasic/blob/main/Examples/Raycaster_3d_optimized.bas
104
- A smooth first-person raycaster engine written entirely in BazzBasic. Minimap, depth shading and real-time movement — proves that BazzBasic can handle serious graphics work.
105
- - https://ek-bass.itch.io/rgb-vision
106
- A speedrun platformer made for Jam for All BASIC Dialects #7 (itch.io).
107
- - https://github.com/EkBass/BazzBasic/blob/main/Examples/2D_maze_FOV_demo_console.bas
108
- 2D maze with FOV in your console
109
- - https://ek-bass.itch.io/eliza-bazzbasic-edition
110
- A classic DOCTOR script from the 60s which made people honestly believe they are talking to a real human. Grandmother of modern AIs.
111
- - https://github.com/EkBass/BazzBasic/blob/main/Examples/rosetta-code/BrainFuck.bas
112
- BrainFuck interpreter. One of the first programs ever done with BazzBasic.
113
- - https://github.com/EkBass/BazzBasic/blob/main/Examples/Anthropic_Claude_API_call.bas & https://github.com/EkBass/BazzBasic/blob/main/Examples/OpenAI_request.bas
114
- Anthropic Claude API and OpenAI ChatGPT API available, both with less than 50 lines of code.
115
-
116
- **Note:** All ".../Examples/..." codes are delivered with BazzBasic and they are found on the BazzBasic executable folder.
117
-
118
- ### STORY
119
- Although over the years, as my own skills have grown, I have moved on to more versatile and modern languages, BASIC has always been something that has been fun to try out many different things with. Sometimes it's great to just make a simple adventure game again, a lottery machine, a quiz, or even just those balls bouncing on the screen. BazzBasic was created with this in mind. I wanted to create a language that makes it easy for you to give free rein to your curiosity and program something. And when you finish your first little game, you may crave something bigger and better. Maybe one day you will move on to another programming language, but then BazzBasic will have succeeded in doing what it was intended for.
120
- To arouse your curiosity.
121
- *EkBass*, author of BazzBasic
122
-
123
- ---
124
-
125
- ## Case sensitivity
126
- BazzBasic is not case-sensitive except with string contents.
127
- ```basic
128
- let blaa$ = "Hello"
129
- PriNT BLAA$ ' output: Hello
130
- ```
131
-
132
- ---
133
-
134
- ## Variables & Constants
135
- Variables and constants must be declared with LET before they can be used.
136
- BazzBasic variables are not typed, but work the same way as in JavaScript, for example.
137
- A variable needs the suffix $ which often is linked as STRING in traditional basic variants.
138
-
139
- Variables require suffix "$". Constants require suffix "#".
140
- The suffix does not define data type — only "#" indicates immutability.
141
- ```basic
142
- LET a$ ' Declare variable without value
143
- LET b$, c$, d$ ' Multiple variable declarations without value
144
- LET e$, f$ = "foo", g$ = 10 ' Multiple variable declaration, e$ stays empty, f$ and g$ gets values
145
-
146
- LET name$ = "Alice" ' String variable
147
- LET score$ = 0 ' Numeric variable
148
-
149
- LET PI# = 3.14159 ' Constant (immutable)
150
- LET TITLE# = "My Game" ' String constant
151
- ```
152
-
153
- **Compound assignment operators** (variables only — **not** allowed with `#` constants):
154
-
155
- ```basic
156
- LET x$ = 1
157
- x$ += 5 ' add
158
- x$ -= 3 ' subtract
159
- x$ *= 2 ' multiply
160
- x$ /= 4 ' divide
161
- LET s$ = "Hello"
162
- s$ += " World" ' string concatenation
163
- ```
164
-
165
- **Scope:** All main-code variables share one scope (IF, FOR, and WHILE do NOT create new scope).
166
- Main-code variables ($) are NOT accessible inside DEF FN
167
- `DEF FN` functions are fully isolated — only global constants (`#`) accessible inside.
168
-
169
- **Comparison:** `"123" = 123` is TRUE (cross-type), but keep types consistent for speed.
170
-
171
- ---
172
-
173
- ### Built-in Constants
174
- - **Boolean:** `TRUE`, `FALSE`
175
- - **Math:** `PI#`, `HPI#` (π/2 = 90°), `QPI#` (π/4 = 45°), `TAU#` (2π = 360°), `EULER#` (e) — `#` suffix required
176
- - **System:** `PRG_ROOT#` (program base directory path)
177
- - **Keyboard:** `KEY_ESC#`, `KEY_ENTER#`, `KEY_SPACE#`, `KEY_UP#`, `KEY_DOWN#`, `KEY_LEFT#`, `KEY_RIGHT#`, `KEY_F1#`…`KEY_F12#`, `KEY_A#`…`KEY_Z#`, `KEY_0#`…`KEY_9#`, `KEY_LSHIFT#`, `KEY_LCTRL#`, etc.
178
-
179
- ---
180
-
181
- ## Arrays
182
- BazzBasic arrays are fully dynamic and support numeric, string, or mixed indexing.
183
-
184
-
185
- ```basic
186
- DIM scores$ ' Declare (required before use)
187
- DIM a$, b$, c$ ' Multiple
188
- scores$(0) = 95 ' Numeric indices are 0-based.
189
- scores$("name") = "Alice" ' String keys are associative and unordered.
190
- matrix$(0, 1) = "A2" ' Multi-dimensional
191
-
192
- DIM sounds$
193
- sounds$("guns", "shotgun_shoot") = "shoot_shotgun.wav"
194
- sounds$("guns", "shotgun_reload") = "reload_shotgun.wav"
195
- sounds$("guns", "ak47_shoot") = "shoot_ak47.wav"
196
- sounds$("guns", "ak47_reload") = "reload_ak47.wav"
197
- sounds$("food", "ham_eat") = "eat_ham.wav"
198
-
199
- PRINT LEN(sounds$()) ' 5 as full size of arrayas there is total of 5 values in array
200
- PRINT ROWCOUNT(sounds$()) ' 2, "guns" & "food"
201
- ```
202
-
203
- | Function/Command | Description |
204
- |-----------------|-------------|
205
- | `LEN(arr$())` | Total element count (LEN counts all nested elements across all dimensions) |
206
- | `ROWCOUNT(arr$())` | Count of first-dimension rows — use this for FOR loops over multi-dim arrays |
207
- | `HASKEY(arr$(key))` | 1 if exists, 0 if not |
208
- | `DELKEY arr$(key)` | Remove one element |
209
- | `DELARRAY arr$` | Remove entire array (can re-DIM after) |
210
- | `JOIN dest$, src1$, src2$` | Merge two arrays; `src2$` keys overwrite `src1$`. Use empty `src1$` as `COPYARRAY`. |
211
-
212
- **JOIN** Matching keys from src2$ overwrite src1$ at the same level
213
-
214
- **Always check with `HASKEY` before reading uninitialized elements.**
215
-
216
- ---
217
-
218
- ## Control Flow
219
-
220
- ```basic
221
- ' Block IF
222
- IF score$ >= 90 THEN
223
- PRINT "A"
224
- ELSEIF score$ >= 80 THEN
225
- PRINT "B"
226
- ELSE
227
- PRINT "F"
228
- END IF ' ENDIF also works
229
-
230
- ' One-line IF
231
- IF lives$ = 0 THEN GOTO [game_over]
232
- IF key$ = KEY_ESC# THEN GOTO [menu] ELSE GOTO [play]
233
-
234
- ' FOR (auto-declares variable)
235
- FOR i$ = 1 TO 10 STEP 2 : PRINT i$ : NEXT
236
- FOR i$ = 10 TO 1 STEP -1 : PRINT i$ : NEXT
237
-
238
- ' WHILE
239
- WHILE x$ < 100
240
- x$ = x$ * 2
241
- WEND
242
-
243
- ' Labels, GOTO, GOSUB
244
- [start]
245
- GOSUB [sub:init]
246
- ' Avoid jumping into the middle of logical blocks (e.g. inside IF/WHILE)
247
- GOTO [start]
248
-
249
- [sub:init]
250
- LET x$ = 0
251
- RETURN
252
-
253
- ' Dynamic jump (variable must contain "[label]" with brackets)
254
- LET target$ = "[menu]"
255
- GOTO target$
256
-
257
- ' Other
258
- SLEEP 2000 ' Pause for milliseconds
259
- END ' Terminate program
260
- ```
261
- **Note:** Labels are case-insensitive and may contain almost any printable character.
262
- Whitespace is trimmed. Everything between "[" and "]" is treated as the label name.
263
- ---
264
-
265
- ## I/O
266
-
267
- | Command | Description |
268
- |---------|-------------|
269
- | `PRINT expr; expr` | `;` = no space, `,` = tab |
270
- | `PRINT "text";` | Trailing `;` suppresses newline |
271
- | `INPUT "prompt", var$` | Splits on whitespace/comma |
272
- | `INPUT "prompt", a$, b$` | Multiple values |
273
- | `LINE INPUT "prompt", var$` | Read entire line with spaces |
274
- | `CLS` | Clear screen |
275
- | `LOCATE row, col` | Move cursor (1-based) |
276
- | `CURPOS("row")` / `CURPOS("col")` | Read cursor row or col (1-based, matches LOCATE) |
277
- | `CURPOS()` | Read cursor as `"row,col"` string |
278
- | `COLOR fg, bg` | Text colors (0–15 palette) |
279
- | `SHELL("cmd")` | Run shell command, returns output |
280
- | `SHELL("cmd", ms)` | With timeout in ms (default 5000) |
281
-
282
- ' ; prints without spacing
283
- ' , prints tab separation
284
- ' Both can be mixed in a single PRINT statement
285
- ' INPUT splits on whitespace and comma; quoted strings are treated as single values
286
-
287
- **Escape sequences in strings:** `\"` `\n` `\t` `\\`
288
-
289
- ### Keyboard Input
290
- | Function | Returns | Notes |
291
- |----------|---------|-------|
292
- | `INKEY` | Key value or 0 | Non-blocking |
293
- | `KEYDOWN(key#)` | TRUE/FALSE | Held-key detection; KEYDOWN only works in graphics mode |
294
- | `WAITKEY(key#, ...)` | Key value | Blocks until key pressed; `WAITKEY()` = any key |
295
-
296
- ### Mouse (graphics mode only)
297
- `MOUSEX`, `MOUSEY` — cursor position
298
- `MOUSELEFT`, MOUSERIGHT, MOUSEMIDDLE — return 1 if pressed, 0 otherwise
299
- `MOUSEHIDE` — hide the mouse cursor (graphics screen only)
300
- `MOUSESHOW` — restore the mouse cursor (graphics screen only)
301
-
302
- ### Console Read
303
- Returns numeric or character depending on type parameter
304
- `GETCONSOLE(row, col, type)` — type: 0=char (ASCII), 1=fg color, 2=bg color
305
-
306
- ---
307
-
308
- ## User-Defined Functions
309
-
310
- ```basic
311
- ' Define BEFORE calling. Name must end with $.
312
- DEF FN Clamp$(val$, lo$, hi$)
313
- IF val$ < lo$ THEN RETURN lo$
314
- IF val$ > hi$ THEN RETURN hi$
315
- RETURN val$
316
- END DEF
317
-
318
- PRINT FN Clamp$(5, 1, 10) ' ✓ OK — return value used
319
- LET v$ = FN Clamp$(15, 0, 10) ' ✓ OK
320
- FN Clamp$(5, 1, 10) ' ✗ ERROR — return value unused
321
- ' Return value must be assigned with LET or consumed by PRINT — there is no way to silently discard it.
322
- ```
323
-
324
- - Isolated scope: no access to global variables, only global constants (`#`)
325
- - Parameters passed **by value**
326
- - Labels inside functions are local — GOTO/GOSUB cannot jump outside
327
- - Supports recursion
328
- - Arrays as parameters not allowed. Use ASJSON to make array as JSON-string to pass it.
329
- - Use `INCLUDE` to load functions from separate files if many
330
-
331
- ---
332
-
333
- ## String Functions
334
-
335
- | Function | Description |
336
- |----------|-------------|
337
- | `ASC(s$)` | ASCII code of first char |
338
- | `CHR(n)` | Character from ASCII code |
339
- | `INSTR(s$, search$)` | Position (1-based), 0=not found; case-sensitive by default |
340
- | `INSTR(s$, search$, mode)` | mode: 0=case-insensitive, 1=case-sensitive |
341
- | `INSTR(start, s$, search$)` | Search from position (case-sensitive) |
342
- | `INVERT(s$)` | Reverse string |
343
- | `LCASE(s$)` / `UCASE(s$)` | Lower / upper case |
344
- | `LEFT(s$, n)` / `RIGHT(s$, n)` | First/last n chars |
345
- | `LEN(s$)` | String length |
346
- | `LTRIM(s$)` / `RTRIM(s$)` / `TRIM(s$)` | Strip whitespace |
347
- | `MID(s$, start)` | Substring from start (1-based) |
348
- | `MID(s$, start, len)` | Substring with length |
349
- | `REPEAT(s$, n)` | Repeat string n times |
350
- | `REPLACE(s$, a$, b$)` | Replace a$ with b$ in s$ |
351
- | `SPLIT(arr$, s$, sep$)` | Split into array, returns count. Expects array is declared with DIM |
352
- | `SRAND(n)` | Random alphanumeric string of length n |
353
- | `STR(n)` | Number to string |
354
- | `VAL(s$)` | String to number |
355
- | `SHA256(s$)` | SHA256 hash (64-char hex) |
356
- | `BASE64ENCODE(s$)` / `BASE64DECODE(s$)` | Base64 encode/decode |
357
-
358
- ---
359
-
360
- ## Math Functions
361
-
362
- | Function | Description |
363
- |----------|-------------|
364
- | `ABS(n)` | Absolute value |
365
- | `ATAN(n)` | Arc tangent |
366
- | `ATAN2(n, n2)` | Returns the angle, in radians, between the positive x-axis and a vector to the point with the given (x, y) coordinates in the Cartesian plane |
367
- | `BETWEEN(n, min, max)` | TRUE if min ≤ n ≤ max |
368
- | `INBETWEEN(n, min, max)` | TRUE if min < n < max (strictly between, not equal) |
369
- | `CEIL(n)` / `FLOOR(n)` | Round up / down |
370
- | `CINT(n)` | Round to nearest integer |
371
- | `CLAMP(n, min, max)` | Constrain n to [min, max] |
372
- | `COS(n)` / `SIN(n)` / `TAN(n)` | Trig (radians) |
373
- | `DEG(rad)` / `RAD(deg)` | Radians ↔ degrees |
374
- | `DISTANCE(x1,y1, x2,y2)` | 2D Euclidean distance |
375
- | `DISTANCE(x1,y1,z1, x2,y2,z2)` | 3D Euclidean distance |
376
- | `EXP(n)` | e^n |
377
- | `INT(n)` | Truncate toward zero |
378
- | `LERP(start, end, t)` | Linear interpolation (t: 0.0–1.0) |
379
- | `LOG(n)` | Natural logarithm |
380
- | `MAX(a, b)` / `MIN(a, b)` | Larger / smaller of two |
381
- | `MOD(a, b)` | Remainder |
382
- | `POW(base, exp)` | Power |
383
- | `RND(n)` | Random integer 0 to n-1 if n > 0 |
384
- | `RND(0)` | Float between 0.0 and 1.0 (IE: 0.5841907423666761) |
385
- | `ROUND(n)` | Standard rounding |
386
- | `SGN(n)` | Sign: -1, 0, or 1 |
387
- | `SQR(n)` | Square root |
388
-
389
- **Math constants:** `PI#`, `HPI#` (PI/2), `QPI#` (PI/4), `TAU#` (PI*2), `EULER#`
390
-
391
- ---
392
-
393
- ## Graphics
394
-
395
- ```basic
396
- SCREEN 12 ' 640×480 VGA
397
- SCREEN 0, 800, 600 ' Custom size
398
- SCREEN 0, 1024, 768, "My Game" ' Custom size + title
399
- FULLSCREEN TRUE ' Borderless fullscreen (graphics only)
400
- FULLSCREEN FALSE ' Windowed
401
- ```
402
-
403
- | Mode | Resolution |
404
- |------|-----------|
405
- | 1 | 320×200 |
406
- | 2 | 640×350 |
407
- | 7 | 320×200 |
408
- | 9 | 640×350 |
409
- | 12 | 640×480 ← recommended |
410
- | 13 | 320×200 |
411
-
412
- ### Drawing Primitives
413
- ```basic
414
- PSET (x, y), color ' Pixel
415
- LINE (x1,y1)-(x2,y2), color ' Line
416
- LINE (x1,y1)-(x2,y2), color, B ' Box outline
417
- LINE (x1,y1)-(x2,y2), color, BF ' Box filled (FAST — use instead of CLS)
418
- CIRCLE (cx,cy), radius, color ' Circle outline
419
- CIRCLE (cx,cy), radius, color, 1 ' Circle filled
420
- PAINT (x, y), fillColor, borderColor ' Flood fill
421
- LET c$ = POINT(x, y) ' Read pixel color
422
- LET col$ = RGB(r, g, b) ' Create color (0–255 each)
423
- ```
424
-
425
- **Color palette (COLOR command, 0–15):** 0=Black, 1=Blue, 2=Green, 3=Cyan, 4=Red, 5=Magenta, 6=Brown, 7=Lt Gray, 8=Dk Gray, 9=Lt Blue, 10=Lt Green, 11=Lt Cyan, 12=Lt Red, 13=Lt Magenta, 14=Yellow, 15=White
426
-
427
- ### Screen Control
428
- ```basic
429
- SCREENLOCK ON ' Buffer drawing (start frame)
430
- SCREENLOCK OFF ' Present buffer (end frame)
431
- VSYNC(TRUE) ' Enable VSync (default, ~60 FPS)
432
- VSYNC(FALSE) ' Disable VSync (benchmarking)
433
- CLS ' Clear screen
434
- ' Use CLS only with console, in graphics screen its slow
435
- ' With graphics screen, prefer LINE BF
436
- ```
437
-
438
- ### Shapes & Images
439
- ```basic
440
- ' Create shape
441
- ' LOADSHAPE and LOADIMAGE return a stable integer handle — never reassigned.
442
- ' SDL2 owns the resource; your code only ever holds this one reference. Use constants.
443
- LET RECT# = LOADSHAPE("RECTANGLE", w, h, color) ' or "CIRCLE", "TRIANGLE"
444
- LET IMG_PLAYER# = LOADIMAGE("player.png") ' PNG (alpha) or BMP
445
- LET IMG_REMOTE# = LOADIMAGE("https://example.com/a.png") ' Download + load
446
-
447
- ' Sprite sheet — sprites indexed 1-based
448
- DIM sprites$
449
- LOADSHEET sprites$, 128, 128, "sheet.png" ' tileW, tileH, file
450
- MOVESHAPE sprites$(1), x, y ' sprites$(1) = first sprite
451
-
452
- ' Transform
453
- MOVESHAPE RECT#, x, y ' Position by center point
454
- ROTATESHAPE RECT#, angle ' Degrees (absolute)
455
- SCALESHAPE RECT#, scale ' 1.0 = original size
456
- DRAWSHAPE RECT# ' Render to buffer
457
- SHOWSHAPE RECT# / HIDESHAPE RECT# ' Toggle visibility
458
- REMOVESHAPE RECT# ' Free memory (always clean up)
459
- ```
460
-
461
-
462
- ---
463
-
464
- ### Text Rendering (SDL2_ttf.dll required)
465
- #### DRAWSTRING & LOADFONT
466
-
467
-
468
- ```basic
469
- ' Default font (Arial)
470
- DRAWSTRING "Hello!", 100, 200, RGB(255, 255, 255)
471
-
472
- ' Load alternative font — becomes the new default
473
- LOADFONT "comic.ttf", 24
474
- DRAWSTRING "Hello!", 100, 200, RGB(255, 255, 255)
475
-
476
- ' Reset to Arial
477
- LOADFONT
478
- ```
479
-
480
- `DRAWSTRING x, y` positions the top-left of the text. Requires `SDL2_ttf.dll` in the same directory as the interpreter. Prefer this over PRINT, which makes graphic screen easily blinking.
481
-
482
- ---
483
-
484
- ## Sound
485
-
486
- ```basic
487
- ' LOADSOUND returns a stable integer handle — SDL2 manages the resource.
488
- ' The handle never changes; store it in a constant to protect it from accidental reassignment.
489
- LET SND_JUMP# = LOADSOUND("jump.wav") ' Load (WAV recommended)
490
- SOUNDONCE(SND_JUMP#) ' Play once, non-blocking
491
- SOUNDONCEWAIT(SND_JUMP#) ' Play once, wait for finish
492
- SOUNDREPEAT(SND_JUMP#) ' Loop continuously
493
- SOUNDSTOP(SND_JUMP#) ' Stop specific sound
494
- SOUNDSTOPALL ' Stop all sounds
495
- ```
496
-
497
- Load all sounds at startup. Call `SOUNDSTOPALL` before `END`.
498
-
499
- ---
500
-
501
- ## File I/O
502
-
503
- ```basic
504
- LET data$ = FileRead("file.txt") ' Read as string
505
- DIM cfg$ : LET cfg$ = FileRead("settings.txt") ' Read as key=value array
506
- FILEWRITE "save.txt", data$ ' Create/overwrite
507
- FILEAPPEND "log.txt", entry$ ' Append
508
- LET ok$ = FILEEXISTS("file.txt") ' 1=exists, 0=not
509
- FILEDELETE "temp.dat" ' Delete file
510
- ```
511
-
512
- **key=value parsing:** When `FileRead` assigns to a `DIM`'d array, lines `key=value` become `arr$("key")`. Lines starting with `#` are comments. Perfect for `.env` files.
513
-
514
- ```basic
515
- DIM env$
516
- LET env$ = FileRead(".env")
517
- LET API_KEY# = env$("OPENAI_API_KEY")
518
- ```
519
-
520
- **Paths:** Use `/` or `\\` — never single `\` (it's an escape char). Relative paths are from `PRG_ROOT#`.
521
- **FileWrite with array** saves in key=value format (round-trips with FileRead).
522
-
523
- ---
524
-
525
- ## Network
526
-
527
- ```basic
528
- LET res$ = HTTPGET("https://api.example.com/data")
529
- LET res$ = HTTPPOST("https://api.example.com/submit", "{""key"":""val""}")
530
-
531
- ' With headers (optional last parameter)
532
- DIM headers$
533
- headers$("Authorization") = "Bearer mytoken"
534
- headers$("Content-Type") = "application/json"
535
- LET res$ = HTTPGET("https://api.example.com/data", headers$)
536
- LET res$ = HTTPPOST("https://api.example.com/data", body$, headers$)
537
- ```
538
-
539
- ---
540
-
541
- ## Arrays & JSON
542
-
543
- Nested JSON maps to comma-separated keys: `data$("player,name")`, `data$("skills,0")`
544
-
545
- ```basic
546
- ' Array → JSON string
547
- LET json$ = ASJSON(arr$)
548
-
549
- ' JSON string → array (returns element count)
550
- DIM data$
551
- LET count$ = ASARRAY(data$, json$)
552
-
553
- ' Load/save JSON files
554
- LOADJSON arr$, "file.json"
555
- SAVEJSON arr$, "file.json"
556
- ```
557
-
558
- ---
559
-
560
- ## Fast Trigonometry
561
-
562
- ~20× faster than `SIN(RAD(x))`, 1-degree precision. Uses ~5.6 KB memory.
563
-
564
- ```basic
565
- FastTrig(TRUE) ' Enable lookup tables (must call first)
566
- LET x$ = FastCos(45) ' Degrees, auto-normalized 0–359
567
- LET y$ = FastSin(90)
568
- LET r$ = FastRad(180) ' Deg→rad (no FastTrig needed)
569
- FastTrig(FALSE) ' Free memory
570
- ```
571
-
572
- Use for raycasting, sprite rotation, particle systems, any high-freq trig.
573
-
574
- ---
575
-
576
- ## Command-Line Arguments
577
-
578
- ```basic
579
- ' bazzbasic.exe myprog.bas arg1 arg2
580
- PRINT ARGCOUNT ' number of args (2 in this example)
581
- PRINT ARGS(0) ' first arg → "arg1"
582
- PRINT ARGS(1) ' second arg → "arg2"
583
- ```
584
-
585
- `ARGCOUNT` and `ARGS(n)` are 0-based; ARGS does not include the interpreter or script name.
586
-
587
- ---
588
-
589
- ## Libraries & INCLUDE
590
-
591
- ```basic
592
- INCLUDE "helpers.bas" ' Insert source at this point
593
- INCLUDE "MathLib.bb" ' Load compiled library
594
-
595
- ' Compile library (functions only — no loose code)
596
- ' bazzbasic.exe -lib MathLib.bas → MathLib.bb
597
- ' Function names auto-prefixed: MATHLIB_functionname$
598
- PRINT FN MATHLIB_add$(5, 3)
599
- ```
600
-
601
- Library functions can read main-program constants (`#`). `.bb` files are version-locked.
602
-
603
- ---
604
-
605
- ## Passing Arrays to Functions
606
-
607
- Arrays cannot be passed directly to `DEF FN` functions, but a clean workaround exists using JSON serialization. Convert the array to a JSON string with `ASJSON`, pass the string as a parameter, then deserialize inside the function with `ASARRAY`. This is the accepted pattern in BazzBasic v1.2+.
608
-
609
- ```basic
610
- DEF FN ProcessPlayer$(data$)
611
- DIM arr$
612
- LET count$ = ASARRAY(arr$, data$)
613
- RETURN arr$("name") + " score:" + arr$("score")
614
- END DEF
615
-
616
- [inits]
617
- DIM player$
618
- player$("name") = "Alice"
619
- player$("score") = 9999
620
- player$("address,city") = "New York"
621
-
622
- [main]
623
- LET json$ = ASJSON(player$)
624
- PRINT FN ProcessPlayer$(json$)
625
- END
626
- ```
627
-
628
- **Notes:**
629
- - The function receives a full independent copy — changes inside do not affect the original array
630
- - Nested keys work normally: `arr$("address,city")` etc.
631
- - Overhead is similar to copying an array manually; acceptable for most use cases
632
-
633
- ---
634
-
635
- ## Program Structure
636
-
637
- ```basic
638
- ' ---- 1. FUNCTIONS (or INCLUDE "functions.bas/bb") ----
639
- DEF FN Clamp$(v$, lo$, hi$)
640
- IF v$ < lo$ THEN RETURN lo$
641
- IF v$ > hi$ THEN RETURN hi$
642
- RETURN v$
643
- END DEF
644
-
645
- ' ---- 2. INIT (declare ALL constants & variables here, not inside loops) ----
646
- ' Performance: variables declared outside loops avoid repeated existence checks.
647
- [inits]
648
- LET SCREEN_W# = 640
649
- LET SCREEN_H# = 480
650
- LET MAX_SPEED# = 5
651
-
652
- SCREEN 0, SCREEN_W#, SCREEN_H#, "My Game"
653
-
654
- LET x$ = 320
655
- LET y$ = 240
656
- LET running$ = TRUE
657
-
658
- ' ---- 3. MAIN LOOP ----
659
- [main]
660
- WHILE running$
661
- IF INKEY = KEY_ESC# THEN running$ = FALSE
662
- GOSUB [sub:update]
663
- GOSUB [sub:draw]
664
- SLEEP 16
665
- WEND
666
- SOUNDSTOPALL
667
- END ' place at the end of main program so BazzBasic wont fall to subs in runtime
668
-
669
- ' ---- 4. SUBROUTINES (or INCLUDE "subs.bas") ----
670
- [sub:update]
671
- IF KEYDOWN(KEY_LEFT#) THEN x$ = x$ - MAX_SPEED#
672
- IF KEYDOWN(KEY_RIGHT#) THEN x$ = x$ + MAX_SPEED#
673
- RETURN
674
-
675
- [sub:draw]
676
- SCREENLOCK ON
677
- LINE (0,0)-(SCREEN_W#, SCREEN_H#), 0, BF
678
- CIRCLE (x$, y$), 10, RGB(0, 255, 0), 1
679
- SCREENLOCK OFF
680
- RETURN
681
- ```
682
-
683
- **Key conventions:**
684
- - Variables: `camelCase$` | Constants: `UPPER_SNAKE_CASE#` | Functions: `PascalCase$`
685
- - Labels: `[gameLoop]` for jump targets, `[sub:name]` for subroutines
686
- - Image/sound/shape IDs are stable integer handles — **always** store as constants: `LET MY_IMG# = LOADIMAGE("x.png")` — never use `$` variables for these
687
- - Group many IDs → use arrays: `DIM sprites$` / `sprites$("player") = LOADIMAGE(...)` — but prefer named constants when count is small
688
-
689
- ---
690
-
691
- ## Performance Tips
692
-
693
- - `LINE (0,0)-(W,H), 0, BF` to clear — much faster than `CLS`
694
- - Always wrap draw code in `SCREENLOCK ON` / `SCREENLOCK OFF`
695
- - Store `RGB()` results in constants/variables — don't call RGB in hot loops
696
- - Declare all variables in `[inits]`, not inside loops or subroutines
697
- - Use `FastTrig` for any loop calling trig hundreds of times per frame
698
- - `SLEEP 16` in game loop → ~60 FPS
699
-
700
- ---
701
-
702
- ## Known Limitations
703
-
704
- Things to be aware of — most are deliberate design decisions, a few are practical constraints.
705
-
706
- - **Platform: Windows x64 only.** Linux and macOS are not supported. Browser/Android are not feasible — the runtime depends on SDL2 via P/Invoke.
707
- - **Arrays cannot be passed to functions.** Use the `ASJSON` / `ASARRAY` round-trip pattern. The function receives an independent copy.
708
- - **`DEF FN` scope is fully isolated.** Functions can read global `#` constants but cannot see any `$` variables from the outer scope. Pass values as parameters.
709
- - **No block scope.** `IF`, `FOR`, and `WHILE` do not create new variable scopes — all main-code variables share one namespace.
710
- - **No exception handling.** There is no `TRY`/`CATCH` and no `ON ERROR`. Runtime errors halt the program with a line-numbered message printed to the console.
711
- - **Division by zero returns `0` silently.** No exception, no warning. Guard with `IF b$ = 0 THEN ...` when correctness matters.
712
- - **No integer-division operator.** Use `INT(a / b)`, `FLOOR(a / b)`, or `CINT(a / b)` depending on the rounding behavior you need.
713
- - **Compiled `.bb` libraries are version-locked** to the BazzBasic build that produced them. Recompile after upgrading the interpreter.
714
- - **`DEF FN` definitions must precede their first call** in source order. Forward references don't resolve.
715
-
716
- ---
717
-
718
- ## IDE Features (v1.3)
719
-
720
- ### New File Template
721
- When the IDE opens with no file (or a new file), this template is auto-inserted:
722
- ```basic
723
- ' BazzBasic version {ver_num}
724
- ' https://ekbass.github.io/BazzBasic/
725
- ```
726
-
727
- ### Beginner's Guide
728
- - **IDE:** Menu → **Help** → **Beginner's Guide** — opens `https://github.com/EkBass/BazzBasic-Beginners-Guide/releases` in default browser
729
- - **CLI:** `bazzbasic.exe -guide` or `bazzbasic.exe -help` — prints URL to terminal
730
-
731
- ### Check for Updates
732
- - **IDE:** Menu → **Help** → **Check for updated...** — IDE reports if a newer version is available
733
- - **CLI:** `bazzbasic.exe -checkupdate`
734
-
735
- ### Compile via IDE
736
- - **Menu → Run → Compile as Exe** — compiles open file to standalone `.exe` (auto-saves first)
737
- - **Menu → Run → Compile as Library (.bb)** — compiles open file as reusable `.bb` library (auto-saves first)
738
-
739
- EOF