Linux环境 - 贪吃蛇小程序(C 语言)

1、Ubuntu22.04, Vmware Workstation17 pro

在这里插入图片描述

2、源码 b_snake.c

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <curses.h>

#define WIDTH 30
#define HEIGHT 20

int x, y, fruit_x, fruit_y, score;
int gameover = 0;
int tail_x[100], tail_y[100];
int ntail = 0;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
enum eDirection dir;

int kbhit(void) {
    struct termios oldt, newt;
    int ch;
    int oldf;

    tcgetattr(STDIN_FILENO, &oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
    fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

    ch = getchar();

    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    fcntl(STDIN_FILENO, F_SETFL, oldf);

    if (ch != EOF) {
        ungetc(ch, stdin);
        return 1;
    }

    return 0;
}

void Setup()
{
    score = 0;
    dir = STOP;
    x = WIDTH / 2;
    y = HEIGHT / 2;
    srand(time(NULL));
    fruit_x = rand() % (WIDTH - 2) + 1;
    fruit_y = rand() % (HEIGHT - 2) + 1;
}

void Draw()
{
    system("clear");
    for (int i = 0; i < WIDTH + 2; i++)
        printf("#");
    printf("\n");

    for (int i = 0; i < HEIGHT; i++)
    {
        for (int j = 0; j < WIDTH; j++)
        {
            if (j == 0 || j == WIDTH - 1)
                printf("#");
            if (i == y && j == x)
                printf("O");
            else if (i == fruit_y && j == fruit_x)
                printf("F");
            else
            {
                int print = 0;
                for (int k = 0; k < ntail; k++)
                {
                    if (tail_x[k] == j && tail_y[k] == i)
                    {
                        printf("o");
                        print = 1;
                    }
                }
                if (!print)
                    printf(" ");
            }
        }
        printf("\n");
    }

    for (int i = 0; i < WIDTH + 2; i++)
        printf("#");
    printf("\n");
    printf("Score: %d\n", score);
}

void Input()
{
    struct termios old, new;
    tcgetattr(STDIN_FILENO, &old);
    new = old;
    new.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &new);

    if (kbhit())
    {
        switch (getchar())
        {
            case 'a':
                dir = LEFT;
                break;
            case 'd':
                dir = RIGHT;
                break;
            case 'w':
                dir = UP;
                break;
            case 's':
                dir = DOWN;
                break;
            case 'x':
                gameover = 1;
                break;
        }
    }

    tcsetattr(STDIN_FILENO, TCSANOW, &old);
}

void Logic()
{
    int prev_x = tail_x[0];
    int prev_y = tail_y[0];
    int prev2_x, prev2_y;
    tail_x[0] = x;
    tail_y[0] = y;
    for (int i = 1; i < ntail; i++)
    {
        prev2_x = tail_x[i];
        prev2_y = tail_y[i];
        tail_x[i] = prev_x;
        tail_y[i] = prev_y;
        prev_x = prev2_x;
        prev_y = prev2_y;
    }

    switch (dir)
    {
        case LEFT:
            x--;
            break;
        case RIGHT:
            x++;
            break;
        case UP:
            y--;
            break;
        case DOWN:
            y++;
            break;
        default:
            break;
    }

    if (x <= 0 || x >= WIDTH - 1 || y <= 0 || y >= HEIGHT)
        gameover = 1;

    for (int i = 0; i < ntail; i++)
        if (tail_x[i] == x && tail_y[i] == y)
            gameover = 1;

    if (x == fruit_x && y == fruit_y)
    {
        score += 10;
        srand(time(NULL));
        fruit_x = rand() % (WIDTH - 2) + 1;
        fruit_y = rand() % (HEIGHT - 2) + 1;
        ntail++;
    }
}

int main()
{
    Setup();
    while (!gameover)
    {
        Draw();
        Input();
        Logic();
        usleep(100000);
    }
    return 0;
}

3、编译命令gcc b_snake.c

在这里插入图片描述

4、 运行 /a.out

在这里插入图片描述

5、游戏画面,键盘上的W、S、A、D 四个按键代表上、下、左、右的控制按键,可以控制O(代表蛇)的移动,去吃掉F,然后变长蛇,一旦碰到边界,蛇就会死掉,只能结束程序的运行 Ctral+c, 然后重新运行程序 ./a.out

在这里插入图片描述


在这里插入图片描述

6、只能在Linux下运行,不能在windows 系统下运行。

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值