Update gtk140

This commit is contained in:
daleclack 2022-09-06 20:15:47 +08:00
parent 7c599576d8
commit 7a1fedd823
2 changed files with 22 additions and 2 deletions

View File

@ -1,7 +1,8 @@
#include "MineSweeper.hh" #include "MineSweeper.hh"
#include "../json_nlohmann/json.hpp"
#include <string> #include <string>
// #include <fstream>
// #include <iostream> using json = nlohmann::json;
MineSweeper::MineSweeper() MineSweeper::MineSweeper()
: main_box(Gtk::ORIENTATION_VERTICAL, 5), : main_box(Gtk::ORIENTATION_VERTICAL, 5),
@ -51,6 +52,11 @@ MineSweeper::MineSweeper()
void MineSweeper::reset_game() void MineSweeper::reset_game()
{ {
// Reset timer
mytimer.disconnect();
timer_count = 0;
mytimer = Glib::signal_timeout().connect(sigc::mem_fun(*this, &MineSweeper::timer_func), 1000);
mine_count = 0; mine_count = 0;
// Reset all data // Reset all data
for (int i = 0; i < 7; i++) for (int i = 0; i < 7; i++)
@ -131,6 +137,15 @@ void MineSweeper::show_mines(){
} }
} }
bool MineSweeper::timer_func(){
// Set timer
char tmp[50];
timer_count++;
sprintf(tmp, "Time:%d", timer_count);
status_label.set_label(tmp);
return true;
}
void MineSweeper::cell_clicked(MineCell *cell) void MineSweeper::cell_clicked(MineCell *cell)
{ {
if (!game_ended && !cell->cleared) if (!game_ended && !cell->cleared)
@ -139,10 +154,12 @@ void MineSweeper::cell_clicked(MineCell *cell)
// If get mine, the game will end now // If get mine, the game will end now
if (cell->has_mine) if (cell->has_mine)
{ {
// Set game to stop
winned = false; winned = false;
show_mines(); show_mines();
status_label.set_label("You lost!"); status_label.set_label("You lost!");
game_ended = true; game_ended = true;
mytimer.disconnect();
} }
else else
{ {
@ -166,6 +183,7 @@ void MineSweeper::cell_clicked(MineCell *cell)
status_label.set_label("You winned!"); status_label.set_label("You winned!");
winned = true; winned = true;
game_ended = true; game_ended = true;
mytimer.disconnect();
} }
} }
} }

View File

@ -31,6 +31,7 @@ private:
int mines_clear, mine_count; int mines_clear, mine_count;
// Timer // Timer
int timer_count;
sigc::connection mytimer; sigc::connection mytimer;
// Signal Handlers // Signal Handlers
@ -38,4 +39,5 @@ private:
void calc_mines(); void calc_mines();
void show_mines(); void show_mines();
void cell_clicked(MineCell *cell); void cell_clicked(MineCell *cell);
bool timer_func();
}; };