EkBass commited on
Commit
32925a1
·
verified ·
1 Parent(s): a31960f

Delete BazzBasic-AI-guide.txt

Browse files
Files changed (1) hide show
  1. BazzBasic-AI-guide.txt +0 -600
BazzBasic-AI-guide.txt DELETED
@@ -1,600 +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.3 | **Author:** Kristian Virtanen (EkBass) | **Platform:** Windows x64
8
- **GitHub:** https://github.com/EkBass/BazzBasic
9
- **Manual:** https://ekbass.github.io/BazzBasic/manual/#/
10
- **Examples:** https://github.com/EkBass/BazzBasic/tree/main/Examples
11
- **Rosetta Code:** https://rosettacode.org/wiki/Category:BazzBasic
12
-
13
-
14
- ---
15
-
16
- ## ⚠️ Critical Rules — Read First
17
-
18
- | Rule | Detail |
19
- |------|--------|
20
- | Variables end with `$` | `name$`, `score$`, `x$` |
21
- | Constants end with `#` | `MAX#`, `PI#`, `TITLE#` |
22
- | Arrays declared with `DIM`, end with `$` | `DIM items$` |
23
- | First use of variable requires `LET` | `LET x$ = 0` — after that `x$ = x$ + 1` |
24
- | FOR and INPUT auto-declare, no LET needed | `FOR i$ = 1 TO 10` |
25
- | Functions defined **before** they are called | Put at top or INCLUDE |
26
- | Function name ends with `$`, called with `FN` | `FN MyFunc$(a$, b$)` |
27
- | Function return value **must** be used | `PRINT FN f$()` or `LET v$ = FN f$()` |
28
- | Arrays **cannot** be passed to functions directly | Pass individual elements, or serialize to JSON string — see *Passing Arrays to Functions* section |
29
- | Case-insensitive | `PRINT`, `print`, `Print` all work |
30
- | `+` operator does both add and concatenate | `"Hi" + " " + name$` |
31
- | Division always returns float | `10 / 3` → `3.333...` |
32
-
33
- ---
34
-
35
-
36
- ## ABOUT
37
- BazzBasic is built around one simple idea: starting programming should feel nice and even fun.
38
-
39
- Ease of learning, comfort of exploration and small but important moments of success.
40
-
41
- Just like the classic BASICs of decades past, but with a fresh and modern feel.
42
-
43
- ## STORY
44
- 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.
45
-
46
- 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.
47
-
48
- BazzBasic was created with this in mind.
49
-
50
- I wanted to create a language that makes it easy for you to give free rein to your curiosity and program something.
51
-
52
- And when you finish your first little game, you may crave something bigger and better.
53
-
54
- Maybe one day you will move on to another programming language, but then BazzBasic will have succeeded in doing what it was intended for.
55
-
56
- To arouse your curiosity.
57
-
58
- ## Variables & Constants
59
-
60
- ```basic
61
- LET a$ ' Declare without value
62
- LET name$ = "Alice" ' String variable
63
- LET score$ = 0 ' Numeric variable
64
- LET x$, y$, z$ = 10 ' Multiple declaration
65
- LET PI# = 3.14159 ' Constant (immutable)
66
- LET TITLE# = "My Game" ' String constant
67
- ```
68
-
69
- **Compound assignment operators** (variables only — **not** allowed with `#` constants):
70
-
71
- ```basic
72
- x$ += 5 ' add
73
- x$ -= 3 ' subtract
74
- x$ *= 2 ' multiply
75
- x$ /= 4 ' divide
76
- s$ += " World" ' string concatenation
77
- ```
78
-
79
- **Scope:** All main-code variables share one scope (even inside IF blocks).
80
- `DEF FN` functions are fully isolated — only global constants (`#`) accessible inside.
81
-
82
- **Comparison:** `"123" = 123` is TRUE (cross-type), but keep types consistent for speed.
83
-
84
- ### Built-in Constants
85
- - **Boolean:** `TRUE`, `FALSE`
86
- - **Math:** `PI#`, `HPI#` (π/2 = 90°), `QPI#` (π/4 = 45°), `TAU#` (2π = 360°), `EULER#` (e) — `#` suffix required
87
- - **System:** `PRG_ROOT#` (program base directory path)
88
- - **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.
89
-
90
- ---
91
-
92
- ## Arrays
93
-
94
- ```basic
95
- DIM scores$ ' Declare (required before use)
96
- DIM a$, b$, c$ ' Multiple
97
- scores$(0) = 95 ' Numeric index (0-based)
98
- scores$("name") = "Alice" ' String key (associative)
99
- matrix$(0, 1) = "A2" ' Multi-dimensional
100
- ```
101
-
102
- | Function/Command | Description |
103
- |-----------------|-------------|
104
- | `LEN(arr$())` | Total element count (note empty parens) |
105
- | `ROWCOUNT(arr$())` | Count of first-dimension rows — use this for FOR loops over multi-dim arrays |
106
- | `HASKEY(arr$(key))` | 1 if exists, 0 if not |
107
- | `DELKEY arr$(key)` | Remove one element |
108
- | `DELARRAY arr$` | Remove entire array (can re-DIM after) |
109
- | `JOIN dest$, src1$, src2$` | Merge two arrays; `src2$` keys overwrite `src1$`. Use empty `src1$` as `COPYARRAY`. |
110
-
111
- **Always check with `HASKEY` before reading uninitialized elements.**
112
-
113
- ---
114
-
115
- ## Control Flow
116
-
117
- ```basic
118
- ' Block IF
119
- IF score$ >= 90 THEN
120
- PRINT "A"
121
- ELSEIF score$ >= 80 THEN
122
- PRINT "B"
123
- ELSE
124
- PRINT "F"
125
- END IF ' ENDIF also works
126
-
127
- ' One-line IF (GOTO/GOSUB only)
128
- IF lives$ = 0 THEN GOTO [game_over]
129
- IF key$ = KEY_ESC# THEN GOTO [menu] ELSE GOTO [play]
130
-
131
- ' FOR (auto-declares variable)
132
- FOR i$ = 1 TO 10 STEP 2 : PRINT i$ : NEXT
133
- FOR i$ = 10 TO 1 STEP -1 : PRINT i$ : NEXT
134
-
135
- ' WHILE
136
- WHILE x$ < 100 : x$ = x$ * 2 : WEND
137
-
138
- ' Labels, GOTO, GOSUB
139
- [start]
140
- GOSUB [sub:init]
141
- GOTO [main]
142
-
143
- [sub:init]
144
- LET x$ = 0
145
- RETURN
146
-
147
- ' Dynamic jump (variable must contain "[label]" with brackets)
148
- LET target$ = "[menu]"
149
- GOTO target$
150
-
151
- ' Other
152
- SLEEP 2000 ' Pause ms
153
- END ' Terminate program
154
- ```
155
-
156
- ---
157
-
158
- ## I/O
159
-
160
- | Command | Description |
161
- |---------|-------------|
162
- | `PRINT expr; expr` | `;` = no space, `,` = tab |
163
- | `PRINT "text";` | Trailing `;` suppresses newline |
164
- | `INPUT "prompt", var$` | Splits on whitespace/comma |
165
- | `INPUT "prompt", a$, b$` | Multiple values |
166
- | `LINE INPUT "prompt", var$` | Read entire line with spaces |
167
- | `CLS` | Clear screen |
168
- | `LOCATE row, col` | Move cursor (1-based) |
169
- | `CURPOS("row")` / `CURPOS("col")` | Read cursor row or col (1-based, matches LOCATE) |
170
- | `CURPOS()` | Read cursor as `"row,col"` string |
171
- | `COLOR fg, bg` | Text colors (0–15 palette) |
172
- | `SHELL("cmd")` | Run shell command, returns output |
173
- | `SHELL("cmd", ms)` | With timeout in ms (default 5000) |
174
-
175
- **Escape sequences in strings:** `\"` `\n` `\t` `\\`
176
-
177
- ### Keyboard Input
178
- | Function | Returns | Notes |
179
- |----------|---------|-------|
180
- | `INKEY` | Key value or 0 | Non-blocking |
181
- | `KEYDOWN(key#)` | TRUE/FALSE | Held-key detection; **graphics mode only** |
182
- | `WAITKEY(key#, ...)` | Key value | Blocks until key pressed; `WAITKEY()` = any key |
183
-
184
- ### Mouse (graphics mode only)
185
- `MOUSEX`, `MOUSEY` — cursor position
186
- `MOUSELEFT`, `MOUSERIGHT`, `MOUSEMIDDLE` — 1 if pressed, 0 otherwise
187
-
188
- ### Console Read
189
- `GETCONSOLE(row, col, type)` — type: 0=char (ASCII), 1=fg color, 2=bg color
190
-
191
- ---
192
-
193
- ## User-Defined Functions
194
-
195
- ```basic
196
- ' Define BEFORE calling. Name must end with $.
197
- DEF FN Clamp$(val$, lo$, hi$)
198
- IF val$ < lo$ THEN RETURN lo$
199
- IF val$ > hi$ THEN RETURN hi$
200
- RETURN val$
201
- END DEF
202
-
203
- PRINT FN Clamp$(5, 1, 10) ' ✓ OK — return value used
204
- LET v$ = FN Clamp$(15, 0, 10) ' ✓ OK
205
- FN Clamp$(5, 1, 10) ' ✗ ERROR — return value unused
206
- ```
207
-
208
- - Isolated scope: no access to global variables, only global constants (`#`)
209
- - Parameters passed **by value**
210
- - Labels inside functions are local — GOTO/GOSUB cannot jump outside
211
- - Supports recursion
212
- - Arrays as parameters not allowed. Use ASJSON to make array as JSON-string to pass it.
213
- - Use `INCLUDE` to load functions from separate files if many
214
-
215
- ---
216
-
217
- ## String Functions
218
-
219
- | Function | Description |
220
- |----------|-------------|
221
- | `ASC(s$)` | ASCII code of first char |
222
- | `CHR(n)` | Character from ASCII code |
223
- | `INSTR(s$, search$)` | Position (1-based), 0=not found; case-sensitive by default |
224
- | `INSTR(s$, search$, mode)` | mode: 0=case-insensitive, 1=case-sensitive |
225
- | `INSTR(start, s$, search$)` | Search from position (case-sensitive) |
226
- | `INVERT(s$)` | Reverse string |
227
- | `LCASE(s$)` / `UCASE(s$)` | Lower / upper case |
228
- | `LEFT(s$, n)` / `RIGHT(s$, n)` | First/last n chars |
229
- | `LEN(s$)` | String length |
230
- | `LTRIM(s$)` / `RTRIM(s$)` / `TRIM(s$)` | Strip whitespace |
231
- | `MID(s$, start)` | Substring from start (1-based) |
232
- | `MID(s$, start, len)` | Substring with length |
233
- | `REPEAT(s$, n)` | Repeat string n times |
234
- | `REPLACE(s$, a$, b$)` | Replace a$ with b$ in s$ |
235
- | `SPLIT(arr$, s$, sep$)` | Split into array, returns count |
236
- | `SRAND(n)` | Random alphanumeric string of length n |
237
- | `STR(n)` | Number to string |
238
- | `VAL(s$)` | String to number |
239
- | `SHA256(s$)` | SHA256 hash (64-char hex) |
240
- | `BASE64ENCODE(s$)` / `BASE64DECODE(s$)` | Base64 encode/decode |
241
-
242
- ---
243
-
244
- ## Math Functions
245
-
246
- | Function | Description |
247
- |----------|-------------|
248
- | `ABS(n)` | Absolute value |
249
- | `ATAN(n)` | Arc tangent |
250
- | `BETWEEN(n, min, max)` | TRUE if min ≤ n ≤ max |
251
- | `CEIL(n)` / `FLOOR(n)` | Round up / down |
252
- | `CINT(n)` | Round to nearest integer |
253
- | `CLAMP(n, min, max)` | Constrain n to [min, max] |
254
- | `COS(n)` / `SIN(n)` / `TAN(n)` | Trig (radians) |
255
- | `DEG(rad)` / `RAD(deg)` | Radians ↔ degrees |
256
- | `DISTANCE(x1,y1, x2,y2)` | 2D Euclidean distance |
257
- | `DISTANCE(x1,y1,z1, x2,y2,z2)` | 3D Euclidean distance |
258
- | `EXP(n)` | e^n |
259
- | `INT(n)` | Truncate toward zero |
260
- | `LERP(start, end, t)` | Linear interpolation (t: 0.0–1.0) |
261
- | `LOG(n)` | Natural logarithm |
262
- | `MAX(a, b)` / `MIN(a, b)` | Larger / smaller of two |
263
- | `MOD(a, b)` | Remainder |
264
- | `POW(base, exp)` | Power |
265
- | `RND(n)` | Random integer 0 to n-1 |
266
- | `ROUND(n)` | Standard rounding |
267
- | `SGN(n)` | Sign: -1, 0, or 1 |
268
- | `SQR(n)` | Square root |
269
-
270
- **Math constants:** `PI#`, `HPI#` (PI/2), `QPI#` (PI/4), `TAU#` (PI*2), `EULER#`
271
-
272
- ---
273
-
274
- ## Graphics
275
-
276
- ```basic
277
- SCREEN 12 ' 640×480 VGA (recommended)
278
- SCREEN 0, 800, 600 ' Custom size
279
- SCREEN 0, 1024, 768, "My Game" ' Custom size + title
280
- FULLSCREEN TRUE ' Borderless fullscreen (graphics only)
281
- FULLSCREEN FALSE ' Windowed
282
- ```
283
-
284
- | Mode | Resolution |
285
- |------|-----------|
286
- | 1 | 320×200 |
287
- | 2 | 640×350 |
288
- | 7 | 320×200 |
289
- | 9 | 640×350 |
290
- | 12 | 640×480 ← recommended |
291
- | 13 | 320×200 |
292
-
293
- ### Drawing Primitives
294
- ```basic
295
- PSET (x, y), color ' Pixel
296
- LINE (x1,y1)-(x2,y2), color ' Line
297
- LINE (x1,y1)-(x2,y2), color, B ' Box outline
298
- LINE (x1,y1)-(x2,y2), color, BF ' Box filled (FAST — use instead of CLS)
299
- CIRCLE (cx,cy), radius, color ' Circle outline
300
- CIRCLE (cx,cy), radius, color, 1 ' Circle filled
301
- PAINT (x, y), fillColor, borderColor ' Flood fill
302
- LET c$ = POINT(x, y) ' Read pixel color
303
- LET col$ = RGB(r, g, b) ' Create color (0–255 each)
304
- ```
305
-
306
- **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
307
-
308
- ### Screen Control
309
- ```basic
310
- SCREENLOCK ON ' Buffer drawing (start frame)
311
- SCREENLOCK OFF ' Present buffer (end frame)
312
- VSYNC(TRUE) ' Enable VSync (default, ~60 FPS)
313
- VSYNC(FALSE) ' Disable VSync (benchmarking)
314
- CLS ' Clear screen (slow — prefer LINE BF)
315
- ```
316
-
317
- ### Shapes & Images
318
- ```basic
319
- ' Create shape
320
- ' LOADSHAPE and LOADIMAGE return a stable integer handle — never reassigned.
321
- ' SDL2 owns the resource; your code only ever holds this one reference. Use constants.
322
- LET RECT# = LOADSHAPE("RECTANGLE", w, h, color) ' or "CIRCLE", "TRIANGLE"
323
- LET IMG_PLAYER# = LOADIMAGE("player.png") ' PNG (alpha) or BMP
324
- LET IMG_REMOTE# = LOADIMAGE("https://example.com/a.png") ' Download + load
325
-
326
- ' Sprite sheet — sprites indexed 1-based
327
- DIM sprites$
328
- LOADSHEET sprites$, 128, 128, "sheet.png" ' tileW, tileH, file
329
- MOVESHAPE sprites$(1), x, y ' sprites$(1) = first sprite
330
-
331
- ' Transform
332
- MOVESHAPE RECT#, x, y ' Position by center point
333
- ROTATESHAPE RECT#, angle ' Degrees (absolute)
334
- SCALESHAPE RECT#, scale ' 1.0 = original size
335
- DRAWSHAPE RECT# ' Render to buffer
336
- SHOWSHAPE RECT# / HIDESHAPE RECT# ' Toggle visibility
337
- REMOVESHAPE RECT# ' Free memory (always clean up)
338
- ```
339
-
340
-
341
- ---
342
-
343
- ### Text Rendering (SDL2_ttf.dll required)
344
- #### DRAWSTRING & LOADFONT
345
-
346
-
347
- ```basic
348
- ' Default font (Arial)
349
- DRAWSTRING "Hello!", 100, 200, RGB(255, 255, 255)
350
-
351
- ' Load alternative font — becomes the new default
352
- LOADFONT "comic.ttf", 24
353
- DRAWSTRING "Hello!", 100, 200, RGB(255, 255, 255)
354
-
355
- ' Reset to Arial
356
- LOADFONT
357
- ```
358
-
359
- `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.
360
-
361
- ---
362
-
363
- ## Sound
364
-
365
- ```basic
366
- ' LOADSOUND returns a stable integer handle — SDL2 manages the resource.
367
- ' The handle never changes; store it in a constant to protect it from accidental reassignment.
368
- LET SND_JUMP# = LOADSOUND("jump.wav") ' Load (WAV recommended)
369
- SOUNDONCE(SND_JUMP#) ' Play once, non-blocking
370
- SOUNDONCEWAIT(SND_JUMP#) ' Play once, wait for finish
371
- SOUNDREPEAT(SND_JUMP#) ' Loop continuously
372
- SOUNDSTOP(SND_JUMP#) ' Stop specific sound
373
- SOUNDSTOPALL ' Stop all sounds
374
- ```
375
-
376
- Load all sounds at startup. Call `SOUNDSTOPALL` before `END`.
377
-
378
- ---
379
-
380
- ## File I/O
381
-
382
- ```basic
383
- LET data$ = FileRead("file.txt") ' Read as string
384
- DIM cfg$ : LET cfg$ = FileRead("settings.txt") ' Read as key=value array
385
- FileWrite "save.txt", data$ ' Create/overwrite
386
- FileAppend "log.txt", entry$ ' Append
387
- LET ok$ = FileExists("file.txt") ' 1=exists, 0=not
388
- FileDelete "temp.dat" ' Delete file
389
- ```
390
-
391
- **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.
392
-
393
- ```basic
394
- DIM env$
395
- LET env$ = FileRead(".env")
396
- LET API_KEY# = env$("OPENAI_API_KEY")
397
- ```
398
-
399
- **Paths:** Use `/` or `\\` — never single `\` (it's an escape char). Relative paths are from `PRG_ROOT#`.
400
- **FileWrite with array** saves in key=value format (round-trips with FileRead).
401
-
402
- ---
403
-
404
- ## Network
405
-
406
- ```basic
407
- LET res$ = HTTPGET("https://api.example.com/data")
408
- LET res$ = HTTPPOST("https://api.example.com/submit", "{""key"":""val""}")
409
-
410
- ' With headers (optional last parameter)
411
- DIM headers$
412
- headers$("Authorization") = "Bearer mytoken"
413
- headers$("Content-Type") = "application/json"
414
- LET res$ = HTTPGET("https://api.example.com/data", headers$)
415
- LET res$ = HTTPPOST("https://api.example.com/data", body$, headers$)
416
- ```
417
-
418
- ---
419
-
420
- ## Arrays & JSON
421
-
422
- Nested JSON maps to comma-separated keys: `data$("player,name")`, `data$("skills,0")`
423
-
424
- ```basic
425
- ' Array → JSON string
426
- LET json$ = ASJSON(arr$)
427
-
428
- ' JSON string → array (returns element count)
429
- DIM data$
430
- LET count$ = ASARRAY(data$, json$)
431
-
432
- ' Load/save JSON files
433
- LOADJSON arr$, "file.json"
434
- SAVEJSON arr$, "file.json"
435
- ```
436
-
437
- ---
438
-
439
- ## Fast Trigonometry
440
-
441
- ~20× faster than `SIN(RAD(x))`, 1-degree precision. Uses ~5.6 KB memory.
442
-
443
- ```basic
444
- FastTrig(TRUE) ' Enable lookup tables (must call first)
445
- LET x$ = FastCos(45) ' Degrees, auto-normalized 0–359
446
- LET y$ = FastSin(90)
447
- LET r$ = FastRad(180) ' Deg→rad (no FastTrig needed)
448
- FastTrig(FALSE) ' Free memory
449
- ```
450
-
451
- Use for raycasting, sprite rotation, particle systems, any high-freq trig.
452
-
453
- ---
454
-
455
- ## Command-Line Arguments
456
-
457
- ```basic
458
- ' bazzbasic.exe myprog.bas arg1 arg2
459
- PRINT ARGCOUNT ' number of args (2 in this example)
460
- PRINT ARGS(0) ' first arg → "arg1"
461
- PRINT ARGS(1) ' second arg → "arg2"
462
- ```
463
-
464
- `ARGCOUNT` and `ARGS(n)` are 0-based; ARGS does not include the interpreter or script name.
465
-
466
- ---
467
-
468
- ## Libraries & INCLUDE
469
-
470
- ```basic
471
- INCLUDE "helpers.bas" ' Insert source at this point
472
- INCLUDE "MathLib.bb" ' Load compiled library
473
-
474
- ' Compile library (functions only — no loose code)
475
- ' bazzbasic.exe -lib MathLib.bas → MathLib.bb
476
- ' Function names auto-prefixed: MATHLIB_functionname$
477
- PRINT FN MATHLIB_add$(5, 3)
478
- ```
479
-
480
- Library functions can read main-program constants (`#`). `.bb` files are version-locked.
481
-
482
- ---
483
-
484
- ## Passing Arrays to Functions
485
-
486
- 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+.
487
-
488
- ```basic
489
- DEF FN ProcessPlayer$(data$)
490
- DIM arr$
491
- LET count$ = ASARRAY(arr$, data$)
492
- RETURN arr$("name") + " score:" + arr$("score")
493
- END DEF
494
-
495
- [inits]
496
- DIM player$
497
- player$("name") = "Alice"
498
- player$("score") = 9999
499
- player$("address,city") = "New York"
500
-
501
- [main]
502
- LET json$ = ASJSON(player$)
503
- PRINT FN ProcessPlayer$(json$)
504
- END
505
- ```
506
-
507
- **Notes:**
508
- - The function receives a full independent copy — changes inside do not affect the original array
509
- - Nested keys work normally: `arr$("address,city")` etc.
510
- - Overhead is similar to copying an array manually; acceptable for most use cases
511
-
512
- ---
513
-
514
- ## Program Structure
515
-
516
- ```basic
517
- ' ---- 1. FUNCTIONS (or INCLUDE "functions.bas") ----
518
- DEF FN Clamp$(v$, lo$, hi$)
519
- IF v$ < lo$ THEN RETURN lo$
520
- IF v$ > hi$ THEN RETURN hi$
521
- RETURN v$
522
- END DEF
523
-
524
- ' ---- 2. INIT (declare ALL constants & variables here, not inside loops) ----
525
- ' Performance: variables declared outside loops avoid repeated existence checks.
526
- [inits]
527
- LET SCREEN_W# = 640
528
- LET SCREEN_H# = 480
529
- LET MAX_SPEED# = 5
530
-
531
- SCREEN 0, SCREEN_W#, SCREEN_H#, "My Game"
532
-
533
- LET x$ = 320
534
- LET y$ = 240
535
- LET running$ = TRUE
536
-
537
- ' ---- 3. MAIN LOOP ----
538
- [main]
539
- WHILE running$
540
- IF INKEY = KEY_ESC# THEN running$ = FALSE
541
- GOSUB [sub:update]
542
- GOSUB [sub:draw]
543
- SLEEP 16
544
- WEND
545
- SOUNDSTOPALL
546
- END
547
-
548
- ' ---- 4. SUBROUTINES (or INCLUDE "subs.bas") ----
549
- [sub:update]
550
- IF KEYDOWN(KEY_LEFT#) THEN x$ = x$ - MAX_SPEED#
551
- IF KEYDOWN(KEY_RIGHT#) THEN x$ = x$ + MAX_SPEED#
552
- RETURN
553
-
554
- [sub:draw]
555
- SCREENLOCK ON
556
- LINE (0,0)-(SCREEN_W#, SCREEN_H#), 0, BF
557
- CIRCLE (x$, y$), 10, RGB(0, 255, 0), 1
558
- SCREENLOCK OFF
559
- RETURN
560
- ```
561
-
562
- **Key conventions:**
563
- - Variables: `camelCase$` | Constants: `UPPER_SNAKE_CASE#` | Functions: `PascalCase$`
564
- - Labels: `[gameLoop]` for jump targets, `[sub:name]` for subroutines
565
- - Image/sound/shape IDs are stable integer handles — **always** store as constants: `LET MY_IMG# = LOADIMAGE("x.png")` — never use `$` variables for these
566
- - Group many IDs → use arrays: `DIM sprites$` / `sprites$("player") = LOADIMAGE(...)` — but prefer named constants when count is small
567
-
568
- ---
569
-
570
- ## Performance Tips
571
-
572
- - `LINE (0,0)-(W,H), 0, BF` to clear — much faster than `CLS`
573
- - Always wrap draw code in `SCREENLOCK ON` / `SCREENLOCK OFF`
574
- - Store `RGB()` results in constants/variables — don't call RGB in hot loops
575
- - Declare all variables in `[inits]`, not inside loops or subroutines
576
- - Use `FastTrig` for any loop calling trig hundreds of times per frame
577
- - `SLEEP 16` in game loop → ~60 FPS
578
-
579
- ---
580
-
581
- ## IDE Features (v1.3)
582
-
583
- ### New File Template
584
- When the IDE opens with no file (or a new file), this template is auto-inserted:
585
- ```basic
586
- ' BazzBasic version 1.3
587
- ' https://ekbass.github.io/BazzBasic/
588
- ```
589
-
590
- ### Beginner's Guide
591
- - **IDE:** Menu → **Help** → **Beginner's Guide** — opens `https://github.com/EkBass/BazzBasic-Beginners-Guide/releases` in default browser
592
- - **CLI:** `bazzbasic.exe -guide` or `bazzbasic.exe -help` — prints URL to terminal
593
-
594
- ### Check for Updates
595
- - **IDE:** Menu → **Help** → **Check for updated...** — IDE reports if a newer version is available
596
- - **CLI:** `bazzbasic.exe -checkupdate`
597
-
598
- ### Compile via IDE
599
- - **Menu → Run → Compile as Exe** — compiles open file to standalone `.exe` (auto-saves first)
600
- - **Menu → Run → Compile as Library (.bb)** — compiles open file as reusable `.bb` library (auto-saves first)