字符版本贪吃蛇游戏

编程入门 行业动态 更新时间:2024-10-10 00:29:51

<a href=https://www.elefans.com/category/jswz/34/1771063.html style=字符版本贪吃蛇游戏"/>

字符版本贪吃蛇游戏

字符版本贪吃蛇游戏

void snake_move();
void output();
int gameover();
void creat_food();

首先定义地图:

接着定义蛇的属性:
int snake_length = 5;
int snake_location_x[10] = {5, 4, 3, 2, 1};
int snake_location_y[10] = {1, 1, 1, 1, 1};
int food_x;
int food_y;

然后定义如何让蛇动起来并且能让它吃到食物长大:

int main() {
creat_food();
char choice;
output();
while (1) {
scanf(” %c”, &choice);
snake_move();
if (choice == ‘w’) {
snake_location_y[0] -= 1;
map[snake_location_y[0]][snake_location_x[0]] = ‘H’;
}
if (choice == ‘s’) {
snake_location_y[0] += 1;
map[snake_location_y[0]][snake_location_x[0]] = ‘H’;
}
if (choice == ‘a’) {
snake_location_x[0] -= 1;
map[snake_location_y[0]][snake_location_x[0]] = ‘H’;
}
if (choice == ‘d’) {
snake_location_x[0] += 1;
map[snake_location_y[0]][snake_location_x[0]] = ‘H’;
}
if (snake_location_x[0] == food_x && snake_location_y[0] == food_y) {
creat_food();
snake_length++;
snake_location_x[snake_length - 1] = snake_location_x[snake_length - 2];
snake_location_y[snake_length - 1] = snake_location_y[snake_length - 2];
map[snake_location_y[snake_length - 1]][snake_location_x[snake_length - 1]] = ‘X’;
}
if (!gameover()) {
printf(“gameove\n”);
return 0;
} else {
output(); }
}
return 0;
}

void snake_move() {
int i;
map[snake_location_y[snake_length - 1]][snake_location_x[snake_length - 1]] = ’ ‘;
for (i = snake_length - 1; i > 0; i–) {
snake_location_x[i] = snake_location_x[i - 1];
snake_location_y[i] = snake_location_y[i - 1];
map[snake_location_y[i]][snake_location_x[i]] = ‘X’; }
}

int gameover() {
if (snake_location_x[0] == 10 || snake_location_x[0] == 0) {
return 0;
}
if (snake_location_y[0] == 10 || snake_location_y[0] == 0) {
return 0;
}
for (int i = 1; i < snake_length; i++) {
if (snake_location_x[0] == snake_location_x[i] && snake_location_y[0] == snake_location_y[i]) {
return 0; }
}
return 1;
}
void output() {
for (int i = 0; i <= 11; i++) {
for (int j = 0; j <= 11; j++) {
printf(“%c”, map[i][j]);
}
printf(“\n”); }
}
void creat_food() {
srand((unsigned)(time(NULL)));
food_x = rand() % 9 + 1;
food_y = rand() % 9 + 1;
while (map[food_y][food_x] != ’ ‘) {
food_x = rand() % 9 + 1;
food_y = rand() % 9 + 1;
}
map[food_y][food_x] = ‘$’;
}

最后,字符版的贪吃蛇小游戏就大功告成啦!

更多推荐

字符版本贪吃蛇游戏

本文发布于:2024-02-06 16:21:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1750536.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   贪吃蛇   版本   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!