Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 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.     }
  41.     return coltrue;
  42. }
  43. void main(){
  44.     DISPLAY_ON;
  45.     set_bkg_data(0, 2, tile);           //load 3 tiles into background memory
  46.     set_bkg_tiles(0, 0, 20, 18, map);   //load map on display
  47.     set_sprite_data(0, 0, sprite);      //load 1 tile into sprite memory
  48.     set_sprite_tile(0, 0);              //load sprite on display
  49.     SHOW_BKG;
  50.     SHOW_SPRITES;
  51.     move_sprite(0,sprx,spry);
  52.     while(coltest() == 0){
  53.         move_sprite(0,sprx,spry);       //move sprite
  54.         if(joypad() == J_UP){
  55.             spry --;
  56.         }
  57.         if(joypad() == J_DOWN){
  58.             spry ++;
  59.         }
  60.         if(joypad() == J_RIGHT){
  61.             sprx ++;
  62.         }
  63.         if(joypad() == J_LEFT){
  64.             sprx --;
  65.         }
  66.        
  67.         delay(20);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement