写一个带界面的贪吃蛇C++
时间: 2024-01-06 09:03:45 浏览: 34
以下是一个基于Windows Console的简单贪吃蛇游戏,带有基本的界面设计和控制:
```c++
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
// 游戏界面的大小和位置
const int WIDTH = 40;
const int HEIGHT = 20;
const int LEFT = 10;
const int TOP = 5;
// 贪吃蛇的方向
const int UP = 0;
const int RIGHT = 1;
const int DOWN = 2;
const int LEFT = 3;
// 贪吃蛇的长度和初始位置
const int INIT_LEN = 3;
const int INIT_X = WIDTH / 2;
const int INIT_Y = HEIGHT / 2;
// 食物的位置
int foodx, foody;
// 贪吃蛇的身体
struct Snake {
int x, y;
Snake* next;
};
// 全局变量
int direction = RIGHT;
int score = 0;
int len = INIT_LEN;
Snake* head = NULL;
// 初始化游戏
void init() {
// 初始化随机数生成器
srand(time(NULL));
// 初始化贪吃蛇的身体
head = new Snake{ INIT_X, INIT_Y, NULL };
for (int i = 1; i < INIT_LEN; i++) {
Snake* body = new Snake{ INIT_X - i, INIT_Y, head };
head = body;
}
// 随机生成食物的位置
foodx = rand() % WIDTH;
foody = rand() % HEIGHT;
// 初始化界面
system("cls");
for (int i = 0; i < WIDTH + 2; i++) {
cout << "#";
}
cout << endl;
for (int i = 0; i < HEIGHT; i++) {
cout << "#";
for (int j = 0; j < WIDTH; j++) {
if (i == foody && j == foodx) {
cout << "F";
}
else if (i == head->y && j == head->x) {
cout << "O";
}
else {
cout << " ";
}
}
cout << "#" << endl;
}
for (int i = 0; i < WIDTH + 2; i++) {
cout << "#";
}
cout << endl;
}
// 更新贪吃蛇的位置
bool update() {
// 记录贪吃蛇的尾巴
Snake* tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
// 根据方向移动贪吃蛇的头部
switch (direction) {
case UP:
head = new Snake{ head->x, head->y - 1, head };
break;
case RIGHT:
head = new Snake{ head->x + 1, head->y, head };
break;
case DOWN:
head = new Snake{ head->x, head->y + 1, head };
break;
case LEFT:
head = new Snake{ head->x - 1, head->y, head };
break;
}
// 判断贪吃蛇是否撞墙或吃到食物
if (head->x < 0 || head->x >= WIDTH || head->y < 0 || head->y >= HEIGHT) {
return false;
}
else if (head->x == foodx && head->y == foody) {
score++;
len++;
tail->next = new Snake{ tail->x, tail->y, NULL };
foodx = rand() % WIDTH;
foody = rand() % HEIGHT;
}
// 判断贪吃蛇是否撞到自己的身体
Snake* cur = head->next;
while (cur != NULL) {
if (head->x == cur->x && head->y == cur->y) {
return false;
}
cur = cur->next;
}
// 删除贪吃蛇的尾巴
cur = head;
while (cur->next->next != NULL) {
cur = cur->next;
}
delete cur->next;
cur->next = NULL;
return true;
}
// 显示游戏界面
void display() {
// 清空控制台
system("cls");
// 显示分数和长度
cout << "Score: " << score << endl;
cout << "Length: " << len << endl;
// 显示游戏界面
for (int i = 0; i < WIDTH + 2; i++) {
cout << "#";
}
cout << endl;
for (int i = 0; i < HEIGHT; i++) {
cout << "#";
for (int j = 0; j < WIDTH; j++) {
if (i == foody && j == foodx) {
cout << "F";
}
else {
Snake* cur = head;
while (cur != NULL) {
if (i == cur->y && j == cur->x) {
cout << "O";
break;
}
cur = cur->next;
}
if (cur == NULL) {
cout << " ";
}
}
}
cout << "#" << endl;
}
for (int i = 0; i < WIDTH + 2; i++) {
cout << "#";
}
cout << endl;
}
// 游戏主循环
void loop() {
while (true) {
// 延时
Sleep(100);
// 获取键盘输入
if (_kbhit()) {
int ch = _getch();
if (ch == 224) {
ch = _getch();
switch (ch) {
case 72:
if (direction != DOWN) {
direction = UP;
}
break;
case 77:
if (direction != LEFT) {
direction = RIGHT;
}
break;
case 80:
if (direction != UP) {
direction = DOWN;
}
break;
case 75:
if (direction != RIGHT) {
direction = LEFT;
}
break;
}
}
else if (ch == 'q' || ch == 'Q') {
return;
}
}
// 更新贪吃蛇的位置
if (!update()) {
// 游戏结束
cout << "Game Over!" << endl;
return;
}
// 显示游戏界面
display();
}
}
int main() {
// 初始化游戏
init();
// 进入游戏主循环
loop();
return 0;
}
```