Advertisement
theanonym

mario.c

Sep 19th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.50 KB | None | 0 0
  1. /*
  2. sudo apt-get install libsdl-dev
  3. gcc mario.c -o mario `sdl-config --cflags --libs`
  4. ./mario
  5. */
  6.  
  7.  
  8. #include <SDL/SDL.h>/* MURIO by jabberx 01/I/2013 collect all coins to win */
  9. SDL_Surface *screen; SDL_Event event; Uint8 bset=0; Uint8 KEY[4]; int lev=1;
  10. enum Keys{LEFT,RIGHT,UP,ESCAPE};Uint32 *img; const int WIDTH=128, HEIGHT=112;
  11. #define FLIP() if(SDL_MUSTLOCK(screen)) if(SDL_LockSurface(screen)<0) return; \
  12.     memcpy(screen->pixels, img, 4*WIDTH*HEIGHT*sizeof(int)); \
  13.     if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); SDL_Flip(screen);
  14. #define PSET(X,Y,C) if ((X)>0 && (X)<WIDTH && (Y)>0 && (Y)<HEIGHT) { \
  15.     img[(X)*2 + (Y)*2*WIDTH*2] = (C); img[(X)*2+1 + (Y)*2*WIDTH*2] = (C); \
  16.     img[(X)*2+((Y)*2+1)*WIDTH*2] = (C);img[(X)*2+1 + ((Y)*2+1)*WIDTH*2] = (C);}
  17. #define CLS(C) { int x, l = 4*WIDTH*HEIGHT; for (x=0;x<l;x++) img[x] = (C); }
  18. #define KEYUPD(KN) if(event.key.keysym.sym==SDLK_##KN)KEY[(KN)]=bset;
  19. #define INPUT() KEYUPD(LEFT); KEYUPD(RIGHT); KEYUPD(UP); KEYUPD(ESCAPE);
  20. #define SLEEP(T) SDL_Delay((T))
  21. #define ABS(X) (((X)<0)?-(X):(X))
  22. Uint32 COLOR[9],COLOR1[]={0,0xEE2211,0xEEAA88,0x884422,0xEE6633,0x22CC44,
  23. 0x7799AA,0xFFFFFF},COLOR3[]={0,0x1122EE,0x88CCEE,0x112244,0x447788,0x44CC22,
  24. 0x010101,0xFFFFFF},COLOR2[]={0,0xEE2211,0xEE6633,0x884422,0xEEAA88,0x22CC44,
  25. 0xAA7799,0xFFFFFF};enum Colors{ALPHA,RED,FACE,BLACK,ORANGE,GREEN,SKY,MWBLH};
  26. Uint8 mario1[] = { 0,1,1,1,1,0,4,4,4,2,4,0,4,4,2,2,2,2,0,4,2,2,4,4,
  27. 0,1,1,1,1,0,1,1,1,1,2,2,1,1,1,1,1,1,3,3,0,0,3,3 }; /* 6x8 */
  28. Uint8 mario2[] = { 0,1,1,1,1,0,4,4,4,2,4,0,4,4,2,2,2,2,0,4,2,2,4,4,
  29. 0,1,1,1,1,0,1,1,1,1,1,1,1,1,2,2,1,1,0,3,3,3,3,0 };
  30. Uint8 mario3[] = { 0,1,1,1,1,0,4,4,4,2,4,0,4,4,2,2,2,2,0,4,2,2,4,4,
  31. 0,1,1,1,1,0,1,1,1,1,1,1,2,2,1,1,1,1,3,3,0,0,3,3 };
  32. Uint8 bricks1[] = { 3,3,3,3,3,3,3,3,4,4,4,3,4,4,4,3,1,1,1,3,1,1,1,
  33. 3,3,3,3,3,3,3,3,3,4,3,4,4,4,3,4,4,1,3,1,1,1,3,1,1,3,3,3,3,3,3,3,3 }; /* 8x7 */
  34. Uint8 spikes1[] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,0,0,0,7,0,
  35. 0,0,7,0,0,0,7,0,0,7,7,7,0,7,7,7,7,7,7,7,7,7,7,7,3,3,3,3,3,3,3,3 }; /* 8x7 */
  36. Uint8 coin1[] = { 0,0,0,3,3,0,0,0,0,0,3,2,2,3,0,0,0,3,2,4,4,2,3,0,
  37. 0,3,2,4,4,2,3,0,0,3,2,4,4,2,3,0,0,0,3,2,2,3,0,0,0,0,0,3,3,0,0,0 }; /* 8x7 */
  38. Uint8 coin2[] = { 0,0,0,3,3,0,0,0,0,0,3,4,4,3,0,0,0,3,4,2,2,4,3,0,
  39. 0,3,4,2,2,4,3,0,0,3,4,2,2,4,3,0,0,0,3,4,4,3,0,0,0,0,0,3,3,0,0,0 }; /* 8x7 */
  40. Uint8 *coin[] = { coin1, coin2 }; float coin_frame = 0, dummy2_frame = 0;
  41. Uint8 *mario[] = { mario2, mario3, mario2, mario1 }; float mario_frame = 0;
  42. Uint8 *bricks[] = { bricks1 }, *spikes[] = { spikes1 }; float dummy_frame = 0;
  43. char level0[79*20], level2[] = "\
  44. ###############################################################################\
  45. #$$................$$.......................................................$.#\
  46. #$.................$$.........................................................#\
  47. #........$$$.......$$.................#.......................................#\
  48. #..................$$.................#.....................................#.#\
  49. #....................................$#..................#....................#\
  50. #................##..####....#$......$##......#...........#...................#\
  51. #..#............#.#..#........#$.....$#.......#^....#......#...............#.##\
  52. #..............#..#..#.........#$....$#.......##..............................#\
  53. #.....##......#...#^^#....##....#$...$#...#...#...............................#\
  54. #.....##.....#....####.....$.....#$..$#.......#..#.............####........####\
  55. #^^^^^##^^^^#.....................#$.$#.......#................#..............#\
  56. ############..........$....$.......#$$##......#................#..............#\
  57. #......................$$$$.........###.......##....#..........#....#^#^#.....#\
  58. #.............................................#...........$....#.....###......#\
  59. #..............................$...$..........#................#$$$$.....$$$$$#\
  60. #.............#####.....##......$$$......######..........####..#####.....######\
  61. #..............................$...$...........................#$$$$$$$$$$$$$$#\
  62. #..............................................................#$$$$$$..$$$$$$#\
  63. ###########..........#.......###########........########.......######^#^^######\
  64. ",level3[] = "\
  65. ###############################################################################\
  66. #$............................................................................#\
  67. #.............................................................................#\
  68. ##..........$$$.....$$$..................##.......#...........#..........#$$$$#\
  69. #..........$...$...$...$.................................................#$$$$#\
  70. #.............................##########^^^^^####^^^##^#^####^^^##^^#^##$######\
  71. #.....####........#.......#...##########################################$#$$$$#\
  72. #.#...........................#.$$$............$$$$...............$$$$.#$#$$$$#\
  73. #............#...#....#..#....#$$$$$$.........$$$$$$.............$$$$$$#$#....#\
  74. #.............................#...............$$$$$$...................#$#...##\
  75. #........^^#^^^^^^^^^^^^^^#.$.##########..................##############$#....#\
  76. ##.......##################.$##...........#............#...............#$#....#\
  77. #........#................#.$...$$$$$$........###.###..........$$$$$$..#$##...#\
  78. #........#................#.$..................................$$$$$$..#$#....#\
  79. #..........#.....$$$$.....#.$.##########...............................#$#....#\
  80. #....#......###..####.....#.$.#.............................############$#...##\
  81. #....................#....#.$.#................##.##...................#$#....#\
  82. #.............................#........................................$$$....#\
  83. #.........^^^^^^^.............#...............................$$$$$$$$$$$$$$$$#\
  84. ########################################^^^^^^###.#####^^^#####################\
  85. ",level1[] = "\
  86. ###############################################################################\
  87. #.$$$.........................................................................#\
  88. #$$$$$........................................................................#\
  89. #$$$$$.........................................$$$$$$$$$$$$$$$$$$$$$..........#\
  90. #######.............$.....................################################....#\
  91. #...........................$...............................................#.#\
  92. #........######...............................................................#\
  93. #.................#^^^#.......................................................#\
  94. #.................#####...#^^^#.....###^^^#..................................##\
  95. #.........................#####.....#######...................................#\
  96. #.............................................................................#\
  97. #...........................................................................#.#\
  98. #.............................................................................#\
  99. #.............................................................................#\
  100. #.............................................................................#\
  101. #........$$$........$$.............$$..$$.........$$$$$$$$$$$$$$$$$.......#...#\
  102. #.........................................................................$$$.#\
  103. #........................................................................$$$$$#\
  104. #........................................................................$$$$$#\
  105. #########...#######....########..##..##..#########.#.##.#.#.#.##.##.###########\
  106. ",level4[] = "\
  107. ###############################################################################\
  108. #$$....................$......................................................#\
  109. #.............................................................................#\
  110. #....###..............#.#.#.#.#.#.#########.......^#########..................#\
  111. #.......#.................................##########....$..##.................#\
  112. #........############........................................#................#\
  113. ###.................#...$....................................#................#\
  114. #...................#...$.........$......#...........#.......#######.........$#\
  115. #....####.#.##.##.###.........##......^..##.............#....#................#\
  116. #.....................................######.................#.........^##^#.$#\
  117. #.$$$.#^^^^^^^^#.............#^^^^^#.....####...............##.........########\
  118. #####################################....##########..........#................#\
  119. #.......#....................................................#................#\
  120. #.......#...............................................#....#$.#^^^#.........#\
  121. #.....$.#........##########....$$.$$$...$$......^^^^.........########.........#\
  122. #....$..#......$#.............$.$.$.$..$.$......####.........#................#\
  123. #.....$.###.##.........$.....$..$.$$$.$..$#.................##.$..............#\
  124. #..................................................$........$#$$$......#^^^#.$#\
  125. #..........................#^^^#............................$#$$$#.....########\
  126. ##############...####..#..########..###...##...#.#.###.#...#######..#.........#\
  127. ";const int MAP_W = 79, BRICK_W = 8, BRICK_H = 7, MAP_H = 20; int coins_total=0;
  128. SPRITE(int px, int py, int reverse, int stop,
  129.         Uint8 *anim[], float *frame, float speed, int frames, int w, int h) {
  130.     int x,y; Uint32 col; Uint8 *spr_fr;
  131.     if (stop) *frame = 0; *frame+=speed; if (*frame>=frames) *frame = 0;
  132.     spr_fr = anim[(int)*frame]; for(x=0;x<w;x++)for(y=0;y<h;y++) {
  133.         col = COLOR[spr_fr[x+y*w]]; if(!col) continue;
  134.         PSET(px+(reverse?w-x:x),py+y,col);  }}
  135. LOOP() { static float xs = 0, ys = 0, px = 7*3, py = 18*5, camx = 0, camy = 0;
  136.     static int a, dir = 0, moves = 0, x,y, collides = 0, can_jump = 0, capt=0;
  137.     static float mx, xstep, ystep, i; static Uint32 last_ticks, delta;
  138.     static char buffer[256]; if (KEY[ESCAPE]) exit(0); 
  139.     if (can_jump && KEY[UP] && ys>=0 && ys<1){
  140.             ys = -3.5; KEY[UP] = 0; can_jump = 0; }
  141.     if (KEY[LEFT]) xs = -1; else if (KEY[RIGHT]) xs = +1; else xs = 0;
  142.     if (ys<0) xs *= 1.5;       
  143. #define MARIO_COLLISION(XO,YO,CO) { \
  144.         int ix, iy; ix = (px+XO)/BRICK_W; iy = (py+YO)/(BRICK_H-1); \
  145.         if (ix>=0 && ix<MAP_W && iy>=0 && iy<MAP_H) { \
  146.             if (level1[ix+iy*MAP_W]==(CO)) { if ((CO)=='$'){ \
  147.             level1[ix+iy*MAP_W]='.'; coins_total--; }collides = 1; }}}
  148. #define COLLISION_COLLISION_COLLISION_COLLISION(CO) \
  149.     collides = 0; for (x=1;x<=7;x+=3) for (y=0;y<=8;y+=4){ \
  150.         MARIO_COLLISION(x,y,CO);}
  151.     if (ABS(xs)>ABS(ys)) mx = ABS(xs); else mx = ABS(ys);
  152.     mx *= 24; xstep = xs/mx; ystep = ys/mx;
  153.     for (i=0; i<mx; i++) {
  154.         px += xstep; COLLISION_COLLISION_COLLISION_COLLISION('#');
  155.         if (collides) px -= xstep;
  156.         py += ystep; COLLISION_COLLISION_COLLISION_COLLISION('#');
  157.         if (collides){if (ys<0){int ix, iy; ix = (px+3.5)/BRICK_W;
  158.         iy = (py-4)/(BRICK_H-1);if (ix>=0 && ix<MAP_W && iy>=0 && iy<MAP_H){
  159.             if (level1[ix+iy*MAP_W]=='.') { if (px>ix*BRICK_W) px--; else px++;
  160.             } else { ys *= 0.01; can_jump = 1;  }}
  161.             }else{ys *= 0.01; can_jump = 1; }py -= ystep;}}
  162.     ys = ys + 0.25; if (xs == 0) moves = 0; else moves = 1;
  163.     if (xs > 0) dir = 0; else if (xs < 0) dir = 1;
  164.     if (py>HEIGHT) { memcpy(level1,level0,79*20*sizeof(char)); coins_total=0;
  165.     for(a=0;a<79*20;a++) if (level0[a]=='$') coins_total++;
  166.     CLS(COLOR[SKY]); FLIP(); SLEEP(250); px = 7*3; py = 18*5; }
  167.     COLLISION_COLLISION_COLLISION_COLLISION('^');
  168.     if (collides) { memcpy(level1,level0,79*20*sizeof(char)); coins_total=0;
  169.     for(a=0;a<79*20;a++) if (level0[a]=='$') coins_total++;
  170.     CLS(COLOR[RED]); FLIP(); SLEEP(250); px = 7*3; py = 18*5; }
  171.     COLLISION_COLLISION_COLLISION_COLLISION('$');
  172.     if (coins_total==0) {
  173.     CLS(COLOR[GREEN]); FLIP(); SLEEP(500);
  174.     if (lev==1){memcpy(level0,level2,79*20*sizeof(char));
  175.         memcpy(level1,level0,79*20*sizeof(char));memcpy(COLOR,COLOR2,9*4);
  176.     }else if (lev==2){memcpy(level0,level3,79*20*sizeof(char));
  177.         memcpy(level1,level0,79*20*sizeof(char));memcpy(COLOR,COLOR3,9*4);
  178.     }else if (lev==3){memcpy(level0,level4,79*20*sizeof(char));
  179.         memcpy(level1,level0,79*20*sizeof(char));memcpy(COLOR,COLOR1,9*4);
  180.     }else if (lev==4){exit(0);} for(a=0;a<79*20;a++)
  181.     if (level0[a]=='$') coins_total++;lev++; px = 7*3; py = 18*5;}
  182.     camx = px - WIDTH/2+3; camy = py - HEIGHT/2+4;
  183.     CLS(COLOR[SKY]); coin_frame += 0.09; if (coin_frame>2) coin_frame=0;
  184.     for (x=0; x<(WIDTH+BRICK_W)/BRICK_W; x++)
  185.     for (y=0; y<MAP_H; y++) { const int ix = (camx + x*BRICK_W)/BRICK_W;
  186.         if (ix>=0 && ix<MAP_W) { dummy2_frame = coin_frame;
  187.             if(level1[ix+y*MAP_W]=='#')
  188.                 SPRITE(ix*BRICK_W-camx,y*(BRICK_H-1)-camy,
  189.                     0,1,bricks,&dummy_frame,1,1,8,7);
  190.             if(level1[ix+y*MAP_W]=='$')
  191.             SPRITE(ix*BRICK_W-camx,y*(BRICK_H-1)-camy,
  192.                     0,0,coin,&dummy2_frame,1,2,8,7);
  193.             if(level1[ix+y*MAP_W]=='^')
  194.             SPRITE(ix*BRICK_W-camx,y*(BRICK_H-1)-camy,
  195.                     0,1,spikes,&dummy_frame,1,1,8,7); }}
  196.     SPRITE(px - camx, py-camy, dir, !moves, mario, &mario_frame,0.18, 4, 6, 8);
  197.     FLIP(); if (capt){sprintf(buffer,"Coins:%d Level:%d/4", coins_total,lev);
  198.     SDL_WM_SetCaption(buffer, NULL);} delta = SDL_GetTicks() - last_ticks;
  199.     if (delta<14) SLEEP(14-delta); last_ticks = SDL_GetTicks(); capt=!capt; }
  200. int main(int argc, char **argv){
  201.     int t;screen = SDL_SetVideoMode(WIDTH*2, HEIGHT*2, 32, SDL_HWSURFACE);
  202.     memcpy(level0,level1,79*20*sizeof(char)); memcpy(COLOR,COLOR1,9*4);
  203.     for(t=0;t<79*20;t++) if (level0[t]=='$') coins_total++;
  204.     img = (Uint32*)malloc(4*WIDTH*HEIGHT*sizeof(Uint32)); for(;;) {
  205.     while(SDL_PollEvent(&event)){if(event.type==SDL_QUIT){SDL_Quit(); exit(0);}
  206.             if(event.type==SDL_KEYDOWN) { bset=1; INPUT(); }
  207.             if(event.type==SDL_KEYUP) { bset=0; INPUT(); }}LOOP();}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement