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

[Allegro 5] Kolizje

Ostatnio zmodyfikowano 2013-05-22 17:09
Autor Wiadomość
aaadam
Temat założony przez niniejszego użytkownika
[Allegro 5] Kolizje
» 2013-03-27 19:00:26
czy mógłby mi ktoś wytłumaczyć jak stworzyć  kolizje wczytuje mapę z pliku.Kolizje jako kolizje na ekranie umiem zrobić ale chcę zacząć wczytywać mapę i na tej mapie robić kolizje . help
 
a o to kod
 
C/C++
#include <allegro5\allegro5.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_primitives.h>
#include <fstream>
#include "load.h"
#define ScreenSzer 800
#define ScreenWys 600
#define  BlockSize 40
int top, bottom, left, right;
bool Collision( ALLEGRO_BITMAP * player, ALLEGRO_BITMAP * sciana,
float x, float y, float ex, float ey, int width, int height, float & moveSpeed, int dir )
{
    if( x + width < ex || x > ex + width || y + height < ey || y > ey + height )
    {
        return false;
    }
    else
    {
        top = max( y, ey );
        bottom = min( y + height, ey + height );
        left = max( x, ex );
        right = min( x + width, ex + width );
       
        for( int i = top; i < bottom; i++ )
        {
            for( int j = left; j < right; j++ )
            {
                al_lock_bitmap( player, al_get_bitmap_format( player ), ALLEGRO_LOCK_READONLY );
                al_lock_bitmap( sciana, al_get_bitmap_format( sciana ), ALLEGRO_LOCK_READONLY );
                ALLEGRO_COLOR color = al_get_pixel( player, j - x, i - y );
                ALLEGRO_COLOR color2 = al_get_pixel( sciana, j - ex, i - ey );
               
               
            }
        }
       
    }
    return false;
}


int loadCounterX = 0, loadCounterY = 0, mapSizeX, mapSizeY;

void LoadMap( const char * filename, int map[][ 100 ] )
{
    std::ifstream openfile( filename );
    if( openfile.is_open() )
    {
        openfile >> mapSizeX >> mapSizeY;
        while( !openfile.eof() )
        {
            openfile >> map[ loadCounterX ][ loadCounterY ];
            loadCounterX++;
            if( loadCounterX >= mapSizeX )
            {
                loadCounterX = 0;
                loadCounterY++;
            }
        }
    }
    else
    {
       
    }
}
void DrawMap( int map[][ 100 ], ALLEGRO_BITMAP *, ALLEGRO_BITMAP *, ALLEGRO_BITMAP * pod );
int main()
{
    enum Direction { DOWN, LEFT, RIGHT, UP };
    const float FPS = 60.0;
    bool done = false, draw = true, active = false;
    float x = 10, y = 10, moveSpeed = 3;
    int map[ 100 ][ 100 ];
    int dir = DOWN, prevDir, index = 0, sourceX = 32, sourceY = 0;
    ALLEGRO_DISPLAY * display;
    if( !al_init() )
    {
        al_show_native_message_box( NULL, NULL, NULL, "blad", NULL, NULL );
    }
    display = al_create_display( 320, 320 );
    if( !display )
    {
        al_show_native_message_box( NULL, NULL, NULL, "blad", NULL, NULL );
    }
    al_install_keyboard();
    al_init_image_addon();
    ALLEGRO_BITMAP * player = al_load_bitmap( "PlayerImage1.png" );
    ALLEGRO_BITMAP * sciana = al_load_bitmap( "sprite/sciana.png" );
    ALLEGRO_BITMAP * pod = al_load_bitmap( "sprite/pod.png" );
    ALLEGRO_BITMAP * mur = al_load_bitmap( "sprite/mur.png" );
    ALLEGRO_TIMER * timer = al_create_timer( 1.0 / FPS );
    ALLEGRO_TIMER * frameTimer = al_create_timer( 1.0 / 15 );
    ALLEGRO_KEYBOARD_STATE keyState;
    ALLEGRO_EVENT_QUEUE * event_queue = al_create_event_queue();
    al_register_event_source( event_queue, al_get_timer_event_source( timer ) );
    al_register_event_source( event_queue, al_get_timer_event_source( frameTimer ) );
    al_register_event_source( event_queue, al_get_display_event_source( display ) );
    al_register_event_source( event_queue, al_get_keyboard_event_source() );
    LoadMap( "map.txt", map );
    al_start_timer( timer );
    al_start_timer( frameTimer );
    while( !done )
    {
        int * i = 0, * j = 0;
        ALLEGRO_EVENT events;
        al_wait_for_event( event_queue, & events );
        al_get_keyboard_state( & keyState );
       
        if( events.type == ALLEGRO_EVENT_DISPLAY_CLOSE )
        {
            done = true;
        }
        else if( events.type == ALLEGRO_EVENT_TIMER )
        {
            if( events.timer.source == timer )
            {
                active = true;
                if( al_key_down( & keyState, ALLEGRO_KEY_DOWN ) )
                {
                    y += moveSpeed;
                    dir = DOWN;
                }
                else if( al_key_down( & keyState, ALLEGRO_KEY_UP ) )
                {
                    y -= moveSpeed;
                    dir = UP;
                }
                else if( al_key_down( & keyState, ALLEGRO_KEY_RIGHT ) )
                {
                    x += moveSpeed;
                    dir = RIGHT;
                }
                else if( al_key_down( & keyState, ALLEGRO_KEY_LEFT ) )
                {
                    x -= moveSpeed;
                    dir = LEFT;
                }
                else
                     active = false;
               
                if( Collision( player, sciana, x, y, 50, 50, 32, 32, moveSpeed, dir ) )
                {
                   
                }
                else
                     draw = true;
               
            }
           
            else if( events.timer.source == frameTimer )
            {
                if( active )
                     sourceX += al_get_bitmap_width( player ) / 3;
                else
                     sourceX = 32;
               
                if( sourceX >= al_get_bitmap_width( player ) )
                     sourceX = 0;
               
                sourceY = dir;
            }
           
            draw = true;
        }
       
        if( draw ) {
            DrawMap( map, sciana, mur, pod );
            al_draw_bitmap_region( player, sourceX, sourceY * al_get_bitmap_height( player ) / 4, 32, 32, x, y, NULL );
            al_flip_display();
            al_clear_to_color( al_map_rgb( 0, 0, 0 ) );
        }
    }
    al_destroy_display( display );
    al_destroy_timer( timer );
    al_destroy_bitmap( player );
    al_destroy_bitmap( sciana );
    al_destroy_bitmap( mur );
    al_destroy_event_queue( event_queue );
    return 0;
   
}

void DrawMap( int map[][ 100 ], ALLEGRO_BITMAP * sciana, ALLEGRO_BITMAP * mur, ALLEGRO_BITMAP * pod )
{
    for( long int i = 0; i < mapSizeX; i++ )
    {
        for( long int j = 0; j < mapSizeX; j++ )
        {
            if( map[ i ][ j ] == 0 )
            {
                al_draw_bitmap( mur, i * 32, j * 32, NULL );
            }
            else if( map[ i ][ j ] == 1 )
            {
                al_draw_bitmap( sciana, i * 32, j * 32, NULL );
            }
            else if( map[ i ][ j ] == 2 )
            {
                al_draw_bitmap( pod, i * 32, j * 32, NULL );
            }
        }
    }
}
P-79488
Gabes
» 2013-05-21 23:50:06
Jeśli masz tablice dwuwymiarową to wszelkie sprawdzanie kolizji, zabieranie przedmiotów przeprowadzasz na tablicy i dopiero na koniec wyświetlasz wszystko. x i y kafla, ludka to pozycja na tablicy * szer. i wys. kafla czy ludka.
moja mała gra w allegro5 z "kolizją tablicową". razem z kodem  
P-83572
DejaVu
» 2013-05-22 17:09:38
Dorzucę jeszcze:
Frazy, które należy wpisać w wyszukiwarkę google:
P-83601
« 1 »
  Strona 1 z 1