Hi!
I would like to share this scene with the community. I have a Teletype since twenty days ago, so it’s the first scene I code. It’s based on pong scene by @scanner_darkly but it has a more “complex” collision logic. It’s incomplete because I need more available lines of code. Or maybe someone could tell me how to code the same functionality with less lines of code.
I:
P.N 0
Z RAND 127
D + 1 RAND 3
G.BTX 0 0 0 1 1 1 0 0 16 8
Z is the current position. Since Grid has 128 buttons, Z is a number between 0 and 127. At the begining it’s a random number between those boundaries.
D is the direction of the movement. It’s represented by a number between 1 and 4:
M:
M SCALE 0 V 10 V 1 50 PRM
P 4 Z; X % Z 16; Y / Z 16
G.CLR; G.LED X Y 15
P 0 G.BTN.V - Z 17
P 1 G.BTN.V - Z 16
SCRIPT 8
Index 4 of pattern 0 stores current position. Indexes 0, 1, 2, 3, 5, 6, 7, 8 are destined to store potential button presses:
X and Y are the coordinates of Z. Since Z = X + 16Y then X = Z mod 16 and Y = Z div 16.
This picture may help to understand what’s going on M, script 8 and script 7:
SCRIPT 8:
P 2 G.BTN.V - Z 15
P 3 G.BTN.V - Z 1
P 5 G.BTN.V + Z 1
P 6 G.BTN.V + Z 15
P 7 G.BTN.V + Z 16
SCRIPT 7
SCRIPT 7:
P 8 G.BTN.V + Z 17
IF EQ D 1: SCRIPT 1
IF EQ D 2: SCRIPT 2
IF EQ D 3: SCRIPT 3
IF EQ D 4: SCRIPT 4
SCRIPT 6
Scripts 1, 2, 3 and 4 are the ones in charge of collision logic. Since we can have 4 directions of movement, we have 4 collision logic scripts.
SCRIPT 1:
IF && P 7 P 5: D 4; SCRIPT 5
IF P 8: D 4; SCRIPT 5
IF && P 7 ! P 5: D 3; SCRIPT 5
IF && P 5 ! P 7: D 2; SCRIPT 5
IF EQ Z 127: D 4; SCRIPT 5
ELIF EQ Y 7: D 3; SCRIPT 5
Here we check if there is a collindant pressed button. We may also update direction. We also check if current position is on the Grid corners. But one case left:
I need one extra line of code:
ELIF EQ X 15: D 2; SCRIPT 5
This pictures may help to understand collision logic:
Scripts 2, 3 and 4 are similar to script 1.
SCRIPT 2:
IF && P 7 P 3: D 3; SCRIPT 5
IF P 6: D 3; SCRIPT 5
IF && P 7 ! P 3: D 4; SCRIPT 5
IF && P 3 ! P 7: D 1; SCRIPT 5
IF EQ Z 112: D 3; SCRIPT 5
ELIF EQ Y 7: D 4; SCRIPT 5
In this case I need this extra line of code:
ELIF EQ X 0: D 1; SCRIPT 5
SCRIPT 3:
IF && P 1 P 5: D 2; SCRIPT 5
IF P 2: D 2; SCRIPT 5
IF && P 1 ! P 5: D 1; SCRIPT 5
IF && P 5 ! P 1: D 4; SCRIPT 5
IF EQ Z 15: D 2; SCRIPT 5
ELIF EQ Y 0: D 1; SCRIPT 5
In this case I need this extra line of code:
ELIF EQ X 15: D 4; SCRIPT 5
SCRIPT 4:
IF && P 1 P 3: D 1; SCRIPT 5
IF P 0: D 1; SCRIPT 5
IF && P 1 ! P 3: D 2; SCRIPT 5
IF && P 3 ! P 1: D 3; SCRIPT 5
IF EQ Z 0: D 1; SCRIPT 5
ELIF EQ Y 0: D 2; SCRIPT 5
In this case I need this extra line of code:
ELIF EQ X 0: D 3; SCRIPT 5
Script 5 is a simple one. If script 5 is triggered it means there is a collision.
SCRIPT 5:
TR.P 1 1
Script 6 is where Z (current position) is updated:
IF EQ D 1: Z + Z 17
IF EQ D 2: Z + Z 15
IF EQ D 3: Z - Z 15
IF EQ D 4: Z - Z 17
(I don’t have a USB drive, so I’ve retyped code manually. May be errors…)