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

Author Topic: only one bullet moves  (Read 1956 times)

0 Members and 1 Guest are viewing this topic.

bax533

  • Newbie
  • *
  • Posts: 2
    • View Profile
only one bullet moves
« on: August 06, 2016, 12:13:14 am »
Only the last bullet i shot has speed and move. When another bullet is shot, previous stays in the ship position please help i dont know what to do :(

#include "SFML/Graphics.hpp"
#include "bits/stdc++.h"
#include "bullet.hpp"

using namespace sf;
const float PI=3.141592654;
float DEGTORAD = 0.017453f;
const int W=1200, H=800;

int main()
{
       
        RenderWindow Window( VideoMode( W, H ), "app!" );

        Texture shipTex, bckTex, bulletTex;
        Sprite  shipImage, bckImage, cel;
        Bullet bul;
        std::vector<Bullet> bullets;
        std::vector<Bullet>::iterator it;      


if(!shipTex.loadFromFile("images/spaceship.png") or !bckTex.loadFromFile("images/background.jpg")
or !bulletTex.loadFromFile("images/bullet.png"))
                std::cout<<"nope\n";
   
        bul.zrob();
    Window.setVerticalSyncEnabled(1);
    Window.setFramerateLimit(60);
       
        shipImage.setTexture(shipTex); bckImage.setTexture(bckTex); cel.setTexture(bulletTex);
        shipImage.setTextureRect(IntRect(40,0,40,40));
        shipImage.setPosition(200, 200);       

        shipImage.setOrigin(20,20);
       
//////DEKLARACJE//////

        float x=300, y=300, angle=0, dx=0, dy=0;
        const int maxSpeed=15;
        float xcel=300, ycel=300;
        float distance=0;
        float vX=0, vY=0;
        float a=0; int r=50;

        while(Window.isOpen())/////////////////////////////////////////////////////
        {
                Event Event;
                while(Window.pollEvent(Event))
                {
                        switch(Event.type)
                        {
                                case Event::Closed:
                                        Window.close();
                                        break;
                        }

                }
///DeklaracjeWP&#281;tli///
        int num=0;
        bool thrust=0, left=0, right=0, strzal=0;
        int x0=shipImage.getPosition().x, y0=shipImage.getPosition().y;
//////KLAWIATURA//////////

        if(Keyboard::isKeyPressed(Keyboard::Right)) {angle+=3;right=1;}
        if(Keyboard::isKeyPressed(Keyboard::Space))
        {
                strzal=1;
                bul.obraz.setPosition(shipImage.getPosition());
                bullets.push_back(bul);
               
                bullets[num].obraz.setPosition(shipImage.getPosition());
                bullets[num].speed.x=cel.getPosition().x-shipImage.getPosition().x;
                bullets[num].speed.y=cel.getPosition().y-shipImage.getPosition().y;
                num++;
               
        }
        if(Keyboard::isKeyPressed(Keyboard::Left))  {angle-=3;left=1;}
        if(Keyboard::isKeyPressed(Keyboard::Up))    {thrust=true;}
        else                                                                    thrust=false;
       

//////ANIMACJE//////
        if(left)shipImage.setTextureRect(IntRect(0,0,37,40));
        else if(right)shipImage.setTextureRect(IntRect(80,0,40,40));
        else if(Event::KeyReleased)shipImage.setTextureRect(IntRect(40,0,40,40));
       


//////PORUSZANIE/////
       
        if(thrust)
        {              
        dx+=cos(angle*DEGTORAD)*0.2;
        dy+=sin(angle*DEGTORAD)*0.2;
        }
        else
        {dx*=0.99;
         dy*=0.99;}
       

        float speed =sqrt(dx*dx+dy*dy);
        if(x>W){x=0;}  if(x<0){x=W;}
        if(y>H){y=0;}  if(y<0){y=H;}
        if(speed>maxSpeed)
        {dx*=maxSpeed/speed;
         dy*=maxSpeed/speed;}

    x+=dx;
        y+=dy;
//////buleta ==o




        if(left)a-=0.0525f;
        if(right)a+=0.0525f;
       
        xcel=cos(a)*r+x0;
        ycel=sin(a)*r+y0;

        /////////DRAW/////////

        shipImage.setPosition(x, y);
        shipImage.setRotation(angle+90);
       
        cel.setPosition(xcel, ycel);
        cel.setRotation(shipImage.getRotation());      

       
        Window.draw(bckImage);Window.draw(shipImage);//Window.draw(cel);

        for(int i=0; i<bullets.size(); i++)
        {
                bullets[i].obraz.move(bullets[i].speed.x/2, bullets[i].speed.y/2);

                if(bullets[i].pozycja().x<=W && bullets[i].pozycja().x>=0 && bullets[i].pozycja().y>0 && bullets[i].pozycja().y<=H )
                Window.draw(bullets[i].obraz);

        }
       
        Window.display();
        Window.clear();
               

        }
       
}
« Last Edit: August 06, 2016, 12:29:19 am by bax533 »

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: only one bullet moves
« Reply #1 on: August 06, 2016, 04:34:54 pm »
Every shot you set parameters of bullets[0] as you set "num" to zero every frame. Do sth like that:
 bul.obraz.setPosition(shipImage.getPosition());
 bul.speed.x=cel.getPosition().x-shipImage.getPosition().x;
 bul.speed.y=cel.getPosition().y-shipImage.getPosition().y;
 bullets.push_back(bul);

 

anything