貪吃蛇游戲通常涉及到終端圖形編程和簡單的游戲邏輯。以下是一個基本的實現示例,包括貪吃蛇的移動、食物生成、碰撞檢測等功能。
1. 貪吃蛇游戲的基本結構
貪吃蛇游戲可以分為以下幾個部分:
游戲地圖和終端繪制:使用二維數組表示游戲地圖,通過終端輸出來實現游戲界面。
貪吃蛇的控制:包括蛇身的增長、移動和碰撞檢測。
食物的生成和消失:隨機生成食物,蛇吃到食物時長度增加,重新生成食物。
2. C語言實現示例
下面是一個基本的貪吃蛇游戲的C語言實現,適用于終端環境。注意,這個示例比較簡單,沒有加入復雜的邊界檢測、游戲結束判斷等功能,可以根據需要進行擴展。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>#define WIDTH 10
#define HEIGHT 10
#define SNAKE_MAX_LENGTH (WIDTH * HEIGHT)
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'struct Point {int x;int y;
};struct Snake {struct Point body[SNAKE_MAX_LENGTH];int length;char direction;
};struct Food {struct Point position;int is_alive;
};struct Snake snake;
struct Food food;
int score = 0;
int game_over = 0;void initialize_game() {snake.length = 1;snake.body[0].x = WIDTH / 2;snake.body[0].y = HEIGHT / 2;snake.direction = RIGHT;food.is_alive = 0;srand(time(NULL));
}void draw_game() {system("cls");// 繪制游戲地圖for (int i = 0; i <= WIDTH + 1; i++) {printf("#");}printf("\n");for (int y = 0; y < HEIGHT; y++) {printf("#"); // 左邊界for (int x = 0; x < WIDTH; x++) {// 繪制蛇身、食物或空格int is_snake_body = 0;for (int i = 0; i < snake.length; i++) {if (snake.body[i].x == x && snake.body[i].y == y) {printf("O");is_snake_body = 1;break;}}if (!is_snake_body && food.is_alive && food.position.x == x && food.position.y == y) {printf("@");} else {printf(" ");}}printf("#\n"); // 右邊界}for (int i = 0; i <= WIDTH + 1; i++) {printf("#");}printf("\n");// 打印得分printf("Score: %d\n", score);
}void generate_food() {// 生成隨機位置的食物int x, y;do {x = rand() % WIDTH;y = rand() % HEIGHT;} while (snake.length > 1 && snake.body[0].x == x && snake.body[0].y == y);food.position.x = x;food.position.y = y;food.is_alive = 1;
}void update_game() {// 移動蛇身for (int i = snake.length - 1; i > 0; i--) {snake.body[i] = snake.body[i - 1];}// 根據方向移動蛇頭switch (snake.direction) {case UP:snake.body[0].y--;break;case DOWN:snake.body[0].y++;break;case LEFT:snake.body[0].x--;break;case RIGHT:snake.body[0].x++;break;default:break;}// 碰撞檢測// 撞墻檢測if (snake.body[0].x < 0 || snake.body[0].x >= WIDTH || snake.body[0].y < 0 || snake.body[0].y >= HEIGHT) {game_over = 1;return;}// 自撞檢測for (int i = 1; i < snake.length; i++) {if (snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y) {game_over = 1;return;}}// 吃食物檢測if (food.is_alive && snake.body[0].x == food.position.x && snake.body[0].y == food.position.y) {score += 10;snake.length++;food.is_alive = 0;}// 重新生成食物if (!food.is_alive) {generate_food();}
}int main() {initialize_game();while (!game_over) {draw_game();update_game();if (_kbhit()) {char key = _getch();switch (key) {case UP:case DOWN:case LEFT:case RIGHT:// 防止反向移動if ((snake.direction == UP && key != DOWN) ||(snake.direction == DOWN && key != UP) ||(snake.direction == LEFT && key != RIGHT) ||(snake.direction == RIGHT && key != LEFT)) {snake.direction = key;}break;default:break;}}// 控制游戲速度_sleep(100); // 每次移動之間暫停100ms}printf("Game Over! Your score: %d\n", score);return 0;
}
3. 游戲說明
游戲以命令行界面展示,使用 # 表示游戲邊界,O 表示貪吃蛇身體,@ 表示食物。
初始狀態下,貪吃蛇在游戲區域的中心,游戲隨機生成食物。
使用方向鍵(w, s, a, d)控制貪吃蛇的移動方向。
貪吃蛇每吃到一個食物,得分增加10分,貪吃蛇長度增加1節。
如果貪吃蛇撞墻或自撞,游戲結束,顯示得分并退出。
這個示例提供了一個基本的貪吃蛇游戲框架,可以根據需要進行功能擴展和界面優化。在實際游戲開發中,可能需要更復雜的邊界檢測、游戲邏輯和用戶交互處理。