Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include <gb/gb.h>
  2. #include <gb/drawing.h>
  3. #include "tiles/tiles.c"
  4. #include "tiles/sprite.c"
  5. #include "maps/map.c"
  6. unsigned int sprx = 24; //sprite's x pos
  7. unsigned int spry = 32; //sprite's y pos
  8. unsigned int colx, coly;    //collision detection coordinates
  9. unsigned int colmap[360] = {    //collision map
  10.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  11.     1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
  12.     1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
  13.     1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
  14.     1,1,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1,
  15.     1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
  16.     1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
  17.     1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
  18.     1,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,1,
  19.     1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,
  20.     1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,
  21.     1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
  22.     1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  23.     1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  24.     1,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,
  25.     1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,
  26.     1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,
  27.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  28. };
  29. unsigned int coltest(){         //bounding box collision detection
  30.     unsigned int i;
  31.     unsigned int coltrue = 0;
  32.     for(i = 0; i < 360; i++){
  33.         if (colmap[i] == 1){
  34.             colx = ((i%20)*8) + 8;
  35.             coly = ((i/20)*8) + 16;
  36.             if((sprx < (colx + 8)) && ((sprx + 8) > colx) && (spry < (coly + 8)) && ((spry + 8) > coly)){
  37.                 coltrue ++; //return 1 for collision
  38.             }
  39.         }
  40.         gotogxy(0,0);
  41.         gprintf("%d    ",coltrue);
  42.         gotogxy(0,1);
  43.         gprintf("%d    ",sprx);
  44.         gotogxy(0,2);
  45.         gprintf("%d    ",spry);
  46.         gotogxy(0,3);
  47.         gprintf("%d    ",colx);
  48.         gotogxy(0,4);
  49.         gprintf("%d    ",coly);
  50.         move_sprite(0,colx,coly);
  51.         delay(100);
  52.     }
  53.     return coltrue;
  54. }
  55. void main(){
  56.     DISPLAY_ON;
  57.     set_bkg_data(0, 2, tile);           //load 3 tiles into background memory
  58.     set_bkg_tiles(0, 0, 20, 18, map);   //load map on display
  59.     set_sprite_data(0, 0, sprite);      //load 1 tile into sprite memory
  60.     set_sprite_tile(0, 0);              //load sprite on display
  61.     SHOW_BKG;
  62.     SHOW_SPRITES;
  63.     move_sprite(0,sprx,spry);
  64.     while(coltest() == 0){
  65.         move_sprite(0,sprx,spry);       //move sprite
  66.         if(joypad() == J_UP){
  67.             spry --;
  68.         }
  69.         if(joypad() == J_DOWN){
  70.             spry ++;
  71.         }
  72.         if(joypad() == J_RIGHT){
  73.             sprx ++;
  74.         }
  75.         if(joypad() == J_LEFT){
  76.             sprx --;
  77.         }
  78.        
  79.         delay(20);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement