WPF實(shí)現(xiàn)2048小游戲
前幾天空閑的時(shí)候,實(shí)現(xiàn)了一個(gè)2048游戲。除了可以設(shè)置行數(shù)和列數(shù)之外,支持修改顯示名稱,比如下面,改成神雕俠侶中的角色名稱:
游戲邏輯比較簡(jiǎn)單,大家都應(yīng)該玩過(guò)。
這里主要實(shí)現(xiàn)了四個(gè)類:Game、GameBoard還有ColorBlock和BoardGridLine。
Game類主要用來(lái)實(shí)現(xiàn)游戲的控制,比如初始化、添加新的色塊、移除色塊、控制色塊上下左右移動(dòng)、改變積分,觸發(fā)游戲結(jié)束等。
GameBoard繼承自Canvas,實(shí)現(xiàn)了色塊的合并、檢測(cè)每個(gè)格子的狀態(tài)等,另外提供了Game控制色塊移動(dòng)的接口。
ColorBlock類繼承自Shape類,用于自定義色塊的顯示,包含XY坐標(biāo)、顏色、顯示文字等依賴屬性,可以進(jìn)行動(dòng)畫(huà),另外還實(shí)現(xiàn)了具體的上下左右移動(dòng)的方法。最初幾個(gè)顏色是手動(dòng)設(shè)置,等到色塊越來(lái)越多,就隨機(jī)生成一種顏色。
BoardGridLine也繼承自Shape類,用于繪制Canvas底部的網(wǎng)格。
另外,游戲使用一個(gè)簡(jiǎn)單的文本文件保存設(shè)置,包括行數(shù)與列數(shù),以及顯示文字及其對(duì)應(yīng)顏色,具體操作在Settings類中。
最后,按鍵事件封裝在KeysNavigation中。
圖標(biāo)使用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");
}
//合并,是否繼續(xù)
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小游戲
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C# 啟用事務(wù)提交多條帶參數(shù)的SQL語(yǔ)句實(shí)例代碼
欄 目:C#教程
下一篇:Unity中C#和Java的相互調(diào)用實(shí)例代碼
本文標(biāo)題:WPF實(shí)現(xiàn)2048小游戲
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5252.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?


