#include "iostream"
#include "stdlib.h"
#include "iomanip"
#include "windows.h"
#include "conio.h" //有用于键盘输入字符而不用回车的函数
#include "ctime" //调用time()
using namespace std;
HANDLE hConsole; //句柄
int X = 10;
int INI = 35;
int &x = INI, &y = X;
int u = 0;
int v = 0;
int &U = u;
int &V = v;
int len=0;
int &length = len;
int sc = 0;
int &score = sc;
char a[2];
int flag_i;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(hConsole, coord);
}
void print_screen()
{
const int n = 5;
const int m = 20;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //句柄实例化
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);//设置API字体的颜色
cout << endl;
cout << endl;
cout << " -----------------欢迎来到贪吃蛇新世界----------------" << endl;
cout << " ----------------------开始你的表演--------------------" << endl;
gotoxy(25, 28);
cout << "--------w:上 s:下 a:左 d:右---------";
for (int i = 0; i < 20; i++)
{
gotoxy(n + m, n + i);
cout << "* ";
}
//cout << endl;
for (int i = 0; i < 20; i++)
{
gotoxy(n + m + 2 * i, n);
cout << "*";
}
for (int i = 0; i < 20; i++)
{
gotoxy(n + m + 2 * i, n + 20);
cout << "* ";
}
for (int i = 0; i < 21; i++)
{
gotoxy(n + m + 2 * 20, n + i);
cout << "*" << endl;
}
gotoxy(68,15);
cout << "your are score:" << score;
}
/*void move()
{
for (int i = 0; i < 20; i++)
{
gotoxy(25 + i, 5 + i);
cout << "●";
Sleep(500);
system("cls");//清屏
print_screen();//清屏之后继续将其他信息显示出来
}
}*/
void Up(int &A,int &B)
{
if (a[(flag_i + 1) % 2] == 'd')
{
A=A+length;
}
for (int i = 0; ; i++)
{
if (!_kbhit())
{
gotoxy(U, V);
cout << "$";
gotoxy(A, B );
cout << "@";
for (int j = 0; j < length + 1; j++)
{
gotoxy(A, B +1+ j);
cout << "。";
}
Sleep(500-5*i);
system("cls");
print_screen();
if ((A == U) && (B == V))
{
srand((unsigned int)time(NULL));//
U = 26+rand() % (64-26);
V = 6+rand() % (24-6);
gotoxy(U, V);
cout << "$";
length++;
score = length * 10;
}
B--;
if (B < 6||A<25||A>65||B>25)
{
gotoxy(35, 15);
cout << "you are failed.....";
A = A + 65;
B = B + 60;
break;
}
if (_kbhit())
{
B++;
break;
}
}
else
{
break;
}
}
}
void Down(int &A,int &B)
{
if (a[(flag_i + 1) % 2] == 'd')
{
A=A+ length;
}
for (int i = 0; ; i++)
{
if (!_kbhit()) //判断键盘是否有输入
{
gotoxy(U, V);
cout << "$";
for (int j = 0; j < length + 1; j++)
{
gotoxy(A, B -1-j);
cout << "。";
}
gotoxy(A, B );
cout << "@";
Sleep(500-5*i);
system("cls");
print_screen();
if ((A == U) && (B == V))
{
srand((unsigned int)time(NULL));
U = 26+rand() % (64-27);
V = 6+rand() % (24-6);
gotoxy(U, V);
cout << "$";
length++;
score = length * 10;
}
B++;
if (B < 6 || A<25 || A>65 || B >25)
{
gotoxy(35, 15);
cout << "you are failed.....";
A = A + 65;
B = B + 60;
break;
}
if (_kbhit())
{
B--;
break;
}
}
else
{
break;
}
}
}
void Left(int &A,int &B)
{
if (a[(flag_i + 1) % 2] == 'd')
{
A=A+length;
}
for (int i = 0; ; i++)
{
if (!_kbhit()) //判断键盘是否有输入
{
gotoxy(U, V);
cout << "$";
gotoxy(A, B);
cout << "@";
for (int j = 0; j < length + 1; j++)
{
//gotoxy(A + 1 + j, B);
cout << "。";
}
Sleep(500 - 5 * i);
system("cls");
print_screen();
if ((A == U) && (B == V))
{
srand((unsigned int)time(NULL));
U = 26+rand() % (64-26);
V = 6+rand() % (24-6);
gotoxy(U, V);
cout << "$";
length++;
score = length * 10;
}
A--;
if (B < 6 || A < 25 || A>65 || B > 25)
{
gotoxy(35, 15);
cout << "you are failed.....";
A = A + 65;
B = B + 60;
break;
}
if (_kbhit())
{
A++;
break;
}
}
else
{
break;
}
}
}
void Right(int &A,int &B)
{
if (a[(flag_i + 1) % 2] == 'w')
{
A = A - length;
}
else if (a[(flag_i + 1) % 2] == 's')
{
A=A-length;
}
else if (a[(flag_i + 1) % 2] == 'a')
{
A=A-length;
}
for (int i = 0; ; i++)
{
if (!_kbhit()) //判断键盘是否有输入
{
gotoxy(U, V);
cout << "$";
gotoxy(A - length-2, B);
for (int j=0; j < length + 1; j++)
{
cout << "。";
}
cout << "@";
Sleep(500-5*i);
system("cls");
print_screen();
if ((A ==U)&&(B == V))
{
srand((unsigned int)time(NULL));
U = 26+rand() % (64-26);
V = 6+rand() % (24-6);
gotoxy(U, V);
cout << "$";
length++;
score = length * 10;
}
A++;
if (B < 6 || A < 25 || A>62 || B > 25)
{
gotoxy(35, 15);
cout << "you are failed.....";
A = A + 65;
B = B + 60;
break;
}
if (_kbhit())
{
A--;
break;
}
}
else
{
break;
}
}
}
void select (char a)
{
switch (a)
{
case 'w': Up(x,y);
break;
case 's': Down(x,y);
break;
case 'a': Left(x,y);
break;
case 'd': Right(x,y);
break;
default: a='w';
break;
}
}
int main()
{
//snake s;
print_screen();
srand((unsigned int)time(NULL));
U = 26+rand() % (64-26);
V = 6+rand() % (24-6);
Left(x,y);
//cout << "x:" << x << " y:" << y << endl;
int flag = 0;
char temp;
for (int j = 0;;)
{
flag_i = flag % 2;
temp=_getch();
a[flag_i] = temp;
flag++;
select(a[flag_i]);
}
system("pause");
return 0;
}
渣渣花了好久,但是写出来了心情还是很好的。