Exemple #1
0
        /// <summary>
        /// Hiển thị bộ đếm thời gian sau mỗi giây trôi qua
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            sec--;
            Dispatcher.Invoke(() => lblTimer.Content = FormatTimer(sec));

            if (sec == 0)//người chơi thua cuộc(hết thời gian)
            {
                timer.Stop();
                timer.Dispose();
                Dispatcher.Invoke(() => Notify("You lose\n Unfortunately! :( Try again", true));
                Dispatcher.Invoke(() => ResetGame());
                _games = null;
            }
        }
Exemple #2
0
        //Load lại game
        private void LoadGame_Click(object sender, RoutedEventArgs e)
        {
            var path = PathToProject() + "save.txt";

            try
            {
                using (StreamReader readtext = new StreamReader(path))
                {
                    if (readtext == null)
                    {
                        return;
                    }

                    //Lấy dữ liệu từ file save.txt

                    number = int.Parse(readtext.ReadLine());
                    var image      = readtext.ReadLine();
                    var currentSec = int.Parse(readtext.ReadLine());

                    for (int i = 0; i < number; i++)
                    {
                        for (int j = 0; j < number; j++)
                        {
                            _puzzle[i, j] = int.Parse(readtext.ReadLine());
                        }
                    }

                    Debug.WriteLine($"{number} - {image}");
                    for (int i = 0; i < number; i++)
                    {
                        for (int j = 0; j < number; j++)
                        {
                            Debug.WriteLine($"{_puzzle[i, j]} ");
                        }
                        Debug.WriteLine("");
                    }

                    //Xóa trước khi tải hình vào

                    table.Children.Clear();
                    listImages.Clear();
                    //Tạo lại game
                    var Game = new playImage()
                    {
                        Source = new BitmapImage(),
                        Image  = image,
                        numCut = number
                    };
                    Game.Source.BeginInit();
                    Game.Source.DecodePixelHeight = sizeWidth;
                    Game.Source.DecodePixelWidth  = sizeHeight;
                    Game.Source.UriSource         = new Uri(image, UriKind.Absolute);
                    Game.Source.EndInit();

                    //Gán lại hình mẫu
                    ResultImage.Source = Game.Source;
                    var w      = Game.cropWidth;
                    var h      = Game.cropHeight;
                    var source = Game.Source;

                    _games = Game;
                    //Reset lại thời gian
                    if (timer != null)
                    {
                        ResetTimer(currentSec);
                    }
                    CountDown();


                    //Cắt hình trước
                    //crop Image
                    for (int i = 0; i < number; i++)
                    {
                        for (int j = 0; j < number; j++)
                        {
                            var rect       = new Int32Rect(j * w, i * h, w, h);
                            var cropbitmap = new CroppedBitmap(source, rect);
                            var cropImage  = new Image();
                            Canvas.SetLeft(cropImage, j * (w + 2) + startX);
                            Canvas.SetTop(cropImage, i * (h + 2) + startY);
                            cropImage.Width  = w;
                            cropImage.Height = h;
                            cropImage.Source = cropbitmap;
                            listImages.Add(cropImage);
                            cropImage.Tag = new Tuple <int, int>(i, j);
                        }
                    }
                    Notify("Load game is already", true);

                    // Để lại hình theo trật tự cũ
                    for (int i = 0; i < number; i++)
                    {
                        for (int j = 0; j < number; j++)
                        {
                            var value = _puzzle[i, j];
                            if (value != number * number)
                            {
                                //Xét vị trí và xử lý xự kiện
                                Canvas.SetLeft(listImages[value], j * (w + 2) + startX);
                                Canvas.SetTop(listImages[value], i * (h + 2) + startY);
                                table.Children.Add(listImages[value]);
                                listImages[value].PreviewMouseLeftButtonUp += CropImage_PreviewMouseLeftButtonUp;
                                listImages[value].MouseLeftButtonDown      += CropImage_MouseLeftButtonDown;
                            }
                        }
                    }
                }
            }catch (Exception es)
            {
                Debug.WriteLine(es.Message);
                Notify("Please save game first", false);
            }
        }
Exemple #3
0
 private void ResetGame()
 {
     table.Children.Clear();
     listImages.Clear();
     _games = null;
 }