用链表来实现贪吃蛇游戏

**

贪吃蛇是我们很经常玩的小游戏,而如果用链表来做一个贪吃蛇游戏,你就会对链表有一个深刻的认识,而且链表作为c语言的重点,也是c语言的独特魅力所在 算了,不说那么多了,上干货

**
snake.c

#include <curses.h>
#include <stdlib.h>
#include <pthread.h>
#include "LIST.h"
#define MAP_WIDTH 20
#define MAP_HEIGHT 20
extern struct Snake *g_snake;
extern struct Snake g_food;

int g_flag = 0;

int g_direction = KEY_RIGHT;
int commitsuicide() {
    struct Snake *p;
    p = g_snake;
    struct Snake *tmp;
    tmp = g_snake->next;
    while (tmp != NULL) {
        if ((p->x == tmp->x) && (p->y == tmp->y)) {
            return 0;
        }
        tmp = tmp->next;
    }
    return -1;
}

void initFood() {
    int judge = 0;
    do {
        int x = rand() % 20 + 1;
        int y = rand() % 20 + 1;
        g_food.x = x;
        g_food.y = y;
        g_food.next = NULL;
        if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
            judge = 1;
        }
    } while (g_food.x == 0 || g_food.x > 18 || g_food.y == 0 || g_food.y > 18 ||
             judge == 1);
}

int foodFind(int x, int y) {

    if ((g_food.x == x) && (g_food.y == y)) {
        return 0;
    }

    return -1;
}
int headTofood() {

    if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
        return 0;
    }
    return -1;
}
void initMap() {
    int x;
    int y;
    for (x = 0; x < MAP_WIDTH; x++) {
        for (y = 0; y < MAP_HEIGHT; y++) {
            if (x == 0 || x == MAP_HEIGHT - 1) {
                if (x == 0) {
                    printw("* ");
                } else {
                    printw("* ");
                }
            } else {
                if (y == 0 || y == MAP_WIDTH - 1) {
                    printw("* ");
                } else {
                    if ((snakeFind(g_snake, x, y) == 0)) {
                        printw("[]");
                    } else if (foodFind(x, y) == 0) {
                        printw("++");
                    } else {
                        printw("  ");
                    }
                }
            }
        }
        printw("\n");
    }
}

void handleKey() {
    int key;
    noecho();
    keypad(stdscr, TRUE);
    while (1) {
        key = getch();
        if (adjustDirection(key) == 0) {
            continue;
        }
    }
}

int adjustDirection(int key) {
    if (key == KEY_DOWN || key == KEY_UP || key == KEY_RIGHT ||
        key == KEY_LEFT) {
        if (((g_direction == KEY_RIGHT) && (key == KEY_LEFT)) ||
            ((g_direction == KEY_LEFT) && (key == KEY_RIGHT)) ||
            ((g_direction == KEY_UP) && (key == KEY_DOWN)) ||
            ((g_direction == KEY_DOWN) && (key == KEY_UP))) {
            return 0;
        }
    }
    g_direction = key;
    return -1;
}

void moveSnakeauto() {
    while (1) {
        switch (g_direction) {
            case KEY_DOWN:
                if (headTofood() == 0) {
                    insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
                    g_flag = -1;
                } else {
                    insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
                    deleteSnake(&g_snake);
                }
                break;
            case KEY_UP:
                if (headTofood() == 0) {
                    insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
                    g_flag = -1;
                } else {
                    insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
                    deleteSnake(&g_snake);
                }
                break;
            case KEY_LEFT:
                if (headTofood() == 0) {
                    insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
                    g_flag = -1;
                } else {
                    insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
                    deleteSnake(&g_snake);
                }
                break;
            case KEY_RIGHT:
                if (headTofood() == 0) {
                    insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
                    g_flag = -1;
                } else {
                    insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
                    deleteSnake(&g_snake);
                }
                break;
            default:
                break;
        }
        clear();
        if (g_flag == -1) {
            initFood();
            g_flag = 0;
        }

        if (snakeDie() == 0 || commitsuicide() == 0) {

            destorySnake();
            createSnake();
        }
        initMap();
        refresh();
        usleep(100000);
    }
}

int main() {

    pthread_t t_movesnake;
    pthread_t t_getinput;

    initscr();
    createSnake();
    initFood();
    initMap();
    pthread_create(&t_movesnake, NULL, (void *)moveSnakeauto, NULL);
    pthread_create(&t_getinput, NULL, (void *)handleKey, NULL);
    while (1);
    endwin();
    return 0;
}

list.c

#include <curses.h>
#include <stdlib.h>
#include "LIST.h"
struct Snake *g_snake;
struct Snake g_food;
int snakeDie()  // panduansiwang
{
    if (g_snake->x == 0 || g_snake->x == 19 || g_snake->y == 0 ||
        g_snake->y == 19) {
        return 0;
    }
    return -1;
}
void destorySnake()  // bianlishanchu
{
    struct Snake *p;
    while (g_snake != NULL) {
        p = g_snake;
        g_snake = g_snake->next;
        free(p);
    }
}
void insertSnake(struct Snake **head, int x, int y)  // touchafa
{
    struct Snake *node;
    node = (struct Snake *)malloc(sizeof(struct Snake));
    node->x = x;
    node->y = y;
    node->next = NULL;
    node->next = *head;
    *head = node;
}
void createSnake() {//chushihuashe
    g_snake = (struct Snake *)malloc(sizeof(struct Snake));
    g_snake->x = 3;
    g_snake->y = 3;
    g_snake->next = NULL;
    insertSnake(&(g_snake), 3, 4);
    insertSnake(&(g_snake), 3, 5);
    insertSnake(&(g_snake), 3, 6);
}

int snakeFind(struct Snake *head, int x, int y) {//bianlipanduan
    struct Snake *p;
    p = head;
    while (p != NULL) {
        if ((p->x == x) && (p->y == y)) {
            return 0;
        }
        p = p->next;
    }
    return -1;
}

void deleteSnake(struct Snake **head) {//shanchuweiba
    struct Snake *p;
    struct Snake *tmp;
    tmp = NULL;
    p = *head;
    while ((p != NULL) && (p->next != NULL)) {
        if ((p->next->next == NULL)) {
            tmp = p->next;
            p->next = NULL;
            free(tmp);
            break;
        }
        p = p->next;
    }
}

list.h

#ifndef _LIST_H_
#define _LIST_H_

struct Snake 
{
    int x;
    int y;
    struct Snake *next;
};
void insertSnake(struct Snake **head,int x,int y);
void createSnake();
int snakeFind(struct Snake *head,int x,int y);
void deleteSnake(struct Snake **head);
int foodFind(int x,int y);
int snakeDie();
void  destorySnake();









#endif

Makefile

snake:
	gcc snake_final.c LIST.c -lncurses -lpthread   -o snakepro

猜你喜欢

转载自blog.csdn.net/weixin_44178250/article/details/99708812