如何用C语言编写简单的游戏?

c语言是计算机专业的一门基础学科。总的来说,很无聊。那么,我们能不能编一些小游戏来提高它的趣味性呢?这样,学习编程就不会是一件辛苦枯燥的事情,反而会变得像电脑游戏一样好奇有趣。

比如游戏2048:

方法/步骤:

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & lttime.h & gt

# include & ltconio.h & gt

# include & ltwindows.h & gt

#定义尺寸4

静态int得分= 0;

void putn(int n[][SIZE]);

void getn(int n[][SIZE]);

int isempty(int n[][SIZE]);

int is full(int n[][SIZE]);

void math(int n[][SIZE],char c);

void tow(int n[][SIZE]);

void toa(int n[][SIZE]);

void tos(int n[][SIZE]);

void tod(int n[][SIZE]);

//主函数

int main()

{

int i,j;

int n[SIZE][SIZE];

char c =“”;

for(I = 0;我& lt尺寸;i++)

{

for(j = 0;j & lt尺寸;j++)

{

n[I][j]= 0;

}

}

printf(" * * * * * * * * * * * * * * * * * * * * * \ n "

" ?2048(%dX%d)?\n "

" ?控制:W/A/S/D \n "

"按任意键开始\n "

“* * * * * * * * * * * * * * * * * * * * * * * * * * * \ n”,大小,尺寸);

getch();

系统(“cls”);

//n[0][1]= 2048;

//n[0][3]= 2048;

while(1)

{

if(isempty(n))

getn(n);

putn(n);

如果(!空空如也。& ampisfull(n))

打破;

睡眠(200);

c = getch();

而(c!= ' w ' & amp& ampc!= ' a ' & amp& ampc!= ' s ' & amp& ampc!='d ')

c = getch();

数学(n,c);

系统(“cls”);

}

printf("?游戏结束!\n ",分数);

返回0;

}

//函数

void putn(int n[][SIZE])

{

int i,j;

for(I = 0;我& lt尺寸;i++)

{

for(j = 0;j & lt尺寸;j++)

printf(" | ");

printf(" | \ n ");

for(j = 0;j & lt尺寸;j++)

{

if(n[i][j]==0)

printf(" | ");

其他

printf("|%4d ",n[I][j]);

}

printf(" | \ n ");

for(j = 0;j & lt尺寸;j++)

printf(" | _ _ _ _ _ _ ");

printf(" | \ n ");

}

printf("分数:%d ",分数);

}

void getn(int n[][SIZE])

{

int a,b;

a = rand()% SIZE;

b = rand()% SIZE;

while(n[a][b]!=0)

{

a = rand()% SIZE;

b = rand()% SIZE;

}

n[a][b]= 2;

}

int isempty(int n[][SIZE])

{

int i,j,count = 0;

for(I = 0;我& lt尺寸;i++)

for(j = 0;j & lt尺寸;j++)

if(n[i][j]==0)

count++;

返回计数;

}

int isfull(int n[][SIZE])

{

int i,j,count = 0;

for(I = 0;我& lt尺寸;i++)

{

for(j = 1;j & ltSIZE-1;j++)

{

if(n[I][j]= = n[I][j+1]| | n[I][j]= = n[I][j-1])

count++;

}

}

for(j = 0;j & lt尺寸;j++)

{

for(I = 1;我& ltSIZE-1;i++)

{

if(n[I][j]= = n[I+1][j]| | n[I][j]= = n[I-1][j])

count++;

}

}

返回计数& gt0?0:1;

}

void math(int n[][SIZE],char c)

{

开关(c)

{

格‘w’:拖(n);打破;

情况“a”:toa(n);打破;

案例:tos(n);打破;

案例d:死亡时间;打破;

默认值:;

}

}

void tow(整数[][大小])

{

int i,j,a;

int m[SIZE];

for(a = 0;a & lt尺寸;a++)

m[a]= 0;

for(j = 0;j & lt尺寸;j++)

{

for(a = 0;a & lt尺寸;a++)

{

for(I = 0;我& ltSIZE-1;i++)

{

if(n[i][j]==0)

{

n[I][j]= n[I+1][j];

n[I+1][j]= 0;

}

}

}

}

for(j = 0;j & lt尺寸;j++)

{

for(a=0,I = 0;我& lt尺寸;i++)

{

if(n[i][j]!= n[I+1][j]& amp;& ampn[i][j]!=0||n[i][j]==2048)

{

m[a++]= n[I][j];

n[I][j]= 0;

}

else if(n[I][j]= = n[I+1][j])

{

m[a++]= n[I][j]+n[I+1][j];

score+= m[a-1];

n[i][j]=0,n[I+1][j]= 0;

}

}

for(I = 0;我& lt尺寸;i++)

{

n[I][j]= m[I];

m[I]= 0;

}

}

}

void toa(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a = 0;a & lt尺寸;a++)

m[a]= 0;

for(I = 0;我& lt尺寸;i++)

{

for(a = 0;a & lt尺寸;a++)

{

for(j = 0;j & ltSIZE-1;j++)

{

if(n[i][j]==0)

{

n[I][j]= n[I][j+1];

n[I][j+1]= 0;

}

}

}

}

for(I = 0;我& lt尺寸;i++)

{

for(a=0,j = 0;j & lt尺寸;j++)

{

if(n[i][j]!= n[I][j+1]& amp;& ampn[i][j]!=0||n[i][j]==2048)

{

m[a++]= n[I][j];

n[I][j]= 0;

}

else if(n[I][j]= = n[I][j+1])

{

m[a++]= n[I][j]+n[I][j+1];

score+= m[a-1];

n[i][j]=0,n[I][j+1]= 0;

}

}

for(j = 0;j & lt尺寸;j++)

{

n[I][j]= m[j];

m[j]= 0;

}

}

}

void tos(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a = 0;a & lt尺寸;a++)

m[a]= 0;

for(j = SIZE-1;j & gt=0;j -)

{

for(a = SIZE-1;a & gt=0;答-)

{

for(I = SIZE-1;我& gt0;我-)

{

if(n[i][j]==0)

{

n[I][j]= n[I-1][j];

n[I-1][j]= 0;

}

}

}

}

for(j = SIZE-1;j & gt=0;j -)

{

for(a=SIZE-1,I = SIZE-1;我& gt=0;我-)

{

if(n[i][j]!= n[I-1][j]& amp;& ampn[i][j]!=0||n[i][j]==2048)

{

m[a-]= n[I][j];

n[I][j]= 0;

}

else if(n[I][j]= = n[I-1][j])

{

m[a-]= n[I][j]+n[I-1][j];

score+= m[a+1];

n[i][j]=0,n[I-1][j]= 0;

}

}

for(I = SIZE-1;我& gt=0;我-)

{

n[I][j]= m[I];

m[I]= 0;

}

}

}

void tod(int n[][SIZE])

{

int i,j,a;

int m[SIZE];

for(a = 0;a & lt尺寸;a++)

m[a]= 0;

for(I = SIZE-1;我& gt=0;我-)

{

for(a = SIZE-1;a & gt=0;答-)

{

for(j = SIZE-1;j & gt0;j -)

{

if(n[i][j]==0)

{

n[I][j]= n[I][j-1];

n[I][j-1]= 0;

}

}

}

}

for(I = SIZE-1;我& gt=0;我-)

{

for(a=SIZE-1,j = SIZE-1;j & gt=0;j -)

{

if(n[i][j]!= n[I][j-1]& amp;& ampn[i][j]!=0||n[i][j]==2048)

{

m[a-]= n[I][j];

n[I][j]= 0;

}

else if(n[I][j]= = n[I][j-1])

{

m[a-]= n[I][j]+n[I][j-1];

score+= m[a+1];

n[i][j]=0,n[I][j-1]= 0;

}

}

for(j = SIZE-1;j & gt=0;j -)

{

n[I][j]= m[j];

m[j]= 0;

}

}

}