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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值