⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 连连看
💻 C
📖 第 1 页 / 共 5 页
字号:
  /*according to the data in the algorithm_game,and selected position, redraw all card images    modified from ui_game_begin function*/    for(i=0;i<algorithm_game.row;i++)    {      for(j=0;j<algorithm_game.col;j++)      {        if(algorithm_game.data[i][j] > 0)	    {          /* Draw card back image */          gdk_pixbuf_render_to_drawable_alpha(ui_pixbuf.cardbacks,ui_double_pixmap,                0,ui_pixbuf.cardback_choice*UI_BACK_HEIGHT,                UI_FIXED_START_DRAW_LEFT + (j - algorithm_game.difficulty)*(UI_BACK_WIDTH-UI_BACK_BORDER_1),                UI_FIXED_START_DRAW_TOP + i*(UI_BACK_HEIGHT - UI_BACK_BORDER_2),				UI_BACK_WIDTH,UI_BACK_HEIGHT,                GDK_PIXBUF_ALPHA_BILEVEL,128,                GDK_RGB_DITHER_NORMAL, 0, 0);          /* Draw card's front image */          gdk_pixbuf_render_to_drawable_alpha(ui_pixbuf.cardimages,ui_double_pixmap,                (algorithm_game.data[i][j] - 1)*UI_IMAGE_SIZE,0,                UI_FIXED_START_DRAW_LEFT + (j - algorithm_game.difficulty)*(UI_BACK_WIDTH-UI_BACK_BORDER_1) +                        (UI_BACK_WIDTH-UI_BACK_BORDER_1-UI_IMAGE_SIZE)/2,                UI_FIXED_START_DRAW_TOP + i*(UI_BACK_HEIGHT - UI_BACK_BORDER_2)+                       (UI_BACK_HEIGHT-UI_BACK_BORDER_2-UI_IMAGE_SIZE)/2,                UI_IMAGE_SIZE,UI_IMAGE_SIZE,                GDK_PIXBUF_ALPHA_BILEVEL,128,                GDK_RGB_DITHER_NORMAL, 0, 0);	    }      }    }         /*emerge an widget's redraw singal*/  update_rect.x = 0;  update_rect.y = 0;  update_rect.width = ui_drawingarea->allocation.width;  update_rect.height = ui_drawingarea->allocation.height;  gtk_widget_draw (ui_drawingarea, &update_rect);      return TRUE;  }/*  the back call function of the click event of the drawing area*/gbooleanui_drawingarea_clicked(GtkWidget *window, GdkEventButton *event){  if( algorithm_game.status == ALGORITHM_GAME_RUN )  {    if(event->type == GDK_BUTTON_PRESS && event->button != 2)    {      gint i,j;      if(event->button == 1) /*Mouse, Left button*/      {        if( event->x > UI_FIXED_START_DRAW_LEFT - algorithm_game.difficulty*(UI_BACK_WIDTH-UI_BACK_BORDER_1) &&            event->x < UI_FIXED_START_DRAW_LEFT +(algorithm_game.col-algorithm_game.difficulty)*(UI_BACK_WIDTH-UI_BACK_BORDER_1))        if( event->y > UI_FIXED_START_DRAW_TOP &&            event->y < UI_FIXED_START_DRAW_TOP + algorithm_game.row*(UI_BACK_HEIGHT-UI_BACK_BORDER_2) )        {          j = (event->x - UI_FIXED_START_DRAW_LEFT + algorithm_game.difficulty*(UI_BACK_WIDTH-UI_BACK_BORDER_1))/(UI_BACK_WIDTH-UI_BACK_BORDER_1) ;          i = (event->y - UI_FIXED_START_DRAW_TOP)/(UI_BACK_HEIGHT-UI_BACK_BORDER_2);          /*g_print("i: %d, j: %d\n",i,j);*/	  if(algorithm_game.data[i][j] == 0)	  {return FALSE;}          if(ui_point1.x > -1)  /*there is a selected card already*/          {            if(ui_point1.x != i || ui_point1.y != j)            {              ui_point2.x = i; ui_point2.y = j;	      /*look if the two selected card could be linked or not, if yes,delete them,if no,cancel the selected status*/	      if(algorithm_can_link(ui_point1,ui_point2,NULL,NULL))	      {	        gint tmp;		gchar *message = (gchar *)g_malloc(sizeof(gchar)*20);		/*g_print("This pairs can link.\n");*/		ui_link(ui_point1, ui_point2);		algorithm_link(ui_point1,ui_point2);		/*algorithm_link,must be puted after ui_link function, because ui_link need to judge the link path,and before this,the data must not be modified*/		progress_timeout(GINT_TO_POINTER(1)); /*add 1 second after every link action*/		sprintf(message,"%d",algorithm_game.score);                gtk_label_set_text(GTK_LABEL(ui_top.label_score),message);		g_free(message);		if(gtk_check_menu_item_get_active(ui_menu_item.sound_effect))ui_play("Link.wav");		/*judge if current situation of all the cards has a solution		  ATTENTION: must judge after the game_change funtion		*/		ui_game_change(ui_point1,ui_point2);		tmp = algorithm_game_no_solution();		switch(tmp)		{		  case 1:/*No solution,but there still some cards*/		         ui_game_shuffle(NULL,NULL); /*shuffle cards*/		         break;		  case 0:/*do nothing*/			 break;		  case 2: /*there are no cards,the stage(or Level) is over*/		  	 ui_game_next_level();			 break;		}	      }	      else	      {                ui_redraw_images();  /*ui_redraw_images, this function does not care the selected status*/				ui_point1.x = -1; ui_point1.y = -1;				/*restore the card image at (i,j)*/				if(gtk_check_menu_item_get_active(ui_menu_item.sound_effect))ui_play("CanntLink.wav");	      }            }            else /*click the card that has already been selected,so cancel the selected status*/            {              ui_point1.x = -1; ui_point1.y = -1;              ui_redraw_images();              /*replace the card image at (i,j)*/              if(gtk_check_menu_item_get_active(ui_menu_item.sound_effect))ui_play("Cancle.wav");            }          }          else /*there is no selected cards*/          {            ui_point1.x = i; ui_point1.y = j;	    ui_point2.x=-1;ui_point2.y=-1;            ui_replace_image(ui_point1,ui_point2);            /*replace card image at (i,j)*/            if(gtk_check_menu_item_get_active(ui_menu_item.sound_effect))ui_play("Click.wav");          }        }      }      else  /* if(event->button == 3)*/ /*Mouse,Right button*/      {        if(ui_point1.x > -1)  /*cancel the selected status*/		{	  		ui_redraw_images();	  		ui_point1.x = -1; ui_point1.y = -1;	  		if(gtk_check_menu_item_get_active(ui_menu_item.sound_effect))ui_play("Cancle.wav");		}      }      return TRUE;    }    else    {      return FALSE;    }  }  else  {    return FALSE;  }}/*  darw the background picture of the drawingarea*/void ui_drawingarea_draw_bg(gint choice){  gint width,height,pixmap_width,pixmap_height;  /* Empty all images */  gdk_drawable_get_size(GDK_DRAWABLE(ui_double_pixmap),&pixmap_width,&pixmap_height);  gdk_draw_rectangle (ui_double_pixmap,                      ui_drawingarea->style->black_gc,                      TRUE,0, 0,pixmap_width,pixmap_height);  if(choice == 0)/* ui_game_init call this function, draw mainback*/  {    g_assert(ui_pixbuf.mainback != NULL);    width = gdk_pixbuf_get_width(ui_pixbuf.mainback);    height = gdk_pixbuf_get_height(ui_pixbuf.mainback);    gdk_pixbuf_render_to_drawable_alpha(ui_pixbuf.mainback,ui_double_pixmap,                                        0,0,(pixmap_width - width)/2,(pixmap_height - height)/2,width,height,                                        GDK_PIXBUF_ALPHA_BILEVEL,128,                                        GDK_RGB_DITHER_NORMAL, 0, 0);    return;  }  /* user set to no drawing background pictures */  if(!gtk_check_menu_item_get_active(ui_menu_item.background_picture))  {    return;  }  /* user set to draw bg pictures,but there are no bg picture files in pak */  if(ui_pixbuf.randomback_choice == 0)  {    //g_assert(ui_pixbuf.mainback != NULL);    width = gdk_pixbuf_get_width(ui_pixbuf.mainback);    height = gdk_pixbuf_get_height(ui_pixbuf.mainback);    gdk_pixbuf_render_to_drawable_alpha(ui_pixbuf.mainback,ui_double_pixmap,                                        0,0,(pixmap_width - width)/2,(pixmap_height - height)/2,width,height,                                        GDK_PIXBUF_ALPHA_BILEVEL,128,                                        GDK_RGB_DITHER_NORMAL, 0, 0);    return;  }  else  {    g_assert(ui_pixbuf.randomback != NULL);    width = gdk_pixbuf_get_width(ui_pixbuf.randomback);    height = gdk_pixbuf_get_height(ui_pixbuf.randomback);    gdk_pixbuf_render_to_drawable_alpha(ui_pixbuf.randomback,ui_double_pixmap,        				0,0,(pixmap_width - width)/2,(pixmap_height - height)/2,width,height,        				GDK_PIXBUF_ALPHA_BILEVEL,128,    					GDK_RGB_DITHER_NORMAL, 0, 0);   }}/*  The UI function dealing with the game start process  data: stand for game diffictulty, but it is based on 1,and the game difficulty is based on 0,BE CARE!!!*/voidui_game_begin(GtkWidget *w,gpointer data){  gint current_diff;  if(algorithm_game.status != ALGORITHM_GAME_STOP)return;  if(GPOINTER_TO_INT(data)!=4)  {    /* Read difficulty info from ini file,judge if player can play this difficulty. */    if(ini_file_read_int(ui_ini, "GAMERECORD", "difficulty", &current_diff))    {      if(current_diff<1 || current_diff>3)      {current_diff=1;}    }    else{current_diff=1;}    if( GPOINTER_TO_INT(data)>current_diff )    {      GtkWidget *dialog;      GtkMessageType type;      gchar *message = (gchar *)g_malloc(sizeof(gchar)*100);          switch(GPOINTER_TO_INT(data))        {          case 2:            sprintf(message,_("You should pass difficulty Easy before you play Normal."));	        break;          case 3:            sprintf(message,_("You should pass difficulty Normal before you play Hard."));            break;        }        type = GTK_MESSAGE_WARNING;      dialog = gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,type,GTK_BUTTONS_OK,message);      gtk_dialog_run(GTK_DIALOG(dialog));      gtk_widget_destroy(dialog);      g_free(message);      return;    }  }    /*cancel all the selected status*/  ui_point1.x = -1; ui_point1.y = -1;  ui_point2.x = -1; ui_point2.y = -1;  if(GPOINTER_TO_INT(data) == 4)  {    if(ui_read_game_data())    {      if(algorithm_game.status == ALGORITHM_GAME_RUN)      {        /*init the drawing area,include background picture and card images.*/        ui_redraw_images();        progress_timeout(GINT_TO_POINTER(1));      }      else      {/** FIXME:the timer is not correctly set when pause */        algorithm_game.status = ALGORITHM_GAME_RUN;        ui_game_pause();      }    }    else    {      GtkWidget *dialog;      GtkMessageType type;      gchar *message = (gchar *)g_malloc(sizeof(gchar)*100);      sprintf(message,_("Read saved game data error,Maybe you have not saved a game before.\n"));      type = GTK_MESSAGE_WARNING;      dialog = gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,type,GTK_BUTTONS_OK,message);      gtk_dialog_run(GTK_DIALOG(dialog));      gtk_widget_destroy(dialog);      g_free(message);      return;    }  }  else  {    if(!algorithm_game_begin(data))  /*init the array and other related datas in the algorith_game,ready for start the game.*/    {      g_print(_("Call algorithm_game_begin function error.\n"));      return;    }    /*init the drawing area,include background picture and card images.*/    ui_redraw_images();    progress_timeout(GINT_TO_POINTER(9999));  }  ui_refresh_top();  timer_handle = gtk_timeout_add(1000,progress_timeout,NULL);}/*  time_out function  according to my test result,from the time_out call back function be called, to the call back function return,  the clock is PAUSEED,that is to say,when the game is over,no matter how long you stay on the popup dialog window,  the clock will not emerge another time out signal*/gboolean progress_timeout(gpointer pvalue){  gint value = GPOINTER_TO_INT(pvalue);    if(value == 9999)  {/*full fill the time*/    time_remain = ui_get_time_limited();    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui_top.progress_bar),1);    return TRUE;  }  else  {    if(value != 0)    {      time_remain+=value;      time_remain = time_remain > ui_get_time_limited() ? ui_get_time_limited() : time_remain;      gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui_top.progress_bar),((double)time_remain/(double)ui_get_time_limited()));      return TRUE;    }    else    {      if(time_remain > 0)      {        time_remain--;        gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui_top.progress_bar),((double)time_remain/(double)ui_get_time_limited()));	return TRUE;      }      else      {        /*g_print("Time is out.\n");*/        ui_game_over(FALSE);        return FALSE;      }    }  }}/*  init, ready for game starting*/gboolean ui_game_init(void){  /*deal with UI*/  GdkRectangle update_rect;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -