大一时学c语言写的,写的不好但当时感觉还行。。
环境运行 vc6.0 ,cpp文件。
基本上是c写的,但是改变字体颜色,在控制台移动光标等等好像不是c中的。
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<windows.h>
#define x0 26
#define y0 1
HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
/*显示声明*/
void p(int*a,int i,int x,int y);
/*部分刷新声明*/
void shua(void);
/*胜利画面测试函数声明*/
int test(void);
/*字体空格声明*/
void kg(int*a);
/*画长条函数的声明*/
void ct(int x,int y,int l,int k,int c);
/*标题字幕2048*/
void p2048(int x,int y);
/*单个颜色返回值声明*/
int col(int*a);
/*移动光标*/
void gotoxy(int x,int y)
{
COORD coordScreen={0,0};
coordScreen.X=x;
coordScreen.Y=y;
SetConsoleCursorPosition(hOutput,coordScreen);
}
/*字体颜色*/
void textcolor(int color)
{
SetConsoleTextAttribute(hOutput,color);
}
/*显示函数*/
void prin(int*a,int*score,int*scoremax)
{
int i;
for (i = 0; i < 16; i++)
{
p(a,i,x0,y0);
}
printf("\n\n score=%d MAXscore=%d \n", *score, *scoremax);
}
/* 产生随机数 */
int shu(void)
{
int s = 0;
s = 1 + (int)(12.0 * rand() / (RAND_MAX + 1.0));
if (s == 12)
return 4;
else
return 2;
}
/* 决定位置 */
int rands(void)
{
return (int)(16.0 * rand() / (RAND_MAX + 1.0));
}
/* 主函数*/
int main(void)
{
int flag = 0, c = 0, pd = 1, n = 0, i = 0, m = 0, j =
0, sj, sj1, sj2, a[16] = { 0 }, b[16] ={0}, x = 0, score = 0, t = 0, jx = 0, scoremax = 0,ks=1,yx=0;
char sr,an;
//开始画面
p2048(17,1);
textcolor(255);
gotoxy(35,15);
for(;;)
{
an=getch();
switch(an)
{
case 72:
if(ks==1)
ks=0;
else
ks=1;
break;
case 80:
if(ks==1)
ks=0;
else
ks=1;
break;
case '\r':
yx=1;
break;
default:
break;
}
if(ks==1)
gotoxy(35,15);
else
gotoxy(34,18);
if(ks==1&&yx==1)
break;
else if(ks==0&&yx==1)
{
textcolor(240);
gotoxy(0,0);
for(i=0;i<=500;i++)
{
printf(" ");
}
gotoxy(0,0);
ct(12,5,58,10,223);
gotoxy(13,6);
printf(" 2048游戏说明");
gotoxy(13,8);
printf(" 通过按数字键移动数字,合并相同的数字来的取得更大的数。");
gotoxy(13,9);
printf(" 得到2048时游戏即胜利,当然您也可以选择继续挑战。");
gotoxy(13,11);
printf(" 按键操作:");
gotoxy(13,13);
printf(" 上:↑ 下:↓ 左:← 右:→ 重新开始:r 退出游戏:e");
textcolor(240);
printf("\n\n\n\n\n I know ");
getch();
p2048(17,1);
textcolor(255);
gotoxy(34,18);
}
yx=0;
}
//隐藏光标
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=FALSE;
SetConsoleCursorInfo(hOut,&cci);
//刷新画面
textcolor(240);
gotoxy(0,0);
for(i=0;i<=500;i++)
{
printf(" ");
}
gotoxy(0,0);
/*设置时间种子*/
srand((int)time(0));
/* 设置初始值 */
sj = rands();
sj1 = rands();
sj2 = rands();
for (;;)
{
if (sj == sj1 || sj == sj2 || sj1 == sj2)
{
sj1 = rands();
sj2 = rands();
}
else
break;
}
a[sj] = shu();
a[sj1] = shu();
a[sj2] = shu();
/*显示*/
prin(a,&score,&scoremax);
for (;;)
{
/* 无回显输入 */
sr = getch();
/* scanf("%d",&n); *//* 记录之前的位置,将用以比较移动是否有效 */
for (j = 0; j < 16; j++)
b[j] = a[j];
switch (sr)
{
/* 向上移动 */
case 72:
/* 全部移到上边 */
for (j = 0; j < 4; j++)
{
for (c = 0; c < 3; c++)
{
if (a[j] == 0)
{
a[j] = a[j + 4];
a[j + 4] = 0;
}
if (a[j + 4] == 0)
{
a[j + 4] = a[j + 8];
a[j + 8] = 0;
}
if (a[j + 8] == 0)
{
a[j + 8] = a[j + 12];
a[j + 12] = 0;
}
}
/* 移到上面后的合并操作 */
if (a[j] == a[j + 4])
{
a[j] = a[j] * 2;
score = score + a[j];
a[j + 4] = a[j + 8];
a[j + 8] = a[j + 12];
a[j + 12] = 0;
if (a[j + 4] == a[j + 8])
{
a[j + 4] = 2 * a[j + 4];
score = score + a[j + 4];
a[j + 8] = 0;
}
}
else if (a[j + 4] == a[j + 8])
{
a[j + 4] = 2 * a[j + 4];
score = score + a[j + 4];
a[j + 8] = a[j + 12];
a[j + 12] = 0;
}
else if (a[j + 8] == a[j + 12])
{
a[j + 8] = 2 * a[j + 8];
score = score + a[j + 8];
a[j + 12] = 0;
}
else;
}
break;
/* 向下移动 */
case 80:
/* 全部移动到下面 */
for (j = 12; j < 16; j++)
{
for (c = 0; c < 3; c++)
{
if (a[j] == 0)
{
a[j] = a[j - 4];
a[j - 4] = 0;
}
if (a[j - 4] == 0)
{
a[j - 4] = a[j - 8];