Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- sudo apt-get install libsdl-dev
- gcc mario.c -o mario `sdl-config --cflags --libs`
- ./mario
- */
- #include <SDL/SDL.h>/* MURIO by jabberx 01/I/2013 collect all coins to win */
- SDL_Surface *screen; SDL_Event event; Uint8 bset=0; Uint8 KEY[4]; int lev=1;
- enum Keys{LEFT,RIGHT,UP,ESCAPE};Uint32 *img; const int WIDTH=128, HEIGHT=112;
- #define FLIP() if(SDL_MUSTLOCK(screen)) if(SDL_LockSurface(screen)<0) return; \
- memcpy(screen->pixels, img, 4*WIDTH*HEIGHT*sizeof(int)); \
- if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); SDL_Flip(screen);
- #define PSET(X,Y,C) if ((X)>0 && (X)<WIDTH && (Y)>0 && (Y)<HEIGHT) { \
- img[(X)*2 + (Y)*2*WIDTH*2] = (C); img[(X)*2+1 + (Y)*2*WIDTH*2] = (C); \
- img[(X)*2+((Y)*2+1)*WIDTH*2] = (C);img[(X)*2+1 + ((Y)*2+1)*WIDTH*2] = (C);}
- #define CLS(C) { int x, l = 4*WIDTH*HEIGHT; for (x=0;x<l;x++) img[x] = (C); }
- #define KEYUPD(KN) if(event.key.keysym.sym==SDLK_##KN)KEY[(KN)]=bset;
- #define INPUT() KEYUPD(LEFT); KEYUPD(RIGHT); KEYUPD(UP); KEYUPD(ESCAPE);
- #define SLEEP(T) SDL_Delay((T))
- #define ABS(X) (((X)<0)?-(X):(X))
- Uint32 COLOR[9],COLOR1[]={0,0xEE2211,0xEEAA88,0x884422,0xEE6633,0x22CC44,
- 0x7799AA,0xFFFFFF},COLOR3[]={0,0x1122EE,0x88CCEE,0x112244,0x447788,0x44CC22,
- 0x010101,0xFFFFFF},COLOR2[]={0,0xEE2211,0xEE6633,0x884422,0xEEAA88,0x22CC44,
- 0xAA7799,0xFFFFFF};enum Colors{ALPHA,RED,FACE,BLACK,ORANGE,GREEN,SKY,MWBLH};
- 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,
- 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 */
- 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,
- 0,1,1,1,1,0,1,1,1,1,1,1,1,1,2,2,1,1,0,3,3,3,3,0 };
- 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,
- 0,1,1,1,1,0,1,1,1,1,1,1,2,2,1,1,1,1,3,3,0,0,3,3 };
- 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,
- 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 */
- 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,
- 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 */
- 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,
- 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 */
- 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,
- 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 */
- Uint8 *coin[] = { coin1, coin2 }; float coin_frame = 0, dummy2_frame = 0;
- Uint8 *mario[] = { mario2, mario3, mario2, mario1 }; float mario_frame = 0;
- Uint8 *bricks[] = { bricks1 }, *spikes[] = { spikes1 }; float dummy_frame = 0;
- char level0[79*20], level2[] = "\
- ###############################################################################\
- #$$................$$.......................................................$.#\
- #$.................$$.........................................................#\
- #........$$$.......$$.................#.......................................#\
- #..................$$.................#.....................................#.#\
- #....................................$#..................#....................#\
- #................##..####....#$......$##......#...........#...................#\
- #..#............#.#..#........#$.....$#.......#^....#......#...............#.##\
- #..............#..#..#.........#$....$#.......##..............................#\
- #.....##......#...#^^#....##....#$...$#...#...#...............................#\
- #.....##.....#....####.....$.....#$..$#.......#..#.............####........####\
- #^^^^^##^^^^#.....................#$.$#.......#................#..............#\
- ############..........$....$.......#$$##......#................#..............#\
- #......................$$$$.........###.......##....#..........#....#^#^#.....#\
- #.............................................#...........$....#.....###......#\
- #..............................$...$..........#................#$$$$.....$$$$$#\
- #.............#####.....##......$$$......######..........####..#####.....######\
- #..............................$...$...........................#$$$$$$$$$$$$$$#\
- #..............................................................#$$$$$$..$$$$$$#\
- ###########..........#.......###########........########.......######^#^^######\
- ",level3[] = "\
- ###############################################################################\
- #$............................................................................#\
- #.............................................................................#\
- ##..........$$$.....$$$..................##.......#...........#..........#$$$$#\
- #..........$...$...$...$.................................................#$$$$#\
- #.............................##########^^^^^####^^^##^#^####^^^##^^#^##$######\
- #.....####........#.......#...##########################################$#$$$$#\
- #.#...........................#.$$$............$$$$...............$$$$.#$#$$$$#\
- #............#...#....#..#....#$$$$$$.........$$$$$$.............$$$$$$#$#....#\
- #.............................#...............$$$$$$...................#$#...##\
- #........^^#^^^^^^^^^^^^^^#.$.##########..................##############$#....#\
- ##.......##################.$##...........#............#...............#$#....#\
- #........#................#.$...$$$$$$........###.###..........$$$$$$..#$##...#\
- #........#................#.$..................................$$$$$$..#$#....#\
- #..........#.....$$$$.....#.$.##########...............................#$#....#\
- #....#......###..####.....#.$.#.............................############$#...##\
- #....................#....#.$.#................##.##...................#$#....#\
- #.............................#........................................$$$....#\
- #.........^^^^^^^.............#...............................$$$$$$$$$$$$$$$$#\
- ########################################^^^^^^###.#####^^^#####################\
- ",level1[] = "\
- ###############################################################################\
- #.$$$.........................................................................#\
- #$$$$$........................................................................#\
- #$$$$$.........................................$$$$$$$$$$$$$$$$$$$$$..........#\
- #######.............$.....................################################....#\
- #...........................$...............................................#.#\
- #........######...............................................................#\
- #.................#^^^#.......................................................#\
- #.................#####...#^^^#.....###^^^#..................................##\
- #.........................#####.....#######...................................#\
- #.............................................................................#\
- #...........................................................................#.#\
- #.............................................................................#\
- #.............................................................................#\
- #.............................................................................#\
- #........$$$........$$.............$$..$$.........$$$$$$$$$$$$$$$$$.......#...#\
- #.........................................................................$$$.#\
- #........................................................................$$$$$#\
- #........................................................................$$$$$#\
- #########...#######....########..##..##..#########.#.##.#.#.#.##.##.###########\
- ",level4[] = "\
- ###############################################################################\
- #$$....................$......................................................#\
- #.............................................................................#\
- #....###..............#.#.#.#.#.#.#########.......^#########..................#\
- #.......#.................................##########....$..##.................#\
- #........############........................................#................#\
- ###.................#...$....................................#................#\
- #...................#...$.........$......#...........#.......#######.........$#\
- #....####.#.##.##.###.........##......^..##.............#....#................#\
- #.....................................######.................#.........^##^#.$#\
- #.$$$.#^^^^^^^^#.............#^^^^^#.....####...............##.........########\
- #####################################....##########..........#................#\
- #.......#....................................................#................#\
- #.......#...............................................#....#$.#^^^#.........#\
- #.....$.#........##########....$$.$$$...$$......^^^^.........########.........#\
- #....$..#......$#.............$.$.$.$..$.$......####.........#................#\
- #.....$.###.##.........$.....$..$.$$$.$..$#.................##.$..............#\
- #..................................................$........$#$$$......#^^^#.$#\
- #..........................#^^^#............................$#$$$#.....########\
- ##############...####..#..########..###...##...#.#.###.#...#######..#.........#\
- ";const int MAP_W = 79, BRICK_W = 8, BRICK_H = 7, MAP_H = 20; int coins_total=0;
- SPRITE(int px, int py, int reverse, int stop,
- Uint8 *anim[], float *frame, float speed, int frames, int w, int h) {
- int x,y; Uint32 col; Uint8 *spr_fr;
- if (stop) *frame = 0; *frame+=speed; if (*frame>=frames) *frame = 0;
- spr_fr = anim[(int)*frame]; for(x=0;x<w;x++)for(y=0;y<h;y++) {
- col = COLOR[spr_fr[x+y*w]]; if(!col) continue;
- PSET(px+(reverse?w-x:x),py+y,col); }}
- LOOP() { static float xs = 0, ys = 0, px = 7*3, py = 18*5, camx = 0, camy = 0;
- static int a, dir = 0, moves = 0, x,y, collides = 0, can_jump = 0, capt=0;
- static float mx, xstep, ystep, i; static Uint32 last_ticks, delta;
- static char buffer[256]; if (KEY[ESCAPE]) exit(0);
- if (can_jump && KEY[UP] && ys>=0 && ys<1){
- ys = -3.5; KEY[UP] = 0; can_jump = 0; }
- if (KEY[LEFT]) xs = -1; else if (KEY[RIGHT]) xs = +1; else xs = 0;
- if (ys<0) xs *= 1.5;
- #define MARIO_COLLISION(XO,YO,CO) { \
- int ix, iy; ix = (px+XO)/BRICK_W; iy = (py+YO)/(BRICK_H-1); \
- if (ix>=0 && ix<MAP_W && iy>=0 && iy<MAP_H) { \
- if (level1[ix+iy*MAP_W]==(CO)) { if ((CO)=='$'){ \
- level1[ix+iy*MAP_W]='.'; coins_total--; }collides = 1; }}}
- #define COLLISION_COLLISION_COLLISION_COLLISION(CO) \
- collides = 0; for (x=1;x<=7;x+=3) for (y=0;y<=8;y+=4){ \
- MARIO_COLLISION(x,y,CO);}
- if (ABS(xs)>ABS(ys)) mx = ABS(xs); else mx = ABS(ys);
- mx *= 24; xstep = xs/mx; ystep = ys/mx;
- for (i=0; i<mx; i++) {
- px += xstep; COLLISION_COLLISION_COLLISION_COLLISION('#');
- if (collides) px -= xstep;
- py += ystep; COLLISION_COLLISION_COLLISION_COLLISION('#');
- if (collides){if (ys<0){int ix, iy; ix = (px+3.5)/BRICK_W;
- iy = (py-4)/(BRICK_H-1);if (ix>=0 && ix<MAP_W && iy>=0 && iy<MAP_H){
- if (level1[ix+iy*MAP_W]=='.') { if (px>ix*BRICK_W) px--; else px++;
- } else { ys *= 0.01; can_jump = 1; }}
- }else{ys *= 0.01; can_jump = 1; }py -= ystep;}}
- ys = ys + 0.25; if (xs == 0) moves = 0; else moves = 1;
- if (xs > 0) dir = 0; else if (xs < 0) dir = 1;
- if (py>HEIGHT) { memcpy(level1,level0,79*20*sizeof(char)); coins_total=0;
- for(a=0;a<79*20;a++) if (level0[a]=='$') coins_total++;
- CLS(COLOR[SKY]); FLIP(); SLEEP(250); px = 7*3; py = 18*5; }
- COLLISION_COLLISION_COLLISION_COLLISION('^');
- if (collides) { memcpy(level1,level0,79*20*sizeof(char)); coins_total=0;
- for(a=0;a<79*20;a++) if (level0[a]=='$') coins_total++;
- CLS(COLOR[RED]); FLIP(); SLEEP(250); px = 7*3; py = 18*5; }
- COLLISION_COLLISION_COLLISION_COLLISION('$');
- if (coins_total==0) {
- CLS(COLOR[GREEN]); FLIP(); SLEEP(500);
- if (lev==1){memcpy(level0,level2,79*20*sizeof(char));
- memcpy(level1,level0,79*20*sizeof(char));memcpy(COLOR,COLOR2,9*4);
- }else if (lev==2){memcpy(level0,level3,79*20*sizeof(char));
- memcpy(level1,level0,79*20*sizeof(char));memcpy(COLOR,COLOR3,9*4);
- }else if (lev==3){memcpy(level0,level4,79*20*sizeof(char));
- memcpy(level1,level0,79*20*sizeof(char));memcpy(COLOR,COLOR1,9*4);
- }else if (lev==4){exit(0);} for(a=0;a<79*20;a++)
- if (level0[a]=='$') coins_total++;lev++; px = 7*3; py = 18*5;}
- camx = px - WIDTH/2+3; camy = py - HEIGHT/2+4;
- CLS(COLOR[SKY]); coin_frame += 0.09; if (coin_frame>2) coin_frame=0;
- for (x=0; x<(WIDTH+BRICK_W)/BRICK_W; x++)
- for (y=0; y<MAP_H; y++) { const int ix = (camx + x*BRICK_W)/BRICK_W;
- if (ix>=0 && ix<MAP_W) { dummy2_frame = coin_frame;
- if(level1[ix+y*MAP_W]=='#')
- SPRITE(ix*BRICK_W-camx,y*(BRICK_H-1)-camy,
- 0,1,bricks,&dummy_frame,1,1,8,7);
- if(level1[ix+y*MAP_W]=='$')
- SPRITE(ix*BRICK_W-camx,y*(BRICK_H-1)-camy,
- 0,0,coin,&dummy2_frame,1,2,8,7);
- if(level1[ix+y*MAP_W]=='^')
- SPRITE(ix*BRICK_W-camx,y*(BRICK_H-1)-camy,
- 0,1,spikes,&dummy_frame,1,1,8,7); }}
- SPRITE(px - camx, py-camy, dir, !moves, mario, &mario_frame,0.18, 4, 6, 8);
- FLIP(); if (capt){sprintf(buffer,"Coins:%d Level:%d/4", coins_total,lev);
- SDL_WM_SetCaption(buffer, NULL);} delta = SDL_GetTicks() - last_ticks;
- if (delta<14) SLEEP(14-delta); last_ticks = SDL_GetTicks(); capt=!capt; }
- int main(int argc, char **argv){
- int t;screen = SDL_SetVideoMode(WIDTH*2, HEIGHT*2, 32, SDL_HWSURFACE);
- memcpy(level0,level1,79*20*sizeof(char)); memcpy(COLOR,COLOR1,9*4);
- for(t=0;t<79*20;t++) if (level0[t]=='$') coins_total++;
- img = (Uint32*)malloc(4*WIDTH*HEIGHT*sizeof(Uint32)); for(;;) {
- while(SDL_PollEvent(&event)){if(event.type==SDL_QUIT){SDL_Quit(); exit(0);}
- if(event.type==SDL_KEYDOWN) { bset=1; INPUT(); }
- if(event.type==SDL_KEYUP) { bset=0; INPUT(); }}LOOP();}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement