Add Guess Game

This commit is contained in:
daleclack 2021-10-29 19:25:01 +08:00
parent e09b85a7ec
commit 5d555c54a3
3 changed files with 162 additions and 77 deletions

View File

@ -4,9 +4,14 @@
<template class="GameWin" parent="GtkWindow">
<property name="can-focus">0</property>
<property name="destroy-with-parent">1</property>
<property name="title">Game</property>
<property name="child">
<object class="GtkGrid">
<property name="can-focus">0</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<child>
<object class="GtkLabel" id="label_status">
<property name="can-focus">0</property>
@ -19,11 +24,29 @@
</object>
</child>
<child>
<object class="GtkButton" id="btn_1">
<property name="label" translatable="yes">1</property>
<object class="GtkToggleButton" id="btn_1">
<property name="width-request">100</property>
<property name="height-request">100</property>
<property name="receives-default">1</property>
<child>
<object class = "GtkBox">
<property name="orientation">vertical</property>
<property name="halign">3</property>
<property name="valign">3</property>
<child>
<object class = "GtkImage" id="image_btn1">
<property name="icon-name">My_GtkUI</property>
<property name="visible">0</property>
<property name="pixel-size">48</property>
</object>
</child>
<child>
<object class = "GtkLabel">
<property name="label" translatable="yes">1</property>
</object>
</child>
</object>
</child>
<layout>
<property name="column">1</property>
<property name="row">2</property>
@ -31,11 +54,29 @@
</object>
</child>
<child>
<object class="GtkButton" id="btn_2">
<property name="label" translatable="yes">2</property>
<object class="GtkToggleButton" id="btn_2">
<property name="width-request">100</property>
<property name="height-request">100</property>
<property name="receives-default">1</property>
<child>
<object class = "GtkBox">
<property name="orientation">vertical</property>
<property name="halign">3</property>
<property name="valign">3</property>
<child>
<object class = "GtkImage" id="image_btn2">
<property name="icon-name">My_GtkUI</property>
<property name="visible">0</property>
<property name="pixel-size">48</property>
</object>
</child>
<child>
<object class = "GtkLabel">
<property name="label" translatable="yes">2</property>
</object>
</child>
</object>
</child>
<layout>
<property name="column">3</property>
<property name="row">2</property>
@ -43,11 +84,29 @@
</object>
</child>
<child>
<object class="GtkButton" id="btn_3">
<property name="label" translatable="yes">3</property>
<object class="GtkToggleButton" id="btn_3">
<property name="width-request">100</property>
<property name="height-request">100</property>
<property name="receives-default">1</property>
<child>
<object class = "GtkBox">
<property name="orientation">vertical</property>
<property name="halign">3</property>
<property name="valign">3</property>
<child>
<object class = "GtkImage" id="image_btn3">
<property name="icon-name">My_GtkUI</property>
<property name="visible">0</property>
<property name="pixel-size">48</property>
</object>
</child>
<child>
<object class = "GtkLabel">
<property name="label" translatable="yes">3</property>
</object>
</child>
</object>
</child>
<layout>
<property name="column">1</property>
<property name="row">4</property>
@ -55,11 +114,29 @@
</object>
</child>
<child>
<object class="GtkButton" id="btn_4">
<property name="label" translatable="yes">4</property>
<object class="GtkToggleButton" id="btn_4">
<property name="width-request">100</property>
<property name="height-request">100</property>
<property name="receives-default">1</property>
<child>
<object class = "GtkBox">
<property name="orientation">vertical</property>
<property name="halign">3</property>
<property name="valign">3</property>
<child>
<object class = "GtkImage" id="image_btn4">
<property name="icon-name">My_GtkUI</property>
<property name="visible">0</property>
<property name="pixel-size">48</property>
</object>
</child>
<child>
<object class = "GtkLabel">
<property name="label" translatable="yes">4</property>
</object>
</child>
</object>
</child>
<layout>
<property name="column">3</property>
<property name="row">4</property>

View File

@ -1,9 +1,13 @@
#include "GameWin.h"
#include <ctime>
#include <cstdlib>
#include <cstring>
struct _GameWin
{
GtkWindow parent;
GtkWidget * btn_1, * btn_2, * btn_3, * btn_4;
GtkWidget * image_btn1, * image_btn2, * image_btn3, * image_btn4;
GtkWidget * btn_go, * btn_exit;
GtkWidget * label_status;
int flag;
@ -11,88 +15,91 @@ struct _GameWin
G_DEFINE_TYPE(GameWin,game_win,GTK_TYPE_WINDOW)
static void check_status(GtkToggleButton * togglebtn,GtkLabel * label){
if(gtk_toggle_button_get_active(togglebtn)){
gtk_label_set_label(label,"Good Luck!");
}else{
gtk_label_set_label(label,"The Button is here!");
}
}
// GObject *button[4];
// GObject *label;
// const char *btn_index[4]={"0","1","2","3"};
// int flag;
static void btn_click(GtkWidget *widget,GameWin * win){
//When a button clicked, show image and check if the button with image is clicked
switch(win->flag){
case 1:
gtk_widget_set_visible(win->image_btn1,TRUE);
check_status(GTK_TOGGLE_BUTTON(win->btn_1),GTK_LABEL(win->label_status));
break;
case 2:
gtk_widget_set_visible(win->image_btn2,TRUE);
check_status(GTK_TOGGLE_BUTTON(win->btn_2),GTK_LABEL(win->label_status));
break;
case 3:
gtk_widget_set_visible(win->image_btn3,TRUE);
check_status(GTK_TOGGLE_BUTTON(win->btn_3),GTK_LABEL(win->label_status));
break;
case 4:
gtk_widget_set_visible(win->image_btn4,TRUE);
check_status(GTK_TOGGLE_BUTTON(win->btn_4),GTK_LABEL(win->label_status));
break;
}
// static void btn_click(GtkWidget *widget,gpointer data){
// //Get image
// GdkPixbuf *pixbuf=gdk_pixbuf_new_from_resource("/gtk31/icon.jpg",NULL);
// GdkPixbuf *sized=gdk_pixbuf_scale_simple(pixbuf,50,50,GDK_INTERP_BILINEAR);
// GtkWidget *image=gtk_image_new_from_pixbuf(sized);
// //Get button index
// char str[2];
// strcpy(str,(char*)data);
// //Set a image to the flag button and judge
// gtk_button_set_child((GtkButton*)button[flag],image);
// if(flag==str[0]-48){
// gtk_label_set_label(GTK_LABEL(label),"Good luck!");
// }else{
// gtk_label_set_label(GTK_LABEL(label),"Flag missed.");
// }
// for(int i=0;i<4;i++){
// gtk_widget_set_sensitive(GTK_WIDGET(button[i]),FALSE);
// }
// }
// static void buttons_init(GtkBuilder *builder){
// //Initalize buttons
// for(int i=0;i<4;i++){
// char str[6];
// sprintf(str,"btn_%d",i+1);
// button[i]=gtk_builder_get_object(builder,str);
// g_signal_connect(button[i],"clicked",G_CALLBACK(btn_click),(gpointer)btn_index[i]);
// //gtk_button_set_always_show_image(GTK_BUTTON(button[i]),TRUE);
// }
// }
//disable all buttons
gtk_widget_set_sensitive(win->btn_1,FALSE);
gtk_widget_set_sensitive(win->btn_2,FALSE);
gtk_widget_set_sensitive(win->btn_3,FALSE);
gtk_widget_set_sensitive(win->btn_4,FALSE);
}
static void btngo_click(GtkWidget * widget,GameWin * win){
//Reinitalize the buttons and the flag
gtk_label_set_label(GTK_LABEL(win->label_status),"Select a button");
//Reinitalize Button
gtk_widget_set_visible(win->image_btn1,FALSE);
gtk_widget_set_visible(win->image_btn2,FALSE);
gtk_widget_set_visible(win->image_btn3,FALSE);
gtk_widget_set_visible(win->image_btn4,FALSE);
//Enable All Buttons
gtk_widget_set_sensitive(win->btn_1,TRUE);
gtk_widget_set_sensitive(win->btn_2,TRUE);
gtk_widget_set_sensitive(win->btn_3,TRUE);
gtk_widget_set_sensitive(win->btn_4,TRUE);
//Set All Buttons to inactive
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->btn_1),FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->btn_2),FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->btn_3),FALSE);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(win->btn_4),FALSE);
//Get a new flag
srand((unsigned)time(NULL));
int x=rand();
win->flag=x%4;
}
// static void gtkmain(GtkApplication *app,gpointer user_data){
// //Get objects from the Gtkbuilder and initalize the application
// GtkBuilder *builder=gtk_builder_new();
// GObject *window;
// GdkPixbuf *pixbuf=gdk_pixbuf_new_from_resource("/gtk31/icon.jpg",NULL);
// gtk_builder_add_from_resource(builder,"/gtk31/window.ui",NULL);
// window=gtk_builder_get_object(builder,"window");
// gtk_application_add_window(app,GTK_WINDOW(window));
// gtk_window_set_icon_name(GTK_WINDOW(window),"org.gtk.daleclack");
// //Get buttons from GtkBuilder
// buttons_init(builder);
// GObject *btn_go=gtk_builder_get_object(builder,"btn_go");
// g_signal_connect(btn_go,"clicked",G_CALLBACK(btngo_click),(gpointer)builder);
// GObject *btn_exit=gtk_builder_get_object(builder,"btn_exit");
// g_signal_connect_swapped(btn_exit,"clicked",G_CALLBACK(gtk_window_destroy),window);
// label=gtk_builder_get_object(builder,"label");
// //Initalize the flag
// srand((unsigned)time(NULL));
// int x=rand();
// flag=x%4;
// gtk_widget_show(GTK_WIDGET(window));
// g_object_unref(pixbuf);
// }
static gboolean game_win_close(GtkWindow * win){
gtk_widget_hide(GTK_WIDGET(win));
return TRUE;
win->flag=x%4+1;
}
static void game_win_init(GameWin * game_win){
//Initalize Window
gtk_widget_init_template(GTK_WIDGET(game_win));
gtk_window_set_icon_name(GTK_WINDOW(game_win),"game");
//Initalize Flag
srand((unsigned)time(NULL));
game_win->flag = (rand()%4) + 1;
//link signals
g_signal_connect(game_win->btn_1,"clicked",G_CALLBACK(btn_click),game_win);
g_signal_connect(game_win->btn_2,"clicked",G_CALLBACK(btn_click),game_win);
g_signal_connect(game_win->btn_3,"clicked",G_CALLBACK(btn_click),game_win);
g_signal_connect(game_win->btn_4,"clicked",G_CALLBACK(btn_click),game_win);
g_signal_connect(game_win->btn_go,"clicked",G_CALLBACK(btngo_click),game_win);
g_signal_connect_swapped(game_win->btn_exit,"clicked",G_CALLBACK(gtk_window_destroy),game_win);
}
static void game_win_class_init(GameWinClass * klass){
GTK_WINDOW_CLASS(klass)->close_request=game_win_close;
//GTK_WINDOW_CLASS(klass)->close_request=game_win_close;
//Set template for the game window
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(klass),
"/org/gtk/daleclack/game1.ui");
@ -104,6 +111,10 @@ static void game_win_class_init(GameWinClass * klass){
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass),GameWin,btn_go);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass),GameWin,btn_exit);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass),GameWin,label_status);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass),GameWin,image_btn1);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass),GameWin,image_btn2);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass),GameWin,image_btn3);
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass),GameWin,image_btn4);
}
GameWin * game_win_new(GtkWindow * win){

View File

@ -1,9 +1,6 @@
#pragma once
#include <gtk/gtk.h>
#include <ctime>
#include <cstdlib>
#include <cstring>
G_DECLARE_FINAL_TYPE(GameWin,game_win,GAME,WIN,GtkWindow)