[C++] 纯文本查看 复制代码
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
#include <conio.h>
#include <windows.h>
int s1=0,s2=0;
int pp=10;
void caonima();
void PrintChar(char *ch,UINT count,UINT x,UINT y) //在坐标(x,y)处输出字符串ch,ch里有count个字符
{
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
ULONG unuse;
pos.X=x;
pos.Y=y;
CONSOLE_SCREEN_BUFFER_INFO bInfo; // 窗口缓冲区信息
GetConsoleScreenBufferInfo(h, &bInfo );
WriteConsoleOutputCharacterA(h,ch,count,pos,&unuse);
}
int main()
{
char p[20][20];
srand((int)time(NULL));
int a=0,b=19;
int sjz;
int sjz2;
int t=0;
int f=19;
for(int i=0;i<22;i++){
PrintChar("-",1,i,21);
PrintChar("-",1,i,0);
PrintChar("-",1,0,i);
PrintChar("-",1,21,i);
}
sjz2=(rand()%(f-t+1))+t;
sjz=(rand()%(b-a+1))+a;
PrintChar("#",1,sjz+1,sjz2+1);
//输出图纸
while(true)
{
PrintChar(" ",1,s1+1,s2+1);
switch(pp)
{
case 1:s1--;break;
case 2:s1++;break;
case 3:s2--;break;
case 4:s2++;break;
}
caonima();
PrintChar("*",1,s1+1,s2+1);
while(kbhit())
{
PrintChar(" ",1,s1+1,s2+1);
switch(getch())
{
case 'a':
s1--;pp=1;caonima();break;
case 'd':
s1++;pp=2;caonima();break;
case 'w':
s2--;pp=3;caonima();break;
case 's':
s2++;pp=4;caonima();break;
default:
s2++;
caonima();
break;
}
PrintChar("*",1,s1+1,s2+1);
}
//蛇吃到东西了
if (s1==sjz&s2==sjz2)
{
PrintChar(" ",1,sjz+1,sjz2+1);
sjz2=(rand()%(f-t+1))+t;
sjz=(rand()%(b-a+1))+a;
PrintChar("#",1,sjz+1,sjz2+1);
}
Sleep(100);
//system("cls");
}
return 0;
}
void caonima(void)
{
if(s1<0 || s1>20 || s2<0 || s2>20)
{
system("cls");
std::cout << "sile";
pp=10;
getch();
}
}