在这里插入代码片
/* lIU GUOHUA *
/23/10/20 18:19/
/* snake game /
/ snake game: Difficulty every five minutes automatically add a difficulty (difficult to distinguish the standard speed), also think dead snake met his own body
The control method: ←↑ ↓ →key */
/I’m sorry,professor I use the version of the game can’t display in Korean***/
#include<stdio.h>
#include<time.h>
#include<Windows.h>
#define HEIGHT 20 //Set the map height
#define WIDTH 40 //Set the map width
#define PRINTF printf(“■”);
#define LINE printf(“\n”);
#define EMPTY printf(” “);
typedef struct Snakebody
{
int x, y;//The coordinates of the body
struct Snakebody *next;//Structure pointer
}Snakebody;//To create the first maintain the list of the body, save the snake’s body
typedef struct Snakexy
{
int x;
int y;
}Snakexy; //Coordinate food
int sum = 0; //Calculate a score
int JudgeSum = 0; //Determine whether to speed up
int Hard = 0; //To calculate the difficulty
int Pause = 200000000; //stop
int JudgeDirection = 4; //Determine the direction
int * PJ = &JudgeDirection;
Snakebody *Phead = NULL; //Store of the snake
Snakebody *Phead_1 = NULL;
Snakebody *Pbady = NULL;
Snakebody *end = NULL;
Snakexy * Food = NULL; //Save food place
void Front(); //The game start page
void Jfood(); // check whether to eat the food
void Jwall(); // tests if the snake hit the wall
void Jsnake(); //To test whether the snake hit the snake
void ISnake(); //Initializes the snake
void DeawMap(); //map
void FoodRand(); //Produce food
void ControlMove(); // control move and pause
void MoveCursor(int x, int y);
void Move(); // start
void Showf(); //The score and difficulty
void Free();
int main()
{
Front();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);//green
DeawMap();
Showf();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);// Dark and white
MoveCursor(34, 9);
printf(“↑”);
MoveCursor(31, 10);
printf(” ← →”);
MoveCursor(31, 11);
printf(” ↓ “);
MoveCursor(31, 15);
printf(” Every five min”);
MoveCursor(31, 16);
printf(” UP one level “);
ISnake();
FoodRand();
MoveCursor(40, 20);
Move();
return 0;
}
void Front()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//blue
MoveCursor(18, 9);
printf(” I’m sorry,professor . I use the version of the Dev-C++ can’t display in Korean. “);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//red
MoveCursor(18, 11);
printf(” Welcome to the mini Snake game. “);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//green
MoveCursor(18, 12);
printf(“The author: LIU GUOHAU”);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//blue
MoveCursor(18, 13);
printf(“Please wait a moment, trying to load for you…”);
MoveCursor(18, 14);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);// Color :blue and red
MoveCursor(18, 15);
printf(”\n-> Use arrow keys to move the snake.\n\n-> You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake’s body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n”);
for (int i = 0; i <= 3000000000u; i++){}
system(“cls”);
}
void DeawMap()
{
for (int i = 0; i < WIDTH; i++)PRINTF LINE // On the border
for (int i = 1; i < HEIGHT – 1; i++) // Print borders around
{
for (int j = 0; j < WIDTH; j++)
{
if (j == 0 || j == WIDTH – 1 || j == WIDTH – 10)
{
PRINTF
if (j == WIDTH – 1)LINE
}
else EMPTY
}
}
for (int i = 0; i < WIDTH; i++)PRINTF LINE //Under the frame
}
void MoveCursor(int x, int y)// Set cursor position (position is the beginning of the output shows)
{
/* COORD is Windows The API defined in a structure
- typedef struct _COORD
- {
- SHORT X;
- SHORT Y;
- } COORD;
- */
COORD pos = { x * 2,y };
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);// Handle to the standard output
SetConsoleCursorPosition(output, pos); //Set up the console cursor position
}
void FoodRand()
{
srand((int)time(0));
int x = rand() % 27 + 2;
int y = rand() % 17 + 2;
Phead_1 = Phead;
for (int i = 0; i <= 200; i++)
{
if (Phead_1->x == x&&Phead_1->y == y)
{
x = rand() % 27 + 2;
y = rand() % 17 + 2;
}
else
{
Phead_1 = Phead_1->next;
}
if (Phead_1->next == NULL)
{
break;
}
}
MoveCursor(x, y);
PRINTF
Food = (Snakexy*)malloc(sizeof(Snakexy));
Food->x = x;
Food->y = y;
MoveCursor(33, 5);
printf(” “);
Showf();
sum++;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);// blue
}
void ControlMove()
{
if (GetAsyncKeyState(VK_UP) && 0x8000)
{
if (JudgeDirection == 2)
{
}
else
{
JudgeDirection = 1;
}
}
if (GetAsyncKeyState(VK_DOWN) && 0x8000)
{
if (JudgeDirection == 1)
{
}
else
{
JudgeDirection = 2;
}
}
if (GetAsyncKeyState(VK_RIGHT) && 0x8000)
{
if (JudgeDirection == 3)
{
}
else
{
JudgeDirection = 4;
}
}
if (GetAsyncKeyState(VK_LEFT) && 0x8000)
{
if (JudgeDirection == 4)
{
}
else
{
JudgeDirection = 3;
}
}
if (GetAsyncKeyState(VK_RETURN) && 0x0D)
{
while (1)
{
if (GetAsyncKeyState(VK_RETURN) && 0x0D)
{
break;
}
}
}
}
void ISnake()
{
for (int i = 0; i < 5; i++)
{
Pbady = (Snakebody*)malloc(sizeof(Snakebody));
Pbady->x = 5 – i;
Pbady->y = 5;
if (Phead == NULL)
{
Phead = Pbady;
}
else
{
end->next = Pbady;
}
Pbady->next = NULL;
end = Pbady;
}
Phead_1 = Phead;
while (Phead_1->next != NULL)
{
MoveCursor(Phead_1->x, Phead_1->y);
PRINTF
Phead_1 = Phead_1->next;
}
}
void Move()
{
while (1)
{
Phead_1 = Phead;
while (Phead_1->next->next != NULL)
{
Phead_1 = Phead_1->next;
}
Phead_1->next = NULL;
for (int i = 0; i < Pause; i++) {}
ControlMove();
MoveCursor(Phead_1->x, Phead_1->y);
EMPTY
Snakebody Phead_2 = (Snakebody)malloc(sizeof(Snakebody));
if (*PJ == 1)
{
Phead_2->x = Phead->x;
Phead_2->y = Phead->y – 1;
}
if (*PJ == 2)
{
Phead_2->x = Phead->x;
Phead_2->y = Phead->y + 1;
}
if (*PJ == 3)
{
Phead_2->x = Phead->x – 1;
Phead_2->y = Phead->y;
}
if (*PJ == 4)
{
Phead_2->x = Phead->x + 1;
Phead_2->y = Phead->y;
}
Phead_2->next = Phead;
Phead = Phead_2;
MoveCursor(Phead_2->x, Phead_2->y);
PRINTF
Jfood();
Jwall();
Jsnake();
MoveCursor(40, 20);
}
}
void Jfood()
{
Phead_1 = Phead;
if (Phead_1->x == Food->x&&Phead_1->y == Food->y)
{
FoodRand();
JudgeSum += 1;
if (JudgeSum == 5)
{
JudgeSum = 0;
Hard += 1;
Pause -= 20000000;
}
while (Phead_1->next != NULL)
{
Phead_1 = Phead_1->next;
}
Snakebody S = (Snakebody)malloc(sizeof(Snakebody));
S->x = Food->x;
S->y = Food->y;
S->next = NULL;
Phead_1->next = S;
ControlMove();
MoveCursor(Phead_1->x, Phead_1->y);
PRINTF
}
//get food and the coordinates of the snake do contrast
}
void Jwall()
{
if (Phead->x == 0 || Phead->x == 29 || Phead->y == 0 || Phead->y == 19)
{
MoveCursor(10, 20);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//blue
printf(“I’m sorry, you have died, game over!!! “);
system(“pause>nul”);
exit(0);
}
}
void Jsnake()
{
Phead_1 = Phead->next;
while (Phead_1->next != NULL)
{
if ((Phead->x == Phead_1->x) && (Phead->y == Phead_1->y))
{
MoveCursor(10, 20);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); //red
printf(“I’m sorry, you have died, game over!!! “);
system(“pause>nul”);
exit(0);
}
Phead_1 = Phead_1->next;
}
}
void Showf()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE);// blue
MoveCursor(33, 5);
printf(“score:%d”, sum);
MoveCursor(33, 6);
printf(“level:%d”, Hard);
}
void Free()
{
while (Phead->next != NULL)
{
Phead=Phead->next;
free(Phead);
}
free(Phead);
}
本文地址:https://blog.csdn.net/weixin_45471526/article/details/109247999