Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1427281
  • 博文数量: 430
  • 博客积分: 9995
  • 博客等级: 中将
  • 技术积分: 4388
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-24 18:04
文章存档

2013年(1)

2008年(2)

2007年(14)

2006年(413)

分类: LINUX

2006-05-26 10:42:07

/* ___________________________________________________________________________
        Russia-SDL  Russia.c
        Copyright (c) 2005 by yang_wm <>
 ___________________________________________________________________________
        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 1.4, or (at your option)
        any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

        See the file COPYING in the same directory for more information.
 ___________________________________________________________________________*/
 
/*header file*/
#include
#include
#include
#include

/*video attribute*/
#define WIDTH    315
#define HEIGHT   330
#define BPP      16

#define  TRUE  1
#define  FALSE 0

#define DOWN    0
#define LEFT    1
#define RIGHT   2
#define MAX_LEVELS 4

#define RED     0
#define YELLEW  1
#define WOOD    2
#define GREY    3

/*map of background*/
int bg[22][16]={
 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
};

/*some struct define*/
typedef struct GameEngine {
  SDL_Surface *screen,
           *background,
              *swapscreen,
              *brick,
              *brickg, 
              *brickc, 
              *brickr, 
     *bricky,
           *brickw,
           *lable,
     *start,
     *backcover,
           *black;
  SDL_Event   event;
  int FULLSCREEN;
  Uint32 last_time;
  Uint32 now_time;
  int clock;
  int level;
  Uint32 score; 
}GameEngine;

typedef enum GameState {
  GAME_START, 
  GAME_RUNNING,
  GAME_RESET,
  GAME_WIN,
  GAME_MENU,
  GAME_OVER,
  GAME_EXIT,
  GAME_STOP 
}GameState;

typedef struct KeyState {
  int Left,Right,Down,Quit,stop,transform,fast_down;
}KeyState;

typedef struct Brick_Move{
   int x,y,data;
  }Brick_Move;

Mix_Chunk *go,*show,*over;
GameState    game_state;
GameEngine   game_engine;
KeyState     key_state;
Brick_Move brick_move[][4]={ 
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
 };
Brick_Move brick_move_Transform[][4]={ 
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
 };
Brick_Move brick_Transform[][4]={ 
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
  {{0,0,0},{0,0,0},{0,0,0},{0,0,0}},
 };
int speed=0,Speed_Copy=0,brick_color=0,brick_color_copy=0,Sound=1;

void GameEngine_Init( GameEngine *ge );
void Video_Set( GameEngine *ge ) ;
void Video_Full( GameEngine *ge );
void GameEngine_Close( GameEngine *ge );
void Load_Data( GameEngine *ge );
void Video_Close( GameEngine *ge );
void Game_Menu ();
int  EVENT_FILTER(const SDL_Event *event);
void BoreBrick(GameEngine *);
void Move_Down();
int  Load_Brick(GameEngine *ge);
void ClearBrick(GameEngine *ge);
void DrawBrick(GameEngine *ge);
void Load_Background(GameEngine *ge);
void write_brick_to_bg();
int  Gameover_judge();
void Show_GameOver(GameEngine *ge);
void Left_Right_move(int direction);
int  Down_Move_Able();
void Transform_Brick_Angle(int What_brick);
void Transform_one(int What_brick);
void Transform_seven(int What_brick);
void Transform_S(int What_brick);
void Transform_Z(int What_brick);
void Transform_F(int What_brick);
void Transform_T(int What_brick);
int  Cycle_transform(int What_brick);
void Down_Move();
int  Judge_After_Transform_Able(int No);
int  Judge_Transform_Able();
void Backlog_Return(int marker);
void Cut_Brick(GameEngine *ge);
void Show_Score(GameEngine *ge);
void load_Map(GameEngine *ge);
void Show_Level(GameEngine *ge,int level);
void Brick_Transform();
void Fast_Down();
void PlaySound(Mix_Chunk *snd);
/* ----------------------------------- */
int main()

   game_state=GAME_START;  
  while(game_state!=GAME_EXIT){  
    switch(game_state){
      case GAME_START:
        game_state=GAME_MENU;   
        GameEngine_Init(&game_engine);
  Load_Background(&game_engine);  
        break;
      case GAME_MENU:
        printf("Run GameMenu.......\n");
     game_state = GAME_RUNNING;       
        break;
 case GAME_RUNNING:  
  Load_Data(&game_engine);
  Move_Down(&game_engine);
    break;
 case GAME_WIN:
  game_engine.level++;
        if(game_engine.level>MAX_LEVELS-1)game_engine.level=0;
        game_state=GAME_RUNNING;
   break;
 case GAME_EXIT:  
      Video_Close(&game_engine);
      GameEngine_Close( &game_engine );
        break; 
      }
 }
    return 0; 
}
/* ----------------------------------- */
void GameEngine_Init( GameEngine *ge )

  int SVolume=128;

  if(!Video_Init(ge)){
    printf("Can't init video!\n");
    exit(1);
  }

  SDL_WM_SetCaption("俄罗斯方块", "方块");        
  SDL_ShowCursor(0);
 
  ge->swapscreen=SDL_CreateRGBSurface(SDL_SRCALPHA | SDL_HWSURFACE,WIDTH,HEIGHT,BPP,
                                      ge->screen->format->Rmask,
                                      ge->screen->format->Gmask,
                                      ge->screen->format->Bmask,
                                      ge->screen->format->Amask);
  if(ge->swapscreen==NULL)fprintf(stderr,"Can't allocate SwapScreen!\n");
  ge->swapscreen=SDL_DisplayFormat(ge->swapscreen);

  SDL_SetEventFilter (EVENT_FILTER);     
  SDL_EnableKeyRepeat(0,0);
 
  
  if(Mix_OpenAudio(22050,AUDIO_S16,1,1024)<0){ 
     fprintf(stderr,"Couldn't set 22050 Hz 16-bit audio :%s\n",SDL_GetError());
     Sound=0;
   }
  if(Sound){
   Mix_AllocateChannels(2);
   Mix_Volume(-1,SVolume);
   go=Mix_LoadWAV("./sound/go.wav");
   show=Mix_LoadWAV("./sound/show.wav");
 over=Mix_LoadWAV("./sound/isover.wav");
   }
 
  ge->background=IMG_Load("image/background.png");
  SDL_SetColorKey(ge->background,SDL_SRCCOLORKEY,SDL_MapRGB(ge->background->format,255,0,255));
  ge->background=SDL_DisplayFormat(ge->background);
 
  ge->brickr=IMG_Load("image/brickr.png");
  SDL_SetColorKey(ge->brickr,SDL_SRCCOLORKEY,SDL_MapRGB(ge->brickr->format,255,0,255));
  ge->brickr=SDL_DisplayFormat(ge->brickr);
 
  ge->bricky=IMG_Load("image/bricky.png");
  SDL_SetColorKey(ge->bricky,SDL_SRCCOLORKEY,SDL_MapRGB(ge->bricky->format,255,0,255));
  ge->bricky=SDL_DisplayFormat(ge->bricky);

  ge->brickw=IMG_Load("image/brickw.png");
  SDL_SetColorKey(ge->brickw,SDL_SRCCOLORKEY,SDL_MapRGB(ge->brickw->format,255,0,255));
  ge->brickw=SDL_DisplayFormat(ge->brickw);

  ge->brickg=IMG_Load("image/brickg.png");
  SDL_SetColorKey(ge->brickg,SDL_SRCCOLORKEY,SDL_MapRGB(ge->brickg->format,255,0,255));
  ge->brickg=SDL_DisplayFormat(ge->brickg); 

  ge->black=IMG_Load("image/black.png");
  SDL_SetColorKey(ge->black,SDL_SRCCOLORKEY,SDL_MapRGB(ge->black->format,255,0,255));
  ge->black=SDL_DisplayFormat(ge->black);  
 
  ge->lable=IMG_Load("image/lable.png");
  SDL_SetColorKey(ge->lable,SDL_SRCCOLORKEY,SDL_MapRGB(ge->lable->format,255,0,255));
  ge->lable=SDL_DisplayFormat(ge->lable);

  ge->start=IMG_Load("image/start.png");
  SDL_SetColorKey(ge->start,SDL_SRCCOLORKEY,SDL_MapRGB(ge->start->format,255,0,255));
  ge->start=SDL_DisplayFormat(ge->start); 

  ge->backcover=IMG_Load("image/backcover.png");
  SDL_SetColorKey(ge->backcover,SDL_SRCCOLORKEY,SDL_MapRGB(ge->backcover->format,255,0,255));
  ge->backcover=SDL_DisplayFormat(ge->backcover);
 
  ge->level=0;
  ge->score=0;
  srand(time(NULL));

}
/* ----------------------------------- */
int Video_Init( GameEngine *ge )          
{
  Uint32 InitFlags;
  InitFlags=SDL_INIT_VIDEO;
  if(Sound)InitFlags|=SDL_INIT_AUDIO;
   
  if(SDL_Init(InitFlags)<0){             
    printf("Can't init SDL:%s\n",SDL_GetError());
    return FALSE;
  }
  atexit(SDL_Quit);
  Video_Set(ge);
  return TRUE;
}
/* ----------------------------------- */
void Video_Set( GameEngine *ge )          
{
  Uint32 VideoFlags;

  if(ge->FULLSCREEN){
    VideoFlags= SDL_SRCALPHA | SDL_SWSURFACE | SDL_FULLSCREEN;
  }
  else VideoFlags= SDL_SRCALPHA | SDL_SWSURFACE;

  if((ge->screen= SDL_SetVideoMode( WIDTH,HEIGHT,BPP,VideoFlags ))==NULL){
    printf("Couldn't set %ix%i video mode: %s\n",
            WIDTH,HEIGHT,SDL_GetError());
    Video_Close(ge);
    exit(2);
  }
}
/* ----------------------------------- */
void GameEngine_Close( GameEngine *ge )
{
  if(ge->swapscreen!=NULL)SDL_FreeSurface(ge->swapscreen);
  if(ge->brick!=NULL)SDL_FreeSurface(ge->brick);
  if(ge->brickg!=NULL)SDL_FreeSurface(ge->brickg);
  if(ge->brickc!=NULL)SDL_FreeSurface(ge->brickc);  
  if(ge->brickr!=NULL)SDL_FreeSurface(ge->brickr);  
  if(ge->bricky!=NULL)SDL_FreeSurface(ge->bricky);  
  if(ge->brickw!=NULL)SDL_FreeSurface(ge->brickw); 
  if(ge->background!=NULL)SDL_FreeSurface(ge->background);
  if(ge->black!=NULL)SDL_FreeSurface(ge->black);
  if(ge->lable!=NULL)SDL_FreeSurface(ge->lable);
  if(ge->start!=NULL)SDL_FreeSurface(ge->start);
  if(ge->backcover!=NULL)SDL_FreeSurface(ge->backcover);
  if(Sound){
   Mix_FreeChunk(go);
   Mix_FreeChunk(show);
   Mix_FreeChunk(over);  
   }   
}
/* ----------------------------------- */
void Load_Data( GameEngine *ge )
{
 int i,j;
 SDL_Rect rect;
 if(ge->level==0){
 Speed_Copy=speed=600;
 int tmpbg[][16]={
 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
 1,0,0,0,0,0,0,0,0,0,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1,1,1,1,1,1,1,1,1,1,1,1,1, 
 }; 
 for(i=0;i<22;i++)
 for(j=0;j<16;j++)
  bg[i][j]=tmpbg[i][j]; 
   }
 load_Map(ge);
 Show_Level(ge,ge->level);
 rect.w=150;
   rect.h=20;
   rect.x=45;
   rect.y=155;
 SDL_BlitSurface(ge->start,NULL,ge->screen,&rect);    
 SDL_UpdateRect(ge->screen,0,0,0,0);
 SDL_Delay(1000);
 SDL_BlitSurface(ge->backcover,NULL,ge->screen,&rect);

  key_state.Down=0;
  key_state.Left=0;
  key_state.Right=0;
  key_state.Quit=0;
  ge->score=0;
  key_state.transform=0;
  key_state.fast_down=0;   

}
void Video_Close( GameEngine *ge )
{
  SDL_Quit();
}
/* ----------------------------------- */
void Game_Menu ()
{
 
}

/*-----------------------------------------*/
int  EVENT_FILTER(const SDL_Event *event)

  switch(event->type){
    case SDL_KEYDOWN:
        key_state.Down=1;
      switch(event->key.keysym.sym){  
  case SDLK_UP:  
    key_state.transform=1;
    break;  
        case SDLK_LEFT:   
    key_state.Left=1;
    break;       
        case SDLK_DOWN:
    Speed_Copy=speed;   
    speed=20;
    break;
        case SDLK_RIGHT:   
    key_state.Right=1;
    break;
        case SDLK_ESCAPE:
          key_state.Quit=1;
          break;
        case SDLK_SPACE:
          key_state.fast_down=1;
          break;  
  case SDLK_F4:   
    Video_Full(&game_engine); 
    break;
      }
      break;
    case SDL_KEYUP:     
      switch(event->key.keysym.sym){  
  case SDLK_UP:  
    key_state.transform=0;
  break;
        case SDLK_LEFT:   
    key_state.Left=0;         
    break;       
        case SDLK_DOWN:   
    speed=Speed_Copy;
    break;
        case SDLK_RIGHT:   
    key_state.Right=0;         
    break;
        case SDLK_ESCAPE:
          key_state.Quit=0;
          break;
        case SDLK_SPACE:
          key_state.fast_down=0;
          break;  
 }
    default:
      return(1);
  }
  return(0);
}
/* ----------------------------------- */
int Load_Brick(GameEngine *ge)
{
    SDL_Rect rect;
 int i,j;
 int randnum;
 randnum=(int)(19.0*rand()/(RAND_MAX+1.0));
 switch(randnum)
 {
  case 0:{
  int brick_[][4]={
   {1,1,1,1},
   {0,0,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=RED;  
  break; 
  }
  case 1:{
   int brick_[][4]={
   {1,0,0,0},
   {1,0,0,0},
   {1,0,0,0},
   {1,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=RED;  
  break;
  }
  case 2:{
   int brick_[][4]={
   {1,1,0,0},
   {1,1,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=RED;  
  break;
 }
  case 3:{
   int brick_[][4]={
   {1,1,0,0},
   {0,1,0,0},
   {0,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=YELLEW;  
  break;
 }
  case 4:{
   int brick_[][4]={
   {1,1,1,0},
   {1,0,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=YELLEW;   
  break;
 }
  case 5:{
   int brick_[][4]={
   {1,0,0,0},
   {1,0,0,0},
   {1,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=YELLEW;   
  break;
 }
  case 6:{
   int brick_[][4]={
   {0,0,1,0},
   {1,1,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=YELLEW;   
  break;
 } 
  case 7:{
   int brick_[][4]={
   {1,1,0,0},
   {1,0,0,0},
   {1,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=WOOD;   
  break;
 }
  case 8:{
   int brick_[][4]={
   {1,0,0,0},
   {1,1,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=WOOD;   
  break;
 }
  case 9:{
   int brick_[][4]={
   {0,1,0,0},
   {0,1,0,0},
   {1,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=WOOD;   
  break;
 } 
  case 10:{
   int brick_[][4]={
   {1,1,1,0},
   {0,0,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=WOOD;   
  break;
 } 
  case 11:{
   int brick_[][4]={
   {1,1,0,0},
   {0,1,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=RED;   
  break;
 }
  case 12:{
   int brick_[][4]={
   {0,1,0,0},
   {1,1,0,0},
   {1,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=RED;  
  break;
 }
  case 13:{
   int brick_[][4]={
   {0,1,1,0},
   {1,1,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=GREY;  
  break;
 }   
  case 14:{
   int brick_[][4]={
   {1,0,0,0},
   {1,1,0,0},
   {0,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=GREY;  
  break;
 }
  case 15:{
   int brick_[][4]={
   {0,1,0,0},
   {1,1,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=GREY;  
  break;
 }
  case 16:{
   int brick_[][4]={
   {1,0,0,0},
   {1,1,0,0},
   {1,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=GREY;  
  break;
 }
  case 17:{
   int brick_[][4]={
   {1,1,1,0},
   {0,1,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=GREY;  
  break;
 }
  case 18:{
   int brick_[][4]={
   {0,1,0,0},
   {1,1,0,0},
   {0,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=GREY;  
  break;
 } 
  default :{
   int brick_[][4]={
   {0,1,1,0},
   {1,1,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_Transform[i][j].data=brick_[i][j];
            brick_Transform[i][j].x=8+j-1;
            brick_Transform[i][j].y=2+i-1;   
  }
  brick_color_copy=RED;  
  break;
 } 
}   
        switch(brick_color_copy){
   case RED:
    ge->brickc=ge->brickr;
    break;
   case YELLEW:
    ge->brickc=ge->bricky;
    break;
   case WOOD:
    ge->brickc=ge->brickw;
    break;
   case GREY:
    ge->brickc=ge->brickg;
    break;   
  }
  rect.h=rect.w=60;
        rect.x=240;
        rect.y=15;
  SDL_BlitSurface(ge->lable,NULL,ge->screen,&rect);
     rect.h=rect.w=15;
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
  {
   if(!brick_Transform[i][j].data)continue;   
   rect.x=j*15+240;
   rect.y=i*15+15;
   SDL_BlitSurface(ge->brickc,NULL,ge->screen,&rect);
  }
  return randnum;
  
}
/* ----------------------------------- */
void Load_Background(GameEngine *ge)
{
 SDL_BlitSurface(ge->background,NULL,ge->screen,NULL);
    SDL_UpdateRect(ge->screen,0,0,0,0);  
}
/* ----------------------------------- */
void Move_Down(GameEngine *ge)
{
 Uint32 Time_Interval;
    int i,j,What_brick,Transfrom_brick;
 
 What_brick=Load_Brick(ge);
 ge->brick=ge->brickc;
 brick_color=brick_color_copy;
 Brick_Transform();
 DrawBrick(ge);
 Transfrom_brick=Load_Brick(ge);
 Time_Interval=SDL_GetTicks();
    while(game_state==GAME_RUNNING){
  SDL_PollEvent(&(ge->event));
     if(key_state.Quit)game_state = GAME_EXIT;
  if(key_state.transform){
   SDL_Delay(70);
   ClearBrick(ge);
   Transform_Brick_Angle(What_brick);
   DrawBrick(ge);
   What_brick=Cycle_transform(What_brick);
  }
  if(key_state.Left && Left_Right_move_Able(LEFT)){
   SDL_Delay(50);
   ClearBrick(ge);
   Left_Right_move(LEFT);    
   DrawBrick(ge);
  }
  if(key_state.Right && Left_Right_move_Able(RIGHT)){
   SDL_Delay(50);
   ClearBrick(ge); 
   Left_Right_move(RIGHT);   
   DrawBrick(ge);
  }
  if(key_state.fast_down){
   ClearBrick(ge);
   Fast_Down();
   DrawBrick(ge);   
   if(Gameover_judge()){   
    Show_GameOver(ge);
    PlaySound(over);
    SDL_Delay(1000);
    game_state = GAME_EXIT;
   }
   else{
    PlaySound(go);    
    write_brick_to_bg();
    SDL_Delay(100);
    Cut_Brick(ge);
    What_brick=Transfrom_brick;
    ge->brick=ge->brickc;
    brick_color=brick_color_copy;    
    Brick_Transform();
    DrawBrick(ge);
    Transfrom_brick=Load_Brick(ge);
  }      
  }
  else{
  if((Time_Interval+speed)<=SDL_GetTicks()){
  Time_Interval=SDL_GetTicks();
  if(Down_Move_Able()){
   ClearBrick(ge);   
   Down_Move(ge);
   DrawBrick(ge);      
  }
  else {
   if(Gameover_judge()){
    PlaySound(over);    
    Show_GameOver(ge);
    SDL_Delay(1000);
    game_state = GAME_EXIT;
   }
   else{
    PlaySound(go);    
    write_brick_to_bg();
    SDL_Delay(100);
    Cut_Brick(ge);
    What_brick=Transfrom_brick;
    ge->brick=ge->brickc;
    brick_color=brick_color_copy;    
    Brick_Transform();
    DrawBrick(ge);
    Transfrom_brick=Load_Brick(ge);
  }  
  }
  }
 }

}

/* ----------------------------------- */
int Judge_Transform_Able()
{
 int i,j;
 for(i=0;i<4;i++)
 for(j=0;j<4;j++)
  if(brick_move[i][j].data && (bg[brick_move[i][j].y][brick_move[i][j].x]||brick_move[i][j].x<1||brick_move[i][j].x>14 ||brick_move[i][j].y>20))return 0;
 return 1; 
}
/* ----------------------------------- */
int Judge_After_Transform_Able(int No)
{
 int i,j,k;
 if(!No){  
 for(k=0;k<3;k++){ 
 for(i=0;i<4;i++)
 for(j=0;j<4;j++)
  brick_move[i][j].x-=1;
  if(Judge_Transform_Able()) return 1;  
  }
  return 0;
  }
 return 1;  
}
/* ----------------------------------- */
void Fast_Down()
{
 int i=0;
 while(i<22)
 {
  if(Down_Move_Able())Down_Move();
  else break;
  i++;   
 }
}
/* -----------------------------------*/
void Down_Move()
{
  int i,j;  
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)  
    brick_move[i][j].y+=1;
           
}
/* ----------------------------------- */
int Down_Move_Able()
{
 int i,j;
 for(i=3;i>=0;i--)
 for(j=0;j<4;j++) 
  if((brick_move[i][j].data && brick_move[i][j].y==20)||(brick_move[i][j].data && bg[brick_move[i][j].y+1][brick_move[i][j].x])) return 0;  
 return 1;
}
/* ----------------------------------- */
int Gameover_judge()
{
 int j; 
 for(j=0;j<4;j++) 
  if(brick_move[0][j].data && brick_move[0][j].y<=1)return 1;
 return 0; 
}
/* ----------------------------------- */
void write_brick_to_bg()
{
 int i,j;
 for(i=0;i<4;i++)
 for(j=0;j<4;j++)
 if(brick_move[i][j].data)
  bg[brick_move[i][j].y][brick_move[i][j].x]=1+brick_color;  
}
/* ----------------------------------- */
void ClearBrick(GameEngine *ge)
{
    int i,j;
 SDL_Rect rect;
    rect.h=rect.w=15;
  for(i=0;i<4;i++) 
  for(j=0;j<4;j++){
   if(brick_move[i][j].data){   
   rect.x=brick_move[i][j].x*15;
   rect.y=brick_move[i][j].y*15;
   SDL_BlitSurface(ge->black,NULL,ge->screen,&rect);
   }    
  }  
}
/* ----------------------------------- */
void DrawBrick(GameEngine *ge)
{
 int i,j;
 SDL_Rect rect ;
    rect.h=rect.w=15;  
  for(i=0;i<4;i++)     
      for(j=0;j<4;j++){
               if(brick_move[i][j].data){    
    rect.x=brick_move[i][j].x*15;
       rect.y=brick_move[i][j].y*15;
    SDL_BlitSurface(ge->brick,NULL,ge->screen,&rect);
      }      
  }  
    SDL_UpdateRect(ge->screen,0,0,0,0);
}
/* ----------------------------------- */
void Show_GameOver(GameEngine *ge)
{
  SDL_Surface *tmp;
  SDL_Rect rect;
  tmp=IMG_Load("image/gameover.png"); 
  SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,SDL_MapRGB(tmp->format,255,0,255));
  tmp=SDL_DisplayFormat(tmp);
  rect.w=150;
  rect.h=20;
  rect.x=45;
  rect.y=165;
  SDL_BlitSurface(tmp,NULL,ge->screen,&rect);
  SDL_FreeSurface(tmp);
  SDL_UpdateRect(ge->screen,0,0,0,0);
}
/* ----------------------------------- */
int Left_Right_move_Able(int direction)
{
 int i,j;
 if(direction==LEFT) 
  for(i=0;i<4;i++) 
  for(j=0;j<4;j++)
   if(brick_move[j][i].data && (brick_move[j][i].x<=1 || bg[brick_move[j][i].y][brick_move[j][i].x-1]))return 0;
 if(direction==RIGHT) 
  for(i=3;i>=0;i--) 
  for(j=0;j<4;j++)
   if(brick_move[j][i].data && (brick_move[j][i].x>=14 || bg[brick_move[j][i].y][brick_move[j][i].x+1]))return 0;
 return 1;
}
/* ----------------------------------- */
void Left_Right_move(int direction)
{
 int i,j;
  if(direction==LEFT) 
   for(i=0;i<4;i++) 
   for(j=0;j<4;j++)
    brick_move[j][i].x-=1;
      
  if(direction==RIGHT)
   for(i=3;i>=0;i--) 
   for(j=0;j<4;j++)    
     brick_move[j][i].x+=1;   
    
}
/* ----------------------------------- */
int Cycle_transform(int What_brick)
{
 switch(What_brick){
 case 0:  
 case 3:  
 case 4:
 case 5:  
 case 7:  
 case 8:  
 case 9:  
 case 11:
 case 13:
 case 15:
 case 16:
 case 17:  
  return What_brick+1;
 case 1:  
 case 14:  
 case 12:
  return What_brick-1; 
 case 6:
  return 3;
 case 10:
  return 7;
 case 18:
  return 15; 
 default:
  return 13;   
  }
}
/* ----------------------------------- */
void Transform_Brick_Angle(int What_brick)
{
 switch(What_brick){
 case 0:
 case 1:{
  Backlog_Return(1);
  Transform_one(What_brick);
  if(!Judge_After_Transform_Able(Judge_Transform_Able()))
  Backlog_Return(2); 
  break;
  }
 case 2:  
  break;  
 case 3:
 case 4:
 case 5:
 case 6:{
  Backlog_Return(1);
  Transform_seven(What_brick);
  if(!Judge_After_Transform_Able(Judge_Transform_Able()))
  Backlog_Return(2);
  break;
  }
 case 7: 
 case 8:
 case 9:
 case 10:{
  Backlog_Return(1);
  Transform_F(What_brick);
  if(!Judge_After_Transform_Able(Judge_Transform_Able()))
  Backlog_Return(2);
  break;
  }
 case 11:
 case 12:{
  Backlog_Return(1);
  Transform_Z(What_brick);
  if(!Judge_After_Transform_Able(Judge_Transform_Able()))
  Backlog_Return(2);
  break;
  }
 case 15:
 case 16:
 case 17:
 case 18:{
  Backlog_Return(1);
  Transform_T(What_brick);
  if(!Judge_After_Transform_Able(Judge_Transform_Able()))
  Backlog_Return(2);
  break;
  }  
 case 13:
 case 14:
 default:{
  Backlog_Return(1);
  Transform_S(What_brick);
  if(!Judge_After_Transform_Able(Judge_Transform_Able()))
  Backlog_Return(2);
  break;
  }  
   
  }
}
/*---------------------------------------*/
void Backlog_Return(int marker)
{
 int i,j;
 if(marker==1)
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_move_Transform[i][j].x=brick_move[i][j].x;
   brick_move_Transform[i][j].y=brick_move[i][j].y;
   brick_move_Transform[i][j].data=brick_move[i][j].data;
   }
 if(marker==2)
  for(i=0;i<4;i++)
  for(j=0;j<4;j++){
   brick_move[i][j].x=brick_move_Transform[i][j].x;
   brick_move[i][j].y=brick_move_Transform[i][j].y;
   brick_move[i][j].data=brick_move_Transform[i][j].data;
   }   
}
/*---------------------------------------*/
void Transform_one(int What_brick)
{
 int i,j;
 switch(What_brick){
 case 0:{
 int brick_[][4]={
   {1,0,0,0},
   {1,0,0,0},
   {1,0,0,0},
   {1,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];
   break;
  }
 case 1:{
 int brick_[][4]={
   {1,1,1,1},
   {0,0,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
   break;
  }  
  }  
}
/*---------------------------------------*/
void Transform_seven(int What_brick)
{
 int i,j;
 switch(What_brick){
 case 3:{
 int brick_[][4]={
   {1,1,1,0},
   {1,0,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 4:{
 int brick_[][4]={
   {1,0,0,0},
   {1,0,0,0},
   {1,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 5:{
 int brick_[][4]={
   {0,0,1,0},
   {1,1,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 6:{
 int brick_[][4]={
   {1,1,0,0},
   {0,1,0,0},
   {0,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }  
  }  
}
/*---------------------------------------*/
void Transform_F(int What_brick)
{
 int i,j;
 switch(What_brick){
 case 7:{
 int brick_[][4]={
   {1,0,0,0},
   {1,1,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 8:{
 int brick_[][4]={
   {0,1,0,0},
   {0,1,0,0},
   {1,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 9:{
 int brick_[][4]={
   {1,1,1,0},
   {0,0,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 10:{
 int brick_[][4]={
   {1,1,0,0},
   {1,0,0,0},
   {1,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }  
  }  
}
/*---------------------------------------*/
void Transform_Z(int What_brick)
{
 int i,j;
 switch(What_brick){
 case 11:{
 int brick_[][4]={
   {0,1,0,0},
   {1,1,0,0},
   {1,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 12:{
 int brick_[][4]={
   {1,1,0,0},
   {0,1,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }  
  }  
}
/*---------------------------------------*/
void Transform_S(int What_brick)
{
 int j,i;   
 switch(What_brick){
 case 14:{
  int brick_[][4]={
   {0,1,1,0},
   {1,1,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };   
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 default:{
  int brick_[][4]={
   {1,0,0,0},
   {1,1,0,0},
   {0,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }  
  }  
}
/*---------------------------------------*/
void Transform_T(int What_brick)
{
 int i,j;
 switch(What_brick){
 case 15:{
 int brick_[][4]={
   {1,0,0,0},
   {1,1,0,0},
   {1,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 16:{
 int brick_[][4]={
   {1,1,1,0},
   {0,1,0,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 17:{
 int brick_[][4]={
   {0,1,0,0},
   {1,1,0,0},
   {0,1,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }
 case 18:{
 int brick_[][4]={
   {0,1,0,0},
   {1,1,1,0},
   {0,0,0,0},
   {0,0,0,0},
  };
  for(i=0;i<4;i++)
  for(j=0;j<4;j++)
   brick_move[i][j].data=brick_[i][j];   
  break;
  }  
  }  
}
/*---------------------------------------*/
void Cut_Brick(GameEngine *ge)
{
 int i,j,lable=0,n,m,s=0,k;
 
      i=brick_move[0][0].y;      
   while(i<=brick_move[3][0].y){
    if(i>20) break;
    else{
    j=1;
    lable=1;
    while(j<15){     
     if(!bg[i][j]){
      lable=0;
      break;
     }
     j++;
    }
    if(lable){
     s=1;
     break;
    }
   }
   i++;
   } 
 
   if(s){
         n=i=brick_move[3][0].y;
   k=lable=0;   
   while(i>=brick_move[0][0].y)
   {    
    lable=0;
    if(i>20){
     i--;
     n--;
    }
     else{
      for(j=1;j<15;j++)
       if(!bg[i][j]){
        lable=1;
        break;
       }
       if(lable){
        for(j=1;j<15;j++)
         bg[n-k][j]=bg[i][j];
        k++;
       }
       i--;
     }
    
   }
   ge->score+=100*((n-brick_move[0][0].y)+1-k);
   while(i>0)
   {    
    for(j=1;j<15;j++)
     bg[n-k][j]=bg[i][j];
    i--;
    n--;
   }
   for(i=0;i<4-k;i++)
    for(j=1;j<15;j++)
     bg[i][j]=0;    
     
   load_Map(ge);
   Show_Score(ge);   
   SDL_UpdateRect(ge->screen,0,0,0,0);
   PlaySound(show);    
      if(ge->level==0 && ge->score>=5000){
    game_state=GAME_WIN;
    SDL_Delay(500);
   }   
   if(ge->level==1 && ge->score>=4000){
    game_state=GAME_WIN;
    SDL_Delay(500);
   }   
   if(ge->level==2 && ge->score>=3200){
    game_state=GAME_WIN;
    SDL_Delay(500);
   }
   if(ge->level==3 && ge->score>=3100){
    game_state=GAME_WIN;
    SDL_Delay(500);
   } 
  } 
}
/*----------------------------------- */
void load_Map(GameEngine *ge)
{
 int i,j;
 SDL_Rect rect;
  rect.w=rect.h=15;
  for(i=1;i<21;i++)
   for(j=1;j<15;j++)
    if(bg[i][j]){
     rect.x=j*15;
     rect.y=i*15;
     switch(bg[i][j]-1){
     case RED: 
      SDL_BlitSurface(ge->brickr,NULL,ge->screen,&rect);
      break;
     case YELLEW: 
      SDL_BlitSurface(ge->bricky,NULL,ge->screen,&rect);
      break;
     case WOOD: 
      SDL_BlitSurface(ge->brickw,NULL,ge->screen,&rect);
      break;
     case GREY: 
      SDL_BlitSurface(ge->brickg,NULL,ge->screen,&rect);
      break;     
     }     
     }
                else {
     rect.x=j*15;
     rect.y=i*15;
     SDL_BlitSurface(ge->black,NULL,ge->screen,&rect);
    }
}
/*----------------------------------- */
void Show_Score(GameEngine *ge)
{
  Uint32 score;
  int len,num;
  SDL_Surface *tmp;
  SDL_Rect res_rect,des_rect,rect;
  tmp=IMG_Load("image/fengshu.png");
  SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,SDL_MapRGB(tmp->format,0,0,0));
  tmp=SDL_DisplayFormat(tmp);
  rect.w=60;
  rect.h=60;
  rect.y=120;
  rect.x=240;
  res_rect.h=des_rect.h=15;
  res_rect.w=des_rect.w=6;
  res_rect.y=0;
  des_rect.y=135; 
   
    score=ge->score;
    len=0;
 SDL_BlitSurface(ge->lable,NULL,ge->screen,&rect);
    while(score>=1){
      len++;
      num=score%10;
      res_rect.x=num*6;     
   des_rect.x=300-len*6;
      SDL_BlitSurface(tmp,&res_rect,ge->screen,&des_rect);
      score/=10;
    } 

  SDL_FreeSurface(tmp);
}
/*----------------------------------- */
void Show_Level(GameEngine *ge,int level)
{  
  SDL_Surface *tmp;
  SDL_Rect res_rect,des_rect,rect;
  tmp=IMG_Load("image/fengshu.png");
  SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,SDL_MapRGB(tmp->format,0,0,0));
  tmp=SDL_DisplayFormat(tmp);
  rect.w=60;
  rect.h=60;
  rect.y=240;
  rect.x=240;
  res_rect.h=des_rect.h=15;
  res_rect.w=des_rect.w=6;
  res_rect.y=0;
  des_rect.y=255;
  res_rect.x=(level+1)*6;
  des_rect.x=270;    
   
  SDL_BlitSurface(ge->lable,NULL,ge->screen,&rect);   
  SDL_BlitSurface(tmp,&res_rect,ge->screen,&des_rect);   
  SDL_FreeSurface(tmp);
}
/*----------------------------------- */
void Brick_Transform()
{
 int i,j;
 for(i=0;i<4;i++)
 for(j=0;j<4;j++)
 {
  brick_move[i][j].data=brick_Transform[i][j].data;
  brick_move[i][j].x=brick_Transform[i][j].x;
  brick_move[i][j].y=brick_Transform[i][j].y;  
 }
}
/* ----------------------------------- */
void Video_Full( GameEngine *ge )
{
  SDL_BlitSurface(ge->screen,NULL,ge->swapscreen,NULL);
  ge->FULLSCREEN=!ge->FULLSCREEN;
  Video_Init(ge);
  SDL_BlitSurface(ge->swapscreen,NULL,ge->screen,NULL);
  SDL_UpdateRect(ge->screen,0,0,0,0);
}
/* ----------------------------------- */
void PlaySound(Mix_Chunk *snd)
{
  int Ch;
  if(Sound){
    if(Mix_PlayChannel(-1,snd,0)<0){
      Ch=Mix_GroupOldest(-1);
      Mix_HaltChannel(Ch);
      Mix_PlayChannel(Ch,snd,0);
    }
  }
}
/* ----------------------------------- */

阅读(1217) | 评论(0) | 转发(0) |
0

上一篇:五笔打字

下一篇:Fixed Rate Pig

给主人留下些什么吧!~~