SFML community forums

Help => General => Topic started by: masterassassin1398 on August 26, 2014, 03:52:23 pm

Title: bullet shooting help?
Post by: masterassassin1398 on August 26, 2014, 03:52:23 pm
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;
}
 
Title: Re: bullet shooting help?
Post by: zsbzsb on August 26, 2014, 04:07:41 pm
using namespace std;

Don't do this, it will only cause issues down the road.

sf::Thread movUp(&moveUp);
sf::Thread movDown(&moveDown);

/*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();
}
 

That is a really bad idea, please just don't do this. Forget you even heard of threads and multithreading because this is really bad.


And please, please, please learn to use some indenting in your code.
Title: Re: bullet shooting help?
Post by: Hapax on August 26, 2014, 04:43:39 pm
:( list:
Title: Re: bullet shooting help?
Post by: Jesper Juhl on August 26, 2014, 07:42:42 pm
@Hapax : you took the words right out of my mouth ;)
Title: Re: bullet shooting help?
Post by: Mörkö on August 26, 2014, 08:59:25 pm
sf::Thread movUp(&moveUp);
sf::Thread movDown(&moveDown);
:o

I don't understand.
Title: Re: bullet shooting help?
Post by: Jesper Juhl on August 26, 2014, 09:04:58 pm
He's attempting to create two threads to be called whenever his character needs to move up/down.
No point in trying to understand it any further since it's s bad idea and will just lead to headaches down the road.