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

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

服务器之家 - 编程语言 - C/C++ - C语言实现2048游戏(ege图形库版)

C语言实现2048游戏(ege图形库版)

2021-07-14 16:55什么都只会一点点 C/C++

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

这几天看到我们班上一个大神写了一个2048出来,我自己也想尝试一下,经过几天自己尝试努力下,自己终于写出来了。现在和大家分享一下,也希望能得到大神的指点。

实现的效果如图

C语言实现2048游戏(ege图形库版)

先来讲一下我的思路吧

1.首先肯定是要一个4X4的二维数组来存放数字存放0、2、4……
2.游戏开始与过程中需要随机出现2或者4,所以需要调用time.h这个库
3.游戏开始时,假如当获取字符为‘w'则先用循环判定这个数字的下方有无和它相等的数字。如无则跳过,如有相加。然后在判定是否可以向上移动

下面是我的代码

(我本来是还要写一个撤回的函数 可惜写出来却不能运行。求大神指教)

?
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include<stdio.h>
//#include<conio.h>
#include<graphics.h>
#include<Windows.h>
#include<time.h>
int _back[4][4] = {};
void draw();//绘图
void play();
void init();//初始化数字
void _up();//向上移动
void _down();//向下移动
void _left();//像左移动
void _right();//向右移动
void add_number();//增加一个数字
int a[4][4] = { 0 };
int emtpy;
//空格的数量
void draw()
{
  int i, j;
  for (i = 0; i < 4; i++)
  {
    for (j = 0; j < 4; j++)
    {
      _back[i][j] = a[i][j];
      PIMAGE img;
      img = newimage();
      switch (a[i][j])
      {
      case 0:
      {
        getimage(img, "2048\\0.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 2:
      {
        getimage(img, "2048\\2.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 4:
      {
        getimage(img, "2048\\4.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 8:
      {
        getimage(img, "2048\\8.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 16:
      {
        getimage(img, "2048\\16.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 32:
      {
        getimage(img, "2048\\32.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 64:
      {
        getimage(img, "2048\\64.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 128:
      {
        getimage(img, "2048\\128.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 256:
      {
        getimage(img, "2048\\256.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 512:
      {
        getimage(img, "2048\\512.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 1024:
      {
        getimage(img, "2048\\1024.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      case 2048:
      {
        getimage(img, "2048\\2048.png");
        putimage(j * 180, i * 180, img);
        break;
      }
      }
    }
  }
 
}
void init()
{
  int x, y;
  srand(time(0));
  x = rand() % 4;
  y = rand() % 4;
  a[x][y] = 2;
  emtpy = 15;
 
}
void _up()
{
  int x, y, i;
 
  for (y = 0; y < 4; ++y) {   // 从上向下合并相同的方块
    for (x = 0; x < 4; ++x) {
      if (a[x][y] == 0)
        ;
      else {
        for (i = x + 1; i < 4; ++i) {
          if (a[i][y] == 0)
            ;
          else if (a[x][y] == a[i][y]) {
            a[x][y] += a[i][y];
            a[i][y] = 0;
            ++emtpy;
            x = i;
            break;
 
          }
          else {
 
            break;
          }
        }
      }
    }
  }
 
  for (y = 0; y < 4; ++y)  // 向上移动箱子
    for (x = 0; x < 4; ++x)
    {
      if (a[x][y] == 0)
        ;
      else {
        for (i = x; (i > 0) && (a[i - 1][y] == 0); --i) {
          a[i - 1][y] = a[i][y];
          a[i][y] = 0;
        }
      }
    }
}
void _down() {
  int x, y, i;
 
  for (y = 0; y < 4; ++y) // 向下合并相同的方格
    for (x = 3; x >= 0; --x) {
      if (a[x][y] == 0)
        ;
      else {
        for (i = x - 1; i >= 0; --i) {
          if (a[i][y] == 0)
            ;
          else if (a[x][y] == a[i][y]) {
            a[x][y] += a[i][y];
            a[i][y] = 0;
            ++emtpy;
            x = i;
            break;
          }
          else
            break;
        }
      }
    }
 
  for (y = 0; y < 4; ++y) // 向下移动方格
    for (x = 3; x >= 0; --x) {
      if (a[x][y] == 0)
        ;
      else {
        for (i = x; (i < 3) && (a[i + 1][y] == 0); ++i) {
          a[i + 1][y] = a[i][y];
          a[i][y] = 0;
        }
      }
    }
}
void _left()
{
  int x, y, i;
 
  for (x = 0; x < 4; ++x)  // 向左合并相同的方格
    for (y = 0; y < 4; ++y) {
      if (a[x][y] == 0)
        ;
      else {
        for (i = y + 1; i < 4; ++i) {
          if (a[x][i] == 0)
            ;
          else if (a[x][y] == a[x][i]) {
            a[x][y] += a[x][i];
            a[x][i] = 0;
            emtpy++;
            y = i;
            break;
          }
          else
            break;
        }
      }
    }
 
  for (x = 0; x < 4; ++x) // 向左移动方格
    for (y = 0; y < 4; ++y) {
      if (a[x][y] == 0)
        ;
      else {
        for (i = y; (i > 0) && (a[x][i - 1] == 0); --i) {
          a[x][i - 1] = a[x][i];
          a[x][i] = 0;
        }
      }
    }
}
void _right() {
  int x, y, i;
 
  for (x = 0; x < 4; ++x) // 向右合并相同的方格
    for (y = 3; y >= 0; --y) {
      if (a[x][y] == 0)
        ;
      else {
        for (i = y - 1; i >= 0; --i) {
          if (a[x][i] == 0)
            ;
          else if (a[x][y] == a[x][i]) {
            a[x][y] += a[x][i];
            a[x][i] = 0;
            ++emtpy;
            y = i;
            break;
          }
          else
            break;
        }
      }
    }
 
  for (x = 0; x < 4; ++x)  // 向右移动方格
    for (y = 3; y >= 0; --y) {
      if (a[x][y] == 0)
        ;
      else {
        for (i = y; (i < 3) && (a[x][i + 1] == 0); ++i) {
          a[x][i + 1] = a[x][i];
          a[x][i] = 0;
        }
      }
    }
}
void add_number()
{
  srand(time(0));
  if (emtpy > 0)
  {
    int x, y, temp;
    do
    {
      x = rand() % 4;
      y = rand() % 4;
    } while (a[x][y] != 0);
    temp = rand();
    int i = temp % 2;
    if (i == 1)
    {
      a[x][y] = 2;
      emtpy--;
 
    }
    else
    {
      a[x][y] = 4;
      emtpy--;
    }
 
  }
}
void play()
{
  char c = getch();
  switch (c)
  {
  case 'w':
  case'W':
  {
    _up();
    add_number();
    draw();
 
    break;
 
  }
  case 's':
  case'S':
  {
    _down();
    add_number();
    draw();
    break;
 
  }
  case 'a':
  case'A':
  {
    _left();
    add_number();
    draw();
    break;
 
 
 
  }case 'd':
  case'D':
  {
    _right();
    add_number();
    draw();
    break;
 
  }
  case 'q':
  case 'Q':
  {
    for (int i = 0; i < 4; i++)
    {
      for (int j = 0; j < 4; j++)
      {
        a[i][j] = _back[i][j];
        draw();
      }
    }
  }
  }
 
}
int main()
{
  init();
  int i, j;
  initgraph(724, 724);//初始化
  PIMAGE img;
  img = newimage();
  getimage(img, "2048/背景.jpg");
  putimage(0, 0, img);
  draw();
  for (; is_run(); delay_fps(30))
  {
    for (int i = 0; i < 4; i++)
    {
      for (int j = 0; j < 4; j++)
      {
        _back[i][j] = a[i][j];
      }
    }
    play();
  }
}

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

原文链接:https://blog.csdn.net/qq_36172443/article/details/68943592

延伸 · 阅读

精彩推荐