WPF实现2048小游戏

 更新时间:2020年6月25日 11:16  点击:1720

        前几天空闲的时候,实现了一个2048游戏。除了可以设置行数和列数之外,支持修改显示名称,比如下面,改成神雕侠侣中的角色名称:

        游戏逻辑比较简单,大家都应该玩过。

        这里主要实现了四个类:Game、GameBoard还有ColorBlock和BoardGridLine。

        Game类主要用来实现游戏的控制,比如初始化、添加新的色块、移除色块、控制色块上下左右移动、改变积分,触发游戏结束等。

        GameBoard继承自Canvas,实现了色块的合并、检测每个格子的状态等,另外提供了Game控制色块移动的接口。

        ColorBlock类继承自Shape类,用于自定义色块的显示,包含XY坐标、颜色、显示文字等依赖属性,可以进行动画,另外还实现了具体的上下左右移动的方法。最初几个颜色是手动设置,等到色块越来越多,就随机生成一种颜色。

        BoardGridLine也继承自Shape类,用于绘制Canvas底部的网格。

        另外,游戏使用一个简单的文本文件保存设置,包括行数与列数,以及显示文字及其对应颜色,具体操作在Settings类中。

        最后,按键事件封装在KeysNavigation中。

        图标使用Expression Design制作:

 

游戏效果如下:

Game.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;

namespace game2048
{
 public class Game
 {
  public enum State
  { 
   Idel,
   Start,
   Running,
  }

  ColorBlock[,] fillState;
  private int score = 0;
  private int step = 0;


  public ColorBlock[,] FillState
  { 
   get
   {
    return fillState;
   }
  }

  GameBoard board;

  public Game(GameBoard board)
  {
   this.board = board;
   fillState = new ColorBlock[board.RowCount, board.ColumnCount];
   for (int i = 0; i < board.RowCount; i++)
   {
    for (int j = 0; j < board.ColumnCount; j++)
    {
     fillState[i, j] = null;
    }
   }
  }

  public void init()
  {
   Settings.load();
   ColorBlock block = new ColorBlock(board);
   ColorBlock block1 = new ColorBlock(board);
   //FillState[block.XIndex, block.YIndex] = block;
   // FillState[block1.XIndex, block1.YIndex] = block1;
   //BlockList.Add(block);
   //BlockList.Add(block1);
  }

  public void addNew()
  {
   if (board.hasNoPlace())
   {
    gameOver(false);
    return;
   }
   ColorBlock block = new ColorBlock(board);
   //FillState[block.XIndex, block.YIndex] = block;
   //BlockList.Add(block);
  }

  public void remove(int xIndex,int yIndex)
  {
   if (FillState[yIndex, xIndex] != null)
   {
    board.Children.Remove(FillState[yIndex, xIndex]);
    FillState[yIndex, xIndex] = null;
   }
  }

  public void toLeft()
  {
   bool add = false;
   for (int i = 0; i < board.ColumnCount; i++)
   {
    for (int j = 0; j < board.RowCount; j++)
    {
     if (FillState[j, i] != null)
     {
      add |= FillState[j, i].moveLeft();
     }
    }
   }
   if (add)
   {
    addNew();
    fireSetpChanged();
   }
  }

  public void toRight()
  {
   bool add = false;
   for (int i = board.ColumnCount-1; i >=0 ; i--)
   {
    for (int j = 0; j < board.RowCount; j++)
    {
     if (FillState[j, i] != null)
     {
      add |= FillState[j, i].moveRight();
     }
    }
   }
   if (add)
   {
    addNew();
    fireSetpChanged();
   }
  }

  public void toUp()
  {
   bool add = false;
   for (int i = 0; i < board.ColumnCount; i++)
   {
    for (int j = 0; j < board.RowCount; j++)
    {
     if (FillState[j, i] != null)
     {
      add |= FillState[j, i].moveUp();
     }
    }
   }
   if (add)
   {
    addNew();
    fireSetpChanged();
   }
  }

  public void toDown()
  {
   bool add = false;
   for (int i = 0; i < board.ColumnCount; i++)
   {
    for (int j = board.RowCount-1; j >=0; j--)
    {
     if (FillState[j, i] != null)
     {
      add |= FillState[j, i].moveDown();
     }
    }
   }
   if (add)
   {
    addNew();
    fireSetpChanged();
   }
  }

  public delegate void onScoreChange(int score);
  public event onScoreChange onScoreChangeHandler;
  public delegate void onStepChange(int step);
  public event onStepChange onStepChangeHandler;
  public delegate void onGameOver(bool success);
  public event onGameOver onGameOverHandler;

  public void fireSetpChanged()
  {
   step++;
   if (onStepChangeHandler != null)
    onStepChangeHandler(step);
  }

  /// <summary>
  /// 增加积分
  /// </summary>
  /// <param name="offset"></param>
  public void incScore(int offset)
  {
   score += offset;
   if (onScoreChangeHandler != null)
   {
    onScoreChangeHandler(score);
   }
  }

  public void gameOver(bool success)
  {
   if (onGameOverHandler != null)
   {
    onGameOverHandler(success);
   }
  }

  public void reset()
  {
   score = 0;
   step = 0;
   if (onStepChangeHandler != null)
    onStepChangeHandler(step);
   if (onScoreChangeHandler != null)
    onScoreChangeHandler(score);
   for (int i = 0; i < board.RowCount; i++)
   {
    for (int j = 0; j < board.ColumnCount; j++)
    {
     remove(i, j);
    }
   }
  }
 }
}

GameBoard.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Diagnostics;

namespace game2048
{
 public class GameBoard : Canvas, IControlable
 {
  private int rowCount = 4;
  
  public int RowCount
  {
   get
   {
    return rowCount;
   }
   set
   {
    rowCount = value;
   }
  }

  private int columnCount = 4;
  public int ColumnCount 
  {
   get
   {
    return columnCount;
   }
   set
   {
    columnCount = value;
   }
  }

  Game game = null;

  public GameBoard()
  {
   this.Focusable = true;
   this.Focus();
   this.reset();
  }

  public void reset()
  {
   Settings.load();
   RowCount = Settings.rowCount;
   ColumnCount = Settings.columnCount;
  }

  public void init(Game game)
  {
   this.game = game;
   game.init();
  }

  public void toLeft()
  {
   game.toLeft();
   Debug.WriteLine("Left");
  }

  public void toRight()
  {
   game.toRight();
   Debug.WriteLine("Right");
  }

  public void toUp()
  {
   game.toUp();
   Debug.WriteLine("Up");
  }

  public void toDown()
  {
   game.toDown();
   Debug.WriteLine("Down");
  }

  //合并,是否继续
  public bool union(int xIndex, int yIndex, Direction dirct)
  {
   switch (dirct)
   {
    case Direction.Left:
     game.remove(xIndex - 1, yIndex);
     break;
    case Direction.Right:
     game.remove(xIndex + 1, yIndex);
     break;
    case Direction.Up:
     game.remove(xIndex, yIndex - 1);
     break;
    case Direction.Down:
     game.remove(xIndex, yIndex + 1);
     break;
    default:
     break;
   }
   bool ret = game.FillState[yIndex, xIndex].changeText();
   if (ret)
   {
    game.gameOver(true);
    return false;
   }
   game.incScore(game.FillState[yIndex, xIndex].TextIndex);
   return true;
  }

  public int getState(int xIndex, int yIndex)
  {
   if (xIndex < 0 || xIndex > columnCount - 1)
    return 0;
   if (yIndex < 0 || yIndex > rowCount - 1)
    return 0;
   if (game.FillState[yIndex,xIndex] == null)
    return 0;
   return game.FillState[yIndex, xIndex].TextIndex;
  }

  public bool hasNoPlace()
  {
   return this.Children.Count == this.RowCount * this.ColumnCount+1;
  }

  public bool isLocationFilled(int xIndex,int yIndex)
  {
   if (xIndex < 0 || xIndex > columnCount-1)
    return true;
   if (yIndex < 0 || yIndex > rowCount-1)
    return true;
   if (game.FillState[yIndex, xIndex] == null)
    return false;
   return game.FillState[yIndex, xIndex].TextIndex>0;
  }

  public void setState(int xIndex,int yIndex,ColorBlock block)
  {
   game.FillState[yIndex, xIndex] = block;
  }
 }
}

源码下载地址:2048小游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • c# WPF中通过双击编辑DataGrid中Cell的示例(附源码)

    这篇文章主要介绍了c# WPF中通过双击编辑DataGrid中Cell的示例(附源码),帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...2021-03-03
  • WPF实现类似360安全卫士界面的程序源码分享

    最近在网上看到了新版的360安全卫士,感觉界面还不错,于是用WPF制作了一个,时间有限,一些具体的控件没有制作,用图片代替了。感兴趣的朋友一起跟着小编学习WPF实现类似360安全卫士界面的程序源码分享...2020-06-25
  • WPF仿三星手机充电界面实现代码

    这篇文章主要为大家详细介绍了WPF仿三星手机充电界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • python基于tkinter制作图形界面的2048游戏

    这篇文章主要介绍了python基于tkinter制作图形界面的2048游戏的方法,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-04-06
  • C# WPF 通过委托实现多窗口间的传值的方法

    这篇文章主要介绍了C# WPF 通过委托实现多窗口间的传值的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • WPF TextBox实现按字节长度限制输入功能

    这篇文章主要为大家详细介绍了WPF TextBox实现按字节长度限制输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • C#中WPF使用多线程调用窗体组件的方法

    这篇文章主要介绍了C#中WPF使用多线程调用窗体组件的方法,涉及C#中多线程的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • WPF InkCanvas绘制矩形和椭圆

    这篇文章主要为大家详细介绍了WPF InkCanvas绘制矩形和椭圆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • WPF基础教程之形状画刷与变换详解

    这篇文章主要给大家介绍了关于WPF基础教程之形状画刷与变换的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • WPF如何自定义TabControl控件样式示例详解

    这篇文章主要给大家介绍了关于WPF如何自定义TabControl控件样式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。...2020-06-25
  • 简单实现C语言2048游戏

    这篇文章主要为大家详细介绍了简单实现C语言2048游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • 解析WPF实现音频文件循环顺序播放的解决方法

    本篇文章是对WPF实现音频文件循环顺序播放的方法进行了详细的分析介绍,需要的朋友参考下...2021-09-22
  • WPF水珠效果按钮组的实现教程

    下面小编就为大家分享一篇WPF水珠效果按钮组的实现教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-09-22
  • 在Winform和WPF中注册全局快捷键实现思路及代码

    如果注册快捷键,RegisterHotKey中的fsModifiers参数为0,即None选项,一些安全软件会警报,可能因为这样就可以全局监听键盘而造成安全问题,感兴趣的你可以参考下本文...2020-06-25
  • WPF实现转圈进度条效果

    这篇文章主要为大家详细介绍了WPF实现转圈进度条效果,如何设计自定义的绕圈进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • WPF中自定义GridLengthAnimation

    这篇文章主要为大家详细介绍了WPF中自定义GridLengthAnimation的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • WPF/Silverlight实现图片局部放大的方法分析

    这篇文章主要介绍了WPF/Silverlight实现图片局部放大的方法,结合实例形式分析了WPF/Silverlight针对图片属性操作相关实现技巧,需要的朋友可以参考下...2020-06-25
  • WPF MVVM制作发送短信小按钮

    这篇文章主要为大家详细介绍了WPF MVVM发送短信小按钮的制作方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • WPF中窗体最大化问题的解决方法

    这篇文章主要给大家介绍了关于WPF中窗体最大化问题的解决方法,文中通过示例代码介绍的非常详细,对大家学习或者使用wpf具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • C# WPF 父控件通过使用可视化树找到子控件的示例代码

    这篇文章主要介绍了C# WPF 父控件通过使用可视化树找到子控件的示例代码,需要的朋友可以参考下...2020-06-25