编写字符小游戏——贪吃蛇程序(c语言)

#include <stdio.h>
#include <string.h>

#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define WALL_CELL '*'

void snake_move();
void output();
int gameove();

char map[12][12] = {
    "***********",
    "*XXXXH    *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "***********",
};

int snake_location_x[5] = {1, 2, 3, 4, 5};
int snake_location_y[5] = {1, 1, 1, 1, 1};

int main() {
    char choice;
    output();
    while (1) {
        scanf(" %c", &choice);
        snake_move();
        if (choice == 'w') {
            snake_location_y[4] -= 1;
            map[snake_location_y[4]][snake_location_x[4]] = 'H';
        }
        if (choice == 's') {
            snake_location_y[4] += 1;
            map[snake_location_y[4]][snake_location_x[4]] = 'H';
        }
        if (choice == 'a') {
            snake_location_x[4] -= 1;
            map[snake_location_y[4]][snake_location_x[4]] = 'H';
        }
        if (choice == 'd') {
            snake_location_x[4] += 1;
            map[snake_location_y[4]][snake_location_x[4]] = 'H';
        }
        if (!gameove()) {
            printf("gameove\n");
            return 0;
        } else {
            output();
        }
    }
    return 0;
}

void snake_move() {
    int i;
    map[snake_location_y[0]][snake_location_x[0]] = ' ';
    for (i = 0; i < 4; 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 gameove() {
    if (snake_location_x[4] == 10 || snake_location_x[4] == 0) {
        return 0;
    }
    if (snake_location_y[4] == 10 || snake_location_y[4] == 0) {
        return 0;
    }
    for (int i = 0; i < 4; i++) {
        if (snake_location_x[4] == snake_location_x[i] && snake_location_y[4] == 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");
    }
}

the example:

***********
*XXXXH    *
*         *
*         *
*         *
*         *
*         *
*         *
*         *
*         *
***********

d
***********
* XXXXH   *
*         *
*         *
*         *
*         *
*         *
*         *
*         *
*         *
***********

w
gameove
Program ended with exit code: 0
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>

#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define WALL_CELL '*'
#define food $

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

char map[12][12] = {
    "***********",
    "*XXXXH    *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "*         *",
    "***********",
};

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] = '$';
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值