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

📄 llk_main.c

📁 连连看linux版V1.0,用C语言写的
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "llk_main.h"/*  创建菜单*/voidget_main_menu( GtkWidget *window, GtkWidget **menubar){  GtkItemFactory *item_factory;  GtkAccelGroup *accel_group;  gint n_menu_items = sizeof(menu_items)/sizeof(menu_items[0]);    accel_group = gtk_accel_group_new();  item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",                                        accel_group);  gtk_item_factory_create_items (item_factory, n_menu_items, menu_items, NULL);  gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);  if (menubar)    *menubar = gtk_item_factory_get_widget (item_factory, "<main>");  item_use_mycards = GTK_CHECK_MENU_ITEM(gtk_item_factory_get_widget(item_factory,"/特殊功能(T)/使用自选牌面"));}/*  空回调函数,占位用 */static voidprint_hello( GtkWidget *w, gpointer   data ){  g_print ("data returned %d \n",GPOINTER_TO_INT(data));}/*  给i,j处替换一个图象*/gboolean ui_replace_image(struct AlgorithmPoint p1,struct AlgorithmPoint p2){  GdkPixbuf *pixbuf2;  GdkRectangle update_rect;  /*现在再根据算法中的数组以及被选种的图形位置,重新绘制图形,修改自ui_game_begin函数*/    /*根据算法中的数组数据,填充图形*/  pixbuf2 = gdk_pixbuf_new_subpixbuf(ui_pixbuf,(algorithm_game.data[p1.x][p1.y] - 1)*46,55,UI_IMAGE_WIDTH-UI_IMAGE_BORDER,UI_IMAGE_HEIGHT-UI_IMAGE_BORDER);  gdk_pixbuf_render_to_drawable_alpha(pixbuf2,ui_double_pixmap,                0,0,UI_FIXED_START_DRAW_LEFT - algorithm_game.difficulty*40 + p1.y*40,UI_FIXED_START_DRAW_TOP + p1.x*50,		UI_IMAGE_WIDTH-UI_IMAGE_BORDER,UI_IMAGE_HEIGHT-UI_IMAGE_BORDER,                GDK_PIXBUF_ALPHA_BILEVEL,128,                GDK_RGB_DITHER_NORMAL, 0, 0);  gdk_pixbuf_unref(pixbuf2);  update_rect.x = UI_FIXED_START_DRAW_LEFT - algorithm_game.difficulty*40 + p1.y*40;  update_rect.y = UI_FIXED_START_DRAW_TOP + p1.x*50;  update_rect.width = UI_IMAGE_WIDTH-UI_IMAGE_BORDER;  update_rect.height = UI_IMAGE_HEIGHT-UI_IMAGE_BORDER;  gtk_widget_draw (ui_drawingarea, &update_rect);    if(p2.x != -1 && p2.y != -1)  {    pixbuf2 = gdk_pixbuf_new_subpixbuf(ui_pixbuf,(algorithm_game.data[p2.x][p2.y] - 1)*46,55,UI_IMAGE_WIDTH-UI_IMAGE_BORDER,UI_IMAGE_HEIGHT-UI_IMAGE_BORDER);    gdk_pixbuf_render_to_drawable_alpha(pixbuf2,ui_double_pixmap,                  0,0,UI_FIXED_START_DRAW_LEFT - algorithm_game.difficulty*40 + p2.y*40,UI_FIXED_START_DRAW_TOP + p2.x*50,		  UI_IMAGE_WIDTH-UI_IMAGE_BORDER,UI_IMAGE_HEIGHT-UI_IMAGE_BORDER,                  GDK_PIXBUF_ALPHA_BILEVEL,128,                  GDK_RGB_DITHER_NORMAL, 0, 0);    gdk_pixbuf_unref(pixbuf2);    update_rect.x = UI_FIXED_START_DRAW_LEFT - algorithm_game.difficulty*40 + p2.y*40;    update_rect.y = UI_FIXED_START_DRAW_TOP + p2.x*50;    update_rect.width = UI_IMAGE_WIDTH-UI_IMAGE_BORDER;    update_rect.height = UI_IMAGE_HEIGHT-UI_IMAGE_BORDER;    gtk_widget_draw (ui_drawingarea, &update_rect);  }  return TRUE;}/*  根据算法里数组元素的值重新绘制图形*/gboolean ui_redraw_images(void){  GdkPixbuf *pixbuf2;  GdkRectangle update_rect;  gint i,j;  /*重绘背景图片*/  ui_drawingarea_draw_bg(0);  /*现在再根据算法中的数组以及被选种的图形位置,重新绘制图形,修改自ui_game_begin函数*/    /*根据算法中的数组数据,填充图形*/    for(i=0;i<algorithm_game.row;i++)    {      for(j=0;j<algorithm_game.col;j++)      {        if(algorithm_game.data[i][j] > 0)	{          pixbuf2 = gdk_pixbuf_new_subpixbuf(ui_pixbuf,(algorithm_game.data[i][j] - 1)*46,0,UI_IMAGE_WIDTH,UI_IMAGE_HEIGHT);          gdk_pixbuf_render_to_drawable_alpha(pixbuf2,ui_double_pixmap,                0,0,UI_FIXED_START_DRAW_LEFT - algorithm_game.difficulty*40 + j*40,UI_FIXED_START_DRAW_TOP + i*50,		UI_IMAGE_WIDTH,UI_IMAGE_HEIGHT,                GDK_PIXBUF_ALPHA_BILEVEL,128,                GDK_RGB_DITHER_NORMAL, 0, 0);	  gdk_pixbuf_unref(pixbuf2);	}      }    }         /*触发一次窗体重绘*/  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;  }/*  llk_fixed的鼠标单击事件的回调函数*/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;      /*g_print ("Button %d pressed\n",event->button);*/      if(event->button == 1) /*鼠标左键*/      {        if( event->x > UI_FIXED_START_DRAW_LEFT - algorithm_game.difficulty*40 &&            event->x < UI_FIXED_START_DRAW_LEFT - algorithm_game.difficulty*40 + algorithm_game.col*40)        if( event->y > UI_FIXED_START_DRAW_TOP &&            event->y < UI_FIXED_START_DRAW_TOP + algorithm_game.row*50 )        {          j = (event->x - UI_FIXED_START_DRAW_LEFT + algorithm_game.difficulty*40)/40 ;          i = (event->y - UI_FIXED_START_DRAW_TOP)/50;          /*g_print("i: %d, j: %d\n",i,j);*/	  if(algorithm_game.data[i][j] == 0)	  {return FALSE;}          if(ui_point1.x > -1)  /*已经选中了一个了*/          {            if(ui_point1.x != i || ui_point1.y != j)            {              ui_point2.x = i; ui_point2.y = j;              /*看两点是否能联通,如果可以,消除两点,如果不行,取消两点的选择*/	      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,必须放在ui_link之后,因为ui_link里面要判断连线路径,不能改动data*/		progress_timeout(GINT_TO_POINTER(1)); /*每消一次加1s的时间*/		sprintf(message,"%d",algorithm_game.score);                gtk_label_set_text(GTK_LABEL(ui_top.label_score),message);		g_free(message);		/*判断当前局面是否有解*/		/*注意,必须先做变形,然后再判断是否有解,否则会有错误*/		ui_game_change(ui_point1,ui_point2);		tmp = algorithm_game_no_solution();		/*g_print("Solution state:%d\n",tmp);*/		switch(tmp)		{		  case 1:/*无解,但是牌未消完*/		         /*play some sound here*/		         ui_game_wash(NULL,NULL); /*洗牌*/		         break;		  case 0:/*不做任何事*/			 break;		  case 2: /*牌已经消完,这一关结束*/		  	 ui_game_next_level();			 break;		}	      }	      else	      {                ui_redraw_images();		ui_point1.x = -1; ui_point1.y = -1;		/*恢复i,j处的图象*/	      }            }            else /*点中已经被选中的图象,则恢复图象*/            {              ui_point1.x = -1; ui_point1.y = -1;              ui_redraw_images();              /*给i,j处换图象*/            }          }          else /*一个还都没有选中*/          {            ui_point1.x = i; ui_point1.y = j;	    ui_point2.x=-1;ui_point2.y=-1;            ui_replace_image(ui_point1,ui_point2);            /*给i,j处换图象*/          }        }      }      else  /* if(event->button == 3)*/ /*鼠标右键*/      {        if(ui_point1.x > -1)  /*取消选择*/	{	  ui_redraw_images();	  ui_point1.x = -1; ui_point1.y = -1;	}      }      return TRUE;    }    else    {      return FALSE;    }  }  else  {    return FALSE;  }}/*  绘制游戏区的背景图*/void ui_drawingarea_draw_bg(gint choice){  GdkPixbuf *pixbuf;  gint width,height;  pixbuf = gdk_pixbuf_new_from_inline(-1,main_bg_inline,FALSE,NULL);  //pixbuf = gdk_pixbuf_new_from_file("/mnt/hdc8/Personal/GameDesign/main_bg.png",NULL);  if(pixbuf)  {    width = gdk_pixbuf_get_width(pixbuf);    height = gdk_pixbuf_get_height(pixbuf);    gdk_pixbuf_render_to_drawable_alpha(pixbuf,ui_double_pixmap,        				0,0,0,0,width,height,        				GDK_PIXBUF_ALPHA_BILEVEL,128,    					GDK_RGB_DITHER_NORMAL, 0, 0);    gdk_pixbuf_unref(pixbuf);  }  else  {    gdk_draw_rectangle (ui_double_pixmap,                        ui_drawingarea->style->black_gc,                        TRUE,                        0, 0,                        ui_drawingarea->allocation.width,                        ui_drawingarea->allocation.height);    }}/*  ui处理游戏开始的函数,其中data标志的是游戏难度,based on 1,but difficulty is based on 0,be care!  此函数中还需要加入对当前游戏状态的判断,这一点还没有做!!!*/voidui_game_begin(GtkWidget *w,gpointer data){  if(algorithm_game.status != ALGORITHM_GAME_STOP)return;/*清空表记选中位置的变量*/  ui_point1.x = -1; ui_point1.y = -1;  ui_point2.x = -1; ui_point2.y = -1;  if(algorithm_game_begin(data))  /*初始化算法文件中的数组和其他相关数据,准备开始游戏*/  {    /*根据算法中的数组数据,填充图形*/    ui_redraw_images();  }      ui_refresh_top();  progress_timeout(GINT_TO_POINTER(9999));  timer_handle = gtk_timeout_add(1000,progress_timeout,NULL);}/*  定时  经过测试,在定时回调函数没有返回以前,定时器处于暂停状态  也就是说,游戏结束时,调用ui_game_over弹出的对话窗,不管停留多久,定时器在这段时间内都是暂停了,不会再出发回调函数*/gboolean progress_timeout(gpointer pvalue){  gint value = GPOINTER_TO_INT(pvalue);    if(value == 9999)  {/*填满时间*/    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("时间用完.\n");*/        ui_game_over(FALSE);        return FALSE;      }    }  }}/*  初始化,准备开始游戏*/gboolean ui_game_init(void){  /*处理界面*/  GdkRectangle update_rect;  algorithm_game_init();  /*处理fixed上方显示内容*/  ui_refresh_top();  /*清空drawingarea区域内容,绘制底图*/  ui_drawingarea_draw_bg(0);  /*触发一次窗体重绘*/  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;}/*  处理放弃游戏,或游戏中止的函数*/

⌨️ 快捷键说明

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