EkBass commited on
Commit
b94fa1d
·
verified ·
1 Parent(s): 9c5439a

Upload BazzBasic-AI-guide-23032026e.md

Browse files
Files changed (1) hide show
  1. BazzBasic-AI-guide-23032026e.md +1073 -0
BazzBasic-AI-guide-23032026e.md ADDED
@@ -0,0 +1,1073 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.1 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.0 (Released February 22, 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
+ ---
438
+
439
+ ## Network (HTTP)
440
+
441
+ ```basic
442
+ DIM response$
443
+ LET response$ = HTTPGET("https://api.example.com/data")
444
+ PRINT response$
445
+
446
+ DIM result$
447
+ LET result$ = HTTPPOST("https://api.example.com/post", "{""key"":""value""}")
448
+ PRINT result$
449
+ ```
450
+
451
+ - Returns response body as string
452
+ - Supports HTTPS
453
+ - Use `""` inside strings to escape quotes in JSON bodies
454
+ - Timeout handled gracefully - returns error message string on failure
455
+
456
+ ### Encoding & Hashing
457
+
458
+ | Function | Description |
459
+ |----------|-------------|
460
+ | `BASE64ENCODE(s$)` | Encode string to Base64 |
461
+ | `BASE64DECODE(s$)` | Decode Base64 string |
462
+ | `SHA256(s$)` | Returns lowercase hex SHA256 hash |
463
+
464
+ ```vb
465
+ LET encoded$ = BASE64ENCODE("Hello, World!")
466
+ PRINT encoded$ ' SGVsbG8sIFdvcmxkIQ==
467
+
468
+ LET decoded$ = BASE64DECODE(encoded$)
469
+ PRINT decoded$ ' Hello, World!
470
+
471
+ LET hash$ = SHA256("password123")
472
+ PRINT hash$ ' ef92b778... (64-char lowercase hex)
473
+ ```
474
+
475
+ ### JSON
476
+ BazzBasic arrays map naturally to JSON. Nested objects become comma-separated keys.
477
+
478
+ ```vb
479
+ DIM data$
480
+ data$("name") = "Alice"
481
+ data$("score") = 9999
482
+ data$("address,city") = "New York"
483
+ data$("skills,0") = "JavaScript"
484
+ data$("skills,1") = "Python"
485
+
486
+ LET json$ = ASJSON(data$)
487
+ ' Output: {"name":"Alice","score":9999,"address":{"city":"New York"},"skills":["JavaScript","Python"]}
488
+
489
+ DIM back$
490
+ LET count$ = ASARRAY(back$, json$)
491
+ PRINT back$("name") ' Output: Alice
492
+ PRINT back$("address,city") ' Output: New York
493
+ PRINT back$("skills,0") ' Output: JavaScript
494
+
495
+ SAVEJSON data$, "scores.json"
496
+ DIM loaded$
497
+ LOADJSON loaded$, "scores.json"
498
+ ```
499
+
500
+ | Function | Description |
501
+ |----------|-------------|
502
+ | `ASJSON(arr$)` | Convert array to JSON string |
503
+ | `ASARRAY(arr$, json$)` | Fill array from JSON string, returns element count |
504
+ | `LOADJSON arr$, path$` | Load JSON file into array |
505
+ | `SAVEJSON arr$, path$` | Save array as formatted JSON file |
506
+
507
+ **Nested JSON key convention:** `object,key` and `array,index` (0-based)
508
+
509
+ ---
510
+
511
+ ## Sound (SDL2_mixer)
512
+
513
+ ```basic
514
+ DIM bgm$
515
+ LET bgm$ = LOADSOUND("music.wav")
516
+ LET sfx$ = LOADSOUND("jump.wav")
517
+
518
+ SOUNDREPEAT(bgm$) ' Loop continuously
519
+ SOUNDONCE(sfx$) ' Play once
520
+ SOUNDSTOP(bgm$) ' Stop specific sound
521
+ SOUNDSTOPALL ' Stop all sounds
522
+ ```
523
+
524
+ | Command | Description |
525
+ |---------|-------------|
526
+ | `LOADSOUND(path)` | Load sound file, returns ID |
527
+ | `SOUNDONCE(id$)` | Play once |
528
+ | `SOUNDREPEAT(id$)` | Loop continuously |
529
+ | `SOUNDSTOP(id$)` | Stop specific sound |
530
+ | `SOUNDSTOPALL` | Stop all sounds |
531
+
532
+ - Formats: WAV (recommended), MP3, others via SDL2_mixer
533
+ - Thread-safe, multiple simultaneous sounds supported
534
+ - Load sounds once at startup for performance
535
+
536
+ ---
537
+
538
+ ## Graphics (SDL2)
539
+
540
+ ### Screen Setup
541
+ ```basic
542
+ SCREEN 12 ' 640x480 VGA mode
543
+ SCREEN 0, 800, 600, "Title" ' Custom size with title
544
+ ```
545
+ Modes: 0=640x400, 1=320x200, 2=640x350, 7=320x200, 9=640x350, 12=640x480, 13=320x200
546
+
547
+ ### Fullscreen
548
+ ```basic
549
+ FULLSCREEN TRUE ' Borderless fullscreen
550
+ FULLSCREEN FALSE ' Windowed mode
551
+ ```
552
+ Call after `SCREEN`.
553
+
554
+ ### VSync
555
+ ```basic
556
+ VSYNC(TRUE) ' Enable (default) - caps to monitor refresh
557
+ VSYNC(FALSE) ' Disable - unlimited FPS, may tear
558
+ ```
559
+
560
+ ### Double Buffering (Required for Animation)
561
+ ```basic
562
+ SCREENLOCK ON ' Start buffering
563
+ ' ... draw commands ...
564
+ SCREENLOCK OFF ' Display frame
565
+ SLEEP 16 ' ~60 FPS
566
+ ```
567
+ `SCREENLOCK` without argument = `SCREENLOCK ON`. Do math/logic outside SCREENLOCK block.
568
+
569
+ ### Drawing Primitives
570
+ | Command | Description |
571
+ |---------|-------------|
572
+ | `PSET (x, y), color` | Draw pixel |
573
+ | `POINT(x, y)` | Read pixel color (returns RGB integer) |
574
+ | `LINE (x1,y1)-(x2,y2), color` | Draw line |
575
+ | `LINE (x1,y1)-(x2,y2), color, B` | Box outline |
576
+ | `LINE (x1,y1)-(x2,y2), color, BF` | Filled box (faster than CLS) |
577
+ | `CIRCLE (cx, cy), r, color` | Circle outline |
578
+ | `CIRCLE (cx, cy), r, color, 1` | Filled circle |
579
+ | `PAINT (x, y), fill, border` | Flood fill |
580
+ | `RGB(r, g, b)` | Create color (0-255 each) |
581
+
582
+ ### Shape/Sprite System
583
+ ```basic
584
+ DIM sprite$
585
+ sprite$ = LOADSHAPE("RECTANGLE", 50, 50, RGB(255,0,0)) ' Types: RECTANGLE, CIRCLE, TRIANGLE
586
+ sprite$ = LOADIMAGE("player.png") ' PNG (with alpha) or BMP
587
+
588
+ MOVESHAPE sprite$, x, y ' Position (top-left point)
589
+ ROTATESHAPE sprite$, angle ' Absolute degrees
590
+ SCALESHAPE sprite$, 1.5 ' 1.0 = original
591
+ SHOWSHAPE sprite$
592
+ HIDESHAPE sprite$
593
+ DRAWSHAPE sprite$ ' Render
594
+ REMOVESHAPE sprite$ ' Free memory
595
+ ```
596
+ - PNG recommended (full alpha transparency 0-255), BMP for legacy
597
+ - Images positioned by their **top-left point**
598
+ - Rotation is absolute, not cumulative
599
+ - Always REMOVESHAPE when done to free memory
600
+
601
+ ### LOADIMAGE with url
602
+ ```vb
603
+ LET sprite$ = LOADIMAGE("https://example.com/sprite.png")
604
+ ' ? downloads "sprite.png" to root of your program
605
+ ' ? sprite$ works just as with normal file load
606
+
607
+ ' to remove or move the downloaded file
608
+ SHELL("move sprite.png images\sprite.png") ' move to subfolder
609
+ ' or
610
+ FileDelete "sprite.png" ' just delete it
611
+ ```
612
+
613
+ ### Sprite Sheets (LOADSHEET)
614
+ ```basic
615
+ DIM sprites$
616
+ LOADSHEET sprites$, spriteW, spriteH, "sheet.png"
617
+
618
+ ' Access sprites by 1-based index
619
+ MOVESHAPE sprites$(index$), x#, y#
620
+ DRAWSHAPE sprites$(index$)
621
+ ```
622
+ - Sprites indexed left-to-right, top-to-bottom starting at 1
623
+ - All sprites must be same size (spriteW x spriteH)
624
+
625
+ ### Mouse Input (Graphics Mode Only)
626
+ | Function | Description |
627
+ |----------|-------------|
628
+ | `MOUSEX` / `MOUSEY` | Cursor position |
629
+ | `MOUSEB` | Button state (bitmask, use `AND`) |
630
+
631
+ Button constants: `MOUSE_LEFT#`=1, `MOUSE_RIGHT#`=2, `MOUSE_MIDDLE#`=4
632
+
633
+ ### Color Palette (0-15)
634
+ | 0 Black | 4 Red | 8 Dark Gray | 12 Light Red |
635
+ |---------|-------|-------------|--------------|
636
+ | 1 Blue | 5 Magenta | 9 Light Blue | 13 Light Magenta |
637
+ | 2 Green | 6 Brown | 10 Light Green | 14 Yellow |
638
+ | 3 Cyan | 7 Light Gray | 11 Light Cyan | 15 White |
639
+
640
+ ### Performance Tips
641
+ 1. Use `SCREENLOCK ON/OFF` for all animation
642
+ 2. `LINE...BF` is faster than `CLS` for clearing
643
+ 3. Store `RGB()` values in constants
644
+ 4. REMOVESHAPE unused shapes
645
+ 5. SLEEP 16 for ~60 FPS
646
+ 6. Do math/logic outside SCREENLOCK block
647
+
648
+ ---
649
+
650
+ ## Built-in Constants
651
+
652
+ ### Arrow Keys
653
+ | | | |
654
+ |---|---|---|
655
+ | `KEY_UP#` | `KEY_DOWN#` | `KEY_LEFT#` |
656
+ | `KEY_RIGHT#` | | |
657
+
658
+ ### Special Keys
659
+ | | | |
660
+ |---|---|---|
661
+ | `KEY_ESC#` | `KEY_TAB#` | `KEY_BACKSPACE#` |
662
+ | `KEY_ENTER#` | `KEY_SPACE#` | `KEY_INSERT#` |
663
+ | `KEY_DELETE#` | `KEY_HOME#` | `KEY_END#` |
664
+ | `KEY_PGUP#` | `KEY_PGDN#` | |
665
+
666
+ ### Modifier Keys
667
+ | | | |
668
+ |---|---|---|
669
+ | `KEY_LSHIFT#` | `KEY_RSHIFT#` | `KEY_LCTRL#` |
670
+ | `KEY_RCTRL#` | `KEY_LALT#` | `KEY_RALT#` |
671
+ | `KEY_LWIN#` | `KEY_RWIN#` | |
672
+
673
+ ### Function Keys
674
+ | | | |
675
+ |---|---|---|
676
+ | `KEY_F1#` | `KEY_F2#` | `KEY_F3#` |
677
+ | `KEY_F4#` | `KEY_F5#` | `KEY_F6#` |
678
+ | `KEY_F7#` | `KEY_F8#` | `KEY_F9#` |
679
+ | `KEY_F10#` | `KEY_F11#` | `KEY_F12#` |
680
+
681
+ ### Numpad Keys
682
+ | | | |
683
+ |---|---|---|
684
+ | `KEY_NUMPAD0#` | `KEY_NUMPAD1#` | `KEY_NUMPAD2#` |
685
+ | `KEY_NUMPAD3#` | `KEY_NUMPAD4#` | `KEY_NUMPAD5#` |
686
+ | `KEY_NUMPAD6#` | `KEY_NUMPAD7#` | `KEY_NUMPAD8#` |
687
+ | `KEY_NUMPAD9#` | | |
688
+
689
+ ### Punctuation Keys
690
+ | | | |
691
+ |---|---|---|
692
+ | `KEY_COMMA#` | `KEY_DOT#` | `KEY_MINUS#` |
693
+ | `KEY_EQUALS#` | `KEY_SLASH#` | `KEY_BACKSLASH#` |
694
+ | `KEY_SEP#` | `KEY_GRAVE#` | `KEY_LBRACKET#` |
695
+ | `KEY_RBRACKET#` | | |
696
+
697
+ ### Alphabet Keys
698
+ | | | |
699
+ |---|---|---|
700
+ | `KEY_A#` | `KEY_B#` | `KEY_C#` |
701
+ | `KEY_D#` | `KEY_E#` | `KEY_F#` |
702
+ | `KEY_G#` | `KEY_H#` | `KEY_I#` |
703
+ | `KEY_J#` | `KEY_K#` | `KEY_L#` |
704
+ | `KEY_M#` | `KEY_N#` | `KEY_O#` |
705
+ | `KEY_P#` | `KEY_Q#` | `KEY_R#` |
706
+ | `KEY_S#` | `KEY_T#` | `KEY_U#` |
707
+ | `KEY_V#` | `KEY_W#` | `KEY_X#` |
708
+ | `KEY_Y#` | `KEY_Z#` | |
709
+
710
+ ### Number Keys
711
+ | | | |
712
+ |---|---|---|
713
+ | `KEY_0#` | `KEY_1#` | `KEY_2#` |
714
+ | `KEY_3#` | `KEY_4#` | `KEY_5#` |
715
+ | `KEY_6#` | `KEY_7#` | `KEY_8#` |
716
+ | `KEY_9#` | | |
717
+
718
+ ### Mouse
719
+ ```basic
720
+ MOUSE_LEFT# = 1, MOUSE_RIGHT# = 2, MOUSE_MIDDLE# = 4
721
+ ```
722
+
723
+ ### Logical
724
+ `TRUE` = 1, `FALSE` = 0
725
+
726
+ ---
727
+
728
+ ## TIME & TICKS
729
+
730
+ ### TIME(format$)
731
+ Returns current date/time as a formatted string. Uses .NET DateTime format strings.
732
+ ```vb
733
+ PRINT TIME() ' Default: "16:30:45"
734
+ PRINT TIME("HH:mm:ss") ' "16:30:45"
735
+ PRINT TIME("dd.MM.yyyy") ' "09.01.2026"
736
+ PRINT TIME("yyyy-MM-dd") ' "2026-01-09"
737
+ PRINT TIME("dddd") ' "Friday"
738
+ PRINT TIME("MMMM") ' "January"
739
+ PRINT TIME("dd MMMM yyyy") ' "09 January 2026"
740
+ PRINT TIME("HH:mm") ' "16:30"
741
+ ```
742
+
743
+ **Common format codes:**
744
+ | Code | Description | Example |
745
+ |------|-------------|--------|
746
+ | HH | Hour (00-23) | 16 |
747
+ | mm | Minutes | 30 |
748
+ | ss | Seconds | 45 |
749
+ | dd | Day | 09 |
750
+ | MM | Month (number) | 01 |
751
+ | MMM | Month (short) | Jan |
752
+ | MMMM | Month (full) | January |
753
+ | yy | Year (2 digits) | 26 |
754
+ | yyyy | Year (4 digits) | 2026 |
755
+ | ddd | Weekday (short) | Fri |
756
+ | dddd | Weekday (full) | Friday |
757
+
758
+
759
+ ### TICKS
760
+ Returns milliseconds elapsed since program started. Useful for timing, animations, and game loops.
761
+ ```vb
762
+ LET start$ = TICKS
763
+
764
+ ' Do some work
765
+ FOR i$ = 1 TO 10000
766
+ LET x$ = x$ + 1
767
+ NEXT
768
+
769
+ LET elapsed$ = TICKS - start$
770
+ PRINT "Time taken: "; elapsed$; " ms"
771
+ ```
772
+ ## Source Control
773
+
774
+ ### INCLUDE
775
+ ```basic
776
+ INCLUDE "other_file.bas" ' Insert source at this point
777
+ INCLUDE "MathLib.bb" ' Include compiled library
778
+ ```
779
+
780
+ ### Libraries (.bb files)
781
+ ```basic
782
+ ' MathLib.bas - can ONLY contain DEF FN functions
783
+ DEF FN add$(x$, y$)
784
+ RETURN x$ + y$
785
+ END DEF
786
+ ```
787
+ Compile: `bazzbasic.exe -lib MathLib.bas` ? `MathLib.bb`
788
+
789
+ ```basic
790
+ INCLUDE "MathLib.bb"
791
+ PRINT FN MATHLIB_add$(5, 3) ' Auto-prefix: FILENAME_ + functionName
792
+ ```
793
+ - Libraries can only contain `DEF FN` functions
794
+ - Library functions can access main program constants (`#`)
795
+ - Version-locked: .bb may not work across BazzBasic versions
796
+
797
+ ---
798
+
799
+ ## Common Patterns
800
+
801
+ ### Game Loop
802
+ ```basic
803
+ SCREEN 12
804
+ LET running$ = TRUE
805
+
806
+ WHILE running$
807
+ LET key$ = INKEY
808
+ IF key$ = KEY_ESC# THEN running$ = FALSE
809
+
810
+ ' Math and logic here
811
+
812
+ SCREENLOCK ON
813
+ LINE (0,0)-(640,480), 0, BF ' Fast clear
814
+ ' Draw game state
815
+ SCREENLOCK OFF
816
+ SLEEP 16
817
+ WEND
818
+ END
819
+ ```
820
+
821
+ ### HTTP + Data
822
+ ```basic
823
+ DIM response$
824
+ LET response$ = HTTPGET("https://api.example.com/scores")
825
+ PRINT response$
826
+
827
+ DIM payload$
828
+ LET payload$ = HTTPPOST("https://api.example.com/submit", "{""score"":9999}")
829
+ PRINT payload$
830
+ ```
831
+
832
+ ### Save/Load with Arrays
833
+ ```basic
834
+ DIM save$
835
+ save$("level") = 3
836
+ save$("hp") = 80
837
+ FileWrite "save.txt", save$
838
+
839
+ DIM load$
840
+ LET load$ = FileRead("save.txt")
841
+ PRINT load$("level") ' Output: 3
842
+ ```
843
+
844
+ ## Arrays and JSON
845
+
846
+ BazzBasic arrays map naturally to JSON. Nested JSON objects and arrays become multi-dimensional keys using comma-separated indices.
847
+
848
+ ### ASJSON
849
+ Converts a BazzBasic array to a JSON string:
850
+ ```vb
851
+ DIM player$
852
+ player$("name") = "Alice"
853
+ player$("score") = 9999
854
+ player$("address,city") = "New York"
855
+ player$("skills,0") = "JavaScript"
856
+ player$("skills,1") = "Python"
857
+
858
+ LET json$ = ASJSON(player$)
859
+ PRINT json$
860
+ ' Output: {"name":"Alice","score":9999,"address":{"city":"New York"},"skills":["JavaScript","Python"]}
861
+ ```
862
+
863
+ ### ASARRAY
864
+ Converts a JSON string into a BazzBasic array. Returns number of elements loaded:
865
+ ```vb
866
+ DIM data$
867
+ LET count$ = ASARRAY(data$, "{""name"":""Bob"",""score"":42}")
868
+
869
+ PRINT data$("name") ' Output: Bob
870
+ PRINT data$("score") ' Output: 42
871
+ PRINT count$ ' Output: 2
872
+ ```
873
+
874
+ Nested JSON becomes comma-separated keys:
875
+ ```vb
876
+ DIM data$
877
+ LET json$ = "{""player"":{""name"":""Alice"",""hp"":100},""skills"":[""fire"",""ice""]}"
878
+ ASARRAY data$, json$
879
+
880
+ PRINT data$("player,name") ' Output: Alice
881
+ PRINT data$("player,hp") ' Output: 100
882
+ PRINT data$("skills,0") ' Output: fire
883
+ PRINT data$("skills,1") ' Output: ice
884
+ ```
885
+
886
+ ### LOADJSON
887
+ Loads a JSON file directly into an array:
888
+ ```vb
889
+ DIM scores$
890
+ LOADJSON scores$, "highscores.json"
891
+
892
+ PRINT scores$("first,name") ' Output: depends on file contents
893
+ ```
894
+
895
+ ### SAVEJSON
896
+ Saves an array as a formatted JSON file:
897
+ ```vb
898
+ DIM save$
899
+ save$("level") = 3
900
+ save$("hp") = 80
901
+ save$("position,x") = 100
902
+ save$("position,y") = 200
903
+
904
+ SAVEJSON save$, "savegame.json"
905
+ ```
906
+
907
+ The resulting `savegame.json`:
908
+ ```json
909
+ {
910
+ "level": 3,
911
+ "hp": 80,
912
+ "position": {
913
+ "x": 100,
914
+ "y": 200
915
+ }
916
+ }
917
+ ```
918
+
919
+ ### Practical example: HTTP API + JSON
920
+ ```vb
921
+ DIM response$
922
+ LET raw$ = HTTPGET("https://api.example.com/user/1")
923
+ ASARRAY response$, raw$
924
+
925
+ PRINT "Name: "; response$("name")
926
+ PRINT "Email: "; response$("email")
927
+ ```
928
+
929
+ ### Sound with Graphics
930
+ ```basic
931
+ SCREEN 12
932
+ LET bgm$ = LOADSOUND("music.wav")
933
+ LET sfx$ = LOADSOUND("jump.wav")
934
+ SOUNDREPEAT(bgm$)
935
+
936
+ WHILE INKEY <> KEY_ESC#
937
+ IF INKEY = KEY_SPACE# THEN SOUNDONCE(sfx$)
938
+ SLEEP 16
939
+ WEND
940
+ SOUNDSTOPALL
941
+ END
942
+ ```
943
+
944
+ ---
945
+
946
+ ## Code Style Conventions
947
+
948
+ **Variables** - `camelCase$`
949
+ ```basic
950
+ LET playerName$ = "Hero"
951
+ LET score$ = 0
952
+ ```
953
+
954
+ **Constants** - `UPPER_SNAKE_CASE#`
955
+ ```basic
956
+ LET MAX_HEALTH# = 100
957
+ LET SCREEN_W# = 640
958
+ ```
959
+
960
+ **Arrays** - `camelCase$` (like variables, declared with DIM)
961
+ ```basic
962
+ DIM scores$
963
+ DIM playerData$
964
+ ```
965
+
966
+ **User-defined functions** - `PascalCase$`
967
+ ```basic
968
+ DEF FN CalculateDamage$(attack$, defence$)
969
+ DEF FN IsColliding$(x$, y$)
970
+ ```
971
+
972
+ **Labels** - descriptive names, `[sub:]` prefix recommended for subroutines
973
+ ```basic
974
+ [sub:DrawPlayer] ' Subroutine
975
+ [gameLoop] ' Jump target
976
+ ```
977
+
978
+ ## Program Structure
979
+
980
+ 1. Constants first
981
+ 2. User-defined functions
982
+ 3. Init
983
+ 4. Main program loop
984
+ 5. Subroutines (labels) last
985
+
986
+ ```basic
987
+ ' -- 1. CONSTANTS ----------------------------
988
+ ' or INCLUDE as "constants.bas" etc
989
+
990
+ LET SCREEN_W# = 640
991
+ LET SCREEN_H# = 480
992
+ LET MAX_SPEED# = 5
993
+
994
+ ' -- 2. FUNCTIONS ----------------------------
995
+ ' or INCLUDE as "functions.bas" etc
996
+ DEF FN Clamp$(val$, lo$, hi$)
997
+ IF val$ < lo$ THEN RETURN lo$
998
+ IF val$ > hi$ THEN RETURN hi$
999
+ RETURN val$
1000
+ END DEF
1001
+
1002
+ ' -- 3. INIT ---------------------------------
1003
+ ' or INCLUDE as "inits.bas" etc
1004
+ ' IMPORTANT:
1005
+ ' Avoid declaring variables outside INIT if speed matters.
1006
+ ' If done inside [main] or [subs], Bazzbasic has to check the existence of the variable in each call.
1007
+
1008
+ [inits]
1009
+ SCREEN 0, SCREEN_W#, SCREEN_H#, "My Game"
1010
+ LET x$ = 100
1011
+ LET y$ = 100
1012
+ LET running$ = TRUE
1013
+
1014
+ ' -- 4. MAIN LOOP ----------------------------
1015
+ [main]
1016
+ WHILE running$
1017
+ IF INKEY = KEY_ESC# THEN running$ = FALSE
1018
+ GOSUB [sub:update]
1019
+ GOSUB [sub:draw]
1020
+ SLEEP 16
1021
+ WEND
1022
+ END
1023
+
1024
+ ' -- 5. SUBROUTINES --------------------------
1025
+ ' or INCLUDE as "subs.bas" etc
1026
+
1027
+ [sub:update]
1028
+ IF KEYDOWN(KEY_LEFT#) THEN x$ = x$ - MAX_SPEED#
1029
+ IF KEYDOWN(KEY_RIGHT#) THEN x$ = x$ + MAX_SPEED#
1030
+ RETURN
1031
+
1032
+ [sub:draw]
1033
+ SCREENLOCK ON
1034
+ LINE (0,0)-(SCREEN_W#, SCREEN_H#), 0, BF
1035
+ CIRCLE (x$, y$), 10, RGB(0,255,0), 1
1036
+ SCREENLOCK OFF
1037
+ RETURN
1038
+ ```
1039
+
1040
+ ### Images and sounds
1041
+ When ever there is no reason to change the ID reference of image/sound, we should use constants.
1042
+
1043
+ ```basic
1044
+ LET MY_IMAGE# = LOADIMAGE("temp.bmp") ' Correct
1045
+ LET myImage$# = LOADIMAGE("temp.bmp") ' Wrong
1046
+ ```
1047
+
1048
+ To change image of enemy during the game
1049
+ ```basic
1050
+ LET ENEMY1# = LOADIMAGE("enemy1.bmp")
1051
+ LET ENEMY2# = LOADIMAGE("enemy2.bmp")
1052
+
1053
+ ' begin of game
1054
+ LET curEnemy$ = ENEMY1#
1055
+
1056
+ ' after Enemy1 has died
1057
+ curEnemy$ = ENEMY2#
1058
+ ```
1059
+
1060
+ ### Collect
1061
+ Collect data of same types, characters etc. inside of array if amount of variables starts to pile up.
1062
+
1063
+ ```basic
1064
+ ' while this breaks the idea of making images constants, it helps by packing all images to same array
1065
+ ' notice also how I intent even DIM
1066
+ DIM enemyImages$
1067
+ enemyImages$("enemy1") = LOADIMAGE("enemy1.bmp")
1068
+ enemyImages$("enemy2") = LOADIMAGE("enemy2.bmp")
1069
+
1070
+ DIM levelSprites$
1071
+ levelSprites$("floor") = LOADIMAGE("floor.png")
1072
+ levelSprites$("table") = LOADIMAGE("table.png")
1073
+ ```