码迷,mamicode.com
首页 > 其他好文 > 详细

greedysnake 贪吃蛇

时间:2016-05-13 01:18:29      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

大一上学期软导的贪吃蛇代码,当时实现了很久,努力自己实现了大部分,
另外参考了师兄给的代码,最终才完整实现,但是没有实现自动的部分


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

#define SNAKE_MAX_LENGTH 20
#define SNAKE_HEAD ‘H‘
#define SNAKE_BODY ‘X‘
#define BLANK_CELL ‘ ‘
#define SNAKE_FOOD ‘$‘
#define WALL_CELL ‘*‘

void turnUp(void);
void turnDown(void);
void turnLeft(void);
void turnRight(void);
void put_money(void);
void output(void);
void gameover(void);

int X[20] = {1, 2, 3, 4, 5};
int Y[20] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game = 0;

char map[13][13] =              //  直接打印游戏版面    
      {"************",
      "*XXXXH     *",
      "*          *",
      "*          *",
      "*          *",
      "*          *",
      "*          *",
      "*          *",
      "*          *",
      "*          *",
      "*          *",
      "************"};

void put_money(void) {
    int i = 0, j = 0;
    srand(time(NULL));
    while (map[i][j] == ‘X‘||map[i][j] == ‘Y‘||map[i][j] == ‘*‘) {  //  如果生成的$所在的位置不当,则重新生成 
        i = rand()%10 + 1;       //  生成随机数使$出现 
        j = rand()%10 + 1;
    }
    map[i][j] = ‘$‘;
    return;
} 

void output(void) {
    system("cls");   //  实现清屏的功能  每走一步清屏并将游戏版面输出 
    int i, j; 
    printf("A -> left;  D -> right;  W -> up;  S -> down, then press enter\n" );
    for (i = 0; i < 12; i++)
    for (j = 0; j < 12; j++) {
    printf("%c", map[i][j]);
    if (j == 11) printf("\n");
    }
    return;
}
/* 实现每一步的动作:
以turnup 为例,其他的类似:
IF position of head after movement is not wallsell or bodysell THEN
    IF position of head after movement is money THEN
    Put the head to the money
    ELSE
    Put the head up one step
    Set the last bodysell to be empty
    move every bodysell to the bodysell in front of it
    END IF
ELSE
    gameover
END IF  
*/
void turnUp() {
    int i, sy, sx;
    if ((Y[snakeLength - 1] - 1) == 0||map[Y[snakeLength - 1] - 1][X[snakeLength - 1]] == ‘X‘) {
        gameover();
    } else if (map[Y[snakeLength - 1] - 1][X[snakeLength - 1]] == ‘$‘) {
        map[Y[snakeLength - 1]][X[snakeLength - 1]] = ‘X‘;
        map[Y[snakeLength - 1] - 1][X[snakeLength - 1]] = ‘H‘;
        snakeLength += 1;
        Y[snakeLength - 1] = Y[snakeLength - 2] - 1;
        X[snakeLength - 1] = X[snakeLength - 2];
        put_money();
        output();
    } else {
        map[Y[snakeLength - 1] - 1][X[snakeLength - 1]] = ‘H‘;
        sx = X[snakeLength - 1];
        sy = Y[snakeLength - 1];
        Y[snakeLength - 1] -= 1;
        for (i = 0; i < snakeLength - 1; i++) {
            if (i == 0) map[Y[0]][X[0]] = ‘ ‘;
            if (i == snakeLength - 2) {
                Y[i] = sy;
                X[i] = sx;
            } else {
                X[i] = X[i + 1];
                Y[i] = Y[i + 1]; 
            }
            map[Y[i]][X[i]] = ‘X‘;
        }
        output();
    }
    return;
}

void turnDown() {
    int i, sx, sy;
    if ((Y[snakeLength - 1] + 1) == 11||map[Y[snakeLength - 1] + 1][X[snakeLength - 1]] == ‘X‘) {
        gameover();
    } else if (map[Y[snakeLength - 1] + 1][X[snakeLength - 1]] == ‘$‘) {
        map[Y[snakeLength - 1]][X[snakeLength - 1]] = ‘X‘;
        map[Y[snakeLength - 1] + 1][X[snakeLength - 1]] = ‘H‘;
        snakeLength += 1;
        Y[snakeLength - 1] = Y[snakeLength - 2] + 1;
        X[snakeLength - 1] = X[snakeLength - 2];
        put_money();
        output();
    } else {
        map[Y[snakeLength - 1]][X[snakeLength - 1]] = ‘X‘;
        map[Y[snakeLength - 1] + 1][X[snakeLength - 1]] = ‘H‘;
        sx = X[snakeLength - 1];
        sy = Y[snakeLength - 1];
        Y[snakeLength - 1] += 1;
        for (i = 0; i < snakeLength - 1; i++) {
            if (i == 0) map[Y[0]][X[0]] = ‘ ‘;
            if (i == snakeLength - 2) {
                X[i] = sx;
                Y[i] = sy;
            } else {
            Y[i] = Y[i + 1];
            X[i] = X[i + 1];
            }
            map[Y[i]][X[i]] = ‘X‘;
        }
        output();
    }
    return;
}

void turnLeft() {
    int i, sx, sy;
    if ((X[snakeLength - 1] - 1) == 0||map[Y[snakeLength - 1]][X[snakeLength - 1] - 1] == ‘X‘) {
        gameover();
    } else if (map[Y[snakeLength - 1]][X[snakeLength - 1] - 1] == ‘$‘) {
        map[Y[snakeLength - 1]][X[snakeLength - 1]] = ‘X‘;
        map[Y[snakeLength - 1]][X[snakeLength - 1] - 1] = ‘H‘;
        snakeLength += 1;
        X[snakeLength - 1] = X[snakeLength - 2] - 1;
        Y[snakeLength - 1] = Y[snakeLength - 2];
        put_money();
        output();
    } else {
        map[Y[snakeLength - 1]][X[snakeLength - 1]] = ‘X‘;
        map[Y[snakeLength - 1]][X[snakeLength - 1] - 1] = ‘H‘;
        sx = X[snakeLength - 1];
        sy = Y[snakeLength - 1];
        X[snakeLength - 1] -= 1;
        for (i = 0; i < snakeLength - 1; i++) {
            if (i == 0) map[Y[0]][X[0]] = ‘ ‘;
            if (i == snakeLength - 2) {
               X[i] = sx;
               Y[i] = sy;
            } else {   
            X[i] = X[i + 1];
            Y[i] = Y[i + 1];
            }
            map[Y[i]][X[i]] = ‘X‘;
        }
        output();
    }
    return;
}

void turnRight() {
    int i, sx, sy;
    if ((X[snakeLength - 1] + 1) == 11||map[Y[snakeLength - 1]][X[snakeLength - 1] + 1] == ‘X‘) {
        gameover();
    } else if (map[Y[snakeLength - 1]][X[snakeLength - 1] + 1] == ‘$‘) {
        map[Y[snakeLength - 1]][X[snakeLength - 1]] = ‘X‘;
        map[Y[snakeLength - 1]][X[snakeLength - 1] + 1] = ‘H‘;
        snakeLength += 1;
        X[snakeLength - 1] = X[snakeLength - 2] + 1;
        Y[snakeLength - 1] = Y[snakeLength - 2];
        put_money();
        output();
    } else {
        map[Y[snakeLength - 1]][X[snakeLength - 1]] = ‘X‘;
        map[Y[snakeLength - 1]][X[snakeLength - 1] + 1] = ‘H‘;
        sx = X[snakeLength - 1];
        sy = Y[snakeLength - 1];
        X[snakeLength - 1] += 1;
        for (i = 0; i < snakeLength - 1; i++) {
            if (i == 0) map[Y[0]][X[0]] = ‘ ‘;
            if (i == snakeLength - 2) {
                X[i] = sx;
                Y[i] = sy;
            } else {
            X[i] = X[i + 1];
            Y[i] = Y[i + 1];
            }
            map[Y[i]][X[i]] = ‘X‘;
        }
        output();
    }
    return;
}

void gameover(void) {
    game = 1;
    printf("Game Over!\n");
    return; 
}

int main() {
    int flag = 1;
    put_money();
    output();
    char ch;
    while (flag) {
        scanf("%c", &ch);
        if (ch == ‘A‘) {
        turnLeft(); 
        }
        if (ch == ‘D‘) {
        turnRight();
        }
        if (ch == ‘W‘) {
        turnUp();
        }
        if (ch == ‘S‘) {
        turnDown();
        }
        if (game == 1)
        flag = 0;
    }
    return 0;
} 
/* 伪代码:
While not gameover Do
    Get char
   IF char is A THEN
      turn left
   END IF
   IF char is D THEN
      turn right
   END IF
   IF char is W THEN
      turn up
   END If
   IF char is S THEN
      turn down
   END IF
END WHILE 
*/

自己实现的游戏,挺激动的当时,把代码贴到这里,或许以后有用.

中间的部分,四个方向的实现方法其实都是一样的

greedysnake 贪吃蛇

标签:

原文地址:http://blog.csdn.net/lichen_yun/article/details/51345305

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!