SFML help

I have this game i am working on and the bullet on screen is having a small error. It shoots one bullet at a time, and when the ships y coordinates are over 200 it shoots fine. When they are under 200 it jumps and shoots from there. If anyone can check out my code or test it themselves and give me any words of advice that would be very helpful thank you. Comment out the background image and just draw the shapes if needed =) i was trying to debug this problem but it has gotten frustrating. thanks

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>

using namespace std;

//declare variables
int movex = 0, movey = 0;
int bulletx, bullety;
bool bulletActive = false;

//create functions
void moveUp(){
movey -= 10;
}
void moveDown(){
movey += 10;
}
/*void moveLeft(){
movex -= 10;
}
void moveRight(){
movex += 10;
}*/

//main function
int main()
{
//create the window
sf::RenderWindow window(sf::VideoMode(900, 700), "My window");

//declare functions
sf::Thread movUp(&moveUp);
sf::Thread movDown(&moveDown);
//sf::Thread movLeft(&moveLeft);
//sf:: Thread movRight(&moveRight);

while(window.isOpen()){

//Loads up the background
sf::Texture texture;
texture.loadFromFile("space_background2.jpg");
sf::Sprite background;
background.setTexture(texture);
background.setScale(1.0f, 1.0f);

//create the hexagon(originally triangle)
sf::CircleShape triangle(30, 6);
triangle.setFillColor(sf::Color::Blue);
triangle.setPosition(movex, movey);
triangle.setOrigin(0, 0);

//create the enemy
sf::CircleShape enemy(20, 3);
enemy.setFillColor(sf::Color::Yellow);
enemy.setPosition(800, 300);

//create the bullet
sf::CircleShape bullet(5);
bullet.setFillColor(sf::Color::Red);

//close button
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed)
window.close();
}

//keyboard controls
/*if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
movRight.launch();
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
movLeft.launch();
}*/
/*else*/ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
if(movey <= 300)
movDown.launch();
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
if(movey >= 15)
movUp.launch();
}

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
//shotBullet.launch();
if(!bulletActive){
bulletActive = true;
bulletx = movex;
bullety = movey;
bullet.setPosition(bulletx, bullety);
}
}

//if the bullet is active keep going until it reaches 950 then delete it
if(bulletActive){
bulletx += 60;
if(bulletx > 950)
bulletActive = false;
}
//move then draw everything
window.draw(background);
bullet.move(bulletx, bullety);
triangle.move(movex, movey);
window.draw(triangle);

sf::FloatRect bulletBox = bullet.getGlobalBounds();
sf::FloatRect enemyBox = enemy.getGlobalBounds();
if(!bulletBox.intersects(enemyBox)){
window.draw(enemy);
if(bulletActive)
if(bulletx > 50)
window.draw(bullet);
}
if(bulletBox.intersects(enemyBox))
bulletActive = false;

window.display();


}
return 0;
}

Last edited on
What's this?
1
2
3
4
if (movey < 200)
multiplier = 1;
else
multiplier = 2;
i was trying to multiply the position of the bullet and it got closer yet still not on the ship. I even tracked the position in the console and it said that the amount of pixels was the same so it should be shooting from the same position.
Last edited on
Topic archived. No new replies allowed.