服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - WPF实现2048小游戏

WPF实现2048小游戏

2022-02-20 13:59xddc C#

这篇文章主要为大家详细介绍了WPF实现2048小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

WPF实现2048小游戏

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

        这里主要实现了四个类:game、gameboard还有colorblock和boardgridline。

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

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

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

        boardgridline也继承自shape类,用于绘制canvas底部的网格。

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

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

        图标使用expression design制作:

 WPF实现2048小游戏

游戏效果如下:

WPF实现2048小游戏WPF实现2048小游戏

game.cs

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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小游戏

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

原文链接:http://blog.csdn.net/congduan/article/details/42276619

延伸 · 阅读

精彩推荐
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03