Need help programming a shoot command

Hello, I’m working on a simple game where you are a cowboy and you have to shoot targets. I’ve got animation, and movement down, but I just started working on shooting and am stuck. I have the input, and something fires, however it is not the bullet sprite, it’s a huge mess of random scraps of sprites. I’ve tried a few different ways to shoot and none of them have worked so I’m asking for help here. Thank you for your time! Here’s a github with all my code https://github.com/GerdGames/CowBro

The problem is in this line of code

ardu.drawBitmap(bulletX + count, bulletY, bulletSprite, bulletX + 8 + count, bulletY + 8, WHITE);

Compare that to the prototype of the function in the Arduboy library.

/// Draws a bitmap from program memory to a specific X/Y
void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t w, uint8_t h, uint8_t color);

‘w’ and ‘h’ should be constants referring to the actual size of the bullet sprite (in pixels). You have the bulletX, bulletY and count playing into those two variables which will change the way the function draws the sprite giving back garbage.

2 Likes

Thanks! That worked out well! However now I still get a beam of bullets instead of one at a time, I tried a boolean to stop creating more bullets if one is already active, but it still creates a beam of them. I updated my github with the changes and some comments to make it easier to read.

1 Like

You’re getting a beam because your bullet function is a loop. That loop will run all the way through before the next frame so you’re drawing the bullets stacked in a row every frame. It might be helpful to look at how the projectiles are handled in Sirene or Trolly Fish (both TEAM arg games and available at the website). If you want to be able to have multiple bullets at once than they need to be in an array.

This link might be helpful http://stackoverflow.com/questions/6810656/creating-an-array-of-structs-in-c or this one http://www.cppgameprogramming.com/cgi/nav.cgi?page=arrayclasses

Speaking of sprites can they have behaviour?

Thanks @Gaveno I’ll have to give that a try. @Cody17, not like game maker or something like that, my current plan for hitboxes is to see if the x and y values for the target and bullet are the same.

I’m sorry, that code is just too dense, I can’t find anything helpful or figure out how it works. Currently my bullet doesn’t move at all, but only one comes out so that’s good. Also it’s my intention to only have one bullet at a time so that being accurate is rewarded more.

Ok it’s just I thought you could make bullet behaviour . And I don’t know how to program

You don’t need a loop for the bullet behavior. If you only need one bullet at a time you really only need 3 variables. bulletX, bulletY, and bulletActive. If the bullet is not active (false) and the shoot button is pressed it sets the x and y components of the bullet to wherever the bullet originates from and sets active to true, and then in the main arduboy loop method you update the x and y components as necessary. Once it gets out of the screen or collides with something you set active to false.

In the main loop you also draw the bullet one time at the current x and y conditions if it is active.

2 Likes

Thank you so much, that worked out great!

1 Like