i信息学奥赛

加入QQ群:1025629106,或关注微信公众号:i信息学奥赛,获取更多学习资源。

导航

贪吃蛇 v1.01

Posted on 2018-04-24 10:39  shnoip  阅读(750)  评论(0编辑  收藏  举报

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;
}