📄 main.c
字号:
algorithm_game_init(); time_remain = 0; /*deal with the area above the drawing area*/ ui_refresh_top(); /*empty the drawing area,draw the background picture*/ ui_drawingarea_draw_bg(0); /*emerge an widget redraw signal*/ 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;}/* function dealing with game giveup,and game over*/void ui_game_giveup(GtkWidget *w,gpointer data){ /*shutdown the timing clock*/ if(timer_handle != 0)gtk_timeout_remove(timer_handle); /*giveup,do not record the score,so should not call ui_game_over function here.*/ if(algorithm_game.status != ALGORITHM_GAME_STOP) { ui_game_init(); } else {}}/* Next Levels if the current level is not the last one(NO.10),then,enter the next level otherwise,popup a window,on which give a hint that the player have success this difficulty,then over the game,waiting for player to choose another difficulty.*/void ui_game_next_level(void){ if(timer_handle != 0)gtk_timeout_remove(timer_handle);/* remove the timer */ if( algorithm_game_net_level() ) {/*switch to the next level*/ if(gtk_check_menu_item_get_active(ui_menu_item.sound_effect))ui_play("Win.wav"); /* get and set next card back, and get and set next background picture */ ui_pixbuf.cardback_choice = ui_pixbuf.cardback_choice>=5?0:ui_pixbuf.cardback_choice+1; if(pak_info.back_num > 0){ ui_pixbuf.randomback_choice = ui_pixbuf.randomback_choice>pak_info.back_num-1?1:ui_pixbuf.randomback_choice+1; /* Extract the bg picture and read it to ui_pixbuf.randomback */ gchar bgfilename[30]; sprintf(bgfilename,"back%d.jpg",ui_pixbuf.randomback_choice-1); if(!ExtractSingleFile(bgfilename)){g_print(_("Failed to extract file:%s.\n"),bgfilename);} sprintf(bgfilename,"/tmp/llk_back%d.jpg",ui_pixbuf.randomback_choice-1); if(ui_pixbuf.randomback)gdk_pixbuf_unref(ui_pixbuf.randomback); /* Free old randomback pixbuf */ ui_pixbuf.randomback = gdk_pixbuf_new_from_file(bgfilename,NULL); remove(bgfilename); if(ui_pixbuf.randomback == NULL){ui_pixbuf.randomback = ui_pixbuf.mainback;g_print(_("randomback=NULL,Error occored.\n"));} } else{ ui_pixbuf.randomback = NULL; ui_pixbuf.randomback_choice = 0; } ui_redraw_images(); ui_point1.x=-1; ui_point1.y=-1; progress_timeout(GINT_TO_POINTER(9999)); ui_refresh_top(); timer_handle = gtk_timeout_add(1000,progress_timeout,NULL); } else {/*success this difficulty*/ ui_game_over(TRUE); }}/* shuffle cards*/void ui_game_shuffle(GtkWidget *w,gpointer data){ if(algorithm_game.status != ALGORITHM_GAME_RUN) {return;} if(timer_handle != 0)gtk_timeout_remove(timer_handle); /*turn off the timer*/ if(algorithm_game.life == 0) { ui_game_over(FALSE); return; } else { gchar *message = (gchar *)g_malloc(sizeof(gchar)*20); algorithm_game.life--; sprintf(message,_("Life:%d"),algorithm_game.life); gtk_label_set_text(GTK_LABEL(ui_top.label_life),message); g_free(message); } algorithm_game_shuffle(); 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("Shuffle.wav"); timer_handle = gtk_timeout_add(1000,progress_timeout,NULL);/*turn on the timer*/}/* game over*/void ui_game_over(gboolean success){ GtkWidget *dialog; GtkMessageType type; gchar *message = (gchar *)g_malloc(sizeof(gchar)*100); if(success) { /*popup dialog window*/ switch(algorithm_game.difficulty) { case 0: sprintf(message,_("Congratulations,you have success the Easy difficulty,you may play Normal now!")); break; case 1: sprintf(message,_("Congratulations,you have success the Normal difficulty,you may play Hard now!")); break; case 2: sprintf(message,_("Congratulations,you have success all the difficulties,I admire you so much...")); break; } ini_file_write_int (ui_ini, "GAMERECORD", "difficulty", algorithm_game.difficulty+2); if(!ini_file_write_file(ui_ini,ui_ini->filename)) { g_print(_("Ini file save error!\n")); } type = GTK_MESSAGE_INFO; } else { /*popup dialog window*/ sprintf(message,_("Success and failure are nothing,just try again!")); type = GTK_MESSAGE_WARNING; } if(success){if(gtk_check_menu_item_get_active(ui_menu_item.sound_effect))ui_play("Win.wav");} else{if(gtk_check_menu_item_get_active(ui_menu_item.sound_effect))ui_play("GameOver.wav");} 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); /*Record SCORE*/ ui_record_score(FALSE); ui_game_init();}/* change the image cards' position according to the current level value the two cards which can link have already been deleted,so only change the position of these cards left.*/void ui_game_change(struct AlgorithmPoint p1, struct AlgorithmPoint p2){ algorithm_game_change(p1,p2); ui_redraw_images(); ui_point1.x=-1; ui_point1.y=-1;}/* pause,hide the card images,DO NOT modify the Algorithm_game data values. when restore the game,just show these card images.*/void ui_game_pause(void){ GdkRectangle update_rect; gint width,height,pixmap_width,pixmap_height; if(algorithm_game.status == ALGORITHM_GAME_RUN) { if(timer_handle != 0) { gtk_timeout_remove(timer_handle); timer_handle = 0; } gdk_drawable_get_size(GDK_DRAWABLE(ui_double_pixmap),&pixmap_width,&pixmap_height); algorithm_game.status = ALGORITHM_GAME_PAUSE; /*hide all the images on the drawingarea,then put a picture that stand for the pause status*/ gdk_draw_rectangle (ui_double_pixmap, ui_drawingarea->style->black_gc, TRUE, 0, 0, pixmap_width, pixmap_height); if(ui_pixbuf.pause) { width = gdk_pixbuf_get_width(ui_pixbuf.pause); height = gdk_pixbuf_get_height(ui_pixbuf.pause); gdk_pixbuf_render_to_drawable_alpha(ui_pixbuf.pause,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); } 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); } else if(algorithm_game.status == ALGORITHM_GAME_PAUSE) { /*empty all possible selected status*/ ui_point1.x = -1; ui_point1.y = -1; ui_redraw_images(); /*redraw the drawingarea*/ 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); timer_handle = gtk_timeout_add(1000,progress_timeout,NULL); /*turn on the timer*/ algorithm_game.status = ALGORITHM_GAME_RUN; }}/* refresh the information shown on the top*/void ui_refresh_top(void){ gchar *message = (gchar *)g_malloc(sizeof(gchar)*20); if(algorithm_game.status != ALGORITHM_GAME_STOP) { switch(algorithm_game.difficulty) { case 0: sprintf(message,_("Diff:Easy")); break; case 1: sprintf(message,_("Diff:Normal")); break; case 2: sprintf(message,_("Diff:Hard")); break; } gtk_label_set_text(GTK_LABEL(ui_top.label_difficulty),message); sprintf(message,_("Lev:%d"),algorithm_game.level); gtk_label_set_text(GTK_LABEL(ui_top.label_level),message); switch(algorithm_game.level) { case 0:/*no change*/ sprintf(message,_("No Change")); break; case 1:/*move down*/ sprintf(message,_("Move Down")); break; case 2:/*move left*/ sprintf(message,_("Move Left")); break; case 3:/*up down separate*/ sprintf(message,_("Up Down Separate")); break; case 4:/*left right separate*/ sprintf(message,_("Left Right Separate")); break; case 5:/*up down converge*/ sprintf(message,_("Up Down Converge")); break; case 6:/*left right converge*/ sprintf(message,_("Left Right Converge")); break; case 7:/*up leftward,down rightward*/ sprintf(message,_("Up left,Down right")); break; case 8:/*left downward,right upward*/ sprintf(message,_("Left down,Right up")); break; case 9:/*disperse from center*/ sprintf(message,_("DisperseFromCenter")); break; case 10:/*centralize*/ sprintf(message,_("Centralize")); break; default: /*assert not reach*/ break; } gtk_label_set_text(GTK_LABEL(ui_top.label_change_type),message); sprintf(message,"%d",algorithm_game.score); gtk_label_set_text(GTK_LABEL(ui_top.label_score),message); sprintf(message,_("Life:%d"),algorithm_game.life); gtk_label_set_text(GTK_LABEL(ui_top.label_life),message); sprintf(message,_("Hint:%d"),algorithm_game.hint); gtk_label_set_text(GTK_LABEL(ui_top.label_hint),message); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui_top.progress_bar),((double)time_remain/(double)ui_get_time_limited())); } else { gtk_label_set_text(GTK_LABEL(ui_top.label_difficulty),_("Diff:")); gtk_label_set_text(GTK_LABEL(ui_top.label_level),_("Lev:")); gtk_label_set_text(GTK_LABEL(ui_top.label_change_type),_("Change Type:")); gtk_label_set_text(GTK_LABEL(ui_top.label_score),"0"); gtk_label_set_text(GTK_LABEL(ui_top.label_life),_("Life:")); gtk_label_set_text(GTK_LABEL(ui_top.label_hint),_("Hint:")); gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ui_top.progress_bar),0); } g_free(message);}/* get the time value limit of the current level*/gint ui_get_time_limited(void){ if(algorithm_game.difficulty == 2)return 240; else return 200; }/* Give player a hint*/void ui_game_hint( GtkWidget *w,gpointer data ){ gint i,j; gint k,l; gchar *message=(gchar *)g_malloc(sizeof(gchar)*20); if(algorithm_game.status != ALGORITHM_GAME_RUN)return; if(algorithm_game.hint == 0)return; algorithm_game.hint--; sprintf(message,_("Hint:%d"),algorithm_game.hint); gtk_label_set_text(GTK_LABEL(ui_top.label_hint),message); g_free(message); /*g_assert(!algorithm_game_no_solution());*/ /*copy from algorithm_game_no_solution function,modified part of it*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -