贪吃蛇 v1.01

时间:2022-04-18 20:40:31

1、长度二节;
2、如果触及屏幕边缘,游戏结束;

感谢张瑞阳同学改进

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
void gotoxy(int x,int y){   
   COORD c;   
   c.X = x - 1;   
   c.Y = y - 1;   
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);   
 }

char ch='O',c;
short xx1=13,yy1=40,xx2=13,yy2=40,face;
int main()
{
  HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  CONSOLE_CURSOR_INFO cci;
  GetConsoleCursorInfo(hOut, &cci);
  cci.bVisible = FALSE;
  SetConsoleCursorInfo(hOut, &cci);  //以上5行去除光标显示

gotoxy(yy2,xx2);  
  cout<<ch;
  while(1) {
      if (c=getch()) {
          gotoxy(yy1,xx1);cout<<' ';
          xx1=xx2;yy1=yy2;
          if (c=='w') {xx2--;face=1;}
          if (c=='s') {xx2++;face=3;}
          if (c=='a') {yy2--;face=4;}
          if (c=='d') {yy2++;face=2;}
          gotoxy(yy2,xx2);cout<<ch;
          _sleep(200);
        }
        while (!kbhit()) {
          gotoxy(yy1,xx1);cout<<' ';
          xx1=xx2;yy1=yy2;
          if (face==1) xx2--;
          if (face==2) yy2++;
          if (face==3) xx2++;
          if (face==4) yy2--;
          gotoxy(yy2,xx2);cout<<ch;
          if(xx2==0 || yy2==0 || yy2==80 || xx2==25) {gotoxy(36,13);cout<<"GAME OVER!";_sleep(3000);return 0;}
          _sleep(200);
        }
  }
  return 0;
}