低配版贪吃蛇游戏设计

首先先做出游戏的总体结构的伪代码

  输出字符矩阵
    WHILE not 游戏结束 DO
        ch=等待输入
        CASE ch DO
        ‘A’:左前进一步,break 
        ‘D’:右前进一步,break    
        ‘W’:上前进一步,break    
        ‘S’:下前进一步,break    
        END CASE
        输出字符矩阵
    END WHILE
    输出 Game Over!!! 

大致我们可以写出蛇的移动的代码

void snakeMove(char control){
    switch(control){
        case 'a':
            snakeheady --;
            break;
        case 's':
            snakeheadx ++;
            break;
        case 'w':
            snakeheadx --;
            break;
        case 'd':
            snakeheady ++;
            break;
        default:
            return; 
    }
}

贪吃蛇的移动首先需要设计地图及蛇的坐标和长度

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<windows.h> 

#define SNAKE_MAX_LENGTH 100
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define SNAKE_FOOD '$'
#define WALL_CELL '*'

void printmap(void);
void snakeMove(char);
void putfood(void);
void output(char);
void gameover(void);
char wheregonext(int,int,int,int);

char map[12][26] = 
    { 
    "*************************",
    "*XH                     *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*************************"
};
int snakeheadx = 1;
int snakeheady = 2; 
int snakebodyx[SNAKE_MAX_LENGTH] = {1};
int snakebodyy[SNAKE_MAX_LENGTH] = {1};
int snakelength = 2;
int foodx = 0, foody = 0;
int gamerunning = 1;
int score = 0;

通过stdlib.h下的rand()函数,我们可以在地图上随机产生食物

void putfood(void) {
    foodx = rand() % 10 + 1;
    foody = rand() % 10 + 1;
    while(map[foodx][foody] != BLANK_CELL){
        foodx = rand() % 12 + 1;
        foody = rand() % 26 + 1;
    }
    map[foodx][foody] = SNAKE_FOOD;
}

最为复杂的是蛇移动后的长度增长及记录坐标的函数

int i;
int presnakeheadx = snakeheadx;
int presnakeheady = snakeheady;
snakeMove(control);
if(map[snakeheadx][snakeheady] != BLANK_CELL && map[snakeheadx][snakeheady] != SNAKE_FOOD){
    gameover();
}
else if (map[snakeheadx][snakeheady] == BLANK_CELL){
    map[snakeheadx][snakeheady] = SNAKE_HEAD;
    map[presnakeheadx][presnakeheady] = SNAKE_BODY;
    for(i = 0; i < snakelength - 2; i ++){
        map[snakebodyx[i]][snakebodyy[i]] = map[snakebodyx[i + 1]][snakebodyy[i + 1]];
    }
    map[snakebodyx[i]][snakebodyy[i]] = BLANK_CELL;
        for (i = snakelength - 3; i >= 0; i--) {
        snakebodyx[i + 1] = snakebodyx[i];
        snakebodyy[i + 1] = snakebodyy[i];
    }
    snakebodyx[0] = presnakeheadx;
    snakebodyy[0] = presnakeheady;  
}
else if (map[snakeheadx][snakeheady] == SNAKE_FOOD){
    map[snakeheadx][snakeheady] = SNAKE_HEAD;
    i = 0;
    map[presnakeheadx][presnakeheady] = map[snakebodyx[i]][snakebodyy[i]];
    for (i; i < snakelength - 2; i++) {
        map[snakebodyx[i]][snakebodyy[i]] = map[snakebodyx[i + 1]][snakebodyy[i + 1]];
    }
    map[snakebodyx[i]][snakebodyy[i]] = SNAKE_BODY;
    snakelength ++;
    for (i = snakelength - 3; i >= 0; i--) {
        snakebodyx[i + 1] = snakebodyx[i];
        snakebodyy[i + 1] = snakebodyy[i];
    }
    snakebodyx[0] = presnakeheadx;
    snakebodyy[0] = presnakeheady;
    score ++; 
    putfood();
}

最后在写好总控函数及完善细节基本就能完成一个低配版的贪吃蛇设计了。
这里需要注意的是可以用getch代替getchar或scanf,可以实现不用敲回车就能实现贪吃蛇的走动

    char control;
    srand(time(NULL));
    putfood();
    printmap();
    printf("             得分 %d",score);
    while(gamerunning){
        control = getch();//getch代替getchar/scanf可以不用回车 
        output(control);
        printmap();
        printf("             得分 %d",score);
    }
    putchar('\n');
    printf("Game over\n");
    return 0;
}

最后是总的代码汇总,和大佬们的代码相比逊色许多了

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<windows.h> 

#define SNAKE_MAX_LENGTH 100
#define SNAKE_HEAD 'H'
#define SNAKE_BODY 'X'
#define BLANK_CELL ' '
#define SNAKE_FOOD '$'
#define WALL_CELL '*'

void printmap(void);
void snakeMove(char);
void putfood(void);
void output(char);
void gameover(void);
char wheregonext(int,int,int,int);

char map[12][26] = 
    { 
    "*************************",
    "*XH                     *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*                       *",
    "*************************"
};
int snakeheadx = 1;
int snakeheady = 2; 
int snakebodyx[SNAKE_MAX_LENGTH] = {1};
int snakebodyy[SNAKE_MAX_LENGTH] = {1};
int snakelength = 2;
int foodx = 0, foody = 0;
int gamerunning = 1;
int score = 0;

int main(){
    char control;
    srand(time(NULL));
    putfood();
    printmap();
    printf("             得分 %d",score);
    while(gamerunning){
        control = getch();//getch代替getchar/scanf可以不用回车 
        output(control);
        printmap();
        printf("             得分 %d",score);
    }
    putchar('\n');
    printf("Game over\n");
    return 0;
}
void printmap(void){
    system("cls");
    int i;
    for(i = 0; i < 12; i ++){
        printf("%s\n",map[i]);
    }
}
void gameover(void){
    printf("Game over\n");
    gamerunning = 0;
}
void snakeMove(char control){
    switch(control){
        case 'a':
            snakeheady --;
            break;
        case 's':
            snakeheadx ++;
            break;
        case 'w':
            snakeheadx --;
            break;
        case 'd':
            snakeheady ++;
            break;
        default:
            return; 
    }
}
void putfood(void) {
    foodx = rand() % 10 + 1;
    foody = rand() % 10 + 1;
    while(map[foodx][foody] != BLANK_CELL){
        foodx = rand() % 12 + 1;
        foody = rand() % 26 + 1;
    }
    map[foodx][foody] = SNAKE_FOOD;
}
void output(char control) {
    int i;
    int presnakeheadx = snakeheadx;
    int presnakeheady = snakeheady;
    snakeMove(control);
    if(map[snakeheadx][snakeheady] != BLANK_CELL && map[snakeheadx][snakeheady] != SNAKE_FOOD){
        gameover();
    }
    else if (map[snakeheadx][snakeheady] == BLANK_CELL){
        map[snakeheadx][snakeheady] = SNAKE_HEAD;
        map[presnakeheadx][presnakeheady] = SNAKE_BODY;
        for(i = 0; i < snakelength - 2; i ++){
            map[snakebodyx[i]][snakebodyy[i]] = map[snakebodyx[i + 1]][snakebodyy[i + 1]];
        }
        map[snakebodyx[i]][snakebodyy[i]] = BLANK_CELL;
            for (i = snakelength - 3; i >= 0; i--) {
            snakebodyx[i + 1] = snakebodyx[i];
            snakebodyy[i + 1] = snakebodyy[i];
        }
        snakebodyx[0] = presnakeheadx;
        snakebodyy[0] = presnakeheady;  
    }
    else if (map[snakeheadx][snakeheady] == SNAKE_FOOD){
        map[snakeheadx][snakeheady] = SNAKE_HEAD;
        i = 0;
        map[presnakeheadx][presnakeheady] = map[snakebodyx[i]][snakebodyy[i]];
        for (i; i < snakelength - 2; i++) {
            map[snakebodyx[i]][snakebodyy[i]] = map[snakebodyx[i + 1]][snakebodyy[i + 1]];
        }
        map[snakebodyx[i]][snakebodyy[i]] = SNAKE_BODY;
        snakelength ++;
        for (i = snakelength - 3; i >= 0; i--) {
            snakebodyx[i + 1] = snakebodyx[i];
            snakebodyy[i + 1] = snakebodyy[i];
        }
        snakebodyx[0] = presnakeheadx;
        snakebodyy[0] = presnakeheady;
        score ++; 
        putfood();
    }
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值