跳到正文

个人记录

The_new_game--------Greedy_Snake

Ingressus_Yang约 16 分钟
原文
文章信息与版本

文章信息

发布
2025年3月8日
收录
2025年12月12日
更新
2026年9月21日
篇幅
约 16 分钟
获赞
1

版本

  1. v1当前

这是我新make的贪吃蛇代码^_^

C++
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <bits/stdc++.h>
#include <ctime>
#include <random>
#define N 21
using namespace std;
int S,t=0,aa,bb,Hungrily;
bool flag=0;
string Map[10][40]=
{
  {"■■","■■","■■","■■","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","■■","■■","■■","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","■■","  "},
  {"■■","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","■■","  ","■■","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","■■","  "},
  {"■■","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","■■","  ","■■","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","■■","  "},
  {"■■","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","■■","  ","■■","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","  ","■■","  "},
  {"■■","  ","■■","■■","  ","■■","■■","■■","  ","  ","■■","■■","■■","■■","■■","  ","■■","■■","■■","  ","  ","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","■■","■■","  ","■■","■■","■■","  ","■■","  "},
  {"■■","  ","  ","■■","  ","■■","  ","■■","  ","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","  ","  ","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","  "},
  {"■■","  ","  ","■■","  ","■■","  ","■■","  ","  ","■■","  ","■■","  ","■■","  ","■■","■■","■■","  ","  ","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","■■","■■","  ","■■","  ","  ","  ","■■","  "},
  {"■■","  ","  ","■■","  ","■■","  ","■■","  ","  ","■■","  ","■■","  ","■■","  ","■■","  ","  ","  ","  ","  ","■■","  ","■■","  ","■■","  ","■■","  ","■■","  ","  ","  ","■■","  ","  ","  ","■■","  "},
  {"■■","  ","  ","■■","  ","■■","  ","■■","  ","  ","■■","  ","■■","  ","■■","  ","■■","  ","  ","  ","  ","  ","■■","  ","■■","  ","  ","■■","  ","  ","■■","  ","  ","  ","■■","  ","  ","  ","  ","  "},
  {"■■","■■","■■","■■","  ","■■","■■","■■","■■","  ","■■","  ","■■","  ","■■","  ","■■","■■","■■","  ","  ","  ","■■","■■","■■","  ","  ","■■","  ","  ","■■","■■","■■","  ","■■","  ","  ","  ","■■","  "},
};
void HideCursor()//隐藏光标
{
  CONSOLE_CURSOR_INFO cursor_info = {1, 0};
  SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x,int y)//位置函数
{
  COORD pos;
  pos.X=2*x;
  pos.Y=y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
  int i,j;//初始化围墙
  int wall[N+2][2*(N+2)]= {{0}};
  for(i=1; i<=N; i++)for(j=1; j<=2*N; j++)wall[i][j]=1;
  //2 green/7 grey/8 grey++
  for(i=0; i<N+2; i++)
  {
    for(j=0; j<2*(N+2)-2; j++)
    {
      if(wall[i][j])
      {
        color(7);
        cout<<"■";
      }
      else
      {
        color(8);
        cout<<"□";
      }
    }
    cout<<endl;
  }
  gotoxy(N+3,1);//显示信息
  color(12);
  cout<<"按 W S A D 移动方向"<<endl;
  gotoxy(N+3,2);
  color(12);
  cout<<"按任意键暂停"<<endl;
  gotoxy(N+3,3);
  color(6);
  cout<<"此游戏为安庆四中708班杨奕辰同学编写,如有巧合,纯属巧合,版权所有,侵权必究!"<<endl;
  gotoxy(N+3,4);
  color(12);
  cout<<"得分:"<<endl;
  gotoxy(N+3,5);
  color(12);
  cout<<"饱食度:"<<endl;
  apple[0]=int(rand()%(N-1)+1)+1;//苹果
  apple[1]=int(rand()%(N-1)+1)+1;
  gotoxy(apple[0],apple[1]);
  color(12);
  cout<<"■"<<endl;
}
int main()
{
  int i,j;
  int** snake=NULL;
  int apple[2];
  int score=0;
  int tail[2];
  int len=3;
  char ch='p';
  srand((unsigned)time(0));
  HideCursor();
  //主页部分START
  //Greedy Snake
  cout<<endl;
  gotoxy(20,10);
  color(1),cout<<'G';
  color(2),cout<<'r';
  color(3),cout<<'e';
  color(4),cout<<'e';
  color(5),cout<<'d';
  color(6),cout<<'y';
  color(8),cout<<" S";
  color(9),cout<<'n';
  color(10),cout<<'a';
  color(11),cout<<'k';
  color(12),cout<<'e';
  cout<<endl;
  color(15);
  gotoxy(20,11);
  cout<<"开始游戏(s)";
  gotoxy(15,12);
as:
  char sh=_getch();
  if(sh=='s')
  {
    cout<<"正在加载中...";
    for(int i=1; i<=20; i++)cout<<"■",Sleep(100);
    gotoxy(0,0);
    color(0);
    for(int i=0; i<20; i++)
    {
      for(int j=0; j<80; j++)cout<<' ';
      cout<<endl;
    }
  }
  else
  {
    cout<<"error!!!";
    exit(0);
  }
  color(15);
  gotoxy(0,0);
  cout<<"欢迎来到《贪吃蛇---YangYichen_4.0版本》!"<<endl;
  Sleep(100);
  cout<<"相比3.0版本而言,4.0版本增加了饱食度"<<endl;
  Sleep(100);
  cout<<"当虫子拐弯时,会消耗1格的饱食度"<<endl;
  Sleep(100);
  cout<<"当虫子吃到苹果时,饱食度将会加上当时的分数"<<endl;
  Sleep(100);
  cout<<"当饱食度小于0或蛇撞到墙时,你----死了!!!"<<endl;
  Sleep(100);
  cout<<"Let`s go!!!"<<endl;
  Sleep(100);
  cout<<endl<<endl;
  cout<<"请选择您合适的难度:"<<endl;
  Sleep(100);
  for(int i=1; i<=50; i++)cout<<'-';
  cout<<endl;
  Sleep(100);
  cout<<"Lv.1-----初始速度为250ms/格,加速度为10%,初始饱食度为10格,如选择此项请按a"<<endl;
  Sleep(5);
  cout<<"Lv.2-----初始速度为170ms/格,加速度为11%,初始饱食度为9格,如选择此项请按b"<<endl;
  Sleep(5);
  cout<<"Lv.3-----初始速度为100ms/格,加速度为12%,初始饱食度为8格,如选择此项请按c"<<endl;
  Sleep(5);
  cout<<"Lv.4-----初始速度为70ms/格,加速度为13%,初始饱食度为7格,如选择此项请按d"<<endl;
  Sleep(5);
  cout<<"Lv.5-----初始速度为50ms/格,加速度为15%,初始饱食度为5格,如选择此项请按e"<<endl;
  char CIN=_getch();
  if(CIN=='a')S=250,aa=90,bb=100,Hungrily=10;
  if(CIN=='b')S=170,aa=89,bb=100,Hungrily=9;
  if(CIN=='c')S=100,aa=88,bb=100,Hungrily=8;
  if(CIN=='d')S=70,aa=87,bb=100,Hungrily=7;
  if(CIN=='e')S=50,aa=85,bb=100,Hungrily=5;
  gotoxy(0,0);
  for(int i=0; i<20; i++)
  {
    for(int j=0; j<80; j++)cout<<' ';
    cout<<endl;
  }
  gotoxy(0,0);
  init(apple);
  snake=(int**)realloc(snake,sizeof(int*)*len);
  for(i=0; i<len; i++)
    snake[i]=(int*)malloc(sizeof(int)*2);
  for(i=0; i<len; i++)
  {
    snake[i][0]=N/2;
    snake[i][1]=N/2+i;
    gotoxy(snake[i][0],snake[i][1]);
    color(11);
    cout<<"■"<<endl;
  }
  while(1)//进入消息循环
  {
    tail[0]=snake[len-1][0];
    tail[1]=snake[len-1][1];
    gotoxy(tail[0],tail[1]);
    color(7);
    cout<<"■"<<endl;
    for(i=len-1; i>0; i--)
    {
      snake[i][0]=snake[i-1][0];
      snake[i][1]=snake[i-1][1];
      gotoxy(snake[i][0],snake[i][1]);
      if(t%15+1==7)t++;
      color(t%15+1);
      cout<<"□"<<endl;
    }
    if(kbhit())
    {
      gotoxy(0,N+2);
      ch=getche();
      int l=0,r=0;
      color(0);
      for(int i=1; i<=4; i++)
      {
        for(int j=1; j<=21; j++)
        {
          gotoxy(N+5+j,4+i);
          cout<<' ';
        }
      }
      color(12);
      for(int Read=1; Read<Hungrily; Read++)
      {
        l++;
        gotoxy(N+6+l,5+r);
        cout<<"●";
        if(Read%20==0)
        {
          r++,l=0;
          cout<<endl;
        }
      }
      Hungrily--;
    }
    switch(ch)
    {
      case 'w':
        snake[0][1]--;
        break;
      case 's':
        snake[0][1]++;
        break;
      case 'a':
        snake[0][0]--;
        break;
      case 'd':
        snake[0][0]++;
        break;
      default:
        break;
    }
    gotoxy(snake[0][0],snake[0][1]);
    color(t%15+1);
    cout<<"■"<<endl;
    Sleep(S);//速度设置
    if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
    {
      score++;
      len++;
      snake=(int**)realloc(snake,sizeof(int*)*len);
      snake[len-1]=(int*)malloc(sizeof(int)*2);
      apple[0]=rand()%(19)+1;
      apple[1]=rand()%(19)+1;
      gotoxy(apple[0],apple[1]);
      color(12);
      cout<<"■"<<endl;
      gotoxy(N+6,4);
      color(20);
      cout<<score<<endl;
//      gotoxy(N+6,5);
//      color(20);
      S=S*aa/bb,t++,Hungrily+=score;

    }
    if(snake[0][1]==0||snake[0][1]==N+1||snake[0][0]==0||snake[0][0]==N+1||Hungrily<0)//失败
    {
//      char GO[10]= {'G','a','m','e',' ','O','v','e','r','!'};
      color(30);
//      for(int i=0; i<10; i++)cout<<GO[i],Sleep(1);
      for(int i=0; i<40; i++)
      {
        for(int j=0; j<10; j++)
        {
          gotoxy(i+10,j+10);
          cout<<Map[j][i];//Sleep(0.8);
        }
      }
      for(i=0; i<len; i++)free(snake[i]);
      Sleep(INFINITE);
      exit(0);
    }
  }
  return 0;
}

希望大家多多点赞和评论,最后:

求关啦qwq

原文