使用C++语言写贪吃蛇游戏代码,W,S,A,D控制上下左右移动
时间: 2024-05-09 11:16:55 浏览: 12
好的,这是一个关于编程的问题,可以回答。以下是使用 C 语言编写贪吃蛇游戏代码的代码示例,W,S,A,D 控制上下左右移动:
```c
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
enum boolean { false,true };
enum direction { stop, up, down, left, right };
char map[25][25];
int pos_x[625], pos_y[625];
int snake_length = 3;
int direction_of_snake = stop;
bool is_gameover = false;
int score = 0;
void print_map() {
int i,j;
for (i = 0; i < 25; i++) {
for (j = 0; j < 25; j++)
printf("%c",map[j][i]);
printf("\n");
}
}
void init_map() {
int i, j;
for(i=0;i<25;i++)
for (j = 0; j < 25; j++) {
if (i == 0 || i == 24 || j == 0 || j == 24)
map[i][j] = '#';
else
map[i][j] = ' ';
}
for (i = 1; i <= snake_length; i++) {
pos_x[i] = 12;
pos_y[i] = 12 + i - 1;
map[pos_x[i]][pos_y[i]] = '*';
}
srand((unsigned)time(NULL));
do {
i = rand() % 22 + 1;
j = rand() % 22 + 1;
} while (map[i][j] != ' ');
map[i][j] = '@';
}
void update_direction_of_snake() {
int ch;
if (_kbhit()) {
ch = _getch();
if (ch == -32)
ch = _getch();
if (ch == 119 && direction_of_snake != down)
direction_of_snake = up;
else if (ch == 115 && direction_of_snake != up)
direction_of_snake = down;
else if (ch == 97 && direction_of_snake != right)
direction_of_snake = left;
else if (ch == 100 && direction_of_snake != left)
direction_of_snake = right;
}
}
void update_snake() {
int i;
for (i = snake_length; i > 1; i--) {
pos_x[i] = pos_x[i - 1];
pos_y[i] = pos_y[i - 1];
}
if (direction_of_snake == up)
pos_y[1]--;
else if (direction_of_snake == down)
pos_y[1]++;
else if (direction_of_snake == left)
pos_x[1]--;
else if (direction_of_snake == right)
pos_x[1]++;
if (map[pos_x[1]][pos_y[1]] == '#' || map[pos_x[1]][pos_y[1]] == '*')
is_gameover = true;
if (map[pos_x[1]][pos_y[1]] == '@') {
snake_length++;
score++;
do {
i = rand() % 22 + 1;
j = rand() % 22 + 1;
} while (map[i][j] != ' ');
map[i][j] = '@';
}
for (i = 1; i <= snake_length; i++)
map[pos_x[i]][pos_y[i]] = '*';
map[pos_x[snake_length + 1]][pos_y[snake_length + 1]] = ' ';
}
int main()
{
init_map();
while (!is_gameover) {
print_map();
update_direction_of_snake();
update_snake();
Sleep(200);
system("cls");
}
printf("Game Over!\nYour score is %d.", score);
return 0;
}
```
请注意,这只是一份示例代码,您可能需要对其进行修改或添加代码以满足您的具体要求。
相关推荐
![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)