自己写的基于SDL的C语言游戏!

来源:互联网 发布:linux 添加www用户组 编辑:程序博客网 时间:2024/04/26 18:44

#include<stdlib.h>
#include<SDL/SDL.h>
#include<SDL/SDL_image.h>
#include<time.h>
SDL_Surface *screen;
SDL_Surface *bg,*fish,*fimg;
SDL_Surface *img0,*img1,*img2,*img3,*img4,*img5,*img6,*img7,*img8,*img9;//number picture
SDL_Surface *gate1,*gate2,*gate3,*gate4,*gate5,*gate6,*gate7,*gate8,*gate9;//gate picture
SDL_Surface *f1,*f2,*f3,*f4,*f5;//5 fish picture
SDL_Surface *e1,*e2,*e3;//enemy pic
SDL_Surface *gBox;
SDL_Surface *sMain,*helpBut,*startBut,*helpMain,*returnBut1,*returnBut2;
SDL_Surface *gOver,*returnMain1,*returnMain2,*restart1,*restart2,*gfinish;
SDL_Surface *gcup,*scorea,*gate;
Uint32 color;

int len=1;//fish length
int x=0,y=240,speed=2;
int state=0;//0没开始;1游戏开始;2游戏暂停;
int score=0,live=0;//score;live 0:die,1:live;
int foodCount=5;//food count
int enemyCount=1;//enemy count
//creat snake struct
struct snake{
 int x;
 int y;
}s[100];
//creact cup struct
struct Cup{
 int m;
 int n;
}cup[9];
//creat food struct
struct Food{
 float x,y;
 int isc;
}food[10];
//creat enemy struct
struct Enemy{
 float x,y;
 int die;
}enemy[10];
//creat gold box struct
struct Box{
 int x,y;
 int isExist;//0:noExist;1:Exist;
}box;
//initab game video
void init(){
 if(SDL_Init(SDL_INIT_VIDEO)<0){
  fprintf(stderr,"Can't init video!%s/n",SDL_GetError());
  exit(1);
 }
 screen=SDL_SetVideoMode(500,600,16,SDL_SWSURFACE);
 if(screen==NULL){
  fprintf(stderr,"Can't init video!%s/n",SDL_GetError());
  exit(1);
 }
 SDL_WM_SetCaption("GreekSnake",NULL);
 atexit(SDL_Quit);
 
}
//load a image(bmp,jpeg,jpg,gif,png)
SDL_Surface *load(char *file){
 SDL_Surface *img;
 img=IMG_Load(file);
 if(img==NULL){
  fprintf(stderr,"Can't init video!%s/n",SDL_GetError());
  exit(1);
 }
 return img;
}
//paint game background
int imgx=0;
void DrawBG(int x,int y){
 SDL_Rect dest,imgDest;
 dest.x=x;
 dest.y=y+50;
 if(imgx<2500){
  imgDest.x=imgx;
 }else{
  imgx=0;
  imgDest.x=imgx;
 }
 imgx+=1;
 imgDest.y=y;
 imgDest.w=500;
 imgDest.h=bg->h;
 SDL_BlitSurface(bg,&imgDest,screen,&dest);
}
//paint any picture in window
void DrawIMG(SDL_Surface *img,int x,int y){
 SDL_Rect dest;
 dest.x=x;
 dest.y=y+50;
 SDL_BlitSurface(img,NULL,screen,&dest);
}
//change snake struct x,y;and draw snake body;
void game(){
 printf("%d _____________/n",len);
 int i;
 for(i=len-1;i>=0;i--){
  s[i].x=s[i-1].x-32;
  s[i].y=s[i-1].y;
 }
 s[0].x=x;
 s[0].y=y;
 for(i=0;i<len;i++){
  DrawIMG(fish,s[i].x,s[i].y);
 }
 
}
//gameover
void gamePlay();
void initMain();
void gameInit();
void gameStart();
void gameOver(){
 int ix=1,mouseX,mouseY,bX,bY;
 while(1){
  SDL_Event event;
  while(SDL_PollEvent(&event)){
   switch (event.type) {
    case SDL_MOUSEMOTION:
     mouseX=event.motion.x;
     mouseY=event.motion.y;
     if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=320)&&(mouseY<=370))){
      ix=1;
     }else if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=430)&&(mouseY<=480))){
      ix=2;
     }else{
      ix=3;
     }
     break;
    case SDL_MOUSEBUTTONDOWN:
     if(event.button.button==1){
      bX=event.button.x;
      bY= event.button.y;
      printf("________%d----%d__/n",bX,bY);
      if(((bX>=195)&&(bX<=315))&&((bY>=320)&&(bY<=370))){
       //ix=1;
       color=SDL_MapRGB(screen->format,0,0,0);
       SDL_FillRect(screen,&screen->clip_rect,color);
       gamePlay();
       score=0;
       printf("restart/n");
      }else if(((bX>=195)&&(bX<=315))&&((bY>=430)&&(bY<=480))){
       //ix=2;
       initMain();
      }else{
       //ix=3;
      }
     }
     break;
    case SDL_QUIT:
     exit(0);
   }
  }
  DrawIMG(gOver,0,0);
  if(ix==1){
   DrawIMG(restart2,195,270);
   DrawIMG(returnMain1,195,377);
  }else if(ix==2){
   DrawIMG(restart1,195,270);
   DrawIMG(returnMain2,195,377);
  }else if(ix==3){
   DrawIMG(restart1,195,270);
   DrawIMG(returnMain1,195,377);
  }
  SDL_Flip(screen);
 }
}
void gameFinish(){
 int ix=1,mouseX,mouseY,bX,bY;
 while(1){
  SDL_Event event;
  while(SDL_PollEvent(&event)){
   switch (event.type) {
    case SDL_MOUSEMOTION:
     mouseX=event.motion.x;
     mouseY=event.motion.y;
     if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=280)&&(mouseY<=330))){
      ix=1;
     }else if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=390)&&(mouseY<=440))){
      ix=2;
     }else{
      ix=3;
     }
     break;
    case SDL_MOUSEBUTTONDOWN:
     if(event.button.button==1){
      bX=event.button.x;
      bY= event.button.y;
      printf("________%d----%d__/n",bX,bY);
      if(((bX>=195)&&(bX<=315))&&((bY>=280)&&(bY<=330))){
       //ix=1;
       gamePlay();
       //gameInit();
       //gameStart();
       printf("restart/n");
      }else if(((bX>=195)&&(bX<=315))&&((bY>=390)&&(bY<=440))){
       //ix=2;
       initMain();
      }else{
       //ix=3;
      }
     }
     break;
    case SDL_QUIT:
     exit(0);
   }
  }
  DrawIMG(gfinish,0,0);
  if(ix==1){
   DrawIMG(restart2,195,230);
   DrawIMG(returnMain1,195,337);
  }else if(ix==2){
   DrawIMG(restart1,195,230);
   DrawIMG(returnMain2,195,337);
  }else if(ix==3){
   DrawIMG(restart1,195,230);
   DrawIMG(returnMain1,195,337);
  }
  SDL_Flip(screen);
 }
}
//random creat food X,Y
float fx=100,fy=100;
void rXY(int i){
  fx=468;
  //fx=1+(int)(468.00*rand()/(RAND_MAX+1.00));
  
  //int i;
  //for(i=0;i<5;i++){
   fy=1+(466.00*rand()/(RAND_MAX+1.00));
   food[i].x=fx;
   food[i].y=fy;
   food[i].isc=1;
  //}
}
//paint food
void rFood(){
 int i;
 for(i=0;i<foodCount;i++){
  food[i].x=food[i].x-(i+0.3);
  if(food[i].x<0){
   food[i].x=468;
  }
  food[i].isc=1;
 }
 DrawIMG(f1,food[0].x,food[0].y);
 DrawIMG(f2,food[1].x,food[1].y);
 DrawIMG(f3,food[2].x,food[2].y);
 DrawIMG(f4,food[3].x,food[3].y);
 DrawIMG(f5,food[4].x,food[4].y);
}
//hit food
void hitFood(){
 int d1,d2,i;
 float r=32.00,d;
 for(i=0;i<5;i++){
  d1=abs(s[0].x-food[i].x);
  d2=abs(s[0].y-food[i].y);
  d=sqrt(d1*d1+d2*d2);//the length of food to fish
  if(d<r){
   score++;
    if((score%50)==0){
     len=len+1;
     enemyCount++;
    }
   food[i].isc=0;
  }
 }
}

//random enemy location
void rEXY(int i){
 float ex=468,ey,j;
 //for(j=0;j<5;j++){
 // while((ey=1+(int)(440.00*rand()/(RAND_MAX+1.00)))!=food[j].y){
   ey=1+(440.00*rand()/(RAND_MAX+1.00));
 // }
 //}
 enemy[i].x=ex;
 enemy[i].y=ey;
 enemy[i].die=1;
}
//random draw enemy
void rEnemy(){
 int i;
 for(i=0;i<enemyCount;i++){
  enemy[i].x=enemy[i].x-(i+1.2);
  if(enemy[i].x<0){
   enemy[i].x=468;
   rEXY(i);
  }
  enemy[i].die=1;
 }
 for(i=0;i<enemyCount;i++){
  if((i%2)==0){
   DrawIMG(e1,enemy[i].x,enemy[i].y);
  }else{
   DrawIMG(e2,enemy[i].x,enemy[i].y);
  }
 }
}
//hitEnemy
void hitEnemy(){
 int d1,d2,i,j;
 float r=32.00,d;
 for(j=0;j<enemyCount;j++){
  for(i=0;i<len;i++){
   d1=abs(s[i].x-enemy[j].x);
   d2=abs(s[i].y-enemy[j].y);
   d=sqrt(d1*d1+d2*d2);
   if(d<r){
     if(len>=1){
      len=len-1;
     }
     enemy[j].die=0;
   }
  }
 }
 if(len==0){
   //printf("game over/n");
   gameOver();
 }
}
//creat gold box XY
void rBXY(){
 float ex,ey;
 ex=1+(440.00*rand()/(RAND_MAX+1.00));
 ey=1+(468.00*rand()/(RAND_MAX+1.00));
 box.x=ex;
 box.y=ey;
 box.isExist=1;
}
//creat a gold box;
void rBox(){
  DrawIMG(gBox,box.x,box.y);
}
//hit gold box
void hitBox(){
 int d1,d2,i;
 float r=32.00,d;
 d1=abs(s[0].x-box.x);
 d2=abs(s[0].y-box.y);
 d=sqrt(d1*d1+d2*d2);
 if(d<r){
  score+=10;
  len++;
  box.isExist=0;
 }
}
//game init fish station
void gameInit(){
 srand((int)time(0));
 int i;
 len=1;
 for(i=0;i<len;i++){
  s[i].x=320-i*32;
  s[i].y=240;
 }
}
//start game and init Data;
void gameStart(){
 imgx=0;
 gameInit();
 x=0,y=240;
 enemyCount=1;
 live=1;
 score=0;
 int i;
 for(i=0;i<foodCount;i++){
  rXY(i);
 }
 rFood(fimg);
 for(i=0;i<enemyCount;i++){
  rEXY(i);
 }
 rEnemy(e1);
 state=1;
 //len=4;
}
//load number image
void loadIMG(){
 //load number picture>>>>>>>>>>>>
 img0=load("./img/num/0.gif");
 img1=load("./img/num/1.gif");
 img2=load("./img/num/2.gif");
 img3=load("./img/num/3.gif");
 img4=load("./img/num/4.gif");
 img5=load("./img/num/5.gif");
 img6=load("./img/num/6.gif");
 img7=load("./img/num/7.gif");
 img8=load("./img/num/8.gif");
 img9=load("./img/num/9.gif");
 //load gateNumber picturn>>>>>>>>>>>>>
 gate1=load("./img/1.gif");
 gate2=load("./img/2.gif");
 gate3=load("./img/3.gif");
 gate4=load("./img/4.gif");
 gate5=load("./img/5.gif");
 gate6=load("./img/6.gif");
 gate7=load("./img/7.gif");
 gate8=load("./img/8.gif");
 gate9=load("./img/9.gif");
 //load food fish picture>>>>>>>>>>>>>>>>>>>>>>>
 f1=load("./img/f1.gif");
 f2=load("./img/f2.gif");
 f3=load("./img/f3.gif");
 f4=load("./img/f4.gif");
 f5=load("./img/f5.gif");
 //load background>>>>>>>>>>>>>>>>>>>>>>>>>
 bg=load("./img/bg1.jpg");
 //load mian fish>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
 fish=load("./img/food.gif");
 //load enemy picturn>>>>>>>>>>>>>>>>>>>>>>
 e1=load("./img/em2.gif");
 e2=load("./img/e2.gif");
 //load gold box picture
 gBox=load("./img/box.gif");
 //load start main window picture>>>>>>>>>>>>>>>>>>>
 sMain=load("./img/main.jpg");
 //start button picture>>>>>>>>>
 helpBut=load("./img/help.jpg");
 startBut=load("./img/start.jpg");
 helpMain=load("./img/helpmain.jpg");
 returnBut1=load("./img/return1.jpg");
 returnBut2=load("./img/return2.jpg");
 //game over main window>>>>>>>>>>>>>>>>>>>>>>>>>>>
 gOver=load("./img/gameover.jpg");
 returnMain1=load("./img/returnMain1.jpg");
 returnMain2=load("./img/returnMain2.jpg");
 restart1=load("./img/restart1.jpg");
 restart2=load("./img/restart2.jpg");
 gfinish=load("./img/tongguan.jpg");
 //game load cup window>>>>>>>>>>>>>>>>>>>>>>>
 gcup=load("./img/test.gif");
 gate=load("./img/gate.gif");
 scorea=load("./img/score.gif");
}
//paint Score;
void initCup(int n)
{
 int i,k;
 k=n;
 /*if(i==0)
 {
  cup[i].m=400;
  cup[i].n=-48;
 }
 else if(i>0){*/
  for(i=0;i<=k;i++)
  {
   cup[i].m=400+i*10;
   cup[i].n=-48;
  }
 //}
}
void drawCup()
{
 int i,m;
 if(score>0&&score<49){
  m=0;
  initCup(m);
  /*if(i==0)
  {
   DrawIMG(fish,cup[i].m,cup[i].n);
  }*/
  for(i=0;i<=m;i++)
  {
   DrawIMG(gcup,cup[i].m,cup[i].n);
   //SDL_Flip(screen);
  }
 }
 if(score%50==0){
  m=score/50;
  initCup(m);
  for(i=0;i<=m;i++)
  {
   DrawIMG(gcup,cup[i].m,cup[i].n);
   //SDL_Flip(screen);
  }
 }
}
void drawNum(int n,int xnum,int ynum){
 int w=img0->w;
 if((n<=9)&&(n>=0)){
  DrawIMG(scorea,xnum,ynum);
  DrawIMG(gate,xnum+5*w-20,ynum);
  switch(n){
   case 0:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img0,xnum+3*w-20,ynum);
    break;
   case 1:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img1,xnum+3*w-20,ynum);
    break;
   case 2:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img2,xnum+3*w-20,ynum);
    break;
   case 3:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img3,xnum+3*w-20,ynum);
    break;
   case 4:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img4,xnum+3*w-20,ynum);
    break;
   case 5:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img5,xnum+3*w-20,ynum);
    break;
   case 6:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img6,xnum+3*w-20,ynum);
    break;
   case 7:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img7,xnum+3*w-20,ynum);
    break;
   case 8:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img8,xnum+3*w-20,ynum);
    break;
   case 9:
    DrawIMG(img0,xnum+2*w-20,ynum);
    DrawIMG(img9,xnum+3*w-20,ynum);
    break;
  }
 }else if((n>=10)&&(n<=99)){
  int a=n/10;
  int b=n%10;
  switch(a){
   case 0:
    DrawIMG(img0,xnum+2*w-20,ynum);
    break;
   case 1:
    
    DrawIMG(img1,xnum+2*w-20,ynum);
    break;
   case 2:
    
    DrawIMG(img2,xnum+2*w-20,ynum);
    break;
   case 3:
    
    DrawIMG(img3,xnum+2*w-20,ynum);
    break;
   case 4:
    
    DrawIMG(img4,xnum+2*w-20,ynum);
    break;
   case 5:
    DrawIMG(img5,xnum+2*w-20,ynum);
    break;
   case 6:
    
    DrawIMG(img6,xnum+2*w-20,ynum);
    break;
   case 7:
    
    DrawIMG(img7,xnum+2*w-20,ynum);
    break;
   case 8:
    
    DrawIMG(img8,xnum+2*w-20,ynum);
    break;
   case 9:
    
    DrawIMG(img9,xnum+2*w-20,ynum);
    break;
  }
  switch(b){
   case 0:
    DrawIMG(img0,xnum+3*w-20,ynum);
    break;
   case 1:
    
    DrawIMG(img1,xnum+3*w-20,ynum);
    break;
   case 2:
    
    DrawIMG(img2,xnum+3*w-20,ynum);
    break;
   case 3:
    
    DrawIMG(img3,xnum+3*w-20,ynum);
    break;
   case 4:
    
    DrawIMG(img4,xnum+3*w-20,ynum);
    break;
   case 5:
    
    DrawIMG(img5,xnum+3*w-20,ynum);
    break;
   case 6:
    
    DrawIMG(img6,xnum+3*w-20,ynum);
    break;
   case 7:
    
    DrawIMG(img7,xnum+3*w-20,ynum);
    break;
   case 8:
    
    DrawIMG(img8,xnum+3*w-20,ynum);
    break;
   case 9:
    
    DrawIMG(img9,xnum+3*w-20,ynum);
    break;
  }

 }else if((n>=100)&&(n<=999)){
  int a=n/100;
  int b=(n/10)%10;
  int c=n%10;
  switch(a){
   case 0:
    DrawIMG(img0,xnum+2*w-20,ynum);
    break;
   case 1:
    
    DrawIMG(img1,xnum+2*w-20,ynum);
    break;
   case 2:
    
    DrawIMG(img2,xnum+2*w-20,ynum);
    break;
   case 3:
    
    DrawIMG(img3,xnum+2*w-20,ynum);
    break;
   case 4:
    
    DrawIMG(img4,xnum+2*w-20,ynum);
    break;
   case 5:
    
    DrawIMG(img5,xnum+2*w-20,ynum);
    break;
   case 6:
    
    DrawIMG(img6,xnum+2*w-20,ynum);
    break;
   case 7:
    
    DrawIMG(img7,xnum+2*w-20,ynum);
    break;
   case 8:
    
    DrawIMG(img8,xnum+2*w-20,ynum);
    break;
   case 9:
    
    DrawIMG(img9,xnum+2*w-20,ynum);
    break;
  }
  switch(b){
   case 0:
    DrawIMG(img0,xnum+3*w-20,ynum);
    break;
   case 1:
    
    DrawIMG(img1,xnum+3*w-20,ynum);
    break;
   case 2:
    
    DrawIMG(img2,xnum+3*w-20,ynum);
    break;
   case 3:
    
    DrawIMG(img3,xnum+3*w-20,ynum);
    break;
   case 4:
    
    DrawIMG(img4,xnum+3*w-20,ynum);
    break;
   case 5:
    
    DrawIMG(img5,xnum+3*w-20,ynum);
    break;
   case 6:
    
    DrawIMG(img6,xnum+3*w-20,ynum);
    break;
   case 7:
    
    DrawIMG(img7,xnum+3*w-20,ynum);
    break;
   case 8:
    
    DrawIMG(img8,xnum+3*w-20,ynum);
    break;
   case 9:
    
    DrawIMG(img9,xnum+3*w-20,ynum);
    break;
  }
  switch(c){
   case 0:
    DrawIMG(img0,xnum+4*w-20,ynum);
    break;
   case 1:
    
    DrawIMG(img1,xnum+4*w-20,ynum);
    break;
   case 2:
    
    DrawIMG(img2,xnum+4*w-20,ynum);
    break;
   case 3:
    
    DrawIMG(img3,xnum+4*w-20,ynum);
    break;
   case 4:
    
    DrawIMG(img4,xnum+4*w-20,ynum);
    break;
   case 5:
    
    DrawIMG(img5,xnum+4*w-20,ynum);
    break;
   case 6:
    
    DrawIMG(img6,xnum+4*w-20,ynum);
    break;
   case 7:
    DrawIMG(img7,xnum+4*w-20,ynum);
    break;
   case 8:
    DrawIMG(img8,xnum+4*w-20,ynum);
    break;
   case 9:
    DrawIMG(img9,xnum+4*w-20,ynum);
    break;
  }
 }
}
void gotoGate(int i){
 switch(i){
  case 0:
   DrawIMG(gate1,150,150);
   break;
  case 1:
   DrawIMG(gate2,150,150);
   break;
  case 2:
   DrawIMG(gate3,150,150);
   break;
  case 3:
   DrawIMG(gate4,150,150);
   break;
  case 4:
   DrawIMG(gate5,150,150);
   break;
  case 5:
   DrawIMG(gate6,150,150);
   break;
  case 6:
   DrawIMG(gate7,150,150);
   break;
  case 7:
   DrawIMG(gate8,150,150);
   break;
  case 8:
   DrawIMG(gate9,150,150);
   break;
 }
}
//timer,use a function per 100ms;
void Timer(){
 static Uint32 next_time=0;
 int i;
 Uint32 now;
 now=SDL_GetTicks();
 if(next_time<=now){
  next_time+=100;
  DrawBG(0,0);
  game();
  rFood();
  hitFood();
  rEnemy();
  hitEnemy();
  //rBXY();
  if(score>=30){
   if((score%30)==0){
    rBox();
    hitBox();
   }
  }
 }
}
void gamePlay(){
 Uint8 *keys;
 gameInit();
 gameStart();
 int i=0;
 while(1){
  SDL_Event event;
  while(SDL_PollEvent(&event)){
   if(event.type==SDL_QUIT){
    exit(0);
   }
  }
  keys=SDL_GetKeyState(NULL);
  if(keys[SDLK_UP]){
   if(y<0){
    y=0;
   }else{
    y-=speed;
   }
  }
  if(keys[SDLK_DOWN]){
   if(y>466){
    y=466;
   }else{
    y+=speed;
   }
  }
  if(keys[SDLK_LEFT]){
   if(x<0){
    x=0;
   }else{
    x-=speed;
   }
  }
  if(keys[SDLK_RIGHT]){
   if(x>466){
    x=466;
   }else{
    x+=speed;
   }
  }
  if(keys[SDLK_SPACE]){
   speed=0;
   s[0].x=s[0].x;
   s[0].y=s[0].y;
  }
  if(keys[SDLK_a]){
   speed=2;
  }
  if(state==1){
   if(live==1){
    int i;
    for(i=0;i<foodCount;i++){
     if(food[i].isc==0){
      rXY(i);
     }
    }
    for(i=0;i<enemyCount;i++){
     if(enemy[i].die==0){
      rEXY(i);
     }
    }
    if(box.isExist==0){
     rBXY();
     printf("sdfsdf/n");
    }
    Timer();
    drawNum(score,0,-50);
    if(score==0){
     gotoGate(0);
     drawCup();
    }else if(score==50){
     drawCup();
     gotoGate(1);
     
    }else if(score==100){
     gotoGate(2);
     drawCup();
    }else if(score==150){
     gotoGate(3);
     drawCup();
    }else if(score==200){
     gotoGate(4);
     drawCup();
    }else if(score==250){
     gotoGate(5);
     drawCup();
    }else if(score==300){
     gotoGate(6);
     drawCup();
    }else if(score==350){
     gotoGate(7);
     drawCup();
    }else if(score==400){
     gotoGate(8);
     drawCup();
    }else if(score>=450){
     gameFinish();
    }
    
   }
   //hitSelf(endImg);
  }else if(state==0){
  }else if(state==2){
  }
  SDL_Flip(screen); 
 }
}
void initHelp()
{
 int ix=2,mouseX,mouseY,bX,bY;
 while(1){
  SDL_Event event;
  while(SDL_PollEvent(&event)){
   switch (event.type) {
    case SDL_MOUSEMOTION:
     printf("鼠标移动了%d,%d 到 (%d,%d)/n",
         event.motion.xrel, event.motion.yrel,
         event.motion.x, event.motion.y);
     mouseX=event.motion.x;
     mouseY=event.motion.y;
     if(((mouseX>=350)&&(mouseX<=420))&&((mouseY>=450)&&(mouseY<=500))){
      ix=1;
     }else{
      ix=2;
     }
     break;
    case SDL_MOUSEBUTTONDOWN:
     /*printf("鼠标 %d 点击在 (%d,%d)/n",
         event.button.button, event.button.x, event.button.y);*/
     if(event.button.button==1){
      bX=event.button.x;
      bY= event.button.y;
      if(((bX>=350)&&(bX<=420))&&((bY>=450)&&(bY<=500))){
       //ix=1;
       initMain();
       printf("hit/n");
      }
     }
     break;
    case SDL_QUIT:
     exit(0);
   }
  }
  DrawIMG(helpMain,0,0);
  if(ix==1){
   DrawIMG(returnBut1,350,400);
  }else if(ix==2){
   DrawIMG(returnBut2,350,400);
  }
  SDL_Flip(screen);
 }
}
void initMain(){
 Uint32 color;
 int ix=1,mouseX,mouseY,bX,bY;
 while(1){
  SDL_Event event;
  while(SDL_PollEvent(&event)){
   switch (event.type) {
    case SDL_MOUSEMOTION:
     printf("鼠标移动了%d,%d 到 (%d,%d)/n",
         event.motion.xrel, event.motion.yrel,
         event.motion.x, event.motion.y);
     mouseX=event.motion.x;
     mouseY=event.motion.y;
     if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=280)&&(mouseY<=330))){
      ix=1;
     }else if(((mouseX>=195)&&(mouseX<=315))&&((mouseY>=390)&&(mouseY<=440))){
      ix=2;
     }else{
      ix=3;
     }
     break;
    case SDL_MOUSEBUTTONDOWN:
     /*printf("鼠标 %d 点击在 (%d,%d)/n",
         event.button.button, event.button.x, event.button.y);*/
     if(event.button.button==1){
      bX=event.button.x;
      bY= event.button.y;
      if(((bX>=195)&&(bX<=315))&&((bY>=280)&&(bY<=330))){
       //ix=1;
       gamePlay();
      }else if(((bX>=195)&&(bX<=315))&&((bY>=390)&&(bY<=440))){
       //ix=2;
       initHelp();
      }else{
       //ix=3;
      }
     }
     break;
    case SDL_QUIT:
     exit(0);
   }
  }
  //SDL_Flip(screen);
  color=SDL_MapRGB(screen->format,0,0,0);
  SDL_FillRect(screen,&screen->clip_rect,color);
  DrawIMG(sMain,0,0);
  if(ix==1){
   DrawIMG(startBut,195,230);
  }else if(ix==2){
   DrawIMG(helpBut,195,337);
  }else if(ix==3){
   DrawIMG(sMain,0,0);
  }
  SDL_Flip(screen);
 }
}
//main function start!
int main(int argc, char *argv[])
{
 loadIMG();
 init();
 initMain();
 printf("Hello, world/n");
 return 0;
}
//需要的话到资源里下,图片之类的独有!