Double duty of variables can be helpful, too.
In my version of
TRON lightcycles, I also use the D() array with all directions (1, -22, -1, 22), but additionally the indices C and H access the shape (like in '59+C') so the character faces the correct direction. This is the inner loop of the game:
Code: Select all
9 GETA$
10 IFA$="C"THENC=0
11 IFA$="{F1}"THENC=1
12 IFA$="Z"THENC=2
13 IFA$="{F7}"THENC=3
14 POKEV+11,140:POKEB,58:POKEB+CO,6:B=B+D(C):IFPEEK(B)<>219THENLI=LI-1:GOTO20
15 POKEB,59+C:SC=SC+1:NL=NL-1:PRINT"{HOME,BLK,7 RIGHT,RVS ON}"SC:POKEV+11,0
16 IFNL<0THENNL=NL+1000:LI=LI+1:GOSUB40
17 IFPEEK(G+D(H))<>219ORRND(1)<.1THENI=C:GOSUB41
18 POKEG,58:POKEG+CO,5:G=G+D(H):IFPEEK(G)<>219THENSC=SC+100:NL=NL-100:B=G:GOTO20
19 POKEG,59+H:GOTO9
It is executed ~8 times a second, reading the keyboard, moving 2 lightcycles, leaving the trails behind, updating the score and checking collisions for both cycles. Some 'extraordinary' conditions (extra live awarded, enemy lightcycle shall turn) are handled in sub-routines, and are barely noticible, when done.
ravenxau wrote:line 2 increments and resets the object counter in a single statement.
Code: Select all
1 x=d(rnd(.)*4):pokea(c),32:a(c)=a(c)-x*(peek(a(c)+x)=32)
2 pokea(c),c:c=c+1+10*(c=9):goto1
I presume a FOR..NEXT loop would work quite as fast:
Code: Select all
1 forc=0to9:x=d(rnd(.)*4):pokea(c),32:a(c)=a(c)-x*(peek(a(c)+x)=32)
2 pokea(c),c:next:goto1
if-then statements, which i find are a game programmers nemesis when it comes to fast execution in basic
Is there
really a noticable slow-down here? Of course, the next statement after IF..THEN now forcibly must go into a new line. I even can rewrite this using a temporary address variable (replacing x), which removes unnecessary calculations - and the characters don't "blink" when they can't move:
Code: Select all
1 forc=0to9:t=a(c)+d(rnd(.)*4):ifpeek(t)=32thenpokea(c),32:a(c)=t:pokea(c),c
2 next:goto1
This might also be a good routine for a large "Boss" character at the end of a level. just move six or eight characters as a uniform group.
There also is the mighty
PRINT statement, which is eminently useful in moving large objects. You can frame the object which spaces, so leftover characters on one side are always deleted:
Code: Select all
PRINT"{5 SPACE,DOWN,5 LEFT,SPACE}ABC{SPACE,DOWN,5 LEFT,SPACE}DEF{SPACE,DOWN,5 LEFT,SPACE}GHI{SPACE,DOWN,5 LEFT,5 SPACE}"
This technique is used in 'Tank-V-Ufo', for example. And the positioning can be done in pure BASIC as follows:
Code: Select all
Q$="{HOME,22 DOWN}" (<- assigned once to during initialising)
PRINTLEFT$(Q$,Y)TAB(X)"..."
Greetings,
Michael