Jump to content

Photo

Horizontal Level Scrolling in Batari BASIC Tutorial : LESSON 2


No replies to this topic

#1 Sprybug OFFLINE  

Sprybug

    Dragonstomper

  • 568 posts

Posted Thu Feb 7, 2019 2:37 AM

Now that you've made your level in a text editor and ran the TXT2LEV program to covert it to a DAT file, we're ready for step 2.

 

First, open up the attached file at the bottom in Batari BASIC.  This is a program you can use to test out your level with and scroll through

Second, open that DAT file you created, with your favorite text editor.  I just use regular notepad for this.

Third, Copy all of the contents (Select All, then Copy).

Fourth, go to the bottom of the program in Visual bB where the Level1 Data Table is and replace the data table there (from Princess Rescue Level 1A), with the data from the DAT file you copied.

Fifth, Be sure you indent each line in the table with a space, or else it will error on compile!

Sixth, Save, Compile, and Run!  Use left and right to scroll through your level.

 

If you don't replace the data, you can still run the program with the data that is in there.  It's level 1A from Princess Rescue.

 

Alright, so what's going on here?  Let's examine.

I won't go into kernel stuff as you probably already know about that.

I want to first mention that there is a known bug in bB, where, unless you use COLUPF in the game loop, you must use your pfcolor data table in the game loop, or the first line will not be the correct color.  That is why the pfcolor tables are in loops.  If you're not using pfcolors, then you don't need to worry about it.

 rem scroll level in
 Level_Pointer=0
PopulateScreen
 rem make pfcolors black so we don't see the start of the level scroll in
  pfcolors:
 $00
 $00
 $00
 $00
 $00
 $00
 $00
 $00
 $00
 $00
 $00
end
  gosub scrollleft
  drawscreen
 if Level_Pointer<32 then PopulateScreen

Here we black out the screen so we don't see the contents of the level get scrolled on at the beginning.  We use the scrollleft routine to get the start of the level on there, instead of writing a whole new routine to do it in one blow.  Save ROM space by doing so.  Once that pointer hits beyond 31 (the last column on the right of the screen), then it advances to the main loop so we can scroll through the level with our joystick.

GameLoop
 rem Now colorize and show
  pfcolors:
 $84
 $86
 $86
 $88
 $88
 $8A
 $8A
 $8C
 $8C
 $8E
 $8E
end
 if joy0right && Level_Pointer<128 then gosub scrollleft
 if joy0left && Level_Pointer>32 then gosub scrollright
 drawscreen
 goto GameLoop

Alright, so now we colorize all the rows so you can see it, and you can set these to whatever colors you want.  You don't have to keep them the way I did.  This part polls the joystick for left or right movement.  Left will scroll the level left and right will scroll the level right.  The additional conditional check of the Level_Pointer variable is there so you don't scroll BEYOND the data table.

scrollleft
 pfscroll left
 x=Level_Pointer*2
 y=Level1[x]
 x=x+1
 z=Level1[x]
 if y{0} then var3 = var3 | 128 else var3 = var3 & 127
 if y{1} then var7 = var7 | 128 else var7 = var7 & 127
 if y{2} then var11 = var11 | 128 else var11 = var11 & 127
 if y{3} then var15 = var15 | 128 else var15 = var15 & 127
 if y{4} then var19 = var19 | 128 else var19 = var19 & 127
 if y{5} then var23 = var23 | 128 else var23 = var23 & 127
 if y{6} then var27 = var27 | 128 else var27 = var27 & 127
 if y{7} then var31 = var31 | 128 else var31 = var31 & 127
 if z{0} then var35 = var35 | 128 else var35 = var35 & 127
 if z{1} then var39 = var39 | 128 else var39 = var39 & 127
 if z{2} then var43 = var43 | 128 else var43 = var43 & 127
 Level_Pointer=Level_Pointer+1
 return

scrollright
 pfscroll right
 x=(Level_Pointer-32)*2
 y=Level1[x]
 x=x+1
 z=Level1[x]
 if y{0} then var0 = var0 | 128 else var0 = var0 & 127
 if y{1} then var4 = var4 | 128 else var4 = var4 & 127
 if y{2} then var8 = var8 | 128 else var8 = var8 & 127
 if y{3} then var12 = var12 | 128 else var12 = var12 & 127
 if y{4} then var16 = var16 | 128 else var16 = var16 & 127
 if y{5} then var20 = var20 | 128 else var20 = var20 & 127
 if y{6} then var24=var24 | 128 else var24=var24 & 127
 if y{7} then var28 = var28 | 128 else var28 = var28 & 127
 if z{0} then var32 = var32 | 128 else var32 = var32 & 127
 if z{1} then var36 = var36 | 128 else var36 = var36 & 127
 if z{2} then var40 = var40 | 128 else var40 = var40 & 127
 Level_Pointer=Level_Pointer-1
 return

Now this is the heart of the beast.  These are the two routines that will scroll the level left or right.  Let's break it down.

In batari BASIC, the standard kernel uses 48 bytes of RAM as screen memory.  It is labeled from Var0 - Var47 (Var44-Var47 are in a non-visible 12th row).  Each variable of course is 4 bytes across (32 bits).  So, for example, the first screen playfield row is going to consist of var0, var1, var2, and var3.  The 2nd will consist of var4, var5, var6, var7, and so on.  Now that we know this, we can check for the edges of the screen using these variables, instead of the time consuming batari BASIC commands and functions that will do this for us.  This helps save on cpu cycle time!

 

Whenever you use the command pfscroll (left or right), you'll notice that it scrolls the screen like it's suppose to, but it ends up wrapping the old data on the other side, giving you an endless loop.  But we don't want that, so we need to replace the edges ourselves, manually.  After the screen scrolls, what the rest of this routine does is, read the 2 bytes from the data table and then checks bit by bit of those 2 variables to see if the new screen data is going to have the playfield bit on or off.  It then sets the bit in the screen memory appropriately with the var screen variables on the edges.  After each row is checked and altered, we advance (or subtract) the level pointer counter and return to the main game loop.

 

Pretty simple, huh?  It actually is!  Now, you'll notice that we didn't do anything about those spawn in objects that we put in our level on line 12 of our text file.  We'll tackle that later, so don't worry about it at the moment.  Right now we're just scrolling through our first made level and making sure it looks good!

 

Attached File  Screen_Scroll_Lesson2.bas   3.24KB   13 downloads


Edited by Sprybug, Thu Feb 7, 2019 2:41 AM.





0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users