Jump to content
IGNORED

Squeezing The Cycles


MausGames

Recommended Posts

I'm starting this thread as a place to ask questions about cycle-conscious coding methodology.

 

My first question:

 

What is the best way to check for zero joystick activity?

If !joy0fire && !joy0up && !joy0down && !joy0left && !joy0right then...

Won't even compile.

 

I can do:

If !joy0up && !joy0down then a = 1
if a=1 && !joy0left && !joy0right then a = 2
if a = 2 && !joy0fire then a = 3
if a = 3 then...

But it seems like there would be a much simpler way. Also, if the above example were changed to:

If !joy0up && !joy0down then a = 1
if !joy0left && !joy0right && a = 1 then a = 2
if !joy0fire && a = 2 then a = 3
if a = 3 then...

would that make any difference in cycles used? Is there any simple way to figure out which is the best order to put multiple conditionals in an if/then statement, or does it not matter?

Edited by MausGames
Link to comment
Share on other sites

What is the best way to check for zero joystick activity?

If !joy0fire && !joy0up && !joy0down && !joy0left && !joy0right then...

Won't even compile.

 

There might be some other problem Maus. I do the above all the time, particularly when I want to store a joystick direction so that I can commit an action after the joystick is released. I've never had a problem with it.

 

EDT: I missed the "joy0fire" part.

Edited by jrok
Link to comment
Share on other sites

Agreed. Reading the bB page it looks as if that should be possible.

 

Actually, I might have a use for it and I've never done it before. Thanks for bringing it to my attention guys.

 

What is the best way to check for zero joystick activity?

If !joy0fire && !joy0up && !joy0down && !joy0left && !joy0right then...

Won't even compile.

 

There might be some other problem Maus. I do the above all the time, particularly when I want to store a joystick direction so that I can commit an action after the joystick is released. I've never had a problem with it.

Link to comment
Share on other sites

  if SWCHA>=240 then ...

 

This is correct, but you did not include the check of the firebutton...

 

   if (SWCHA>=240) && (!joy0fire) then

 

If the reason this works is not clear. It is because the bits that indicate a joystick direction are bits 4-7 in the memory location labeled SWCHA. So the bBasic code joy0right is the same as code SWCHA{7}=0, joy0left == SWCHA{6}=0, joy0down == SWCHA{5}=0, and joy0up == SWCHA{4}=0 The bits 4-7 if they were used to represent a number would equal 16+32+64+128 = 240. The bits are set to zero if the joystick is pushed in that direction. So if the joystick is cnentered all 4 bits are set equalling 240. If the joystick is pressed in any direction that bit will be zero and the value in SWCHA will be less than 240. The joystick firebutton is from a different register than SWCHA.

 

 

Personally I think to be quickest in your code you need to get rid of if-then statements for joysticks all together. You can use the ON GOTO statement to jump directly to the correct code for each legal and illegal joystick position. Here is a snippet from my shooter game:

	if joy0fire then Fire

temp1 = SWCHA / 16
rem	RLDU = 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
on temp1 goto Stnd Stnd Stnd Stnd Stnd GoRD GoRU GoR  Stnd GoLD GoLU GoL  Stnd GoD  GoU  Stnd

Fire
rem -- Player shoots
if playerShotYpos then ShotBusy
playerShotXpos = playerXpos + 2
playerShotYpos = 21
ShotBusy	
player0x = playerXpos
REFP0 = playerStatus
goto PlayerKneel

Stnd	
rem -- Player stands still.
player0x = playerXpos
playerStatus{0} = 0
REFP0 = playerStatus
goto PlayerStand

GoD 
GoU
rem -- Player kneels and shows shield if avalable.
player0x = playerXpos
REFP0 = playerStatus
if shieldStrength then playerStatus{0} = 1 : goto PlayerKneel
playerStatus{0} = 0
goto PlayerStand

GoR
GoRD
GoRU
rem -- Move player to Right.
if playerXpos < 136 then playerXpos = playerXpos + 1

player0x = playerXpos
playerStatus{3} = 0
playerStatus{0} = 0
REFP0 = playerStatus
goto AnimatePlayer

GoL
GoLD
GoLU
rem -- Move player to Left.
if playerXpos > 16 then playerXpos = playerXpos - 1

player0x = playerXpos
playerStatus{3} = 1
playerStatus{0} = 0
REFP0 = playerStatus

goto AnimatePlayer

Link to comment
Share on other sites

Personally I think to be quickest in your code you need to get rid of if-then statements for joysticks all together. You can use the ON GOTO statement to jump directly to the correct code for each legal and illegal joystick position.

Pretty cool. So is that a lot faster?

 

I also have a question about this:

 

   on temp1 goto Stnd Stnd Stnd Stnd Stnd GoRD GoRU GoR  Stnd GoLD GoLU GoL  Stnd GoD  GoU  Stnd

I'm guessing GoRD means "go right-down," GoL means "go left" and so on, but what does Stnd mean? Is it simply short for "stand"?

 

 

Thanks.

Link to comment
Share on other sites

Personally I think to be quickest in your code you need to get rid of if-then statements for joysticks all together. You can use the ON GOTO statement to jump directly to the correct code for each legal and illegal joystick position.

Pretty cool. So is that a lot faster?

 

It is very fast, and more importantly it always takes the same short amount of time. Whereas a long list of if-then statements takes an variable amount of time to complete.

 

I also have a question about this:

 

   on temp1 goto Stnd Stnd Stnd Stnd Stnd GoRD GoRU GoR  Stnd GoLD GoLU GoL  Stnd GoD  GoU  Stnd

I'm guessing GoRD means "go right-down," GoL means "go left" and so on, but what does Stnd mean? Is it simply short for "stand"?

You are correct. Note there are 4 bits one for each direction of the joystick. Those 4 bits make 16 possible combinations. Only 9 of those combinations are valid joystick positions. The other seven are illegal positions like Left and Right at the same time. My example code treats the illegal positions the same as if the joystick is centered which is when the player stands.

Link to comment
Share on other sites

It is very fast, and more importantly it always takes the same short amount of time. Whereas a long list of if-then statements takes an variable amount of time to complete.

 

. . . Note there are 4 bits one for each direction of the joystick. Those 4 bits make 16 possible combinations. Only 9 of those combinations are valid joystick positions. The other seven are illegal positions like Left and Right at the same time. My example code treats the illegal positions the same as if the joystick is centered which is when the player stands.

Thanks. Is it OK with you if I add an adapted version of this info to the bB page?

Link to comment
Share on other sites

It is very fast, and more importantly it always takes the same short amount of time. Whereas a long list of if-then statements takes an variable amount of time to complete.

 

. . . Note there are 4 bits one for each direction of the joystick. Those 4 bits make 16 possible combinations. Only 9 of those combinations are valid joystick positions. The other seven are illegal positions like Left and Right at the same time. My example code treats the illegal positions the same as if the joystick is centered which is when the player stands.

Thanks. Is it OK with you if I add an adapted version of this info to the bB page?

 

Sure, information is free. I will be happy to review it for correctness when you are done.

 

Cheers

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...