Ambidextroid

Flappy Bird for Game Boy, GBDK

Sep 10th, 2018
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.95 KB | None | 0 0
  1. #include <gb/gb.h>
  2. #include <gb/drawing.h>
  3. unsigned char flappy[] =    //sprite data
  4. {
  5.   0x00,0x00,0x00,0x00,0x07,0x07,0x0F,0x08,
  6.   0x1F,0x11,0x7F,0x79,0x87,0x85,0x87,0x84,
  7.   0xC7,0x84,0x7F,0x44,0x39,0x3F,0x20,0x3F,
  8.   0x18,0x1F,0x07,0x07,0x00,0x00,0x00,0x00,
  9.   0x00,0x00,0x00,0x00,0xE0,0xE0,0x90,0x90,
  10.   0x08,0x08,0x14,0x14,0x14,0x14,0x84,0x84,
  11.   0xFE,0x7E,0xFF,0x81,0xFE,0x7E,0xFE,0x82,
  12.   0x7C,0xFC,0x80,0x80,0x00,0x00,0x00,0x00,
  13.   0xFF,0xFF,0xFF,0x80,0x9C,0xE3,0x9C,0xE3,
  14.   0x9C,0xE3,0x9C,0xE3,0x9C,0xE3,0x9C,0xE3,
  15.   0xFF,0xFF,0x4E,0x71,0x4E,0x71,0x4E,0x71,
  16.   0x4E,0x71,0x4E,0x71,0x4E,0x71,0x4E,0x71,
  17.   0xFF,0xFF,0xFF,0x01,0x01,0xFF,0x01,0xFF,
  18.   0x01,0xFF,0x01,0xFF,0x01,0xFF,0x01,0xFF,
  19.   0xFF,0xFF,0x02,0xFE,0x02,0xFE,0x02,0xFE,
  20.   0x02,0xFE,0x02,0xFE,0x02,0xFE,0x02,0xFE,
  21.   0x4E,0x71,0x4E,0x71,0x4E,0x71,0x4E,0x71,
  22.   0x4E,0x71,0x4E,0x71,0x4E,0x71,0xFF,0xFF,
  23.   0xFF,0x80,0x9C,0xE3,0x9C,0xE3,0x9C,0xE3,
  24.   0x9C,0xE3,0x9C,0xE3,0x9C,0xE3,0xFF,0xFF,
  25.   0x02,0xFE,0x02,0xFE,0x02,0xFE,0x02,0xFE,
  26.   0x02,0xFE,0x02,0xFE,0x02,0xFE,0xFF,0xFF,
  27.   0xFF,0x01,0x01,0xFF,0x01,0xFF,0x01,0xFF,
  28.   0x01,0xFF,0x01,0xFF,0x01,0xFF,0xFF,0xFF,
  29.   0x4E,0x71,0x4E,0x71,0x4E,0x71,0x4E,0x71,
  30.   0x4E,0x71,0x4E,0x71,0x4E,0x71,0x4E,0x71,
  31.   0x4E,0x71,0x4E,0x71,0x4E,0x71,0x4E,0x71,
  32.   0x4E,0x71,0x4E,0x71,0x4E,0x71,0x4E,0x71,
  33.   0x02,0xFE,0x02,0xFE,0x02,0xFE,0x02,0xFE,
  34.   0x02,0xFE,0x02,0xFE,0x02,0xFE,0x02,0xFE,
  35.   0x02,0xFE,0x02,0xFE,0x02,0xFE,0x02,0xFE,
  36.   0x02,0xFE,0x02,0xFE,0x02,0xFE,0x02,0xFE
  37. };
  38.  
  39. INT16 y = 80, v = 0, xt = 125, xb = 250, score = 0; // y = y ordinate for bird, v = bird's (negative) velocity, xt and xb = x ordinate for pipes (confusingly xt(op) is the lower one, xb(ottom) is the upper one. whoops!)
  40. UINT8 alive = 0, speed = 6; //alive determines if the game loop runs. speed determines the delay between frames (lower = faster)
  41. void main(){
  42.     gotogxy(5,3);           //set text position
  43.     gprintf("Flappy Bird"); //print to screen
  44.     gotogxy(2,5);           //reset text position...
  45.     gprintf("Made by Dante S-K");
  46.     gotogxy(5,14);
  47.     gprintf("PRESS START");
  48.     SPRITES_8x16;           //all sprites are handled as two sprites joined together, one on top of the other, making one 8x16 sprite. everything is made with two halves.
  49.     set_sprite_data(0,16,flappy);               //load all 16 sprites into memory (4 for the bird, 4 for the top pipe, 4 for the bottom pipe and 4 for the mid pipe)
  50.    
  51.     //set each sprite's tiles: set_sprite_tile(spriteno, tileno). numbers in comments to keep track of sprite number.
  52.    
  53.     set_sprite_tile(0,0);   //bird L        0  
  54.     set_sprite_tile(1,2);   //bird R        1
  55.     set_sprite_tile(2,4);   //top L         2
  56.     set_sprite_tile(3,6);   //top R         3
  57.     set_sprite_tile(4,8);   //bottom L      4
  58.     set_sprite_tile(5,10);  //bottom R      5
  59.     set_sprite_tile(6,12);  //pipe L        6   these all use the same tiles, hence the repeated 12 (left half) and 14 (right half)
  60.     set_sprite_tile(7,14);  //pipe R        7
  61.     set_sprite_tile(8,12);  //pipe L        8
  62.     set_sprite_tile(9,14);  //pipe R        9
  63.     set_sprite_tile(10,12); //pipe L        10
  64.     set_sprite_tile(11,14); //pipe R        11
  65.     set_sprite_tile(12,12); //pipe L        12
  66.     set_sprite_tile(13,14); //pipe R        13
  67.     set_sprite_tile(14,12); //pipe L        14
  68.     set_sprite_tile(15,14); //pipe R        15
  69.     set_sprite_tile(16,12); //pipe L        16
  70.     set_sprite_tile(17,14); //pipe R        17
  71.     SHOW_SPRITES;
  72.     move_sprite(0,82,88);   //initial bird position on start screen
  73.     move_sprite(1,90,88);
  74.    
  75.     while(1){               //----------------------- game loop -----------------------
  76.         while(alive == 1){  //you start dead
  77.             delay(speed);   //stops "speed" number of ticks before next frame
  78.            
  79.             xt --;      //pipe movement, constantly moving left
  80.             xb --;
  81.             if (xt == 0){  
  82.                 xt = 250;   //loop pipes when they reach left side of screen
  83.                 score ++;   //increase score for each pipe that passes
  84.                 gotogxy(2,2);
  85.                 gprintf("%d",score);
  86.             }
  87.             if (xb == 0){
  88.                 xb = 250;
  89.                 score ++;
  90.                 gotogxy(2,2);
  91.                 gprintf("%d",score);
  92.             }
  93.             if (score == 10){   //speed increases with score
  94.                 speed = 5;
  95.             }
  96.             if (score == 25){
  97.                 speed = 4;
  98.             }
  99.             v++;        //gravity (v acts downwards)
  100.             y+=v/10;
  101.             move_sprite(0,30,y);        //move bird's left side
  102.             move_sprite(1,38,y);        //move bird's right side
  103.             if (joypad() & J_A && v > 0){v=-30;}    //jump if player presses A while bird is falling
  104.            
  105.             move_sprite(2,xt,96);       //move lower pipe sprite
  106.             move_sprite(3,xt+8,96);
  107.             move_sprite(6,xt,112);
  108.             move_sprite(7,xt+8,112);
  109.             move_sprite(8,xt,128);
  110.             move_sprite(9,xt+8,128);
  111.             move_sprite(10,xt,144);
  112.             move_sprite(11,xt+8,144);
  113.            
  114.             move_sprite(4,xb,64);       //upper pipe
  115.             move_sprite(5,xb+8,64);
  116.             move_sprite(12,xb,48);
  117.             move_sprite(13,xb+8,48);
  118.             move_sprite(14,xb,32);
  119.             move_sprite(15,xb+8,32);
  120.             move_sprite(16,xb,16);
  121.             move_sprite(17,xb+8,16);
  122.                                         //death conditions
  123.             if (y < 16 || y > 144){     //screen top or bottom
  124.                 alive = 0;
  125.             }
  126.             if (y > 82 && xt > 16 && xt < 46){  //collision with pipes
  127.                 alive = 0;
  128.             }
  129.             if (y < 80 && xb > 16 && xb < 46){
  130.                 alive = 0;
  131.             }
  132.            
  133.  
  134.         }   //end of "alive" loop
  135.        
  136.         if (joypad() & J_START){    //restart
  137.             y = 80, v = 0, xt = 125, xb = 250, alive = 1;
  138.            
  139.             move_sprite(0,30,80);   //initial bird position
  140.             move_sprite(1,38,80);
  141.            
  142.             move_sprite(2,125,96);  //initial pipe position
  143.             move_sprite(3,133,96);
  144.             move_sprite(6,125,112);
  145.             move_sprite(7,133,112);
  146.             move_sprite(8,125,128);
  147.             move_sprite(9,133,128);
  148.             move_sprite(10,125,144);
  149.             move_sprite(11,133,144);
  150.            
  151.             move_sprite(4,0,0);     //initial pipe position
  152.             move_sprite(5,0,0);
  153.             move_sprite(12,0,0);
  154.             move_sprite(13,0,0);
  155.             move_sprite(14,0,0);
  156.             move_sprite(15,0,0);
  157.             move_sprite(16,0,0);
  158.             move_sprite(17,0,0);
  159.            
  160.             gotogxy(5,3);           //clear text
  161.             gprintf("           ");
  162.             gotogxy(2,5);
  163.             gprintf("                 ");
  164.             gotogxy(4,14);
  165.             gprintf("            ");
  166.            
  167.             score = 0;
  168.             gotogxy(2,2);
  169.             gprintf("%d   ",score);
  170.             speed = 6;
  171.             waitpad(J_A);       //wait for player to press A
  172.         }
  173.     }
  174.                             //---------------------- end game loop ---------------------
  175.     }
Advertisement
Add Comment
Please, Sign In to add comment