教你用C语言做一个2048小游戏
C语言是一种最基础的编程语言
今天教大家怎么用C语言写一个小游戏
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Key_Up 0x4800 // 向上方向键
#define Key_Down 0x5000 // 向下方向键
#define Key_Right 0x4d00 // 向右方向键
#define Key_Left 0x4b00 // 向左方向键
int map[4][4] = { 0 };
int check[4] = { 0 }; //判断游戏是否结束,如果都不为0,游戏结束
int i, j;
//
//显示在屏幕上
//
void print()
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("%d\t", map[i][j]);
}
printf("\n");
}
}
//
//向右移动
//
int moveRight() {
int flag = 0; //来标记是否整个map矩阵能否改变,以此判断整个游戏是否结束
//这个循环的目的是为了做某一行的加减,如某一行为[2 2 4 4],经过以下运算就会变为[0 4 0 8];
for (i = 0; i < 4; i++)
{
for (j = 4 - 1; j >= 0; j--)
{
int cell = map[i][j];
if (cell != 0)
{
int k = j - 1;
while (k >= 0)
{
int nextcell = map[i][k];
if (nextcell != 0)
{
if (cell == nextcell)
{
flag = 1;
map[i][j] += map[i][k];
map[i][k] = 0;
}
k = -1;
break;
}
k--;
}
}
}
}
//上一步做的是数据加减,这一步对加减后的数据做数据处理
//例如某一行经过上一步的数据为【0 4 0 8】,将会变为【0 0 4 8】
for (i = 0; i < 4; i++)
{
for (j = 4 - 1; j > 0; j--)
{
int cell = map[i][j];
if (cell == 0)
{
int k = j - 1;
while (k >= 0)
{
int nextcell = map[i][k];
if (nextcell != 0)
{
flag = 1;//当前元素为0,说明能移动,改变flag的值
map[i][j] = nextcell;
map[i][k] = 0;
k = -1;
}
k--;
}
}
}
}
if (flag)
return 0;
else
return 4; //游戏结束
}
//
//向左移动
//
int moveLeft() {
int flag=0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
int cell = map[i][j];//cell单词用的不太恰当,表示当前元素,你可以采用更有意义的命名
if (cell != 0)
{
int k = j + 1;
while (k < 4)
{
int nextcell = map[i][k];
if (nextcell != 0)
{
if (cell == nextcell)
{
flag = 1;//相邻两个元素相同,就说明能移动,所以改变flag的值
map[i][j] += map[i][k];
map[i][k] = 0;
}
k = 4;
break;
}
k++;
}
}
}
}
//修改部分:for循环中的i或者j的循环条件
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4 - 1; j++)
{
int cell = map[i][j];
if (cell == 0)
{
int k = j + 1;
while (k < 4)
{
int nextcell = map[i][k];
if (nextcell != 0)
{
flag = 1;
map[i][j] = nextcell;
map[i][k] = 0;
k = 4;
}
k++;
}
}
}
}
if (flag)
return 0;
else
return 3;
}
//
//向下移动
//
int moveDown() {
int flag=0;
for (i = 4 - 1; i >= 0; i--)
{
for (j = 0; j < 4; j++)
{
int cell = map[i][j];
if (cell != 0)
{
int k = i - 1;
while (k >= 0)
{
int nextcell = map[k][j];
if (nextcell != 0)
{
if (map[i][j] == map[k][j])
{
flag = 1;
map[i][j] += map[k][j];
map[k][j] = 0;
}
k = 0;
break;
}
k--;
}
}
}
}
//修改部分:for循环中的i或者j的循环条件
for (i = 4 - 1; i > 0; i--)
{
for (j = 0; j < 4; j++)
{
int cell = map[i][j];
if (cell == 0)
{
int k = i - 1;
while (k >= 0)
{
int nextcell = map[k][j];
if (nextcell != 0)
{
flag = 1;
map[i][j] = nextcell;
map[k][j] = 0;
k = 0;
}
k--;
}
}
}
}
if (flag)
return 0;
else
return 2;
}
//
//向上移动
//
int moveUp() {
int flag=0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
int cell = map[i][j];
if (cell != 0)
{
int k = i + 1;
while (k < 4)
{
int nextcell = map[k][j];
if (nextcell != 0)
{
if (cell == nextcell)
{
flag = 1;
map[i][j] += map[k][j];
map[k][j] = 0;
}
k = 4;
break;
}
k++;
}
}
}
}
//修改部分:for循环中的i或者j的循环条件
for (i = 0; i < 4 - 1; i++)
{
for (j = 0; j < 4; j++)
{
int cell = map[i][j];
if (cell == 0)
{
int k = i + 1;
while (k < 4)
{
int nextcell = map[k][j];
if (nextcell != 0)
{
flag = 1;
map[i][j] = nextcell;
map[k][j] = 0;
k = 4;
}
k++;
}
}
}
}
if (flag==0)
return 0;
else
return 1;
}
//
//随机生成在空格上生成2或者4,并且判断是否游戏结束
//
int randInteger() //有缺陷,【0 0 0 0】
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (map[i][j] == 0)
{
map[i][j] = (rand() % 2 + 1) * 2; //
return 0;
}
}
}
return 0;
}
int main()
{
print();
char key;
while (1)
{
printf("上:w;下:s;左:a;右:d;退出:q\n");
printf("请输入要执行的操作:");
scanf("%c", &key);
switch (key)
{
case 'w':
{
randInteger();
moveUp();
printf(" 向上方向键被按下\n");
print();
break;
}
case 's':
{
randInteger();
moveDown();
printf(" 向下方向键被按下\n");
print();
break;
}
case 'a':
{
randInteger();
moveLeft();
printf(" 向左方向键被按下\n");
print();
break;
}
case 'd':
{
randInteger();
moveRight();
printf(" 向右方向键被按下\n");
print();
break;
}
case 'q':
return 0;
}
}
return 0;
}
如果不会可加入学习群:W.A.B技术组 群号码205119357