Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: bullet shooting help?  (Read 1846 times)

0 Members and 1 Guest are viewing this topic.

masterassassin1398

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
bullet shooting help?
« 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;
}
 
« Last Edit: August 26, 2014, 03:58:00 pm by Laurent »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: bullet shooting help?
« Reply #1 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.
« Last Edit: August 26, 2014, 04:10:16 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3364
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: bullet shooting help?
« Reply #2 on: August 26, 2014, 04:43:39 pm »
:( list:
  • use of threads
  • global variables
  • hard-coded movement amounts
  • no taking into account of time
  • window.clear(); is missing
  • creating the shapes on every cycle/frame instead of before the isOpen loop
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: bullet shooting help?
« Reply #3 on: August 26, 2014, 07:42:42 pm »
@Hapax : you took the words right out of my mouth ;)

Mörkö

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: bullet shooting help?
« Reply #4 on: August 26, 2014, 08:59:25 pm »
sf::Thread movUp(&moveUp);
sf::Thread movDown(&moveDown);
:o

I don't understand.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: bullet shooting help?
« Reply #5 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.