SDL Bullet Movement O.o

Hey guys,

I'm trying to make a little space shooter, and I've gotten asteroids and the player to move but now I'm stuck on getting my space ship to shoot a bullet. All my tries have ended up as fails or worse(I've also tried posting this question on many other sites but ended up getting no/cruddy answers :/ I figured this would be the place to go). I need the bullet to be shot vertically... does anyone have some example source code I could look at, or can someone guide me through this step by step? If you want to see some code I've tried so far, just ask. Thanks.
Last edited on
http://zetcode.com/tutorials/javagamestutorial/spaceinvaders/

Java2D space invaders tutorial. Should give you some ideas as it does go into shooting bullets. I have no experience with SDL so cant give any specific info.
I appreciate you help naraku, but unfortunately for me, I'm not that good as translating one programming language into another programming language :(. I'll try my best though.
(After trying: FAIL )
Last edited on
Alright... I'm getting really desperate here(lol). Anyone? ._.
Just make some sort of a bullet object (probably with an x and y position) and increase (decrease?) it's y every frame. If you have some code you tried I could possibly help you fix it up.
Position Bullet Function
1
2
3
4
5
6
     projectilex = x + 17;
    projectiley = y + -20;
   
        alive = true;
    
//this positions the bullet :/ 

Show Function
1
2
3
4
5
6
7
8
9
10
11
12
 if(alive)
    {

        if(frame == 2)
        {
            frame = 0;
        }
        apply_surface(projectilex,projectiley,ShootStuff,screen,&lazers[frame]);
        frame++;
        projectiley + 1;
    }
// this SHOULD make the bullet do it's job :L 


Please let me know if this seems confusing to you.(Everyone knows I'm a noob :D)

The bullet is playing its animation, but it isn't moving.. and it's following the ship :(

Here is my main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
int main( int argc, char* args[] )
{
    srand(time(0));

    Player p;
    Timer fps;
    bool quit = false;
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }

    clip[ 0 ].x = 0;
    clip[ 0 ].y = 0;
    clip[ 0 ].w = 30;
    clip[ 0 ].h = 36;

    clip[ 1 ].x = 31;
    clip[ 1 ].y = 0;
    clip[ 1 ].w = 39;
    clip[ 1 ].h = 36;

    clip[ 2 ].x = 71;
    clip[ 2 ].y = 0;
    clip[ 2 ].w = 29;
    clip[ 2 ].h = 36;

    lazers [ 0 ].x = 0;
    lazers [ 0 ].y = 0;
    lazers [ 0 ].w = 3;
    lazers [ 0 ].h = 9;

        lazers [ 1 ].x = 5;
    lazers [ 1 ].y = 0;
    lazers [ 1 ].w = 3;
    lazers [ 1 ].h = 7;


    while( quit == false )
    {
        fps.start();
        //While there's an event to handle
        while( SDL_PollEvent( &event ) )
        {
            p.handle_input();
            //If a key was pressed


            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }

        }

        //Scroll background
        bgY += 5;

        //If the background has gone too far
        if( bgY >= GameBackground->h)
        {
            //Reset the offset
            bgY = 0;
        }
        p.move();
        apply_surface( bgX, bgY,GameBackground, screen );
        apply_surface( bgX , bgY  - GameBackground->h, GameBackground, screen );

apply_surface(0,0, FullHealthBar,screen);

 p.shoot();
        p.show();

        Aster.sendasteroids();
        Aster.sendasteroids2();
        Aster.showAsteroids();
        Aster.showAsteroids2();
        //Apply the message
        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }
        SDL_Flip(GameBackground);
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }


    }

    //Clean up
    clean_up();

    return 0;
}


And for some weird reason, I've even tried calling the shoot function(position function above), whenever the user holds down space and it wont show.... it only shows if I insert the function in the while loop in my main function :S
Last edited on
Made a seperate Projectile class...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Projectile: public Player
{
private:

//offsets
int PROJECTILE_X,PROJECTILE_Y;

//velocity

int PROJECTILE_xVel, PROJECTILE_yVel;

public:

Projectile();

void PROJECTILE_handle_input();

void PROJECTILE_move(); /
void PROJECTILE_show();




};
Projectile::Projectile() { //Initialize the offsets
     PROJECTILE_X = 0; PROJECTILE_Y = 0;
     //Initialize the velocity
     PROJECTILE_xVel = 0; PROJECTILE_yVel = 0; }
   void Projectile::PROJECTILE_handle_input()
{
    //If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_x: PROJECTILE_yVel = -4;
        spacebar = true;

        }
    }
}
void Projectile::PROJECTILE_move()
{

PROJECTILE_Y+=PROJECTILE_yVel;

}
void Projectile::PROJECTILE_show()
{ PROJECTILE_X = x;
    //Show the projectile...
    apply_surface( PROJECTILE_X + 17, PROJECTILE_Y, ShootStuff, screen,&lazers[0] );
}


Last edited on
Lol, I ended up making a strafe beam.... good enough :D
Topic archived. No new replies allowed.