I am trying to create a very simple feature where the player shoots
bullets vertically.
``#include "Core.hpp"
#include "Load.hpp"
#include "Create.hpp"
int main(int argc, char *argv[]){
Core core;
Load load;
Create* player;
Create* bullet;
//Create* enemy;
Create *enemy[10];
core.init("window", 0, 0, 1080, 720, false);
float p_x = 1080/2, p_y = 720 - 64 * 2;
int p_acc = 2;
player = new Create("../assets/player.png");
int i = 1;
int a_e_posx[10];
int a_e_posy[10];
for (i = 0; i<=10; i++) {
enemy[i] = new Create("../assets/enemy.png");
a_e_posx[i] = (rand() % 1080)+1;
a_e_posy[i] = (-rand()% 720 - 64 * 2)+1;
}
while (core.is_running) {
player -> update(p_x, p_y);
core.handle_events();
core.start_render();
player -> render();
int j = 1;
while(j <= 10){
int y = 1;
enemy[j] -> update(a_e_posx[j], a_e_posy[j]++);
y++;
enemy[j] -> render();
j++;
}
if (core.key[5] == true) {
bullet = new Create("../assets/bullet.png");
int bullet_posx = p_x;
int bullet_posy = p_y;
bullet_posy--;
bullet -> update(bullet_posx, bullet_posy);
bullet -> render();
std::cout << bullet_posy << std::endl;
}
core.clear_render();
if (core.key[4] == true){
p_x += 0.5 * p_acc;
}
if (core.key[3] == true)
p_x -= 0.5 * p_acc;
}
core.update();
return 0;
}`
`
This is how i tried to implement it,
but the bullet just stays in one place follows the player.
I am assuming this happens because the the bullet object is only rendered and updated once the key is pressed otherwise the loop just goes on.
So how can i implement a simple bullet which just goes straight up like the bullets in space invaders.
Thanks,
SmashTheState is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.