Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Allegro] Strzelanie więcej niż 1 pociskiem

Ostatnio zmodyfikowano 2010-05-29 22:05
Autor Wiadomość
GzZiom
Temat założony przez niniejszego użytkownika
[Allegro] Strzelanie więcej niż 1 pociskiem
» 2010-05-29 17:14:01
Witam! W mojej platformówce, chciałem zrobić rzucanie jabłkami w tą stronę w którą jest się zwróconym. Mając jedno jabłko nie było żadnego problemu żeby to zrobić. Gorzej wtedy jeżeli mam kilka jabłek. Próbowałem zrobić klasy "magazynku" i "naboju", ale przy wcisnieciu strzału gra się wywala:
C/C++
class Naboj
{
public:
    int x, y, dir;
   
    void logic()
    {
        x += dir * 15;
    }
    Naboj() { x = player.x; y = player.y; }
};

class Amunicja
{
public:
    std::vector < Naboj > naboj;
   
    void logic()
    {
        if( key[ KEY_X ] && player.amunicja > 0 )
        {
            naboj.push_back( Naboj() );
            player.amunicja--;
           
            if( player.klatka == 0 )
                 naboj[ naboj.size() ].dir = - 1;
            else
                 naboj[ naboj.size() ].dir = 1;
           
        }
       
        for( int i = 0; i < naboj.size(); i++ )
             naboj[ i ].logic();
       
    }
};

Z góry dziękuję za pomoc.

#pixelmaster: proszę nadawać tytuły tematom ...
P-17293
Pirotechnik
» 2010-05-29 17:16:52
do kodu używaj lepiej
Kod C++
P-17295
pixelmaster
» 2010-05-29 17:49:56
Kiedyś znalazłem taki kod, spróbuj sobie z niego to wyciągnąć
C/C++
#include <allegro.h>

int x, y;
int i;
int shot = 0;
const int MAX_BULLETS = 100;

//Bullet structures
typedef struct some_object
{
    int x, y, alive;
} some_object;

some_object bullets[ MAX_BULLETS ];

int add_bullet( int x, int y )
{
   
    int i;
   
    for( i = 0; i < MAX_BULLETS; i++ ) {
        if( bullets[ i ].alive == 0 ) {
            bullets[ i ].x = x;
            bullets[ i ].y = y;
            return i; // Return bullet index
        }
    }
   
    return( 0 ); // Or -1?
}

// Starts the main loop of the program
int main() {
    // x, and y variables for movement
    int bmp_x = 60;
    int bmp_y = 60;
    // Game status vars
    int shoot = 0;
    int over = 0;
    // initiates Allegro
    allegro_init();
   
    // Sets color depth (duh)
    set_color_depth( 32 );
   
    // Sets graphic mode to windowed and a 800X600 res
    if( set_gfx_mode( GFX_AUTODETECT_WINDOWED, 1024, 768, 0, 0 ) < 0 ) {
        allegro_message( "Couldn't set gfx mode." );
        return - 1;
    }
   
    // Sets the color palette to the desktop original palette
    set_palette( desktop_palette );
   
    // Installs the keyboard for use
    install_keyboard();
   
    install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, "" );
    set_volume( 255, 255 );
   
    // Loads the image placeholder.bmp for use and stores it as bmp
    BITMAP * bmp = load_bitmap( "statek.bmp", NULL );
    BITMAP * bul = load_bitmap( "bullet.bmp", NULL );
    SAMPLE * dzwiek = load_sample( "shot.wav" );
    SAMPLE * dzwiek2 = load_sample( "loop.wav" );
   
    //Loads a buffer
    BITMAP * buffer = create_bitmap( SCREEN_W, SCREEN_H );
   
    //Main function of program that refreshes the buffer while the escape key is not pressed
    //Then it changes the x, and y values depending on what is pressed
    int temp;
   
   
    //set all the alive values to 0 before the game actually starts
    for( i = 0; i < MAX_BULLETS; i++ )
    {
        bullets[ i ].alive = 0;
    }
   
    play_sample( dzwiek2, 200, 127, 1000, 1 );
   
    while( !key[ KEY_ESC ] )
    {
       
        //clear(buffer);  not needed, you have clear_to_color() right after it
        clear_to_color( buffer, makecol( 200, 200, 200 ) );
        blit( bmp, buffer, 0, 0, bmp_x, bmp_y, 64, 64 );
        for( i = 0; i < MAX_BULLETS; i++ ) //brought this up from the bottom
        {
            if( bullets[ i ].alive == 1 )
            {
                blit( bul, buffer, 0, 0, bullets[ i ].x, bullets[ i ].y, 4, 4 );
            }
        }
        blit( buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H );
       
        if( key[ KEY_UP ] )
             bmp_y = bmp_y - 1;
       
        if( key[ KEY_DOWN ] )
             bmp_y = bmp_y + 1;
       
        if( key[ KEY_LEFT ] )
             bmp_x = bmp_x - 1;
       
        if( key[ KEY_RIGHT ] )
             bmp_x = bmp_x + 1;
       
        //if(key[KEY_L]) losuj();
       
        if( key[ KEY_SPACE ] )
        { // shoot here
            if( shot == 0 )
            {
                play_sample( dzwiek, 255, 127, 1000, 0 );
                temp = add_bullet( bmp_x, bmp_y );
                shot = 1;
                bullets[ temp ].alive = 1;
            }
        }
       
        if( !key[ KEY_SPACE ] )
        { // key released?
            if( shot == 1 )
            {
                shot = 0;
            }
        }
       
        for( i = 0; i < MAX_BULLETS; i++ )
        {
            if( bullets[ i ].alive )
            {
                bullets[ i ].y--;
            }
        }
       
    }
   
    //Destroys the buffer
    destroy_bitmap( buffer );
   
}
//Ends program
END_OF_MAIN()
P-17297
GzZiom
Temat założony przez niniejszego użytkownika
» 2010-05-29 22:05:46
Wielkie dzięki pixelmaster ;) Przeczytałem ten kod i wszystko okazało się bardzo łatwe (tia jak zwykle ;]). Teraz wszystko śmiga. Pozdrawiam!

//Patzick: Rozwiązane tematy zamykamy :)
P-17325
« 1 »
  Strona 1 z 1