#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
#include<vector>
#include<string.h>
#include<time.h>
#include<cmath>
using namespace std;
#define WIDTH 40
#define HEIGHT 20
#define SPACE 0
#define SNAKEHEAD 1
#define SNAKEBODY 2
#define EDGE 4
#define APPLE 5
#define POISON 6
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
int area_left, area_right, area_top, area_bottom;
int score;
bool isLose;
bool isPause;
int canvas[HEIGHT + 1][WIDTH + 1] = { 0 };
int snakeDir;
int sprint = 0;
int difficulty = 1;
int start = 0;
int exhabit = 0;
int start2 = 0;
int exhabit2 = 0;
int count = 0;
int edited = 0;
struct apple{
int exit;
int row;
int col;
}apple;
struct POS {
int row;
int col;
POS(int x, int y) :row(x), col(y) {}
};
struct POS lastBody = POS(1,1);
vector<POS> snake;
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(handle, &cursor_info);
}
void show()
{
int i, j;
gotoxy(0, 0);
for (i = area_top; i <= area_bottom; i++)
{
for (j = area_left; j <= area_right; j++)
{
if (canvas[i][j] == SNAKEHEAD)
printf("@");
else if(canvas[i][j] == APPLE)
printf("C");
else if(canvas[i][j] == POISON)
printf("X");
else if (canvas[i][j] == EDGE)
printf("#");
else if (canvas[i][j] == SNAKEBODY)
printf("*");
else printf(" ");
}
printf("\n");
}
printf("targrt:%d",(difficulty*difficulty)-difficulty+10);
printf("score:%d\n", score);
}
void startup()
{
int i, j;
area_left = 0;
area_right = WIDTH;
area_top = 0;
area_bottom = HEIGHT;
apple.exit = 1;
apple.col = 3;
apple.row = 3;
snakeDir = DOWN;
snake.push_back(POS(5, 5));
snake.push_back(POS(5, 6));
snake.push_back(POS(5, 7));
snake.push_back(POS(5, 8));
for(i=0;i<=HEIGHT;i++)
for (j = 0; j <= WIDTH; j++)
{
if (j == area_left || j == area_right)
canvas[i][j] = EDGE;
else if (i == area_top || i == area_bottom)
canvas[i][j] = EDGE;
else canvas[i][j] = SPACE;
}
canvas[apple.row][apple.col] = APPLE;
canvas[snake[0].row][snake[0].col] = SNAKEHEAD;
for (i = 1; i < snake.size(); i++)
canvas[snake[i].row][snake[i].col] = SNAKEBODY;
srand(time(0));
}
void turnLeft()
{
if(snakeDir != RIGHT && edited == 0){
edited = 1;
snakeDir = LEFT;
}
}
void turnRight()
{
if(snakeDir != LEFT && edited == 0){
edited = 1;
snakeDir = RIGHT;
}
}
void turnUp()
{
if(snakeDir != DOWN && edited == 0)
{
edited = 1;
snakeDir = UP;
}
}
void turnDown()
{
if(snakeDir != UP && edited == 0){
edited = 1;
snakeDir = DOWN;
}
}
int crack(){
if(snake[0].row == 0 || snake[0].row == area_bottom || snake[0].col == 0 || snake[0].col == area_right || canvas[snake[0].row][snake[0].col] == SNAKEBODY)
return 1;
return 0;
}
void snakeMove()
{
int i;
lastBody.row = snake[snake.size() - 1].row;
lastBody.col = snake[snake.size() - 1].col;
for (i = snake.size() - 1; i > 0; i--)
snake[i] = snake[i - 1];
switch (snakeDir)
{
case UP: snake[0].row--; break;
case DOWN: snake[0].row++; break;
case LEFT: snake[0].col--; break;
case RIGHT: snake[0].col++; break;
}
}
void refreshApple(){
while(1){
if(apple.exit == 0){
apple.exit = 1;
apple.row = rand()%(HEIGHT-1)+1;
apple.col = rand()%(WIDTH-1)+1;
}
int flag = 0;
for(int i = 0;i < snake.size();i++)
if(apple.row == snake[i].row && apple.col == snake[i].col)
flag = 1;
if(flag == 0)
break;
}
canvas[apple.row][apple.col] = APPLE;
}
int IsEat(){
if(apple.exit==1 && snake[0].row == apple.row && snake[0].col == apple.col)
return 1;
return 0;
}
void updateWithInput()
{
int i;
for (i = 0; i < snake.size(); i++)
canvas[snake[i].row][snake[i].col] = SPACE;
char input;
if (kbhit()) {
input = getch();
switch (input)
{
case 'a':
turnLeft();
if(sprint == 1)
snakeMove();
break;
case 'w':
turnUp();
if(sprint == 1)
snakeMove();
break;
case 'd':
turnRight();
if(sprint == 1)
snakeMove();
break;
case 's':
turnDown();
if(sprint == 1)
snakeMove();
break;
case ' ':isPause = !isPause; break;
default:
break;
}
}
canvas[snake[0].row][snake[0].col] = SNAKEHEAD;
for (i = 1; i < snake.size(); i++)
canvas[snake[i].row][snake[i].col] = SNAKEBODY;
if(apple.exit == 1 && IsEat()){
apple.exit = 0;
canvas[apple.row][apple.col] = SPACE;
score++;
snake.push_back(POS(lastBody.row,lastBody.col));
refreshApple();
}
}
void updateWithoutInput()
{
int i;
for (i = 0; i < snake.size(); i++)
canvas[snake[i].row][snake[i].col] = SPACE;
count ++;
if(difficulty <= 6){
if(count >= (30 - difficulty - score/2)){
count = 0;
edited = 0;
snakeMove();
}
}
else if(difficulty >= 7){
if(count >= (30-(pow((difficulty-4),2) - score/20))){
count = 0;
edited = 0;
snakeMove();
}
}
canvas[snake[0].row][snake[0].col] = SNAKEHEAD;
for (i = 1; i < snake.size(); i++)
canvas[snake[i].row][snake[i].col] = SNAKEBODY;
if(apple.exit == 1 && IsEat()){
apple.exit = 0;
canvas[apple.row][apple.col] = SPACE;
score++;
snake.push_back(POS(lastBody.row,lastBody.col));
refreshApple();
}
}
void choose(){
if(exhabit == 0){
exhabit = 1;
printf("Please choose speed\n\n");
printf("1 ->very easy 2 -> easy 3 -> normal\n4 -> hard 5 -> very hard 6 -> crazy\n7 -> very crazy 8 -> hell 9 -> unbelievable");
}
if(kbhit()){
start = 1;
difficulty = getch() - '0';
system("cls");
}
}
void choose2(){
if(exhabit2 == 0){
exhabit2 = 1;
printf("Do you need sprint?\n\n");
printf("enter y or n.\n");
}
if(kbhit()){
start2 = 1;
char tmp = getch();
if(tmp == 'y')
sprint = 1;
if(tmp == 'n')
sprint = 0;
system("cls");
}
}
int main()
{
HideCursor();
while(start == 0)
choose();
while(start2 == 0)
choose2();
startup();
while (1)
{
show();
updateWithInput();
if (!isPause)
updateWithoutInput();
if (score == ((difficulty*difficulty)-difficulty+10))
{
printf("^V^\n*congratulations!!!\n");
break;
}
if(crack() == 1){
printf("T_T\nyou have lost!!!\n");
break;
}
}
system("pause");
return 0;
}