C++#include <ctime>
#include <iostream>
#include <cstdio>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
using namespace std;
int gamecnt,n,score,len,dx,dy,foodx,foody,lx,ly,llx,lly;
double wait_time;
bool game = true,is_down,lis_down,is_eating;
int snakex[750],snakey[750];
//int tmpx[750],tmpy[750];
bool map[28][28];
char line1[] = {32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,10,0};
char line2[] = {32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,10,0};
char line3[] = {32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,10,0};
char line4[] = {32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,10,0};
char line5[] = {32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,-95,-10,-95,-10,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,10,0};
char name_line[] = {32,32,32,32,32,32,-65,-86,-73,-94,-43,-33,-93,-70,-52,-58,-46,-35,-24,-82,32,32,32,32,32,-41,-18,-70,-13,-46,-69,-76,-50,-72,-4,-48,-62,-93,-70,50,48,50,52,47,54,47,52,32,50,50,58,48,55,32,32,32,32,32,32,10,0};
char dir;
void SetPos(int y,int x);
void green();
void white();
void died();
char S_sleep();
void init();
void check_dir();
void update_the_snake();
void check_special();
void draw_the_update();
void HideCursor();
int main()
{
system("mode con cols=62 lines=34");
HideCursor();
if(gamecnt == 0)
{
SetPos(-1,-4);
cout << line1;
cout << line2;
cout << line3;
cout << line4;
cout << line5;
cout << " ■ ■ ■ \n";
cout << " ■ ■ ■ ■ \n";
cout << " ■ ■■■■ ■ ■■■■■ \n";
cout << " ■ ■■■ ■ \n";
cout << " ■■■■■■ ■ ■ ■■■ \n";
cout << " ■ ■ ■■■ ■ \n";
cout << " ■ ■ ■ ■ \n";
cout << " ■ ■ \n";
cout << " ■ ■ ■ ■ \n";
cout << " ■ ■ ■■■ \n";
cout << " \n";
cout << " ■ ■ \n";
cout << " ■ ■ \n";
cout << " ■ ■■■■■ \n";
cout << " ■■■■■ ■ ■ \n";
cout << " ■ ■ ■ ■ \n";
cout << " ■■■■■ ■ \n";
cout << " ■ ■■■■ \n";
cout << " ■ ■ \n";
cout << " ■ ■ ■ ■ \n";
cout << " ■■■■■ ■■■ \n";
cout << " \n";
cout << " \n";
cout << " 按 y 开始游戏! \n";
cout << " \n";
cout << " 注意!这里 绝对不能是拼音输入法! \n";
cout << " \n";
cout << name_line;
cout << " 请输入你想要的移动间隔时间: \n";
SetPos(28,16);
cin >> wait_time;
while(!(kbhit() && _getch() == 'y'));
SetPos(-1,-4);
}
gamecnt++;
init();
while(true)
{
if(game == true)
{
check_dir();
update_the_snake();
check_special();
draw_the_update();
}
S_sleep();
}
return 0;
}
void SetPos(int y,int x)
{
COORD pos;
pos.X=(x+1)*2,pos.Y=y+4;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void green()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN | FOREGROUND_INTENSITY);
}
void white()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
}
void died()
{
game = false;
system("cls");
system("color 80");
SetPos(5,5);
cout << "You Died!!!\n";
SetPos(6,5);
cout << "按R键重生";
while(true)
{
if(kbhit()&&_getch()=='r')break;
}
main();
}
char S_sleep()//在while等待中检测按键
{
int t = wait_time * CLOCKS_PER_SEC,s = clock();
dir = ' ';
lis_down = is_down;
is_down = false;
while(clock() - s <= t)
{
if(game)
{
if(_kbhit())dir = _getch();
if(GetAsyncKeyState(VK_UP)&0x8000)dir = 'w';
else if(GetAsyncKeyState(VK_DOWN)&0x8000)dir = 's';
else if(GetAsyncKeyState(VK_LEFT)&0x8000)dir = 'a';
else if(GetAsyncKeyState(VK_RIGHT)&0x8000)dir = 'd';
}
if(GetAsyncKeyState(VK_SPACE)&0x8000)
{
if(is_down == false/* && lis_down == false*/)
{
is_down = true;
game ^= 1;
}
}
}
return dir;
}
void init()
{
system("cls");
system("color 07");
n = 27;
cout << " w ↑ 提示:1.空格可以暂停,暂停后空格可\n";
cout << " a d ← → 移动 以恢复\n";
cout << " s ↓ 2.不要碰到自己或者边界,否则\n";
cout << " 分数: 会死 \n";
cout << " ";
for(int i = 1;i <= n+2;i++)
{
cout << "■";
}
cout << "\n";
for(int i = 1;i <= n;i++)
{
cout << " ■";
for(int j = 1;j <= n;j++)
{
cout << " ";
}
cout << "■\n";
}
cout << " ";
for(int i = 1;i <= n+2;i++)
{
cout << "■";
}
dir = 'd';
len = 3;
dx = 0;dy = 1;
foodx = 14;foody = 20;
memset(snakex,0,sizeof snakex);
memset(snakey,0,sizeof snakey);
memset(map,0,sizeof map);
snakex[1] = 14;snakey[1] = 15;
snakex[2] = 14;snakey[2] = 14;
snakex[3] = 14;snakey[3] = 13;
for(int i = 1;i <= len;i++)
{
SetPos(snakex[i],snakey[i]);
cout << "○";
map[snakex[i]][snakey[i]] = true;
}
SetPos(snakex[1],snakey[1]);
cout << "◎";
SetPos(foodx,foody);
green();
cout << "●";
white();
game = true;
}
void check_dir()
{
if(dir == 'a')
{
dx = 0;
dy = -1;
}
else if(dir == 'd')
{
dx = 0;
dy = 1;
}
else if(dir == 'w')
{
dx = -1;
dy = 0;
}
else if(dir == 's')
{
dx = 1;
dy = 0;
}
}
void update_the_snake()
{
llx = lx;
lly = ly;
lx = snakex[len];
ly = snakey[len];
for(int i = len;i >= 2;i--)
{
snakex[i] = snakex[i-1];
snakey[i] = snakey[i-1];
}
snakex[1] += dx;
snakey[1] += dy;
}
void check_special()
{
if(!(1 <= snakex[1] && snakex[1] <= n && 1 <= snakey[1] && snakey[1] <= n))
{
died();
}
if(map[snakex[1]][snakey[1]])
{
died();
}
if(snakex[1] == foodx && snakey[1] == foody)
{
len++;
snakex[len] = lx;
snakey[len] = ly;
green();
int nfoodx = rand()%27+1,nfoody = rand()%27+1;
while(map[nfoodx][nfoody])nfoodx = rand()%27+1,nfoody = rand()%27+1;
foodx = nfoodx;
foody = nfoody;
SetPos(nfoodx,nfoody);
cout << "●";
white();
is_eating = true;
}
}
void draw_the_update()
{
SetPos(snakex[1],snakey[1]);
cout << "◎";
SetPos(snakex[2],snakey[2]);
cout << "○";
map[snakex[1]][snakey[1]] = true;
if(!is_eating)
{
SetPos(lx,ly);
cout << " ";
map[lx][ly] = false;
}
else
{
is_eating = false;
SetPos(-1,4);
score++;
cout << score;
}
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}