欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF實現(xiàn)2048小游戲

 更新時間:2018年02月12日 08:48:53   作者:xddc  
這篇文章主要為大家詳細(xì)介紹了WPF實現(xiàn)2048小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下

        前幾天空閑的時候,實現(xiàn)了一個2048游戲。除了可以設(shè)置行數(shù)和列數(shù)之外,支持修改顯示名稱,比如下面,改成神雕俠侶中的角色名稱:

        游戲邏輯比較簡單,大家都應(yīng)該玩過。

        這里主要實現(xiàn)了四個類:Game、GameBoard還有ColorBlock和BoardGridLine。

        Game類主要用來實現(xiàn)游戲的控制,比如初始化、添加新的色塊、移除色塊、控制色塊上下左右移動、改變積分,觸發(fā)游戲結(jié)束等。

        GameBoard繼承自Canvas,實現(xiàn)了色塊的合并、檢測每個格子的狀態(tài)等,另外提供了Game控制色塊移動的接口。

        ColorBlock類繼承自Shape類,用于自定義色塊的顯示,包含XY坐標(biāo)、顏色、顯示文字等依賴屬性,可以進行動畫,另外還實現(xiàn)了具體的上下左右移動的方法。最初幾個顏色是手動設(shè)置,等到色塊越來越多,就隨機生成一種顏色。

        BoardGridLine也繼承自Shape類,用于繪制Canvas底部的網(wǎng)格。

        另外,游戲使用一個簡單的文本文件保存設(shè)置,包括行數(shù)與列數(shù),以及顯示文字及其對應(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小游戲

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在C#程序中對MessageBox進行定位的方法

    在C#程序中對MessageBox進行定位的方法

    這篇文章主要介紹了在C#程序中對MessageBox進行定位的方法,針對圖形化界面進行調(diào)試,需要的朋友可以參考下
    2015-07-07
  • C# 使用Fiddler捕獲本地HttpClient發(fā)出的請求操作

    C# 使用Fiddler捕獲本地HttpClient發(fā)出的請求操作

    這篇文章主要介紹了C# 使用Fiddler捕獲本地HttpClient發(fā)出的請求操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Unity為軟件添加使用有效期的具體步驟

    Unity為軟件添加使用有效期的具體步驟

    今天小編遇到這樣一個需求需要為軟件設(shè)定一個使用有效期,當(dāng)超過指定時間后,程序無法執(zhí)行,實現(xiàn)思路并不復(fù)雜,今天小編通過本文給大家分享Unity為軟件添加使用有效期的具體步驟,感興趣的朋友一起看看吧
    2022-03-03
  • 驗證本機的excel版本的C#代碼

    驗證本機的excel版本的C#代碼

    安裝的excel的版本,0為沒有安裝,大于1說明安裝了多個. 需要的朋友可以參考下。
    2011-08-08
  • Unity常用命令模式詳解

    Unity常用命令模式詳解

    這篇文章主要為大家詳細(xì)介紹了Unity常用命令模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 使用JsonConverter處理上傳文件的路徑問題

    使用JsonConverter處理上傳文件的路徑問題

    我們上傳一個文件,把文件保存到服務(wù)器上,會有一個明確的物理路徑,由于需要從前端訪問這個文件,還需要web服務(wù)器中的一個虛擬路徑,我們可以使用JsonConverter 來自動處理一下,這篇文章主要介紹了使用JsonConverter處理上傳文件的路徑,需要的朋友可以參考下
    2022-12-12
  • c# 多線程環(huán)境下控制對共享資源訪問的解決方法

    c# 多線程環(huán)境下控制對共享資源訪問的解決方法

    這篇文章主要介紹了c# 多線程環(huán)境下控制對共享資源訪問的解決方法,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • C#實現(xiàn)獲取mp3 Tag信息的方法

    C#實現(xiàn)獲取mp3 Tag信息的方法

    這篇文章主要介紹了C#實現(xiàn)獲取mp3 Tag信息的方法,涉及C#針對MP3文件屬性的相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • c# 通過內(nèi)存映射實現(xiàn)文件共享內(nèi)存的示例代碼

    c# 通過內(nèi)存映射實現(xiàn)文件共享內(nèi)存的示例代碼

    這篇文章主要介紹了c# 通過內(nèi)存映射實現(xiàn)文件共享內(nèi)存的示例代碼,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • 輕松學(xué)習(xí)C#的正則表達式

    輕松學(xué)習(xí)C#的正則表達式

    輕松學(xué)習(xí)C#的正則表達式,對C#的正則表達式感興趣的朋友可以參考本篇文章,幫助大家更靈活的運用C#的正則表達式
    2015-11-11

最新評論