写一段可以运行在windows的贪吃蛇代码
时间: 2024-03-23 21:41:32 浏览: 70
贪吃蛇的代码实现
好的,以下是一个简单的运行在Windows上的贪吃蛇代码,使用了Windows API和C++语言实现。该代码使用控制台作为游戏界面,玩家可以通过键盘控制蛇的移动方向。注意:该代码只是一个简单的示例,仅供参考。
```cpp
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <ctime>
using namespace std;
const int WIDTH = 40; // 游戏界面宽度
const int HEIGHT = 20; // 游戏界面高度
// 蛇的结构体
struct Snake {
int x, y; // 蛇头的坐标
int length; // 蛇的长度
int direction; // 蛇移动的方向
int bodyX[100], bodyY[100]; // 蛇身体的坐标
};
// 食物的结构体
struct Food {
int x, y; // 食物的坐标
bool isExist; // 食物是否存在
};
// 控制台光标移动到指定位置
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// 隐藏光标
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
// 初始化蛇
void initSnake(Snake& snake) {
snake.x = WIDTH / 2;
snake.y = HEIGHT / 2;
snake.length = 4;
snake.direction = 1;
for (int i = 0; i < snake.length; i++) {
snake.bodyX[i] = snake.x - i;
snake.bodyY[i] = snake.y;
}
}
// 初始化食物
void initFood(Food& food) {
food.isExist = false;
}
// 生成食物
void generateFood(Snake& snake, Food& food) {
srand(time(0));
while (!food.isExist) {
food.x = rand() % (WIDTH - 2) + 1;
food.y = rand() % (HEIGHT - 2) + 1;
bool flag = true;
for (int i = 0; i < snake.length; i++) {
if (food.x == snake.bodyX[i] && food.y == snake.bodyY[i]) {
flag = false;
break;
}
}
if (flag) {
food.isExist = true;
gotoxy(food.x, food.y);
cout << "*";
}
}
}
// 判断蛇是否吃到食物
bool isEatFood(Snake& snake, Food& food) {
if (snake.x == food.x && snake.y == food.y) {
snake.length++;
snake.bodyX[snake.length - 1] = snake.bodyX[snake.length - 2];
snake.bodyY[snake.length - 1] = snake.bodyY[snake.length - 2];
food.isExist = false;
return true;
}
return false;
}
// 移动蛇
void moveSnake(Snake& snake) {
for (int i = snake.length - 1; i >= 1; i--) {
snake.bodyX[i] = snake.bodyX[i - 1];
snake.bodyY[i] = snake.bodyY[i - 1];
}
snake.bodyX[0] = snake.x;
snake.bodyY[0] = snake.y;
switch (snake.direction) {
case 1: // 上
snake.y--;
break;
case 2: // 下
snake.y++;
break;
case 3: // 左
snake.x--;
break;
case 4: // 右
snake.x++;
break;
}
}
// 判断蛇是否死亡
bool isDead(Snake& snake) {
if (snake.x <= 0 || snake.x >= WIDTH - 1 || snake.y <= 0 || snake.y >= HEIGHT - 1) {
return true;
}
for (int i = 1; i < snake.length; i++) {
if (snake.x == snake.bodyX[i] && snake.y == snake.bodyY[i]) {
return true;
}
}
return false;
}
// 绘制游戏界面
void draw(Snake& snake, Food& food) {
system("cls");
for (int i = 0; i < WIDTH; i++) {
gotoxy(i, 0);
cout << "#";
gotoxy(i, HEIGHT - 1);
cout << "#";
}
for (int i = 1; i < HEIGHT - 1; i++) {
gotoxy(0, i);
cout << "#";
gotoxy(WIDTH - 1, i);
cout << "#";
}
gotoxy(snake.x, snake.y);
cout << "@";
for (int i = 1; i < snake.length; i++) {
gotoxy(snake.bodyX[i], snake.bodyY[i]);
cout << "O";
}
if (food.isExist) {
gotoxy(food.x, food.y);
cout << "*";
}
}
int main() {
HideCursor();
Snake snake;
Food food;
initSnake(snake);
initFood(food);
generateFood(snake, food);
draw(snake, food);
while (true) {
if (_kbhit()) { // 检测键盘输入
int key = _getch();
if (key == 72 && snake.direction != 2) { // 上
snake.direction = 1;
} else if (key == 80 && snake.direction != 1) { // 下
snake.direction = 2;
} else if (key == 75 && snake.direction != 4) { // 左
snake.direction = 3;
} else if (key == 77 && snake.direction != 3) { // 右
snake.direction = 4;
}
}
moveSnake(snake);
if (isDead(snake)) {
break;
}
if (isEatFood(snake, food)) {
generateFood(snake, food);
}
draw(snake, food);
Sleep(100); // 控制蛇移动速度
}
gotoxy(0, HEIGHT);
cout << "Game Over!" << endl;
return 0;
}
```
阅读全文