Noob Dream

 找回密码
 加入我们[Register]
搜索
查看: 81|回复: 0

C语言写的贪吃蛇小游戏

[复制链接]

21

主题

21

帖子

112

积分

超级版主

Rank: 8Rank: 8

积分
112
发表于 2018-5-7 14:03:57 | 显示全部楼层 |阅读模式
本帖最后由 Gzu_lx 于 2018-5-7 14:06 编辑

初学者可以锻炼一下自己的代码控制能力、程序用的基础的C语言语法

[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <conio.h>

const int maxn = 10000 + 5;

struct node {
    int x;
    int y;
}snake[maxn];

char mpt[maxn][maxn];

int foodx = 9, foody = 9;
// 1 下 2 上 3 左 4 右
int direct = 1;
int snake_len = 3;

void Init() {
    snake[0].x = 3, snake[0].y = 1;
    snake[1].x = 2, snake[1].y = 1;
    snake[2].x = 1, snake[2].y = 1;

    int i, j;
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 20; j++) {
            if (i == 0 || i == 19)
                mpt[i][j] = '-';
            else {
                if (j == 0 || j == 19) {
                    mpt[i][j] = '|';
                }
                else mpt[i][j] = ' ';
            }
        }

    }
}

void Print() {
    int i, j, k, flag;
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 20; j++) {
            flag = 0;
            for (k = 0; k < snake_len; k++) {
                if (i == snake[k].x && j == snake[k].y) {
                    printf("*");
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) printf("%c", mpt[i][j]);
        }
        printf("\n");
    }

}


int main() {
    Init();
    while (1) {
        mpt[foodx][foody] = 3;
        Print();
        Sleep(500);
        system("cls");
        int i;
        for (i = snake_len - 1; i > 0; i--) {
            snake[i].x = snake[i - 1].x;
            snake[i].y = snake[i - 1].y;
        }

        if (direct == 1) snake[0].x++;
        else if (direct == 2) snake[0].x--;
        else if (direct == 3) snake[0].y--;
        else snake[0].y++;
        // 移动

        if (kbhit()) {
            char ch = getch();
            //printf("\n%c\n", ch);
            if (ch == 'a') { // 左
                direct = 3;
            }
            else if (ch == 's') { // 下
                direct = 1;
            }
            else if (ch == 'd') { // 右
                direct = 4;
            }
            else if (ch == 'w') { // 上
                direct = 2;
            }
            else { //其他键

            }
        }
        // 吃到食物
        if (snake[0].x == foodx && snake[0].y == foody) {
            mpt[foodx][foody] = ' ';
            foodx = rand() % 20;
            foody = rand() % 20;
            snake[snake_len].x = snake[snake_len - 1].x;
            snake[snake_len].y = snake[snake_len - 1].y;
            snake_len++;
        }

        // 判断边界
        if (snake[0].x <= 0 || snake[0].x >= 19 || snake[0].y <= 0 || snake[0].y >= 19) {
            printf("Game Over!\n");
            Sleep(3000);
            break;
        }
    }
    return 0;
}
回复

使用道具 举报

本版积分规则

QQ|Noob Dream

GMT+8, 2018-8-15 12:53 , Processed in 0.107887 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表