Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pm.h" // read this library if you don't recognise anything in capital letters
- #include "sprites.h" // 16x16 b/w + transparency bird sprite
- #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
- #include "input.h" // library for input - useful
- #include "rng.h" // randomness
- #define NUM_PIPES 4 // number of pipes, duh
- // stuff for running once per frame
- int frame_counter = 0; // used to count the number of frames that have passed
- int trigger = 0;
- void __interrupt irq_1_prc_done(void) // interrupt that is triggered after a frame is drawn
- {
- IRQ_ACT1 |= IRQ1_PRC_COMPLETE; // acknowledge interrupt
- trigger = 1;
- }
- struct Pipe {
- unsigned char x;
- unsigned char y;
- unsigned char width;
- unsigned char enabled;
- };
- int main(void) // main function
- {
- PRC_MODE = COPY_ENABLE|SPRITE_ENABLE|MAP_ENABLE|MAP_16X12;
- PRC_RATE = RATE_36FPS; // set up PRC to enable sprites, and a 16x12 tile background
- IRQ_ENA1 |= IRQ1_PRC_COMPLETE; // enable the frame complete interrupt to do fun stuff (see irq_1_prc_done)
- IRQ_PRI1 = PRI1_PRC(1); // give this high priority
- for(int i = 0; i < 16 * 12; i++)
- TILEMAP[i] = 0; // initialise the 16x12 tilemap with tile 0 (the chequerboard)
- PRC_SCROLL_X = 0;
- PRC_SCROLL_Y = 0; // reset the background scroll values
- PRC_SPR = (long)sprites; // load in sprite tiles
- PRC_MAP = (long)map; // load in map tiles
- int yspeed = 0;
- #define BIRD OAM[0]
- BIRD.x = 64;
- BIRD.y = 0x10;
- BIRD.tile = BIRD_TILE_0; // initialise the first and only sprite with position (32, 32), the bird sprite and enable it
- BIRD.ctrl = OAM_ENABLE;
- #define GAME_OVER OAM[1]
- GAME_OVER.x = 56;
- GAME_OVER.y = 40;
- GAME_OVER.tile = GAME_OVER_TILE;
- GAME_OVER.ctrl = 0;
- struct pipe[NUM_PIPES];
- randomise_pipe_0();
- PIPE_1_0.x = 0x60;
- PIPE_1_1.x = 0x50;
- PIPE_1_2.x = 0x40;
- PIPE_1_3.x = 0x30;
- PIPE_1_4.x = 0x20;
- PIPE_1_5.x = 0x10;
- PIPE_1_0.y = 0xF0;
- PIPE_1_1.y = 0xF0;
- PIPE_1_2.y = 0xF0;
- PIPE_1_3.y = 0xF0;
- PIPE_1_4.y = 0xF0;
- PIPE_1_5.y = 0xF0;
- // randomise_pipe_1();
- int pipe_1_state = 0;
- for(;;) // do this infinitely:
- {
- while(!trigger)
- ; // wait till the last frame has been drawn
- trigger = 0;
- frame_counter++; // increment the frame counter
- if(frame_counter == 3) // if 4 frames have passed,
- {
- frame_counter = 0; // reset the counter,
- PRC_SCROLL_Y++; // increment the map x scroll by 1
- PRC_SCROLL_Y &= 0x1F; // equivalent to % 32; makes it appear to scroll infinitely
- if(PIPE_0_0.y < 0x30 && !pipe_1_state)
- {
- pipe_1_state = 1;
- randomise_pipe_1();
- }
- if(PIPE_0_0.y > 0)
- {
- PIPE_0_0.y--;
- PIPE_0_1.y--;
- PIPE_0_2.y--;
- PIPE_0_3.y--;
- PIPE_0_4.y--;
- PIPE_0_5.y--;
- }
- else
- {
- randomise_pipe_0();
- PIPE_0_0.y = 0x50;
- PIPE_0_1.y = 0x50;
- PIPE_0_2.y = 0x50;
- PIPE_0_3.y = 0x50;
- PIPE_0_4.y = 0x50;
- PIPE_0_5.y = 0x50;
- }
- if(PIPE_1_0.y > 0)
- {
- PIPE_1_0.y--;
- PIPE_1_1.y--;
- PIPE_1_2.y--;
- PIPE_1_3.y--;
- PIPE_1_4.y--;
- PIPE_1_5.y--;
- }
- else
- {
- randomise_pipe_1();
- PIPE_1_0.y = 0x50;
- PIPE_1_1.y = 0x50;
- PIPE_1_2.y = 0x50;
- PIPE_1_3.y = 0x50;
- PIPE_1_4.y = 0x50;
- PIPE_1_5.y = 0x50;
- }
- if(pipe_1_state == 0)
- {
- PIPE_1_0.y = 0x50;
- PIPE_1_1.y = 0x50;
- PIPE_1_2.y = 0x50;
- PIPE_1_3.y = 0x50;
- PIPE_1_4.y = 0x50;
- PIPE_1_5.y = 0x50;
- }
- }
- /*
- ----------------PHYSICS----------------
- */
- if(BIRD.x + yspeed > 0x00) // if the bird won't be killed by the next move,
- {
- BIRD.x += yspeed; // add the y speed to the bird's y position (screen is rotated so x is y)
- if(!(frame_counter % 3))
- yspeed -= 1;
- }
- else
- {
- yspeed = 2;
- /*IRQ_ENA1 &= ~IRQ1_PRC_COMPLETE; // disable the frame end interrupt
- BIRD.ctrl &= ~OAM_ENABLE; // disable the bird sprite
- break; // break this infinite loop*/
- }
- if(BIRD.x > 0x60) // if the bird goes off the top of the screen,
- {
- yspeed = 0; // kill the y speed
- BIRD.x = 0x60; // lock the height
- }
- if(get_key(KEY_RIGHT))
- yspeed = 2; // if the right key is pressed, jump
- }
- // this happens when the bird dies
- IO_DATA |= BIT4; // write 1 to rumble data line to start rumbling
- for(int j = 0; j < 5000; j++) // wait for a bit
- ;
- IO_DATA &= ~BIT4; // write 0 to rumble data line to stop rumbling
- GAME_OVER.ctrl = OAM_ENABLE; // display the lose sprite
- for(int i = 0; i < 12; i++)
- {
- GAME_OVER.ctrl ^= OAM_INVERT; // flash it a few times
- for(int j = 0; j < 1000; j++)
- ;
- }
- return 0; // good practice
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement