Jump to content
Sign in to follow this  
Xan

Collision function for multi sprites

Recommended Posts

I used this example to distinguish which item is being hit by the player, I tried to be economical

 set kernel multisprite

 player0x =  10: player0y = 88
 player1x =  80: player1y = 78
 player2x =  30: player2y = 65	
 player3x =  90: player3y = 52
 player4x = 110: player4y = 39	
 player5x = 130: player5y = 26

 player0:
 %01111110
 %10000011
 %10110011
 %10101011
 %10101011
 %10101011
 %10011011
 %10000011
 %10101011
 %11010110
 %01111100
end

 player1:
 %01101111
 %10000011
 %10111011
 %10010011
 %10010011
 %10110011
 %10010011
 %10000011
 %10101011
 %11000110
 %01101100
end

 player2:
 %01101111
 %10000011
 %10111011
 %10100011
 %10111011
 %10001011
 %10111011
 %10000011
 %10101011
 %11000110
 %01101100
end
 
 player3:
 %01101111
 %10000011
 %10110011
 %10001011
 %10011011
 %10001011
 %10110011
 %10000011
 %10101011
 %11000110
 %01101100
end

 player4:
 %01101111
 %10000011
 %10001011
 %10001011
 %10111011
 %10101011
 %10101011
 %10000011
 %10101011
 %11000110
 %01101100
end

 player5:
 %01101111
 %10000011
 %10111011
 %10001011
 %10111011
 %10100011
 %10111011
 %10000011
 %10101011
 %11000110
 %01101100
end

  COLUBK = 56
 _COLUP1 = 18: rem coloring player1 _  
  COLUP2 = 25
  COLUP3 = 10
  COLUP4 = 45
  COLUP5 = 60

loop
 COLUP0 = 155: rem color player0 must be updated 

control
 if joy0up    && player0y <  82 then player0y = player0y + 1
 if joy0down  && player0y >  29 then player0y = player0y - 1
 if joy0left  && player0x >  16 then player0x = player0x - 1
 if joy0right && player0x < 136 then player0x = player0x + 1

colidir
 y = colide(player0x, player1x, player0y, player1y)
 if y then player1x = (rand&127)+20

 y = colide(player0x, player2x, player0y, player2y)
 if y then player2x = (rand&127)+20

 y = colide(player0x, player3x, player0y, player3y)
 if y then player3x = (rand&127)+20

 y = colide(player0x, player4x, player0y, player4y)
 if y then player4x = (rand&127)+20

 y = colide(player0x, player5x, player0y, player5y)
 if y then player5x = (rand&127)+20

 drawscreen
 goto loop

 function colide
  y = temp1 + 9: x = temp2 - 7
  if y > x && temp1 < temp2 then y = temp3 -13: x = temp4 -10: if temp3 > x && y < temp4 then return 1
  return 0

  rem p0 width  9,  p0 height -13
  rem px width -7,  px height -10





 

Hit_Multi_Sprite.bin

  • Like 2

Share this post


Link to post
Share on other sites
On 2/19/2021 at 6:47 AM, Gemintronic said:

I know gosubs have overhead.. what about functions?

 

Functions are gosubs with additional overhead

They always pass two parameters whether you specify them or not (but bB is likely to complain if you don't pass any parameters)

You can sometimes sort of use them in place of an operand or an expression

But I'd just use a subroutine.  More predicable.

Functions are so finicky they're almost (but not quite) useless

 

Subroutines have overhead, but not much.

A gosub uses 12 cycles and two bytes of the stack

a = b + c uses 11 cycles

 

Depending on the kernel, the stack might be only 6 bytes so you could only go 3 levels deep provided

you don't use any expressions that use stack (and I think drawscreen goes 3 deep)

And the way bB uses the stack in expressions is all kinds of goofy  

  • Like 1
  • Thanks 1

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...