gboolean on_mouse_click(GtkWidget *widget,GdkEventButton *event,gpointer data)//GtkEventButton对应鼠标按键事件,结构成员event->button等于1对应按下鼠标左键,等于3对应按下鼠标右键。 { gint index; gint row,col; gchar buf[4]; vbox3 = gtk_vbox_new(FALSE,0); gtk_box_pack_start(GTK_BOX(vbox3),s_image,FALSE,FALSE,0); if(time_out_tag==0) { time_out_tag = g_timeout_add(1000,(GSourceFunc)tick,NULL); } if(game_over == TRUE) return TRUE; index = (gint)data; switch(event->button) { case 1: row = index/width; col = index%width; open_block(col,row);//鼠标左键按一下,表示打开一个方块,调用open_block()处理。 break; case 2: break; case 3: if(map[index].opened == TRUE) break; if(map[index].marked != TRUE) { map[index].marked = TRUE; gtk_button_set_label(GTK_BUTTON(widget),"@");//按下鼠标右键,表示标记本方块是地雷,直接在方块上画个“@”符号。 marked_count++; } else { map[index].marked = FALSE; gtk_button_set_label(GTK_BUTTON(widget),""); marked_count--; } g_snprintf(buf,4,"%d",MAX(0,mines-marked_count)); gtk_label_set_text(GTK_LABEL(mine_label),buf); } gtk_toggle_button_set_mode(GTK_TOGGLE_BUTTON(map[index].button),TRUE); return TRUE; }
void Block::fetch (unsigned char *buffer) { int fd; int block_size; if (!buffer) die ("fetch_block_data: unallocated buffer\n"); set_buffer (buffer); fd = open_block (name); block_size = file_size (fd); if (block_size != size) { set_corrupted (); } read (fd, buffer, size); close (fd); }
//打开某一个格子对应的函数 void open_block(gint x,gint y) { gint index; GtkWidget *button; index = x+y*width; if(map[index].marked == TRUE) return;//防止某家打开已标记的盒子 button = map[index].button; gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button),TRUE);//将状态改为按下 if(map[index].opened == TRUE) return;//防止打开已经打开的盒子 map[index].opened = TRUE;//打开盒子 if(map[index].mine == TRUE)//如果此盒子下有雷 { gtk_button_set_label(GTK_BUTTON(button),"*"); gameover(FALSE);//踩到地雷游戏结束 return; } if(map[index].count > 0)//若周围有雷 { gchar buf[2]; g_snprintf(buf,2,"%d",map[index].count); gtk_button_set_label(GTK_BUTTON(button),buf); } opened_count++;//增加一个已掀开的格子数 if(opened_count + mines == width * height)//获胜的标志 { gameover(TRUE); return; } if(map[index].count == 0)//若周围没有雷则掀开周围的格子 { if(y > 0) { if(x > 0) open_block(x-1,y-1); open_block(x,y-1); if(x < width-1) open_block(x+1,y-1); } if(x > 0) open_block(x-1,y); if(x < width-1) open_block(x+1,y); if(y < height-1) { if(x > 0) open_block(x-1,y+1); open_block(x,y+1); if(x < width-1) open_block(x+1,y+1); } } }