#include<iostream>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
const int COLS = 22;
const int ROWS = 22;
const string S_WALL = "∷";
const string S_SPACE = " ";
const string S_HEAD = "●";
const string S_BODY = "⊙";
const string S_FOOD = "★";
int snake [COLS * ROWS][2];
int food_x, food_y;//苹果的坐标
int Score = 5;
bool gameOver = false;
bool isPosInSnake( int x , int y );
char ch = 'd';
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
void p_wall();
void p_snake();
void p_food();
void move( char ch );
void locate( int x, int y )
{
coord.X = x * 2;
coord.Y = y;
SetConsoleCursorPosition(hout, coord);
}
double random( double start , double end )//生成一个start到end之间的随机数
{
return start + ( end - start ) * rand() / ( RAND_MAX + 1.0 );
}
int main()
{
system("pause");
system ( "cls" );
//初始化屏幕的一些参数,隐藏光标
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hout, &cursor_info);
snake [0][0] = 6;//蛇头的坐标
snake [0][1] = 2;
snake [1][0] = 5;//蛇的第二个关节
snake [1][1] = 2;
snake [2][0] = 4;//蛇的第二个关节
snake [2][1] = 2;
snake [3][0] = 3;//蛇的第三个关节
snake [3][1] = 2;
snake [4][0] = 2;//蛇的第四个关节
snake [4][1] = 2;
//初始化苹果位置
do
{
food_x = random( 3 , COLS - 2 );
food_y = random( 2 , ROWS - 2 );
}
while ( isPosInSnake( food_x , food_y ) );
p_wall();//调用绘制墙面
clock_t a , b;
while( true )//死循环
{
//初始化屏幕的一些参数,隐藏光标
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hout, &cursor_info);
if( gameOver )
{
system ( "cls" );
cout << "Game Over!" << endl;
cout << "You lose!" << endl;
cout << "Score : " << Score << endl;
system("pause");
return 0;
}
p_snake();//调用绘制蛇头
p_food();//调用绘制苹果
a = clock();
while ( 1 )
{
b = clock();
if( b - a >= 230 )
{
break;
}
}
if( kbhit() )
{
char next_ch = getch();//死循环获取用户按下的字符
if( next_ch >= 'A' && next_ch <= 'Z' )
{
next_ch += 'a' - 'A';
}
if( ( 'd' == ch || 'a' == ch ) && ( next_ch == 'w' || next_ch == 's' ) )
{
ch = next_ch;
}
else if( ( 'w' == ch || 's' == ch ) && ( next_ch == 'a' || next_ch == 'd' ) )
{
ch = next_ch;
}
}
move( ch );//调整坐标的x,y位置
locate( 0 , COLS + 1 );
cout << "Now Score : " << Score;
}
}
bool isPosInSnake( int x , int y )
{
for( int i = 0; i < COLS * ROWS; i++ )
{
if( snake [i][0] == x && snake [i][1] == y )
{
return true;
}
}
return false;
}
void move( char ch )
{
int x = snake [0][0] , y = snake [0][1];
switch( ch )
{
case 'W' ://按下W,向上
case 'w' ://按下w,向上
y--;
break;
case 'S' ://按下S,向下
case 's' ://按下s,向下
y++;
break;
case 'A' ://按下A,向左
case 'a' ://按下a,向左
x--;
break;
case 'D' ://按下D,向右
case 'd' ://按下d,向右
x++;
break;
default :
return;
}
if( x < 2 || x > COLS - 1 || y < 2 || y > ROWS - 1 )//检测是否撞墙
{
gameOver = true;
return;
}
if( isPosInSnake( x , y ) )
{
gameOver = true;
return;
}
for( int i = COLS * ROWS - 1; i >= 1; i-- )//身体所有部位向前移
{
snake [i][0] = snake [i - 1][0];
snake [i][1] = snake [i - 1][1];
}
snake [0][0] = x; snake [0][1] = y;
if( x == food_x && y == food_y )//检测是否吃掉苹果
{
Score+=1;
//吃了苹果,分数加1
do
{
food_x = random( 3 , COLS - 2 );
food_y = random( 2 , ROWS - 2 );
}
while ( isPosInSnake( food_x , food_y ) );
}
else
{
for( int i = COLS * ROWS - 1; i >= 1; i-- )//删除尾巴节点
{
if( snake [i][0] > 0 )
{
locate( snake [i][0] , snake [i][1] );
cout << S_SPACE;
snake [i][0] = 0;
snake [i][1] = 0;
break;
}
}
}
}
void p_snake()//绘制蛇头+蛇身
{
for( int i = 0; i < COLS * ROWS; i++ )
{
if( snake [i][0] <= 0 )
{
break;
}
if( i == 0 )//绘制蛇头
{
locate( snake [i][0] , snake [i][1] );
cout << S_HEAD;
}
else//绘制蛇身
{
locate( snake [i][0] , snake [i][1] );
cout << S_BODY;
}
}
}
void p_wall()//绘制围墙
{
locate( 0 , 0 );
cout << endl;
for( int i = 1; i <= ROWS; i++ )
{
cout << " ";
for( int j = 1; j <= COLS; j++ )
{
if( i == 1 || i == ROWS || j == 1 || j == COLS )
{
cout << S_WALL;
}
else
{
cout << S_SPACE;
}
}
cout << endl;
}
cout << endl;
}
void p_food()//绘制苹果
{
locate( food_x , food_y );
cout << S_FOOD;
}
0.0分
2 人评分