#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include <iostream>
using namespace std;
class Color {
private:
HANDLE hConsole;
WORD defaultColor;
public:
Color() {
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(hConsole, &info);
defaultColor = info.wAttributes;
}
};
int mai0(int fvsjhdvhjsd)
{
Color color;
char l[17][17];
srand(time(0));
int ans=0;
printf(" ");
Sleep(300);
printf("猛");
Sleep(300);
printf("攻");
Sleep(300);
printf(" ");
Sleep(300);
printf("&&");
Sleep(300);
}
int mai1(int hgd)
{
Color color;
srand(time(0));
int ans=0;
printf(" ");
Sleep(300);
printf("猛");
Sleep(300);
printf("攻");
Sleep(300);
printf(" ");
Sleep(300);
printf("&&");
Sleep(300);
printf(" ");
Sleep(300);
printf("计");
Sleep(300);
printf("时");
Sleep(300);
printf("器");
Sleep(300);
printf("\n");
Sleep(300);
printf(" ");
Sleep(300);
printf("原");
Sleep(300);
printf("创:");
color.red(); // 设置为红色
}
const int WIDTH = 20;
const int HEIGHT = 15;
const int MAX_SNAKE = 100;
const int UP = 1;
const int DOWN = 2;
const int LEFT = 3;
const int RIGHT = 4;
int snakeX[MAX_SNAKE];
int snakeY[MAX_SNAKE];
int snakeLength = 3;
int direction = RIGHT;
int foodX, foodY;
int score = 0;
bool gameOver = false;
void hideCursor() {
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.bVisible = false;
SetConsoleCursorInfo(console, &cursorInfo);
}
void initGame() {
snakeX[0] = WIDTH -19;
snakeY[0] = HEIGHT -15;
snakeX[1] = WIDTH ;
snakeY[1] = HEIGHT ;
snakeX[2] = WIDTH;
snakeY[2] = HEIGHT;
srand(time(0));
foodX = rand() % (WIDTH - 2) + 1;
foodY = rand() % (HEIGHT - 2) + 1;
}
void drawGame() {
Color color;
system("cls");
for (int i = 0; i < WIDTH; i++) {
cout << "#";
}
cout << endl;
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (x == 0 || x == WIDTH - 1) {
cout << "#";
}
else {
bool isSnake = false;
for (int i = 0; i < snakeLength; i++) {
if (snakeX[i] == x && snakeY[i] == y) {
cout << (i == 0 ? "●" : "○");
isSnake = true;
break;
}
}
if (!isSnake) {
if (x == foodX && y == foodY) {
color.green();
cout << "★";
color.white();
}
else {
cout << " ";
}
}
}
}
cout << endl;
}
}
void handleInput() {
if (_kbhit()) {
switch (_getch()) {
case 72:
if (direction != DOWN)
direction = UP;
break;
case 80:
if (direction != UP)
direction = DOWN;
break;
case 75:
if (direction != RIGHT)
direction = LEFT;
break;
case 77:
if (direction != LEFT)
direction = RIGHT;
break;
case 'q':
case 'Q':
gameOver = true;
break;
}
}
}
void moveSnake() {
for (int i = snakeLength - 1; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
if (direction == UP)
snakeY[0]--;
if (direction == DOWN)
snakeY[0];
if (direction == LEFT)
snakeX[0]--;
if (direction == RIGHT)
snakeX[0];
if (snakeX[0] <= 0 || snakeX[0] >= WIDTH - 1 ||
snakeY[0] < 0 || snakeY[0] >= HEIGHT) {
gameOver = true;
}
for (int i = 1; i < snakeLength; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
gameOver = true;
}
}
if (snakeX[0] == foodX && snakeY[0] == foodY) {
score += 10;
snakeLength++;
bool foodOnSnake;
do {
foodOnSnake = false;
foodX = rand() % (WIDTH - 2) + 1;
foodY = rand() % (HEIGHT - 2) + 1;
for (int i = 0; i < snakeLength; i++) {
if (snakeX[i] == foodX && snakeY[i] == foodY) {
foodOnSnake = true;
break;
}
}
} while (foodOnSnake);
}
}
int mai2(int hdr) {
initGame();
hideCursor();
while (!gameOver) {
drawGame();
handleInput();
moveSnake();
Sleep(300);
}
system("cls");
cout << "===== 游戏结束 =" << endl;
cout << "你的得分: " << score << endl;
cout << "================" << endl;
cout << "按任意键关闭窗口" << endl;
cout << "尤其鸣谢豆包(www.doubao)" << endl;
system("pause");
}
int main()
{
while(true)
{
}