C語言:貪喫蛇遊戲

相信每個人都接觸過貪喫蛇遊戲,而對於學習C語言的同學來說,一開始是不是覺得C語言寫不出什麼東西來呢?

那麼,貪喫蛇應該是第一步,開始瞭解一些模塊化的知識,一些面向對象的思想,一些小項目的編寫。


效果:

通過“WASD”移動蛇,蛇能夠喫隨機產生的食物,並且變長。


基本實現:

用兩個數組snakeX,snakeY來記錄蛇的位置。

創建並初始化一個地圖map,並對其不斷更新。


不足:

當蛇長度超過允許的最大長度時,就會出現錯誤。


源代碼如下:

// snake
// Created by Climber_PG
// Copyright (c) 2014 Sun Yat-sen University. All rights reserved.

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

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

// clear the map
void clear_map(void);
// update the map
void update_map(void);
// put a food randomized on a blank cell
void put_money(void);
// snake stepping: dy = -1(up), 1(down); dx = -1(left),1(right),0(no move)
void snakeMove(int, int);
// out cells of the grid
void output(void);
// outs when game is over
void gameover(void);

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

// define variables for snake, notice name of variables in C
int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int score = 0, food_x = 0, food_y = 0;

int main() {
    char ch;
    int flag = 1;
    put_money();
    output();
    while (flag) {
        ch = getch();
        switch(ch) {
        case 'a':
            snakeMove(-1, 0);
            system("CLS");
            output();
            break;
        case 'w':
            snakeMove(0, -1);
            system("CLS");
            output();
            break;
        case 's':
            snakeMove(0, 1);
            system("CLS");
            output();
            break;
        case 'd':
            snakeMove(1, 0);
            system("CLS");
            output();
            break;
        default:
            break;
        }
    }
    return 0;
}

void clear_map(void) {
    int i, j;
    for (i = 1; i < 11; i++)
        for (j = 1; j < 11; j++)
            map[i][j] = ' ';
    return;
}

void update_map(void) {
    int i;
    map[snakeY[snakeLength - 1]][snakeX[snakeLength - 1]] = 'H';
    for (i = 0; i < snakeLength - 1; i++) {
        map[snakeY[i]][snakeX[i]] = 'X';
    }
    return;
}
void put_money(void) {
    int i;
    update_map();
    // create a new food
    for (; ;) {
        srand(time(NULL));
        food_x = rand()%10 + 1;
        food_y = rand()%10 + 1;
        if (map[food_x][food_y] == ' ') break;
    }
    // put the food into the map
    map[food_x][food_y] = '$';
    return;
}

void snakeMove(int x, int y) {
    int i, j;

    // if the command is invalid, then return
    int check_x = snakeX[snakeLength - 1] - snakeX[snakeLength - 2];
    int check_y = snakeY[snakeLength - 1] - snakeY[snakeLength - 2];
    if (x != 0 && check_x == -x) return;
    if (y != 0 && check_y == -y) return;

    clear_map();
    map[food_x][food_y] = '$';

    int head_x = snakeX[snakeLength - 1] + x;
    int head_y = snakeY[snakeLength - 1] + y;

    if (map[head_y][head_x] == '$') {
        snakeLength++;
        score++;
        snakeX[snakeLength - 1] = head_x;
        snakeY[snakeLength - 1] = head_y;
        if (snakeLength < SNAKE_MAX_LENGTH - 1) put_money();
    } else {
        // move the BODY of snake
        for (i = 1; i < snakeLength; i++) {
            snakeX[i - 1] = snakeX[i];
            snakeY[i - 1] = snakeY[i];
        }

        // move the HEAD of snake
        snakeX[snakeLength - 1] += x;
        snakeY[snakeLength - 1] += y;
    }
    gameover();
    update_map();
    return;
}

void output(void) {
    int i, j;
    for (i = 0 ; i < 12; i++) {
        for (j = 0; j < 12; j++)
            printf("%c", map[i][j]);
        printf("\n");
    }
    return;
}

void gameover(void) {
    int i, j, flag = 0;
    for (i = 0; i < snakeLength - 1; i++) {
        if (snakeX[snakeLength - 1] == snakeX[i] && snakeY[snakeLength - 1] == snakeY[i])
            flag = 1;
    }
    if (flag || snakeX[snakeLength - 1] < 1 || snakeX[snakeLength - 1] > 10 \
            || snakeY[snakeLength - 1] < 1 || snakeY[snakeLength - 1] > 10) {
        snakeLength = 5;
        clear_map();
        // initialize the coordinates of snake
        for (i = 0; i < snakeLength; i++) {
            snakeX[i] = i + 1;
            snakeY[i] = 1;
        }
        put_money();
        printf("Gameover!\nYour Score is %d!\nPress any key to continue.\n", score);
        score = 0;
        getch();
    }
    return;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章