ScumSoft, on Mon May 30, 2011 5:45 PM, said:
I take it this is for reversing the direction of a ball when it collides with a wall or the like?
Yeah, I forgot to answer that. It's for bouncing off the edges of the playfield.
RevEng, on Mon May 30, 2011 8:52 PM, said:
As you say, there's hardly a difference between the solutions if you're running them only a few times. In that case the solution that's the most readable and maintainable in bB should be the winner, so the prize goes to RT!

It will be running constantly in the example program. This is a condensed version of what I originally had:
dim B_Direction_x = h
dim B_Direction_y = i
ballx = (rand&127) + 8 : bally = 9 : rem * Starting position of ball.
CTRLPF = $11 : ballheight = 2 : rem * Defines ball size.
B_Direction_x = 255 + (rand&2) : B_Direction_y = 1 : rem * Ball starting direction.
Main_Loop
rem ****************************************************************
rem *
rem * Bounce the ball if it hits the edge of the screen.
rem *
rem '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
rem '
if ballx < 3 || ballx > 158 then B_Direction_x = -B_Direction_x
if bally < 3 || bally > 88 then B_Direction_y = -B_Direction_y
rem ****************************************************************
rem *
rem * Move the ball.
rem *
rem '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
rem '
ballx = ballx + B_Direction_x : bally = bally + B_Direction_y
drawscreen
goto Main_Loop
Seems like I've done code like that a billion times on the VIC-20 and the Commodore 64, but then it hit me that I was wasting 2 precious variables when I could do the same thing with 2 bits. So here's the condensed version of what I changed it to:
rem * other bit variable names removed since they are not needed for this post
dim B_Direction_x = t
dim B_Direction_y = t
ballx = (rand&127) + 8 : bally = 9 : rem * Starting position of ball.
CTRLPF = $11 : ballheight = 2 : rem * Defines ball size.
temp5 = (rand & %00100000) : B_Direction_x = B_Direction_x ^ temp5 : rem * Ball starting direction.
B_Direction_y{6} = 1 : rem * Ball starting direction.
Main_Loop
rem ****************************************************************
rem *
rem * Bounce the ball if it hits the edge of the screen.
rem *
rem '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
rem '
if ballx < 3 || ballx > 158 then B_Direction_x = B_Direction_x ^ %00100000
if bally < 3 || bally > 88 then B_Direction_y = B_Direction_y ^ %01000000
rem ****************************************************************
rem *
rem * Move the ball.
rem *
rem '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
rem '
temp5 = 255 : if B_Direction_x{5} then temp5 = 1
ballx = ballx + temp5
temp5 = 255 : if B_Direction_y{6} then temp5 = 1
bally = bally + temp5
drawscreen
goto Main_Loop
Since you said it's readable and maintainable and the speed isn't that bad, I guess I'll leave it the way it is.
SeaGtGruff, on Mon May 30, 2011 9:25 PM, said:
Use temp5 = temp5 ^ %11111110 (or temp5 = temp5 ^ 254). Assuming you've initialized temp5 to 1, this will toggle it between 1 and 255 (-1).
Michael
Edit: Or you could initialize temp5 to 255.
Edit #2: I'm not sure if my answer helps with your specific problem. I was answering the question "How do I toggle a variable between 1 and 255," which isn't exactly the question you were asking.
Yeah, I don't need that for this example program, but I should add that to the bB page.
Thanks.
Edited by Random Terrain, Tue May 31, 2011 3:57 AM.