Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gb/gb.h>
- #include <gb/drawing.h>
- #include "tiles/tiles.c"
- #include "tiles/sprite.c"
- #include "maps/map.c"
- unsigned int sprx = 24; //sprite's x pos
- unsigned int spry = 32; //sprite's y pos
- unsigned int colx, coly; //collision detection coordinates
- unsigned int colmap[360] = { //collision map
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
- 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,
- 1,1,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,1,
- 1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
- 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
- 1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
- 1,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,
- 1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,
- 1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
- };
- unsigned int coltest(){ //bounding box collision detection
- unsigned int i;
- unsigned int coltrue = 0;
- for(i = 0; i < 360; i++){
- if (colmap[i] == 1){
- colx = ((i%20)*8) + 8;
- coly = ((i/20)*8) + 16;
- if((sprx < (colx + 8)) && ((sprx + 8) > colx) && (spry < (coly + 8)) && ((spry + 8) > coly)){
- coltrue ++; //return 1 for collision
- }
- }
- gotogxy(0,0);
- gprintf("%d ",coltrue);
- gotogxy(0,1);
- gprintf("%d ",sprx);
- gotogxy(0,2);
- gprintf("%d ",spry);
- gotogxy(0,3);
- gprintf("%d ",colx);
- gotogxy(0,4);
- gprintf("%d ",coly);
- move_sprite(0,colx,coly);
- delay(100);
- }
- return coltrue;
- }
- void main(){
- DISPLAY_ON;
- set_bkg_data(0, 2, tile); //load 3 tiles into background memory
- set_bkg_tiles(0, 0, 20, 18, map); //load map on display
- set_sprite_data(0, 0, sprite); //load 1 tile into sprite memory
- set_sprite_tile(0, 0); //load sprite on display
- SHOW_BKG;
- SHOW_SPRITES;
- move_sprite(0,sprx,spry);
- while(coltest() == 0){
- move_sprite(0,sprx,spry); //move sprite
- if(joypad() == J_UP){
- spry --;
- }
- if(joypad() == J_DOWN){
- spry ++;
- }
- if(joypad() == J_RIGHT){
- sprx ++;
- }
- if(joypad() == J_LEFT){
- sprx --;
- }
- delay(20);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement