概述
#include
void callback1(GtkButton*button,gpointer user_data);void callback2(GtkButton*button,gpointer user_data);void callback3(GtkButton*button,gpointer user_data);void close_window(GtkWidget *widget, GdkEvent *event, gpointer data)
{
gtk_main_quit();
}
GtkWidget*label_title;
GtkWidget*label_man;
GtkWidget*label_vs;
GtkWidget*label_vs_s;
GtkWidget*label_me;
GtkWidget*label_me_s;
GtkWidget*label_men_s;//GtkWidget *table;
GtkWidget*vbox_me_s;int main(int argc, char *argv[])
{//初始化
gtk_init(&argc,&argv);//创建顶层窗口
GtkWidget *window =gtk_window_new(GTK_WINDOW_TOPLEVEL);//创建按钮,文本信息为"window"
gtk_window_set_title(GTK_WINDOW(window), "table");
g_signal_connect(window,"destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_container_set_border_width(GTK_CONTAINER(window),10);//创建布局
GtkWidget *table = gtk_table_new(4, 4, TRUE); 容器加入窗口
gtk_container_add(GTK_CONTAINER(window), table);
label_title= gtk_label_new("猜拳游戏");
gtk_table_attach_defaults(GTK_TABLE(table), label_title,1, 2, 0, 1);
label_man= gtk_label_new("机器人");
gtk_table_attach_defaults(GTK_TABLE(table), label_man,0, 1, 1, 2);
label_vs= gtk_label_new("VS");
gtk_table_attach_defaults(GTK_TABLE(table), label_vs,1, 2, 1, 2);
label_me= gtk_label_new("自己");
gtk_table_attach_defaults(GTK_TABLE(table), label_me,2, 3, 1, 2);//设置剪刀选项
GtkWidget *vbox_men_s = gtk_vbox_new(TRUE,20);
label_men_s= gtk_label_new("");
gtk_container_add(GTK_CONTAINER(vbox_men_s),label_men_s);
gtk_table_attach_defaults(GTK_TABLE(table), vbox_men_s,0, 1, 2, 3);//设置输赢选项
GtkWidget *vbox_vs = gtk_vbox_new(TRUE,20);
label_vs_s= gtk_label_new("");
gtk_container_add(GTK_CONTAINER(vbox_vs),label_vs_s);
gtk_table_attach_defaults(GTK_TABLE(table), vbox_vs,1, 2, 2, 3);//设置石头选项
GtkWidget *vbox_me_s = gtk_vbox_new(TRUE,20);/****/g_signal_connect(vbox_me_s,"pressed",G_CALLBACK(callback1), NULL);
g_signal_connect(vbox_me_s,"pressed",G_CALLBACK(callback2), NULL);
g_signal_connect(vbox_me_s,"pressed",G_CALLBACK(callback3), NULL);/****/label_me_s= gtk_label_new("");
gtk_container_add(GTK_CONTAINER(vbox_me_s),label_me_s);
gtk_table_attach_defaults(GTK_TABLE(table), vbox_me_s,2, 3, 2, 3);
GtkWidget*button_jd = gtk_button_new_with_label("剪刀");
g_signal_connect(button_jd,"pressed",G_CALLBACK(callback2), NULL);
gtk_table_attach_defaults(GTK_TABLE(table), button_jd,1, 2, 3, 4);
GtkWidget*button_st = gtk_button_new_with_label("石头");
g_signal_connect(button_st,"pressed",G_CALLBACK(callback1), NULL);
gtk_table_attach_defaults(GTK_TABLE(table), button_st,0, 1, 3, 4);
GtkWidget*button_bu = gtk_button_new_with_label("布");
g_signal_connect(button_bu,"pressed",G_CALLBACK(callback3), NULL);
gtk_table_attach_defaults(GTK_TABLE(table), button_bu,2, 3, 3, 4);
gtk_widget_show_all(window);
gtk_main();return 0;
}int i=0,j=0,k=0;void callback1(GtkButton*button,gpointer user_data)
{
i++;
j++;char haha[100] = "";char hehe[100] = "";char hihi[100] = "";
sprintf(haha,"石头",i);
gtk_label_set_text(GTK_LABEL(label_me_s),haha);int a = rand()%3+1;if(a==1){
sprintf(hehe,"石头",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"平局",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}else if(a==2){
sprintf(hehe,"剪刀",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"赢",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}else{
sprintf(hehe,"布",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"输",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}
}void callback2(GtkButton*button,gpointer user_data)
{
i++;
j++;char haha[100] = "";char hehe[100] = "";char hihi[100] = "";
sprintf(haha,"剪刀",i);
gtk_label_set_text(GTK_LABEL(label_me_s),haha);int a = rand()%3+1;if(a==1){
sprintf(hehe,"石头",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"输",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}else if(a==2){
sprintf(hehe,"剪刀",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"平局",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}else{
sprintf(hehe,"布",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"赢",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}
}void callback3(GtkButton*button,gpointer user_data)
{
i++;
j++;char haha[100] = "";char hehe[100] = "";char hihi[100] = "";
sprintf(haha,"布",i);
gtk_label_set_text(GTK_LABEL(label_me_s),haha);int a = rand()%3+1;if(a==1){
sprintf(hehe,"石头",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"赢",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}else if(a==2){
sprintf(hehe,"剪刀",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"输",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}else{
sprintf(hehe,"布",j);
gtk_label_set_text(GTK_LABEL(label_men_s),hehe);
sprintf(hihi,"平局",k);
gtk_label_set_text(GTK_LABEL(label_vs_s),hihi);
}
}
最后
以上就是淡然哈密瓜为你收集整理的linux猜拳游戏代码,在Linux下通过gtk做一个简单的小游戏(猜拳)的全部内容,希望文章能够帮你解决linux猜拳游戏代码,在Linux下通过gtk做一个简单的小游戏(猜拳)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复