Advice on general approaches or feasibility and discussions about game design
Post a reply

Looping bullet problem - Robotron-style game

Fri Jun 05, 2015 10:13 am

I'm having a bit of a problem and I'm not sure how to go about it. I have a single bullet which is supposed to basically stop and reset every time it leaves the screen. As you can see from the .gif below, this works fine when it passes LCDWIDTH or LCDHEIGHT, but seems to make some weird looping problems when it becomes < 0.

Image

I'm not sure what the problem could be, here is the section of code I used, perhaps it's some trick with the LCD but I'm not sure:

Code:
void UpdateBullet()
{
   if(bullet.fired)
   {
      switch(bullet.direction)
      {
         case 1:
            bullet.y -= bullet.vy;
            if(bullet.y < 0)
            {
               bullet.fired = false;
            }
            break;
         case 2:
            bullet.x += bullet.vx;
            if(bullet.x > LCDWIDTH)
            {
               bullet.fired = false;
            }
            break;
         case 3:
            bullet.y += bullet.vy;
            if(bullet.y > LCDHEIGHT)
            {
               bullet.fired = false;
            }
            break;
         case 4:
            bullet.x -= bullet.vx;
            if(bullet.x < 0)
            {
               bullet.fired = false;
            }
            break;
      }
      DrawBullet();
   }   
}


To elaborate on the directions:
1 = up
2 = right
3 = down
4 = left

This was working in Pico-8 but it doesn't seem to want to do it here, any ideas?

Re: Looping bullet problem - Robotron-style game

Fri Jun 05, 2015 10:21 am

As what is bullet.y / bullet.x defined? If they are defined as byte then 0 - 1 = 255, meaning the number will never be less than zero. You could overgo that by removing the bullet if it is greater than the screen width/height respectively, as that means that the number looped around.

Re: Looping bullet problem - Robotron-style game

Fri Jun 05, 2015 10:28 am

Oh man, so that was the problem! As you can imagine I've not used bytes before, I assumed they could be negative. Changed the definition as int instead of byte and it worked. Is there a better variable I should be using or is that right for this situation?

Re: Looping bullet problem - Robotron-style game

Fri Jun 05, 2015 10:49 am

That is a very bad idea as int is two bytes long, and cannot be hold in a single register, causing a lot of slowdown. If you need to have a one-byte long signed variable use the type "char" but i'd suggest just sticking with byte and checking, instead of if it is less than zero, for if it is greater than screen width / screen height.

Re: Looping bullet problem - Robotron-style game

Fri Jun 05, 2015 10:51 am

You can use int8_t the value are valide between -128 to 127

Re: Looping bullet problem - Robotron-style game

Fri Jun 05, 2015 11:21 am

That's perfect, thanks for the help guys!
Post a reply