[C/C++] Frage zu "tile based maps"?

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von JoE THE, 17. Juli 2007 .

Schlagworte:
  1. Diese Seite verwendet Cookies. Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies. Weitere Informationen
  1. #1 17. Juli 2007
    Frage zu "tile based maps"?

    Hallo

    Ich habe ien Programm in C++ geschrieben. Dabei habe ich die Level-Wände mit tiles erzeugt. Nun habe ich jedoch das Problem, dass meine Spielfigur durch die Wände läuft.

    Meine Frage lautet, wie kann ich bei jeder Bewegung der Figur kontrollieren ob sie in der Wand ist oder nicht?

    Quelltext:

    Code:
    #include <allegro.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <time.h>
    #include <stdlib.h>
    
    const int MAP_W = 40;
    const int MAP_H = 30;
    const int TILE_W = 20;
    const int TILE_H = 20;
    const int hero_images = 4;
    
    char char2tile[] ="201RLUDP";
    
    
    void setupMap(char*, int);
    void drawTile(char, char, int, int, int);
    void drawMap(char*, BITMAP*, BITMAP*);
    
    class mand { //Mandliposition bestimmen
    private:
     int x;
     int y;
    public:
     void screengroesse(int h) {
     y = h - (h - 13*20);
     x = 0;
     }
     int posx() {return(x);}
     int posy() {return(y);}
    };
    
    int main(int argc, char **argv) {
    
    //Klassen deklarieren
    mand mandli;
    
    
    char tileMapStr[MAP_W * MAP_H+1] = 
    "1111111111111111111111111111111111111111"
    "1PPP000000010000000000000PP10000000PPPP1"
    "1PPP0000000100000000000000010000000PPPP1"
    "1000000000011110000000000001000000000001"
    "1000000000000010000000000001000000000000"
    "10000000000000L0000000000001000000000001"
    "1000000000000010000000000001000000000001"
    "1000000000011110000000000001000000000001"
    "1000000000010000000000000001000000000001"
    "1000000000010000000000000001000000000001"
    "100000000001PP00000000000001000000000001"
    "11111D111111111111000000000111111D111111"
    "1000000000000000010000000001PP0000000001"
    "0000000000000000010000000001P00000000001"
    "10000000000000PPP11111U11111000000000001"
    "10000000000000PPP10000000001000000000001"
    "1111111111111111110000000001000000000001"
    "1111111111111111110000000001000000000001"
    "1111111111111111110000000001000000000001"
    "111111111111111111000000000L000000000001"
    "11111111111111111100000000P10000000000P1"
    "1111111111111111110000000PP1111111111111"
    "111111111111111111000000PPP1111111111111"
    "1111111111111111111111111111111111111111"
    "1111111111111111111111111111111111111111"
    "1111111111111111111111111111111111111111"
    "2222222222222222222222222222222222222222"
    "2222222222222222222222222222222222222222"
    "2222222222222222222222222222222222222222";
    
    allegro_init();
    install_keyboard();
    
    set_color_depth(24);
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,800,600,0,0) < 0) {
     allegro_message("Unable to set graphic mode!");
     exit(0);
    }
    // Loading Bitamps
    BITMAP *tiles = load_bitmap("bausteine2.bmp", NULL);
    BITMAP *doublebuffer = create_bitmap(SCREEN_W,SCREEN_H);
    BITMAP *herobitmaps[hero_images];
    
    //Loading Hero Bitmaps
    char *filename[] = {"hero_up.bmp","hero_down.bmp","hero_left.bmp","hero_right.bmp"};
    for(int i = 0; i < hero_images; i++) {
     herobitmaps[i] = load_bitmap(filename[i], NULL);
    }
    setupMap(tileMapStr, MAP_W*MAP_H);
    
    mandli.screengroesse(SCREEN_H);
    int changeimage = 3;
    int x = mandli.posx();
    int y = mandli.posy();
    
    while(!key[KEY_ESC]) 
    {
     if(key[KEY_DOWN]) {
     y+=1;
     changeimage = 1;}
     if(key[KEY_UP]) {
     y-=1;
     changeimage = 0;}
     if(key[KEY_LEFT]) {
     x-=0;
     changeimage = 2;}
     if(key[KEY_RIGHT]) {
     x+=1;
     changeimage = 3;}
    
     //Level zeichen
     drawMap(tileMapStr, tiles, doublebuffer);
     
     //Mandli zeichnen
     draw_sprite(doublebuffer,herobitmaps[changeimage],x,y);
    
     //Doublebuffer
     blit(doublebuffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
    }
    
    return(0);
    
    }END_OF_MAIN()
    
    void setupMap(char *map, int size) {
     for (int a=0; a < size; a++) {
     for (int tile=0; char2tile[tile]; ++tile) {
     if (map[a] == char2tile[tile]) {
     map[a] = tile;
     break;
     }
     }
     }
    }
    
    void drawTile(BITMAP *dest, BITMAP *tile, int index, int x, int y) {
     blit(tile, dest, index * TILE_W, 0, x, y, TILE_W, TILE_H);
    }
    
    void drawMap(char* map, BITMAP *tiles, BITMAP *doublebuffer) {
     int pos = 0;
     for (int y=0; y < MAP_H; ++y) {
     for (int x=0; x < MAP_W; ++x) {
     drawTile(doublebuffer, tiles, map[pos], x*TILE_W, y*TILE_H);
     ++pos;
     }
     }
    }
    
    
     

  2. Anzeige
  3. #2 18. Juli 2007

  4. Videos zum Thema
Die Seite wird geladen...