【Winform开发2048小游戏】
2020-12-13 16:49
标签:winform blog http io color ar os sp for 先来看一下界面: 游戏帮助类 定义一个结构: 窗体代码: OK ,完成了。 【Winform开发2048小游戏】 标签:winform blog http io color ar os sp for 原文地址:http://www.cnblogs.com/liuruitao/p/4087153.htmlclass GameCore
{
//游戏地图
private int[,] map = new int[4, 4];
//合并时用到的临时数组
private int[] mergeArr = new int[4];
//空位数量
private int emptyCount = 16;
//所有空位位置
private Location[] emptyArr = new Location[16];
private Random random = new Random();
//画板
private Graphics gp = null;
//用位图做画板
private Image image = null;
// 游戏项大小
private const int ITEMSIZE = 80;
//游戏得分
private int score = 0;
public bool IsChange { get; set; }
///
struct Location
{
private int x;
private int y;
public Location(int x, int y)
{
this.x = x;
this.y = y;
}
public int X
{
get { return x; }
}
public int Y
{
get { return y; }
}
}
public partial class FrmMain : Form
{
private GameCore gameCore = null;
public FrmMain()
{
InitializeComponent();
}
public void DrawGameMap()
{
this.picMap.Image = gameCore.DrawMap();
}
//开始游戏
private void FrmMain_Load(object sender, EventArgs e)
{
StartGame();
}
private void StartGame()
{
//得分清零
lblScore.Text = "0";
gameCore = new GameCore();
DrawGameMap();
}
private void btnStart_Click(object sender, EventArgs e)
{
StartGame();
}
//用键盘控制游戏动作
private void FrmMain_KeyDown(object sender, KeyEventArgs e)
{
string keyString = e.KeyData.ToString();
switch (keyString)
{
case "Up":
case "Down":
case "Left":
case "Right":
gameCore.Move(keyString);
//更新游戏地图
UpdateGameMap();
break;
}
}
public void UpdateGameMap()
{
//游戏流程?
//地图发生过变化
if (gameCore.IsChange)
{
//绘制游戏地图
DrawGameMap();
//刷新得分
this.lblScore.Text = gameCore.Score.ToString();
//重新计算空位
gameCore.ComputeEmptyCount();
//如果空位数大于0
if (gameCore.EmptyCount > 0)
{
//产生新数
Location loc;
int num = gameCore.GenerateNum(out loc);
//开启绘制新数的动画 异步调用
Action