第一次发,只想提供一个程序
#include <iostream>
#include <vector>
#include <Windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
void frame();//输出边框
void snake_move();//蛇的移动
void cursor_move(int x, int y);//光标移动
void welcome();//开始界面
void keyboard_cleck();//键盘响应
void snake_print();//输出当前蛇身
void snake_delete();//清除上一步蛇身
void food();//生成食物
void eating();//判断是否吃到食物
void end();//结束条件
struct snakes
{
int x;
int y;
};
snakes snake[100] = {};
int length = 3;
int score = 0;
char cleck = 77;
bool food_eated = false;
int flag = 1;
int food_x, food_y;
int head_x, head_y;
bool gameend = false;
int main()
{
srand(time(0));
snake[2].x = 34;
snake[2].y = 13;
snake[1].x = 33;
snake[1].y = 13;
snake[0].x = 32;
snake[0].y = 13;
SetConsoleTextAttribute(HANDLE(GetStdHandle(STD_OUTPUT_HANDLE)), 11);
welcome();
frame();
SetConsoleTextAttribute(HANDLE(GetStdHandle(STD_OUTPUT_HANDLE)), 14);
food_x = (int)rand() % 63 + 2;
food_y = (int)rand() % 23 + 2;
cursor_move(food_x, food_y);
cout << "$";
keyboard_cleck();
}
void cursor_move(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(HANDLE(GetStdHandle(STD_OUTPUT_HANDLE)), pos);
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void frame()
{
for (int i = 1; i <= 25; i++)
{
for (int j = 1; j <= 65; j++)
{
if (i == 1 || i == 25 || j == 1 || j == 65)
{
cursor_move(j, i);
cout << "■";
}
}
}
}
void welcome()
{
cursor_move(25, 13);
cout << "快来玩呀";
cursor_move(26, 15);
cout << "按任意键开始";
char a;
a = _getch();
system("cls");
}
void snake_move()
{
switch ((int)cleck)
{
case 72:
for (int i = 0; i < length - 1; i++)
{
snake[i].x = snake[i + 1].x;
snake[i].y = snake[i + 1].y;
}
snake[length - 1].x = snake[length - 2].x;
snake[length - 1].y = snake[length - 2].y - 1;
break;
case 80:
for (int i = 0; i < length - 1; i++)
{
snake[i].x = snake[i + 1].x;
snake[i].y = snake[i + 1].y;
}
snake[length - 1].x = snake[length - 2].x;
snake[length - 1].y = snake[length - 2].y + 1;
break;
case 75:
for (int i = 0; i < length - 1; i++)
{
snake[i].x = snake[i + 1].x;
snake[i].y = snake[i + 1].y;
}
snake[length - 1].x = snake[length - 2].x - 2;
snake[length - 1].y = snake[length - 2].y;
break;
case 77:
for (int i = 0; i < length - 1; i++)
{
snake[i].x = snake[i + 1].x;
snake[i].y = snake[i + 1].y;
}
snake[length - 1].x = snake[length - 2].x + 2;
snake[length - 1].y = snake[length - 2].y;
break;
}
}
void keyboard_cleck()
{
while (1)
{
if (_kbhit())
{
cleck = _getch();
}
cursor_move(75, 12);
cout << "得分:" << score;
snake_delete();
eating();
if (!food_eated)
{
snake_move();
}
food();
snake_print();
end();
if (gameend)
{
cursor_move(33, 12);
cout << "继续努力";
cursor_move(1, 26);
break;
}
Sleep(50);
}
}
void snake_print()
{
for (int i = 0; i < length; i++)
{
cursor_move(snake[i].x, snake[i].y);
cout << "@";
}
}
void snake_delete()
{
for (int i = 0; i < length; i++)
{
cursor_move(snake[i].x, snake[i].y);
cout << " ";
}
}
void food()
{
if (food_eated)
{
food_x = rand() % 63 + 2;
food_y = rand() % 23 + 2;
cursor_move(food_x, food_y);
cout << "$";
score++;
cursor_move(75, 12);
cout << "得分:" << score;
food_eated = false;
}
else
{
cursor_move(food_x, food_y);
cout << "$";
}
}
void eating()
{
head_x = snake[length - 1].x;
head_y = snake[length - 1].y;
switch ((int)cleck)
{
case 72:
head_y = head_y - 1;
break;
case 80:
head_y = head_y + 1;
break;
case 75:
head_x = head_x - 1;
break;
case 77:
head_x = head_x + 1;
break;
}
if (cleck == 75 || cleck == 77)
{
if ((head_x == food_x || head_x == food_x - 1) && head_y == food_y)
{
food_eated = true;
length++;
snake[length - 1].x = food_x;
snake[length - 1].y = food_y;
}
}
else
{
if (head_x == food_x && head_y == food_y)
{
food_eated = true;
length++;
snake[length - 1].x = food_x;
snake[length - 1].y = food_y;
}
}
}
void end()
{
for (int i = 0; i < length - 1; i++)
{
if (snake[length - 1].x == snake[i].x && snake[length - 1].y == snake[i].y)
{
gameend = true;
}
if ((snake[length - 1].x <= 2 || snake[length - 1].x >= 64) || (snake[length - 1].y <= 1 || snake[length - 1].y >= 25))
{
gameend = true;
}
}
}