用c++面向过程方法实现简单的贪吃蛇
时间: 2024-03-18 13:42:39 浏览: 13
好的,下面是一个用 C++ 面向过程方法实现简单的贪吃蛇的示例代码:
```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 BLOCK_SIZE = 2;
// 定义颜色
const int WHITE = 15;
const int GREEN = 10;
const int RED = 12;
// 定义贪吃蛇初始位置和长度
int snake[100][2] = {
{WIDTH / 2, HEIGHT / 2},
{WIDTH / 2 - BLOCK_SIZE, HEIGHT / 2},
{WIDTH / 2 - 2 * BLOCK_SIZE, HEIGHT / 2}
};
int snake_length = 3;
// 定义初始移动方向
char direction = 'r';
// 定义食物位置
int food[2];
// 定义游戏结束状态和分数
bool game_over = false;
int score = 0;
// 定义清空屏幕函数
void clear_screen() {
system("cls");
}
// 定义设置光标位置函数
void set_cursor_pos(int x, int y) {
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
// 定义隐藏光标函数
void hide_cursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
// 定义绘制边框函数
void draw_border() {
for (int i = 0; i < WIDTH; i += BLOCK_SIZE) {
set_cursor_pos(i, 0);
cout << char(219);
set_cursor_pos(i, HEIGHT * BLOCK_SIZE - BLOCK_SIZE);
cout << char(219);
}
for (int i = 0; i < HEIGHT; i++) {
set_cursor_pos(0, i * BLOCK_SIZE);
cout << char(219);
set_cursor_pos(WIDTH * BLOCK_SIZE - BLOCK_SIZE, i * BLOCK_SIZE);
cout << char(219);
}
}
// 定义绘制贪吃蛇函数
void draw_snake() {
for (int i = 0; i < snake_length; i++) {
set_cursor_pos(snake[i][0], snake[i][1]);
cout << char(219);
}
}
// 定义绘制食物函数
void draw_food() {
set_cursor_pos(food[0], food[1]);
cout << char(254);
}
// 定义更新分数函数
void update_score() {
set_cursor_pos(0, HEIGHT * BLOCK_SIZE);
cout << "Score: " << score;
}
// 定义生成食物函数
void generate_food() {
srand(time(0));
bool valid;
do {
valid = true;
food[0] = rand() % (WIDTH - 2 * BLOCK_SIZE) + BLOCK_SIZE;
food[1] = rand() % (HEIGHT - 2 * BLOCK_SIZE) + BLOCK_SIZE;
for (int i = 0; i < snake_length; i++) {
if (food[0] == snake[i][0] && food[1] == snake[i][1]) {
valid = false;
break;
}
}
} while (!valid);
draw_food();
}
// 定义处理键盘输入函数
void handle_input() {
if (_kbhit()) {
char key = _getch();
if ((key == 'a' || key == 'A') && direction != 'r') {
direction = 'l';
} else if ((key == 'd' || key == 'D') && direction != 'l') {
direction = 'r';
} else if ((key == 'w' || key == 'W') && direction != 'd') {
direction = 'u';
} else if ((key == 's' || key == 'S') && direction != 'u') {
direction = 'd';
}
}
}
// 定义移动贪吃蛇函数
void move_snake() {
int head[2] = {snake[0][0], snake[0][1]};
if (direction == 'l') {
head[0] -= BLOCK_SIZE;
} else if (direction == 'r') {
head[0] += BLOCK_SIZE;
} else if (direction == 'u') {
head[1] -= BLOCK_SIZE;
} else if (direction == 'd') {
head[1] += BLOCK_SIZE;
}
for (int i = snake_length - 1; i > 0; i--) {
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
snake[0][0] = head[0];
snake[0][1] = head[1];
}
// 定义判断游戏是否结束函数
bool is_game_over() {
if (snake[0][0] <= 0 || snake[0][0] >= WIDTH - BLOCK_SIZE ||
snake[0][1] <= 0 || snake[0][1] >= HEIGHT * BLOCK_SIZE - BLOCK_SIZE) {
return true;
}
for (int i = 1; i < snake_length; i++) {
if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) {
return true;
}
}
return false;
}
// 游戏循环
int main() {
hide_cursor();
draw_border();
generate_food();
while (!game_over) {
clear_screen();
draw_border();
draw_snake();
draw_food();
update_score();
handle_input();
move_snake();
if (snake[0][0] == food[0] && snake[0][1] == food[1]) {
snake_length++;
score += 10;
generate_food();
}
if (is_game_over()) {
game_over = true;
}
Sleep(100);
}
set_cursor_pos(WIDTH / 2 - 5, HEIGHT / 2);
cout << "Game Over!";
_getch();
return 0;
}
```
这个示例代码实现了一个简单的贪吃蛇游戏,游戏窗口大小为 40x20,贪吃蛇方块大小为 2x2,初始长度为 3,初始移动方向为向右,食物随机生成。玩家通过键盘控制贪吃蛇的移动方向,当贪吃蛇吃到食物时,长度加一,分数加10,食物重新生成。当贪吃蛇碰到边界或者自己时,游戏结束。在游戏画面中,显示当前分数。