贪吃蛇

代码

/*
************************************
* wsad控制上下左右,q退出,空格暂停
* 编译命令 gcc snake.c -lcurses
* 需要下载ncurses库,具体方法请百度
*************************************
*/
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <curses.h>
#include <stdlib.h>
#include <sys/time.h>

typedef struct snake {
    int x, y;
    struct snake *next, *prev;
} SNAKE, *Snake;

typedef struct food {
    int x, y;
} FOOD, *Food;

int a;
int dir_x, dir_y;
Snake head, tail;
FOOD food;

int set_ticker(int n_msec)
{
    struct itimerval timeset;
    long n_sec, n_usec;

    n_sec = n_msec / 1000;
    n_usec = (n_msec % 1000) * 1000L;

    timeset.it_interval.tv_sec = n_sec;
    timeset.it_interval.tv_usec = n_usec;

    timeset.it_value.tv_sec = n_sec;
    timeset.it_value.tv_usec = n_usec;

    return setitimer(ITIMER_REAL, &timeset, NULL);
}

void Snake_Move()
{
    Snake p, tmp;

    for (p = tail; p != head; p = p->prev) {
        p->x = p->prev->x;
        p->y = p->prev->y;
    }
    p->x += dir_x;
    p->y += dir_y;

    if (head->x > 79)
        head->x = 0;
    if (head->x < 0)
        head->x = 79;
    if (head->y > 23)
        head->y = 0;
    if (head->y < 0)
        head->y = 23;

    move(head->y, head->x);
    if ((char)inch() == '*') { //eat self
        Game_Over();
    }
    if ((char)inch() == 'o') { //eat food
        move(head->y, head->x);
        addch('*');
        refresh();

        tmp = (Snake)malloc(sizeof(SNAKE));
        tmp->x = head->x + dir_x;
        tmp->y = head->y + dir_y;
        tmp->next = head;
        head->prev = tmp;
        head = tmp;

        do {
            food.x = rand() % 80;
            food.y = rand() % 24;
            move(food.y, food.x);
        } while(((char)inch()) == '*');
        move(food.y, food.x);
        addch('o');
        refresh();
    }

    move(head->y, head->x);
    addch('*');
    refresh();
    move(tail->y, tail->x);
    addch(' ');
    refresh();
}

int Game_Over()
{
    sleep(1);
    endwin();
    exit(0);
}

void key_ctl()
{
    int ch = getch();
    switch (ch) {
        case 'W':
        case 'w':
            if (dir_x == 0)
                break;
            dir_x = 0;
            dir_y = -1;
            break;
        case 'S':
        case 's':
            if (dir_x == 0)
                break;
            dir_x = 0;
            dir_y = 1;
            break;
        case 'A':
        case 'a':
            if (dir_y == 0)
                break;
            dir_y = 0;
            dir_x = -1;
            break;
        case 'D':
        case 'd':
            if (dir_y == 0)
                break;
            dir_y = 0;
            dir_x = 1;
            break;
        case ' ':
            set_ticker(0);
            do {
                ch = getch();
            } while(ch != ' ');
            set_ticker(200);
            break;
        case 'Q':
        case 'q':
            Game_Over();
            break;
        default:break;
    }
}

void Init()
{
    initscr();
    cbreak();
    noecho();
    curs_set(0);
    srand(time(0));

    dir_x = 1;
    dir_y = 0;

    head = (Snake)malloc(sizeof(SNAKE));
    head->x = rand() % 80;
    head->y = rand() % 24;
    head->next = (Snake)malloc(sizeof(SNAKE));
    tail = head->next;
    tail->prev = head;
    tail->x = head->x - dir_x;
    tail->y = head->y - dir_y;

    do {
        food.x = rand() % 80;
        food.y = rand() % 24;
        move(food.y, food.x);
    } while((char)inch() == '*');

    move(head->y, head->x);
    addch('*');
    move(food.y, food.x);
    addch('o');
    refresh();
}

void sig_alrm(int singo)
{
    set_ticker(200);
    Snake_Move();
}

int main()
{
    char ch;
    Init();
    signal(SIGALRM, sig_alrm);
    set_ticker(200);
    while(1) {
        key_ctl();
    }
    endwin();
    return 0;
}

结果

这里写图片描述

视频

c语言小游戏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值