Displaying elements

An array of data (or any other data structure) is pretty much useless unless you do something with it. Here we'll make the subroutine DA that displays all the elements in the array.

First, make a new program called ASQUR and put this inside. All our routines will go in it.

PROGRAM:ASQUR
:..ROUTINES
Variables
  • L: current length of array (initialized to 0 at the beginning of the program and updated by the routine itself)
Input

None.

Output

None. It draws the sprites to the buffer, but that's pretty much it.

Code
:Lbl DA
:If L
:For(I,1,L)
:Pt-Change({I*4+L1-4→J},{J+1},Pic0)
:End
:Return

You probably don't need too much explaining, so I'll keep it short. First, there's If L. This is to avoid an (almost) infinite loop when L happens to be zero. (We loop from 1 to L and subtract four more instead of from L-1 to L-1 because it's a lot faster to check each pass of the loop.) Then the routine loops through every element, using Pt-Change( to display each one.

We use Pt-Change( here because it's the easiest to work with when you want to draw a moving object. It works like this: Just before the main program calls DispGraph, we draw all the squares onto the screen so they appear, and once the screen finishes displaying, all the squares get removed with the same routine to make it "clean" again. This makes things easier if you're making a complex tilemap game where redrawing the entire screen every frame takes too much time.

Anyway, now that you've defined the subroutine, you can compile your program now! And run it! what does it do?

Nothing. It waits until you press CLEAR. That's because you haven't added any elements to the array yet. We'll get there in the next step.

+ Tweet