贪吃蛇c语言
时间: 2023-06-21 14:05:43 浏览: 87
以下是一个简单的贪吃蛇游戏的 C 语言代码,可以帮助你入门:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define WIDTH 60
#define HEIGHT 20
#define SNAKE_LENGTH 100
int map[HEIGHT][WIDTH], snake[SNAKE_LENGTH][2];
int snake_length = 3, score = 0;
int dir_x = 1, dir_y = 0;
int food_x = 0, food_y = 0;
void generate_food();
void show();
void update();
int main() {
srand(time(NULL));
snake[0][0] = HEIGHT / 2;
snake[0][1] = WIDTH / 2;
for (int i = 1; i < snake_length; i++) {
snake[i][0] = snake[i - 1][0] - 1;
snake[i][1] = snake[i - 1][1];
}
generate_food();
while (1) {
if (kbhit()) {
char ch = getch();
if (ch == 'w' && dir_x != 1) {
dir_x = -1;
dir_y = 0;
}
else if (ch == 's' && dir_x != -1) {
dir_x = 1;
dir_y = 0;
}
else if (ch == 'a' && dir_y != 1) {
dir_x = 0;
dir_y = -1;
}
else if (ch == 'd' && dir_y != -1) {
dir_x = 0;
dir_y = 1;
}
}
update();
show();
Sleep(100);
}
return 0;
}
void generate_food() {
while (1) {
food_x = rand() % HEIGHT;
food_y = rand() % WIDTH;
if (map[food_x][food_y] == 0) {
map[food_x][food_y] = 2;
break;
}
}
}
void show() {
system("cls");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (i == snake[0][0] && j == snake[0][1]) {
printf("O");
}
else if (map[i][j] == 2) {
printf("*");
}
else if (map[i][j] == 1) {
printf("o");
}
else {
printf(" ");
}
}
printf("\n");
}
printf("score: %d\n", score);
}
void update() {
int new_head[2];
new_head[0] = snake[0][0] + dir_x;
new_head[1] = snake[0][1] + dir_y;
if (new_head[0] < 0 || new_head[0] >= HEIGHT || new_head[1] < 0 || new_head[1] >= WIDTH) {
exit(0);
}
if (map[new_head[0]][new_head[1]] == 1) {
exit(0);
}
if (map[new_head[0]][new_head[1]] == 2) {
snake_length++;
score++;
generate_food();
}
map[snake[snake_length - 1][0]][snake[snake_length - 1][1]] = 0;
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] += dir_x;
snake[0][1] += dir_y;
map[snake[0][0]][snake[0][1]] = 1;
}
```
这个简单的代码实现了一个控制台中的贪吃蛇游戏。通过不断地更新和显示画面,来实现游戏的进行。这个代码还有很多可以改进的地方,比如添加更多关卡和等级,增加音效和动画等等。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)