第三个版本

首先需要进行前台界面的修改

  1. 将背景修改成类似打地鼠的背景的图片
  2. 将之前Button随机颜色的变化的按钮换成Image控件来放图片
  3. 增加一个倒计时随机数

代码:

  • 换背景:
    <Window.Background>
            <ImageBrush ImageSource="Img/Bg.png" />
        </Window.Background>
  • 禁止窗体放大:
    ResizeMode="CanMinimize"
  • 前台图片放的位置采用手动拖动到对应的“洞口”

后台逻辑修改

  1. 将之前button随机数list的集合换成Image类生成对应的集合
  2. 添加一个倒计时器的DispatcherTimer
  3. 计时时执行的程序判断的条件更换
  4. 添加右键鼠标事件
  5. 修改鼠标按下的状态

代码:

  • 在外部声明对象
    List<Image> btns = new List<Image>();
    DispatcherTimer showTimer = new DispatcherTimer();
    DispatcherTimer dispatcherTimer = new DispatcherTimer();
    private int countSecond = 60;
    int score = 0;
  • 在启动项目添加随机数集合
    public MainWindow() {
                InitializeComponent();
                btns.Add(btn1);
                btns.Add(btn2);
                btns.Add(btn3);
                btns.Add(btn4);
                btns.Add(btn5);
                btns.Add(btn6);
                btns.Add(btn7);
                btns.Add(btn8);
                btns.Add(btn9);
                btns.Add(btn10);
    }
  • 随机数计时器
    Random b1 = new Random();
    int imgNum = b1.Next(btns.Count);
    btns[imgNum].Visibility = Visibility.Visible;
    await Task.Delay(1000);
    btns[imgNum].Visibility = Visibility.Collapsed;
    
  • 倒计时计时器
      public void disTimer(object sender, EventArgs e) {
                if (countSecond == 0) {
                    showTimer.Stop();
                    dispatcherTimer.Stop();
                    MessageBox.Show("时间到了游戏结束");
                } else {
                    countSecond--;
                    laSecond.Content = string.Format("倒计时:" + countSecond.ToString());
                }
            }
  • 右键点击事件
     private void imgGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
                this.Cursor = Application.Current.FindResource("HammerCursor") as Cursor;
                Grid grid = sender as Grid;
                if (grid == null) {
                    return;
                }
                grid.Cursor = Application.Current.FindResource("HmmerCursor") as Cursor;
                if (btn7.Visibility==Visibility.Visible) {
                    if (score < 11) {
                        score++;
                        btn7.Visibility = Visibility.Collapsed;
                        txt.Text = string.Format("得分:" + score.ToString());
                        if (score == 10) {
                            MessageBox.Show("游戏成功");
                        }
                    }
                }else {
                    txt.Text = string.Format("得分:" + score.ToString());
                }
            }
posted @ 2021-11-26 14:19  庆喜  阅读(41)  评论(0编辑  收藏  举报