www.pudn.com > SuperMario.zip > mario.cpp


#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include "engine.h" 
#include "pcx.h" 
#include "key.h" 
#include "mario.h" 
 
 
 
MARIO mario; 
WORLD world; 
BITMAP *BackScreen; 
 
void LoadMap(char fn[]) 
{ 
  FILE *MapFile; 
  char c; 
  int x=0,y=0; 
  if((MapFile=fopen(fn,"rb"))==NULL)exit(1); 
  while((c=fgetc(MapFile))!=EOF) 
  { 
    if(c=='\n') 
    { 
      y++; 
      x=0; 
    } 
    else 
    { 
      world.BackGround[x++][y]=c; 
    } 
  } 
  fclose(MapFile); 
} 
 
void ShowMap(BITMAP *screen,int x) 
{ 
 int i,j,start,start_x; 
 char data; 
 start=x/16; 
 start_x=x%16; 
 clean_bitmap(screen,16); 
 for(j=0;j<14;j++) 
   for(i=0;i<21;i++) 
   { 
     data=world.BackGround[i+start][j]; 
     if(data>='a'&&data<='z') 
       blit(screen,world.block[2][data-'a'],i*16-start_x,j*16-16,0,0,16,16); 
     if(data>='A'&&data<='Z') 
       blit_mask(screen,world.block[8][data-'A'],i*16-start_x,j*16-16,0,0,16,16); 
   } 
} 
 
 
void move() 
{ 
 mario.curr_frame=mario.mario[0]; 
 if(mario.x_speed>0) 
 { 
   mario.delay++; 
   mario.curr_frame=mario.mario[mario.frame]; 
   if(!(mario.delay%4)) 
   { 
     mario.frame++; 
     if(mario.frame>3)mario.frame=1; 
//     mario.curr_frame=mario.mario[mario.frame]; 
   } 
   mario.x+=mario.x_speed; 
   if(world.BackGround[(world.screen_x+mario.x)/16+1][mario.y/16+2]<'z' 
     &&world.BackGround[(world.screen_x+mario.x)/16+1][mario.y/16+2]>'a' 
     ||world.BackGround[(world.screen_x+mario.x)/16+1][mario.y/16+1]<'z' 
     &&world.BackGround[(world.screen_x+mario.x)/16+1][mario.y/16+1]>'a') 
   mario.x-=mario.x_speed; 
   if(mario.x>100) 
   { 
      mario.x-=mario.x_speed; 
      world.screen_x+=mario.x_speed; 
 
   } 
 } 
// if(mario.y_speed<0) 
// mario.y+=mario.y_speed; 
 if(mario.x_speed<0) 
 { 
   mario.delay++; 
   mario.curr_frame=mario.mario[mario.frame]; 
   if(!(mario.delay%4)) 
   { 
     mario.frame++; 
     if(mario.frame>3)mario.frame=1; 
   } 
   mario.x+=mario.x_speed; 
   if(world.BackGround[(world.screen_x+mario.x)/16][mario.y/16+2]<'z' 
     &&world.BackGround[(world.screen_x+mario.x)/16][mario.y/16+2]>'a' 
     ||world.BackGround[(world.screen_x+mario.x)/16][mario.y/16+1]<'z' 
     &&world.BackGround[(world.screen_x+mario.x)/16][mario.y/16+1]>'a') 
   mario.x-=mario.x_speed; 
   if(mario.x<0) 
   { 
     mario.x=0; 
//     -=mario.x_speed; 
     world.screen_x+=mario.x_speed; 
   } 
 } 
 
 mario.y_speed+=G; 
 if(!mario.land) 
   mario.curr_frame=mario.mario[6]; 
 if(mario.y_speed>0) 
 { 
   mario.Y+=mario.y_speed; 
   mario.y=mario.Y>>3; 
   if(world.BackGround[(world.screen_x+mario.x)/16][mario.y/16+2]<'z' 
     &&world.BackGround[(world.screen_x+mario.x)/16][mario.y/16+2]>'a' 
     ||world.BackGround[(world.screen_x+mario.x)/16+1][mario.y/16+2]<'z' 
     &&world.BackGround[(world.screen_x+mario.x)/16+1][mario.y/16+2]>'a') 
   { 
     //mario.Y-=mario.y_speed; 
    // mario.y=mario.Y>>3; 
     mario.Y=mario.Y/16*16-1; 
     mario.y=mario.y/16*16-1; 
     mario.y_speed=0; 
     mario.land=1; 
   } 
   mario.top=1; 
 } 
 
 if(mario.y_speed<0) 
 { 
   mario.top=0; 
   mario.Y+=mario.y_speed; 
   mario.y=mario.Y>>3; 
   if(world.BackGround[(world.screen_x+mario.x)/16][mario.y/16+1]<'z' 
      &&world.BackGround[(world.screen_x+mario.x)/16][mario.y/16+1]>'a' 
      ||world.BackGround[(world.screen_x+mario.x)/16+1][mario.y/16+1]<'z' 
      &&world.BackGround[(world.screen_x+mario.x)/16+1][mario.y/16+1]>'a') 
   { 
    // mario.Y=mario.Y/16*16; 
   //  mario.y=mario.y/16*16; 
     mario.y_speed=-mario.y_speed; 
     if(!mario.jump)mario.y_speed=0; 
   } 
 } 
} 
 
 
void key_read() 
{ 
  mario.jump=0; 
  int x_move=0; 
  if(key[RIGHT]) 
  { 
   //mario.dir|=DIR_RIGHT; 
   mario.x_speed++; 
   if(mario.x_speed>2) 
     mario.x_speed=2; 
   x_move=1; 
  } 
 
 /*if(key[UP]) 
    mario.y_speed-=3; 
   */ 
 
 if(key[LEFT]) 
 { 
  // mario.dir|=DIR_LEFT; 
   mario.x_speed--; 
   if(mario.x_speed<-2) 
     mario.x_speed=-2; 
   x_move=1; 
 } 
 
 if(key[CTRL]) 
   mario.x_speed<<=1; 
 
 if(key[ALT]) 
 { 
    mario.jump=1; 
    if(mario.land) 
    { 
      mario.y_speed=V0; 
     mario.land=0; 
    } 
    else if(!mario.top)// if(mario.jump) 
    { 
      mario.y_speed+=V1; 
    } 
 } 
 
 if(!x_move) 
 { 
  if(mario.x_speed>0) 
    mario.x_speed--; 
  if(mario.x_speed<0) 
    mario.x_speed++; 
 } 
} 
 
 
 
 
void main() 
{ 
 BITMAP *resource; 
 int i; 
 int x,y; 
 
 world.screen_x=0; 
 mario.x=20; 
 mario.y=150; 
 mario.Y=150*8; 
 mario.jump=0; 
 mario.land=1; 
 mario.frame=1; 
 mario.delay=0; 
 mario.x_speed=0; 
 mario.y_speed=0; 
 
 set_video_mode(320,200); 
 BackScreen=creat_bitmap(320,200); 
 resource=LoadPcxFile("smb.pcx"); 
 blit(screen,resource,0,0,0,0,resource->w,resource->h); 
 getch( ); 
 
 for(y=0;y<9;y++) 
 { 
  for(x=0;x<18;x++) 
  { 
   world.block[y][x]=creat_bitmap(16,16); 
   blit(world.block[y][x],resource,0,0,17*x+1,17*y+1,16,16); 
  } 
 } 
 clean_bitmap(screen,1); 
 getch( ); 
 
 for(y=0;y<7;y++) 
  mario.mario[y]=world.block[5][y]; 
 y=0; 
 
 LoadMap("level001.dat"); 
 
 x=0; 
 clean_bitmap(screen,0); 
 
  old_handler1=getvect(0x9); 
  old_handler2=getvect(0x1c); 
  for (i=0;i<128;i++) 
    key[i]=0; 
 
    setvect(0x9,key_handler); 
  setvect(0x1c,time_handler); 
 
 mario.curr_frame=mario.mario[1]; 
 while(!key[ESC]) 
 { 
 ShowMap(BackScreen,world.screen_x); 
 key_read(); 
 move(); 
 blit_mask(BackScreen,mario.curr_frame,mario.x,mario.y,0,0,16,16); 
 wait_vsync(); 
 blit(screen,BackScreen,8,0,16,0,BackScreen->w-16,BackScreen->h); 
 } 
 set_text_mode(); 
 setvect(0x9,old_handler1); 
 setvect(0x1c,old_handler2); 
}