 rem *** vwBASIC compiler sample program   ***
 rem *** with examples and comments
 rem -----------------------------------------
 rem ---init section, runs once: -------------
 rem -----------------------------------------
 rem -- line numbers are now optional
10 rem -- but it can be fun to use them!

 COLUP0=$40:COLUP1=100: h=0:g=0:l=0:f=0:u=5:v=5: rem init vars
 loadplayer0(8): rem load 2nd sprite definition
 vwpixel(5,4,bindmissile0):vwpixel(5,5,bindmissile1)
20 rem draw a box of inverted pixels (gosub 20 to call routine again)
 for i = 7 to 9:rem outer loop
 for j = 4 to 7: rem inner loop
 vwpixel(i,j,flip)
 next j,i
 rem BASIC print@ examples - prints binary string patterns on the virtual world
 print %1011101(virtualworld,0)
 print %1011101(virtualworld,12)
 write %1010101(virtualworld,24)
 print %1111101(virtualworld,36)
 virtualworld(1)=%11111101
75 virtualworld(13)=%10101011
 virtualworld(14)=virtualworld(13)
 virtualworld(2)=virtualworld(13) & %01010100
 virtualworld(2)=virtualworld(13) & %10101001
 print virtualworld(13)&%10101001(virtualworld,2)
 print 255(virtualworld,2)
 print %10101000 & %10101001(virtualworld,2)
 rem --------------------------------------
 rem test concatenated inline asm block:
95 asm "ASMroutine: lda #5: sta g: lda #3: cmp g: bne skipASMroutine: lda #7: sta h:skipASMroutine"
105 rem end init section -------------------------------
107 rem ------------------------------------------------
108 rem ---:the next comment line is used by the compiler
109 rem ---gameloop subroutine, runs every frame:    ---
110 rem ------------------------------------------------
111 rem --vars: t - 0/1 toggle      f- framecounter
112 rem --vars: x,y player x,y      w- bigloop
114 rem --vars: o - player dir(1-8) i,j loop/inc/temp vars
116 rem --vars: t - 0/1 flipflop
118 rem samplearray(1)=x*55:r=samplearray(1)
125 t=1-t:rem this expression toggles var t between 0 and 1 
126 rem if samplearray(0)*2 = samplearray(1)*4 then goto 126
126a rem samplearray(1)=samplearray(2)*12
126b e=samplearray(2)*n
127 rem vars: x,y player0 virtual world coordinates (for sprite binding)
128 rem vars: g,l,f - frame based counters
129 rem vars: u,v player1 virtual world coordinates (for sprite binding)
130 f=f+1:rem 
137 if g>64 then g=0:rem reset medium frame counter
138 if l>104 then l=0
140 if f<>4 then goto 142 else vwpixel(u,v,bindplayer1):vwpixel(x,y,bindplayer0):vwpixel(5,4,bindmissile0)
141 if joy1fire=1 or joyfire0=1 then goto 152: rem double speed!
142 if f>7 then f=0 : g=g+1:l=l+1 else vwpixel(u,v,bindplayer1):vwpixel(x,y,bindplayer0):vwpixel(5,4,bindmissile0):return
152 rem ---scroll sprite library section containing message
155 loadplayer1(l)
190 rem read/write array elements in named array variables:
200 if g<19 then read i(mazecolours,g):COLUPF=i:i=i+1:write i-1(mazecolours,g) 
380 rem --player1 can move around onscreen or off like Tails; camera only follows player0
388 if f=4 and joy1fire=0 then goto 448: rem skip double speed if not enabled
390 rem if joy1fire=1 then p=50:rem raise delayed event (set toggle for instant) to call draw box (time intensive routine)

400 if joy1up=1 and v>0 then v=v-1
410 if joy1down=1 and v< 19 then v=v+1
420 if joy1left=1 and u>0 then u=u-1
430 if joy1right=1 and u<91 then u=u+1
448 rem checkpoint
553 if f=4 and joy0fire=0 then goto 570 
555 if joy0up=1 and y>0 then COLUP1=$40:COLUP0=$90:y=y-1
556 if joy0down=1 and y<19 then COLUP1=$64:COLUP0=$70:y=y+1
557 if joy0left=1 and x>0 then COLUP1=$55:COLUP0=$60:x=x-1
558 if joy0right=1 and x<91 then COLUP1=$84:COLUP0=$80:x=x+1

560 rem ---bind player1 to virtual world game grid at player0's location if button is pressed 
664 rem if joy1fire=1 then goto 570: rem player 0 can't bind player1 to grid if turbo is on!
570 if u=x and v=y then COLUBK=$0E else COLUBK=0:rem virtual collision detection

700 rem ------pan the camera horizontally to follow player:
710 if x-BITIndex > 14 and BITIndex < 72 then BITIndex=BITIndex+1:scrollvirtualworldtoggle=1
714 if x < BITIndex+3 and BITIndex>0 then BITIndex=BITIndex-1:scrollvirtualworldtoggle=1
720 rem -----pan camera vertically (BYTErowoffset) if player is near the edge of the playfield CAM
725 rem i = y*12: rem "read i(fastyindex,y)" is faster (12 step lookup table for virtual world y values)
727 i=fastyindex(y): rem index the y*12 lookup table
730 if BYTErowoffset<120 and BYTErowoffset+36<i then BYTErowoffset=BYTErowoffset+12:scrollvirtualworldtoggle=1
735 if i>36 then i=i-36
740 if BYTErowoffset>i then BYTErowoffset=BYTErowoffset-12:scrollvirtualworldtoggle=1	

563 if joy0fire=0 then goto 569
564 rem fliping or setting a pixel is like poll, it polls it's previous state
565 if vwpixel(x,y,flip)>0 then COLUBK=120 else COLUBK=0:MUSICINDEX=MUSICINDEX+1  
569 rem checkpoint
575 rem checkpoint
390b array1(0)=u:array1(1)=v-1
390a if joy1fire=0 then goto 390c2 

 rem couple of comments on vwpixel: 

 rem It takes more CPU time but you can overload vwpixel with array vars and transformations:
 rem when you flip or turn on pixels vwpixel still does a poll and returns the previous pixels state
 rem saves time to use one call instead of vwpixel(x,y,poll):vwpixel(x,y,on)
 rem these calls are expensive, perhaps 7 per frame max - they provide bit access to the binary arrays of the virtual world matrix and it's camera table
390c if vwpixel(array1(0),array1(1)+1,flip)>0 then COLUBK=222:COLUP0=120
390c2 rem checkpoint 
390c4 rem checkpoint
572 rem if vwpixel(0,2,poll)>0 then COLUBK=255: pixel polling example (other args: on,flip)
750 rem if 9/3 > 3 then goto 750
765 rem if w & %11110000 = s^t then COLUP0=255
774 rem ------------------------------------------------
775 rem end game loop-----------------------------------
776 rem ------------------------------------------------
777 rem ---:the next comment line is used by the compiler
780 rem ---gameloop2 subroutine, runs every frame:    --
782 rem ------------------------------------------------
784 o=o+1: rem test code for gameloop2
785 if o=150 then read i(mazecolours,18):COLUP0=i:o=0
444 if joy1fire=1 and f=4 then COLUP1=255:for j=3 to 5:read i(Sprite1SCR,j):write i(Sprite0SCR,j):next j
788 if f<>8 then return
790 rem end gameloop2----------------------------------- 
800 rem ------------------------------------------------
805 rem ---:the next comment line is used by the compiler
810 rem ---KITCHENSINK subroutine, runs when scrollvirtualworldtoggle=1
812 rem ------------------------------------------------
817 if p=50 then gosub 20:p=0:rem call draw box routine in init
900 rem end KITCHENSINK---------------------------------
2000 rem Dynamic RAM array variables are initialised in data statements:
2010 data mazecolours 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19
2015 data samplearray 4,2,100,23,48,77,11,15,12
2020 data array1 1,2,3,4,5
2025 data array2 1,2,3,4,5
virtualworld
X.....................................X...X..X.........X..XXXX.....XXXXXX......XXXXXXXXXXXXX
.X.......XXXXX........................X...X..X.........X..X..X....X......X.....X..........XX
..XX.........X........................X...X...X...X...X...XXXXX..X..X..X..X....X...XXXXXXXXX
....X........X.........................X.X.....X.X.X.X....X....X.X........X....X............
.....X.......X..........................X.......X...X.....XXXXXX.X.X....X.X....XXXXXXXXXX...
......X......X..........XXXXXXXXXX...............................X..XXXX..X.............X...
.......X......X..................X................................X......X..............X...
........X.....X..................X.................................XXXXXX...............X...
.........X.....X.............X...X..............................................XXXXXXXXX...
..........X...X..............X...X..........................................................
...........X.X...............X...X..................................................XXXXXXXX
............X................X...XXXXXXXXX........................................XXX......X
X............................X....................................................X........X
.X.......XXXXX...............XXXXXXXXXXXXX........................................X...XXXXXX
..XX.........X........................................................................X.....
....X........X........................................................................X.....
.....X.......X........................................................................X.....
......X......X........................................................................X.....
.......X......X.......................................................................X.....
X.......X.....XXXX....................................................................XXXXXX

sprites
..XXX...
..XXX...
XXXXX.X.
..XX.XX.
XXXX.X..
X.XXX...
XX.XXX..
.XX.XXXX

.X.X.X..
..XXXX..
X..XX...
XXXXXX..
...XX.XX
...XX...
X.X..X.X
.X....X.

........
XX...XX.
XX...XX.
XXXXXXX.
XXXXXXX.
XX...XX.
XX...XX.
........

........
..XXX...
.X...X..
.XXXXX..
.X......
.X......
..XXXX..
........

........
.XX.....
.XX.....
.XX.....
.XX.....
.XXXXX..
.XXXXX..
........

........
.XX.....
.XX.....
.XX.....
.XX.....
.XXXXX..
.XXXXX..
........

........
..XXXX..
.XXX.XX.
.XX..XX.
.XX..XX.
.XX.XXX.
..XXXX..
........

........
........
........
........
........
........
........
........

........
X......X
X......X
X..XX..X
X..XX..X
X.X..X.X
XX....XX
........

........
..XXXX..
.XXX.XX.
.XX..XX.
.XX..XX.
.XX.XXX.
..XXXX..
........

........
.XXXX...
.XX..X..
.XXXX...
.XX.X...
.XX..X..
.XX..XXX
........

........
.XX.....
.XX.....
.XX.....
.XX.....
.XXXXX..
.XXXXX..
........

........
.XXXX...
.XX.XX..
.XX..XX.
.XX..XX.
.XX..XX.
.XXXXX..
........

chiptunes
7, 30, 0, 0, 8
7, 24, 0, 0, 8
7, 20, 0, 0, 8
12, 9, 12, 7, 22
12, 14, 12, 11, 22
6, 30, 0, 0, 20
6, 24, 6, 30, 10
6, 22, 6, 30, 10
6, 21, 6, 30, 20
12, 26, 12, 19, 20
0, 0, 0, 0, 254
0,0,0,0,0

15, 5, 15, 12, 10
15, 17, 15, 19, 10
15, 5, 15, 12, 10
15, 5, 15, 12, 15
0, 0, 0, 0, 255
29,23,5,10,0

7, 30, 0, 0, 68
7, 24, 0, 0, 8
7, 20, 0, 0, 8
7, 24, 0, 0, 7
30, 0, 0, 8, 7
24, 0, 0, 8, 7
20, 0, 0, 8, 7
24, 0, 0, 8, 7
22, 0, 0, 8, 20
0, 0, 0, 0, 255
27, 0, 0, 8, 15
24, 0, 0, 8, 17
24,24,24,24,0


