WPF实现2048小游戏

所属分类: 软件编程 / C#教程 阅读数: 123
收藏 0 赞 0 分享

        前几天空闲的时候,实现了一个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小游戏

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

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多