2048小游戏C语言实现代码

本文实例为大家分享了C语言实现2048游戏的具体代码,供大家参考,具体内容如下

大一时学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];
              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 75:
        /* 全部移动到左面 */
        for (j = 0; j <= 12; j = j + 4)
        {
          for (c = 0; c < 3; c++)
          {
            if (a[j] == 0)
            {
              a[j] = a[j + 1];
              a[j + 1] = 0;
            }
            if (a[j + 1] == 0)
            {
              a[j + 1] = a[j + 2];
              a[j + 2] = 0;
            }
            if (a[j + 2] == 0)
            {
              a[j + 2] = a[j + 3];
              a[j + 3] = 0;
            }
          }
          /* 移到左面后的合并操作 */
          if (a[j] == a[j + 1])
          {
            a[j] = a[j] * 2;
            score = score + a[j];
            a[j + 1] = a[j + 2];
            a[j + 2] = a[j + 3];
            a[j + 3] = 0;
            if (a[j + 1] == a[j + 2])
            {
              a[j + 1] = 2 * a[j + 1];
              score = score + a[j + 1];
              a[j + 2] = 0;
            }
          }
          else if (a[j + 1] == a[j + 2])
          {
            a[j + 1] = 2 * a[j + 1];
            score = score + a[j + 1];
            a[j + 2] = a[j + 3];
            a[j + 3] = 0;
          }
          else if (a[j + 2] == a[j + 3])
          {
            a[j + 2] = 2 * a[j + 2];
            score = score + a[j + 2];
            a[j + 3] = 0;
          }
          else;
        }
        break;
        /* 向右移动 */
      case 77:
        /* 全部移动到右面 */
        for (j = 3; j <= 16; j = j + 4)
        {
          for (c = 0; c < 3; c++)
          {
            if (a[j] == 0)
            {
              a[j] = a[j - 1];
              a[j - 1] = 0;
            }
            if (a[j - 1] == 0)
            {
              a[j - 1] = a[j - 2];
              a[j - 2] = 0;
            }
            if (a[j - 2] == 0)
            {
              a[j - 2] = a[j - 3];
              a[j - 3] = 0;
            }
          }
          /* 移到右面后的合并操作 */
          if (a[j] == a[j - 1])
          {
            a[j] = a[j] * 2;
            score = score + a[j];
            a[j - 1] = a[j - 2];
            a[j - 2] = a[j - 3];
            a[j - 3] = 0;
            if (a[j - 1] == a[j - 2])
            {
              a[j - 1] = 2 * a[j - 1];
              score = score + a[j - 1];
              a[j - 2] = 0;
            }
          }
          else if (a[j - 1] == a[j - 2])
          {
            a[j - 1] = 2 * a[j - 1];
            score = score + a[j - 1];
            a[j - 2] = a[j - 3];
            a[j - 3] = 0;
          }
          else if (a[j - 2] == a[j - 3])
          {
            a[j - 2] = 2 * a[j - 2];
            score = score + a[j - 2];
            a[j - 3] = 0;
          }
          else;
        }
        break;
        /* 重新开始 */
      case 'r':
        for (j = 0; j < 16; j++)
          a[j] = 0;
        sj1 = rands();
        sj2 = rands();
        for (;;)
        {
          if (sj1 == sj2)
          {
            sj1 = rands();
            sj2 = rands();
          }
          else
            break;
        }
        a[sj1] = shu();
        a[sj2] = shu();
        flag = 0;
        score = 0;
        jx = 0;
        break;
        /* 退出 */
      case 'e':
        shua();
        ct(32,19,15,3,207);
        printf(" 游戏已退出!");
        textcolor(255);
        gotoxy(0,0);
        getch();
        return 0;
        break;
        /* 其他情况(刷新) */
      default:
        ;
        break;
    }
    /*windows下的系统清屏函数*/
    //system ("cls");
    /* 判断最高分 */
    if (score > scoremax)
      scoremax = score;
    /* 判断是否胜利(是否含有2048) */
    for (j = 0; j < 16; j++)
    {
      if (a[j] == 2048 && jx == 0)
      {
        /* 胜利时输出游戏结果 */
        /*显示*/
        prin(a,&score,&scoremax);
        shua();
        ct(20,19,15,3,207);
        printf(" 游戏胜利!");
        textcolor(159);
        gotoxy(38,19);
        printf(" Continue        ");
        textcolor(239);
        gotoxy(49,20);
        printf("  Restart  ");
        textcolor(175);
        gotoxy(49,21);
        printf("  Exit   ");
        t=2;
        an=75;
        yx=0;
        for(;;)
        {
          an=getch();
          switch(an)
          {
          case 72:
            if(t==2)
            {
              t=3;
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(38,21);
         printf(" Exit          ");
         textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(47,19);
         printf("   Continue ");
         textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(49,20);
         printf("  Restart  ");
            }
            else if(t==1)
            {
              t=2;
              textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(38,19);
         printf(" Continue        ");
         textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(47,20);
         printf("   Restart  ");
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(49,21);
         printf("  Exit   ");
            }
            else
            {
              t=1;
              textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(38,20);
         printf(" Restart         ");
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(47,21);
         printf("   Exit   ");
         textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(49,19);
         printf("  Continue ");
            }
            break;
          case 80:
            if(t==2)
            {
              t=1;
               textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(38,20);
         printf(" Restart         ");
         textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(47,19);
         printf("   Continue ");
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(49,21);
         printf("  Exit   ");
            }
            else if(t==1)
            {
              t=3;
              textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(38,21);
         printf(" Exit          ");
         textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(47,20);
         printf("   Restart  ");
         textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(49,19);
         printf("  Continue ");
            }
            else
            {
            t=2;
            textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(38,19);
         printf(" Continue        ");
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(47,21);
         printf("   Exit   ");
         textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(49,20);
         printf("  Restart  ");
            }
            break;
          case '\r':
            yx=1;
            break;
          }
          if(yx==1)
          {
            yx=0;
            break;
          }
        }
        shua();
        textcolor(255);
        gotoxy(0,0);
        /* 重新开始游戏 */
        if (t == 1)
        {
          shua();
          pd = 1;
          x = 0;
          for (j = 0; j < 16; j++)
            a[j] = 0;
          sj1 = rands();
          sj2 = rands();
          for (;;)
          {
            if (sj1 == sj2)
            {
              sj1 = rands();
              sj2 = rands();
            }
            else
              break;
          }
          a[sj1] = shu();
          a[sj2] = shu();
          for (j = 0; j < 16; j++)
            b[j] = a[j];
          flag = 0;
          score = 0;
          break;
        }
        /* 继续游戏 */
        else if (t == 2)
        {
          jx = 1;
          shua();
        }
        else
        {
          shua();
          ct(32,19,15,3,207);
          printf(" 游戏已退出!");
          textcolor(255);
          gotoxy(0,0);
          getch();
          return 0;
        }
      }
    }
    /* 决定是否产生新的数及其位置以及游戏是否失败 */
    /* flag,pd,x赋初值 */
    flag = 0;
    pd = 1;
    x = 0;
    /* 判断移动后是否有空位:pd=0为有空位,pd=1为无空位 */
    for (j = 0; j < 16; j++)
    {
      if (a[j] == 0)
        pd = 0;
    }
    /* 移动是否有效:x=1有效,x=0无效 */
    for (j = 0; j < 16; j++)
    {
      if (a[j] != b[j])
      {
        x = 1;
        break;
      }
    }
    /* 移动有效时且有空位时产生新数 */
    if (pd == 0 && x == 1)
    {
      for (;;)
      {
        sj = rands();
        if (a[sj] == 0)
        {
          a[sj] = shu();
          break;
        }
        else;
      }
    }
    /* 游戏是否失败的判定 */
    /* 移动后无空位 */
    else if (pd != 0)
    {
      /* 判断是否还有可合并的项 */
      for (j = 0; j < 16; j++)
      {
        if (j != 3 && j != 7 && j != 11 && j != 15 && a[j] == a[j + 1])
        {
          flag = flag + 1;
        }
        if (j != 12 && j != 13 && j != 14 && j != 15
          && a[j] == a[j + 4])
        {
          flag = flag + 1;
        }
        if (j != 0 && j != 4 && j != 8 && j != 12 && a[j] == a[j - 1])
        {
          flag = flag + 1;
        }
        if (j != 0 && j != 1 && j != 2 && j != 3 && a[j] == a[j - 4])
        {
          flag = flag + 1;
        }
      }
      /* 无空位且没有可合并的项时游戏失败 */
      if (flag == 0)
      {
        /* 失败时输出游戏结果 */
        /*显示*/
        prin(a,&score,&scoremax);
        shua();
        ct(20,19,15,3,271);
        printf(" 游戏结束!");
        textcolor(240);
        gotoxy(38,19);
        printf("Whether continue the game ?");
        gotoxy(44,21);
        printf("@Yes   No");
        gotoxy(44,21);
        t=1;
        an=75;
        yx=0;
        for(;;)
        {
          an=getch();
          switch(an)
          {
          case 75:
            if(t==1)
            {
              t=0;
              gotoxy(53,21);
              printf("@");
              gotoxy(44,21);
              printf(" ");
            }
            else
            {
              t=1;
              gotoxy(44,21);
              printf("@");
              gotoxy(53,21);
              printf(" ");
            }
            break;
          case 77:
            if(t==1)
            {
              t=0;
              gotoxy(53,21);
              printf("@");
              gotoxy(44,21);
              printf(" "); 

            }
            else
            {
              t=1;
              gotoxy(44,21);
              printf("@");
              gotoxy(53,21);
              printf(" ");
            }
            break;
          case '\r':
            yx=1;
            break;
          }
          if(yx==1)
          {
            yx=0;
            break;
          }
        }
        shua();
        textcolor(255);
        gotoxy(0,0);
        if (t == 1)
        {
          for (j = 0; j < 16; j++)
            a[j] = 0;
          sj=rands();
          sj1 = rands();
          sj2 = rands();
          for (;;)
          {
            if (sj1 ==sj2||sj1==sj||sj2==sj)
            {
              sj1 = rands();
              sj2 = rands();
            }
            else
              break;
          }
          a[sj]=shu();
          a[sj1] = shu();
          a[sj2] = shu();
          score = 0;
          jx = 0;
        }
        else
        {
          shua();
          ct(32,19,15,3,207);
          printf(" 游戏已退出!");
          textcolor(255);
          gotoxy(0,0);
          getch();
          return 0;
        }
      }
    }
    else;
    /* 显示结果 */
    prin(a,&score,&scoremax);
  }
  return 0;
}
/*显示函数*/
void p(int*a,int i,int x,int y)
{
  int x1=x,y1=y;
  textcolor(col(a+i));
  x1=x+(i%4)*7;
  y1=y+(i/4)*4;
  gotoxy(x1,y1);
  printf("   ");
  gotoxy(x1,y1+1);
  kg(a+i);
  gotoxy(x1,y1+2);
  printf("   ");
  textcolor(240);
}
/*字体空格声明*/
void kg(int*a)
{
  if(*a==0)
    printf("   ",*a);
  else if(*a<10)
    printf(" %d ",*a);
  else if(*a<100)
    printf(" %d ",*a);
  else if(*a<1000)
    printf(" %d ",*a);
  else if(*a<10000)
    printf("%d ",*a);
  else
    printf("%d",*a);
}
/*单个颜色返回值*/
int col(int*a)
{
int co=127;
if(*(a)==0)
co=127;
else if(*(a)==2)
co=143;
else if(*(a)==4)
co=191;
else if(*(a)==8)
co=175;
else if(*(a)==16)
co=239;
else if(*(a)==32)
co=223;
else if(*(a)==64)
co=207;
else if(*(a)==128)
co=95;
else if(*(a)==256)
co=159;
else if(*(a)==512)
co=63;
else if(*(a)==1024)
co=111;
else if(*(a)==2048)
co=79;
else if(*(a)==4096)
co=287;
else
co=271;
return co;
}
/*标题字幕2048*/
void p2048(int x,int y)
{
  int i;
  textcolor(240);
  gotoxy(0,0);
  for(i=0;i<=500;i++)
    {
      printf("   ");
    }
  //2
  textcolor(207);
  gotoxy(x,y);
  printf("     ");
  gotoxy(x,y+1);
  printf("     ");
  gotoxy(x+7,y+2);
  printf("  ");
  gotoxy(x+7,y+3);
  printf("  ");
  gotoxy(x,y+4);
  printf("     ");
  gotoxy(x,y+5);
  printf("     ");
  gotoxy(x,y+6);
  printf("  ");
  gotoxy(x,y+7);
  printf("  ");
  gotoxy(x,y+8);
  printf("     ");
  gotoxy(x,y+9);
  printf("     ");
  //0
  textcolor(239);
  gotoxy(x+13,y);
  printf("     ");
  gotoxy(x+13,y+1);
  printf("     ");
  gotoxy(x+13,y+2);
  printf("  ");
  gotoxy(x+13+7,y+2);
  printf("  ");
  gotoxy(x+13,y+3);
  printf("  ");
  gotoxy(x+13+7,y+3);
  printf("  ");
  gotoxy(x+13,y+4);
  printf("  ");
  gotoxy(x+13+7,y+4);
  printf("  ");
  gotoxy(x+13,y+5);
  printf("  ");
  gotoxy(x+13+7,y+5);
  printf("  ");
  gotoxy(x+13,y+6);
  printf("  ");
  gotoxy(x+13+7,y+6);
  printf("  ");
  gotoxy(x+13,y+7);
  printf("  ");
  gotoxy(x+13+7,y+7);
  printf("  ");
  gotoxy(x+13,y+8);
  printf("     ");
  gotoxy(x+13,y+9);
  printf("     ");
  //4
  textcolor(159);
  gotoxy(x+26,y);
  printf("  ");
  gotoxy(x+26+7,y);
  printf("  ");
  gotoxy(x+26,y+1);
  printf("  ");
  gotoxy(x+26+7,y+1);
  printf("  ");
  gotoxy(x+26,y+2);
  printf("  ");
  gotoxy(x+26+7,y+2);
  printf("  ");
  gotoxy(x+26,y+3);
  printf("  ");
  gotoxy(x+26+7,y+3);
  printf("  ");
  gotoxy(x+26,y+4);
  printf("     ");
  gotoxy(x+26,y+5);
  printf("     ");
  gotoxy(x+26,y+6);
  printf("  ");
  gotoxy(x+26,y+7);
  printf("  ");
  gotoxy(x+26,y+8);
  printf("  ");
  gotoxy(x+26,y+9);
  printf("  ");
  //8
  textcolor(175);
  for(i=0;i<10;i++)
  {
  gotoxy(x+39,y+i);
  printf("     ");
  }
  textcolor(240);
  gotoxy(x+42,y+2);
  printf("  ");
  gotoxy(x+42,y+3);
  printf("  ");
  gotoxy(x+42,y+6);
  printf("  ");
  gotoxy(x+42,y+7);
  printf("  ");
  //长条
   ct(35,15,20,1,240);
   printf(" Game Sart");
   ct(35,18,20,1,240);
   printf("How to play");
   printf("\n");
   printf("\n\n\n                              Made by Mr Yan");
   gotoxy(35-25,15);
   textcolor(255);
   gotoxy(0,0);
}
/*画长条函数*/
void ct(int x,int y,int l,int k,int c)
{
  int i=0,j=0;
  textcolor(c);
  gotoxy(x,y);
  for(i=0;i<k;i++)
  {
  gotoxy(x,y+i);
  for(j=0;j<l;j++)
  {
  printf(" ");
  }
  }
  gotoxy(x+1,y+k/2);
}
void shua(void)
{
  gotoxy(0,18);
  textcolor(255);
  printf("                                  \n");
  printf("                                  \n");
  printf("                                  \n");
  printf("                                  \n");
  printf("                                  \n");
  printf("                                  \n");
  gotoxy(0,0);
}
/*胜利画面测试函数*/
int test(void)
{
  int i,t,yx;
  char an;
    textcolor(240);
    gotoxy(0,0);
    for(i=0;i<=500;i++)
    {
      printf("   ");
    }
    gotoxy(0,0);
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);
    cci.bVisible=FALSE;
    SetConsoleCursorInfo(hOut,&cci);
        //printf("\n游戏胜利!t\n重新开始游戏请按1\n继续游戏请按2\n退出请按其他键3\n");  2 1 3 

/*
  2  继续选中 textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(38,19);
         printf(" Continue        "); 

  2  继续平常 textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(49,19);
         printf("  Continue "); 

  1 重新选中 textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(38,20);
         printf(" Restart         "); 

  1  重新平常 textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(49,20);
         printf("  Restart  "); 

  3  退出选中 textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(38,21);
         printf(" Exit          "); 

  3  退出平常 textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(49,21);
         printf("  Exit   ");
     //   0000000000000000000000000 24
    */
        shua();
        ct(20,19,15,3,207);
        printf(" 游戏胜利!");
        textcolor(159);
        gotoxy(38,19);
        printf(" Continue        "); 

        textcolor(239);
        gotoxy(49,20);
        printf("  Restart  ");
        textcolor(175);
        gotoxy(49,21);
        printf("  Exit   ");
        t=2;
        an=75;
        yx=0;
        for(;;)
        {
          an=getch();
          switch(an)
          {
          case 72:
            if(t==2)
            {
              t=3;
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(38,21);
         printf(" Exit          ");
         textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(47,19);
         printf("   Continue ");
         textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(49,20);
         printf("  Restart  ");
            }
            else if(t==1)
            {
              t=2;
              textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(38,19);
         printf(" Continue        ");
         textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(47,20);
         printf("   Restart  ");
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(49,21);
         printf("  Exit   ");
            }
            else
            {
              t=1;
              textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(38,20);
         printf(" Restart         ");
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(47,21);
         printf("   Exit   ");
         textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(49,19);
         printf("  Continue ");
            }
            break;
          case 80:
            if(t==2)
            {
              t=1;
               textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(38,20);
         printf(" Restart         ");
         textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(47,19);
         printf("   Continue ");
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(49,21);
         printf("  Exit   ");
            }
            else if(t==1)
            {
              t=3;
              textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(38,21);
         printf(" Exit          ");
         textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(47,20);
         printf("   Restart  ");
         textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(49,19);
         printf("  Continue ");
            }
            else
            {
            t=2;
            textcolor(240);
         gotoxy(36,19);
         printf("              ");
         textcolor(159);
         gotoxy(38,19);
         printf(" Continue        ");
         textcolor(240);
         gotoxy(36,21);
         printf("              ");
         textcolor(175);
         gotoxy(47,21);
         printf("   Exit   ");
         textcolor(240);
         gotoxy(36,20);
         printf("              ");
         textcolor(239);
         gotoxy(49,20);
         printf("  Restart  ");
            }
            break;
          case '\r':
            yx=1;
            break;
          }
          if(yx==1)
          {
            yx=0;
            break;
          }
        }
        shua();
        textcolor(255);
        gotoxy(0,0);
        textcolor(240);
        if(t==2)
          printf("Continue    ");
        else if(t==1)
          printf("Restart    ");
        else
          printf("Exit      ");
        return 0;
}

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

(0)

相关推荐

  • C语言实现三子棋小游戏

    在这里我们要写出一个三子棋的小游戏,能够实现所需要的三字连珠的功能,并且可以使得游戏让玩家进行选择是否继续的功能. 代码: #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 #define COL 3 void init_board(char arr[ROW][COL]) { int i=0; int j=0; for

  • C语言实现推箱子小游戏

    本文实例为大家分享了C语言实现推箱子游戏的具体代码,供大家参考,具体内容如下 #include<stdio.h> #include<string.h> #include<stdlib.h> #define N 1000 /* r目的地 o箱子 i人 x墙 路(空格) w上 a左 d右 s下 */ void menu(); int level1(); int level2(); void swap(char*,char*); int opera(char *p,int *

  • 利用C语言实现2048小游戏的方法

    准备工作 首先上一张图,因为这里只是在用C语言验证算法,所以没有对界面做很好的优化,丑是理所应当的. 了解了游戏的工作原理,实际上可以将游戏描述为四个带有方向的同一操作: 1.将所有数字向一个方向移动至中间没有空位 2.将相邻的两个相同的数字加和然后放在更靠近移动方向前部的一个位置上 另外需要判断一下玩家当前输入的内容是否可以执行,如果不可以执行等待用户下一条记录. 同时需要对游戏的进程进行控制,如果可以继续游戏,那么运行玩家继续输入下一条指令,而如果不可以进行,那么提示无法继续游戏的提示. 首

  • 原创的C语言控制台小游戏

    最开始左上色块被感染,通过切换颜色,不断感染同色色块.亮点是可以切换图案,设置方块个数和最大限制次数.整体还是比较满意,希望大神指教. #include <stdio.h> #include <windows.h> #include <conio.h> #include <time.h> #include <stdlib.h> int DIFFICULT=44; int count=0 ; int TYPE_SHAPE=2 ; int flag=

  • C语言简易版flappy bird小游戏

    假期在家无聊,想随便码点东西,故有此简陋的小游戏诞生.觉着可能对初学C语言的小伙伴练习有点帮助,故写此博客.游戏界面如下: 首先,先画出整个小游戏实现的流程图,如下: 思路很简单,整个游戏界面是由一个大的char类型数组构成,更新数组的值然后不停的打印出来就形成了动态效果. 由上图看,大循环是保证游戏一直不断的进行下去,小循环是让小鸟的速度大于游戏界面里背景(由#构成的柱子)的速度(小鸟动四下柱子才动一下). 下面是具体代码(水平有限大家多多见谅,但是效果还是有的!) Bird.c文件 #inc

  • C语言开发简易版扫雷小游戏

    前言: 想起来做这个是因为那时候某天知道了原来黑框框里面的光标是可以控制的,而且又经常听人说起这个,就锻炼一下好了. 之前就完成了那1.0的版本,现在想放上来分享却发现有蛮多问题的,而且最重要的是没什么注释[果然那时候太年轻]!现在看了也是被那时候的自己逗笑了,就修改了一些小bug,增加了算是详尽而清楚的注释,嗯,MSDN上面对各种函数的解释很详细的[又锻炼一下英语],顺便让开头和结尾的展示"动"了起来,就当作1.5的版本好了. 这个只是给出了一个实现的思路,其中肯定也有很多不合理的地

  • C语言结构数组实现贪吃蛇小游戏

    一.设计思路 蛇身本质上就是个结构数组,数组里存储了坐标x.y的值,再通过一个循环把它打印出来,蛇的移动则是不断地刷新重新打印.所以撞墙.咬到自己只是数组x.y值的简单比较. 二.用上的知识点 结构数组Windows API函数 三.具体实现 先来实现静态页面,把地图.初始蛇身.食物搞定. 这里需要用到Windows API的知识,也就是对控制台上坐标的修改 //这段代码来自参考1 void Pos(int x, int y) { COORD pos; HANDLE hOutput; pos.X

  • C语言控制台版2048小游戏

    效果不好,见谅,没事就写了一个!!! /** * @author Routh * @main.c * @date 2014, 4, 26 */ #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> // console width #define CONSOLE_WIDTH 80 #define BOX_WIDTH 10 int BOX[4][4] = {

  • C语言贪吃蛇经典小游戏

    一.贪吃蛇小游戏简介: 用上下左右控制蛇的方向,寻找吃的东西,每吃一口就能得到一定的积分,而且蛇的身子会越吃越长,身子越长玩的难度就越大,不能碰墙,也不能咬到自己的身体,等到了一定的分数,就能过关. 二.函数框架 三.数据结构 typedef struct Snake { size_t x; //行 size_t y; //列 struct Snake* next; }Snake, *pSnake; 定义蛇的结构体,利用单链表来表示蛇,每个结点为蛇身体的一部分. 四.代码实现(vs2010  c

  • C语言实现俄罗斯方块小游戏

    C语言实现俄罗斯方块小游戏的制作代码,具体内容如下 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define TTY_PATH "/dev/tty" #define STTY_ON "stty raw -echo -F" #define STTY_OFF "stty -raw echo -F" int map[21][14]; char

  • C语言实现最简单的剪刀石头布小游戏示例

    本文实例讲述了C语言实现最简单的剪刀石头布小游戏.分享给大家供大家参考,具体如下: #include<stdio.h> #include<stdlib.h> #include<time.h> /*************\ * 剪刀 石头 布 * * 最简单小游戏 * \*************/ int main(void){ char gesture[3][10] = {"scissor","stone","clo

  • C语言实现五子棋小游戏

    首先我们先来看一个稍微简单些的实现方式: #include <stdio.h> #include <stdlib.h> #define N 15 int chessboard[N + 1][N + 1] = { 0 }; int whoseTurn = 0; void initGame(void); void printChessboard(void); void playChess(void); int judge(int, int); int main(void) { init

随机推荐