Shooting animation on 1.8 tft

I am trying to figure out how to change some code to shoot a second bullet shortly after the first one shoots but it shoots after the first bullet moves to the edge of the screen

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>

// Pin definitions
#define TFT_CS     10
#define TFT_RST    9
#define TFT_DC     8
#define BUTTON_PIN 2 // Connect your button to this pin and GND

// Initialize Adafruit ST7735 TFT library
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// Custom sprite (e.g., a jet)
#define SPRITE_WIDTH  16
#define SPRITE_HEIGHT 24
const unsigned int sprite[] PROGMEM = {
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x2945, 0x18c3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x7bcf, 0x6b6d, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x840f, 0x7bef, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x83ee, 0x83ee, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xad12, 0xad12, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x5acb, 0xb574, 0xb594, 0x52aa, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xbdd7, 0xad55, 0xad55, 0xbdf7, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xbdf7, 0xbdd7, 0xb5b6, 0xbdd7, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x52aa, 0xbdf7, 0xb596, 0xb5b6, 0xbdd7, 0x52aa, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x8c51, 0xbdf7, 0xbdf7, 0xc618, 0xc618, 0x7bef, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x2124, 0x9cd3, 0xce79, 0xc618, 0xbdf7, 0xbdf7, 0xce59, 0xce79, 0x94b2, 0x2104, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x5aeb, 0xbdf7, 0xce59, 0xd69a, 0xc618, 0xbdf7, 0xbdd7, 0xce59, 0xd69a, 0xd699, 0xbdd7, 0x5aeb, 0x0000, 0x0000, 
  0x0000, 0x738e, 0xc638, 0xce79, 0xd69a, 0xce79, 0xce59, 0xbdd7, 0xbdd7, 0xce79, 0xd69a, 0xd69a, 0xce79, 0xce59, 0x6b6d, 0x0000, 
  0x0000, 0x8c30, 0xce79, 0xce79, 0xce79, 0xce79, 0xce79, 0xbdd7, 0xbdd7, 0xce79, 0xce79, 0xce79, 0xce79, 0xce79, 0x7c4f, 0x0000, 
  0x0000, 0x4208, 0x8c71, 0xad55, 0xc618, 0xc638, 0xbdd7, 0xbdd7, 0xb5b6, 0xbdf7, 0xce59, 0xc618, 0xad55, 0x8c71, 0x39e7, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x632c, 0xb596, 0xb5b6, 0xb5b6, 0xa534, 0x632c, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x7bcf, 0x9492, 0xad75, 0x8451, 0x8c72, 0xa514, 0x9492, 0x73ae, 0x0000, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x8410, 0xd69a, 0xb596, 0xce59, 0x1904, 0x2124, 0xce79, 0xad75, 0xd69a, 0x7bef, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x5acb, 0x9cd3, 0xb5b6, 0xc618, 0x0000, 0x0000, 0xc618, 0xb5b6, 0x9cd3, 0x5acb, 0x0000, 0x0000, 0x0000, 
  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
};

// Custom "bullet" bitmap
#define BULLET_WIDTH  1
#define BULLET_HEIGHT 3
const unsigned int bullet[] PROGMEM = {
  0xFFFF,
  0xFFFF,
  0xFFFF
};

// Custom "bullet" bitmap2
#define BULLET_WIDTH2  1
#define BULLET_HEIGHT2 3
const unsigned int bullet2[] PROGMEM = {
  0xFFFF,
  0xFFFF,
  0xFFFF
};


void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Initialize button pin as input with pull-up
  tft.initR(INITR_BLACKTAB);         // Initialize display
  tft.fillScreen(ST7735_BLACK);      // Clear display
}

void loop() {
  static int spriteX = tft.width() / 2 - SPRITE_WIDTH / 2;
  static int spriteY = tft.height() - SPRITE_HEIGHT - 10;

  // Draw the sprite
  tft.drawRGBBitmap(spriteX, spriteY, sprite, SPRITE_WIDTH, SPRITE_HEIGHT);

  while (digitalRead(BUTTON_PIN) == HIGH) { // Button is pressed
    shoot(spriteX + SPRITE_WIDTH / 1.3 - BULLET_WIDTH / 2, spriteY + 5);
    shoot2(spriteX + SPRITE_WIDTH / 1.3 / 2 / 2 - BULLET_WIDTH2 / 2, spriteY + 5);
  }
}

void shoot(int x, int y) {
  while (y > 0) {
    // Draw the bullet moving up
    tft.drawRGBBitmap(x, y, bullet, BULLET_WIDTH, BULLET_HEIGHT);
    delay(50);
    // Clear the bullet's previous position
    tft.fillRect(x, y, BULLET_WIDTH, BULLET_HEIGHT, ST7735_BLACK);
    y -= 5; // Move the bullet up
  }
}   
  

void shoot2(int x2, int y2) {
  while (y2 > 0) {
    // Draw the bullet moving up
    tft.drawRGBBitmap(x2, y2, bullet2, BULLET_WIDTH2, BULLET_HEIGHT2);
    delay(50);
    // Clear the bullet's previous position
    tft.fillRect(x2, y2, BULLET_WIDTH2, BULLET_HEIGHT2, ST7735_BLACK);
    y2 -= 5; // Move the bullet up
  }
}

shoot() is a blocking function, and returns when its task is completed, so the program is working as expected.

To learn how to write non-blocking code, study the Blink Without Delay tutorial.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.