Advertisement
Jhynjhiruu

Untitled

Apr 11th, 2019
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.76 KB | None | 0 0
  1. #include "pm.h" // read this library if you don't recognise anything in capital letters
  2.  
  3. #include "sprites.h" // 16x16 b/w + transparency bird sprite
  4. #include "map.h" // 8x8 b/w chequerboard map tile - formats for both of these can be found at https://wiki.sublab.net/index.php/PM_PRC
  5.  
  6. #include "input.h" // library for input - useful
  7.  
  8. #include "rng.h" // randomness
  9.  
  10. #define NUM_PIPES 4 // number of pipes, duh
  11.  
  12. // stuff for running once per frame
  13.  
  14. int frame_counter = 0; // used to count the number of frames that have passed
  15.  
  16. int trigger = 0;
  17.  
  18. void __interrupt irq_1_prc_done(void) // interrupt that is triggered after a frame is drawn
  19. {
  20.     IRQ_ACT1 |= IRQ1_PRC_COMPLETE; // acknowledge interrupt
  21.    
  22.     trigger = 1;
  23. }
  24.  
  25. struct Pipe {
  26.   unsigned char x;
  27.   unsigned char y;
  28.   unsigned char width;
  29.   unsigned char enabled;
  30. };
  31.  
  32. int main(void) // main function
  33. {
  34.    
  35.     PRC_MODE = COPY_ENABLE|SPRITE_ENABLE|MAP_ENABLE|MAP_16X12;
  36.     PRC_RATE = RATE_36FPS; // set up PRC to enable sprites, and a 16x12 tile background
  37.    
  38.     IRQ_ENA1 |= IRQ1_PRC_COMPLETE; // enable the frame complete interrupt to do fun stuff (see irq_1_prc_done)
  39.     IRQ_PRI1 = PRI1_PRC(1); // give this high priority
  40.    
  41.     for(int i = 0; i < 16 * 12; i++)
  42.         TILEMAP[i] = 0; // initialise the 16x12 tilemap with tile 0 (the chequerboard)
  43.    
  44.     PRC_SCROLL_X = 0;
  45.     PRC_SCROLL_Y = 0; // reset the background scroll values
  46.    
  47.     PRC_SPR = (long)sprites; // load in sprite tiles
  48.     PRC_MAP = (long)map; // load in map tiles
  49.    
  50.     int yspeed = 0;
  51.    
  52.     #define BIRD OAM[0]
  53.  
  54.     BIRD.x = 64;
  55.     BIRD.y = 0x10;
  56.     BIRD.tile = BIRD_TILE_0; // initialise the first and only sprite with position (32, 32), the bird sprite and enable it
  57.     BIRD.ctrl = OAM_ENABLE;
  58.    
  59.     #define GAME_OVER OAM[1]
  60.  
  61.     GAME_OVER.x = 56;
  62.     GAME_OVER.y = 40;
  63.     GAME_OVER.tile = GAME_OVER_TILE;
  64.     GAME_OVER.ctrl = 0;
  65.    
  66.     struct pipe[NUM_PIPES];
  67.    
  68.    
  69.     randomise_pipe_0();
  70.    
  71.     PIPE_1_0.x = 0x60;
  72.     PIPE_1_1.x = 0x50;
  73.     PIPE_1_2.x = 0x40;
  74.     PIPE_1_3.x = 0x30;
  75.     PIPE_1_4.x = 0x20;
  76.     PIPE_1_5.x = 0x10;
  77.    
  78.     PIPE_1_0.y = 0xF0;
  79.     PIPE_1_1.y = 0xF0;
  80.     PIPE_1_2.y = 0xF0;
  81.     PIPE_1_3.y = 0xF0;
  82.     PIPE_1_4.y = 0xF0;
  83.     PIPE_1_5.y = 0xF0;
  84.    
  85.     // randomise_pipe_1();
  86.    
  87.     int pipe_1_state = 0;
  88.    
  89.     for(;;) // do this infinitely:
  90.     {
  91.         while(!trigger)
  92.             ; // wait till the last frame has been drawn
  93.         trigger = 0;
  94.    
  95.         frame_counter++; // increment the frame counter
  96.    
  97.         if(frame_counter == 3) // if 4 frames have passed,
  98.         {
  99.             frame_counter = 0; // reset the counter,
  100.             PRC_SCROLL_Y++; // increment the map x scroll by 1
  101.             PRC_SCROLL_Y &= 0x1F; // equivalent to % 32; makes it appear to scroll infinitely
  102.            
  103.             if(PIPE_0_0.y < 0x30 && !pipe_1_state)
  104.             {
  105.                 pipe_1_state = 1;
  106.                 randomise_pipe_1();
  107.             }
  108.                        
  109.             if(PIPE_0_0.y > 0)
  110.             {
  111.                 PIPE_0_0.y--;
  112.                 PIPE_0_1.y--;
  113.                 PIPE_0_2.y--;
  114.                 PIPE_0_3.y--;
  115.                 PIPE_0_4.y--;
  116.                 PIPE_0_5.y--;
  117.             }
  118.             else
  119.             {
  120.                 randomise_pipe_0();
  121.                 PIPE_0_0.y = 0x50;
  122.                 PIPE_0_1.y = 0x50;
  123.                 PIPE_0_2.y = 0x50;
  124.                 PIPE_0_3.y = 0x50;
  125.                 PIPE_0_4.y = 0x50;
  126.                 PIPE_0_5.y = 0x50;
  127.             }
  128.            
  129.             if(PIPE_1_0.y > 0)
  130.             {
  131.                 PIPE_1_0.y--;
  132.                 PIPE_1_1.y--;
  133.                 PIPE_1_2.y--;
  134.                 PIPE_1_3.y--;
  135.                 PIPE_1_4.y--;
  136.                 PIPE_1_5.y--;
  137.             }
  138.             else
  139.             {
  140.                 randomise_pipe_1();
  141.                 PIPE_1_0.y = 0x50;
  142.                 PIPE_1_1.y = 0x50;
  143.                 PIPE_1_2.y = 0x50;
  144.                 PIPE_1_3.y = 0x50;
  145.                 PIPE_1_4.y = 0x50;
  146.                 PIPE_1_5.y = 0x50;
  147.             }
  148.            
  149.             if(pipe_1_state == 0)
  150.             {
  151.                 PIPE_1_0.y = 0x50;
  152.                 PIPE_1_1.y = 0x50;
  153.                 PIPE_1_2.y = 0x50;
  154.                 PIPE_1_3.y = 0x50;
  155.                 PIPE_1_4.y = 0x50;
  156.                 PIPE_1_5.y = 0x50;             
  157.             }
  158.            
  159.         }
  160.        
  161.         /*
  162.         ----------------PHYSICS----------------
  163.         */
  164.        
  165.         if(BIRD.x + yspeed > 0x00) // if the bird won't be killed by the next move,
  166.         {
  167.             BIRD.x += yspeed; // add the y speed to the bird's y position (screen is rotated so x is y)
  168.             if(!(frame_counter % 3))
  169.                 yspeed -= 1;
  170.         }
  171.         else
  172.         {
  173.             yspeed = 2;
  174.             /*IRQ_ENA1 &= ~IRQ1_PRC_COMPLETE; // disable the frame end interrupt
  175.             BIRD.ctrl &= ~OAM_ENABLE; // disable the bird sprite
  176.             break; // break this infinite loop*/
  177.         }
  178.        
  179.         if(BIRD.x > 0x60) // if the bird goes off the top of the screen,
  180.         {
  181.             yspeed = 0; // kill the y speed
  182.             BIRD.x = 0x60; // lock the height
  183.         }
  184.        
  185.         if(get_key(KEY_RIGHT))
  186.             yspeed = 2; // if the right key is pressed, jump
  187.        
  188.        
  189.     }
  190.    
  191.     // this happens when the bird dies
  192.    
  193.     IO_DATA |= BIT4; // write 1 to rumble data line to start rumbling
  194.     for(int j = 0; j < 5000; j++) // wait for a bit
  195.         ;
  196.     IO_DATA &= ~BIT4; // write 0 to rumble data line to stop rumbling
  197.    
  198.     GAME_OVER.ctrl = OAM_ENABLE; // display the lose sprite
  199.     for(int i = 0; i < 12; i++)
  200.     {
  201.         GAME_OVER.ctrl ^= OAM_INVERT; // flash it a few times
  202.         for(int j = 0; j < 1000; j++)
  203.             ;          
  204.     }
  205.    
  206.     return 0; // good practice
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement