优化贪吃蛇在前进过程中,前进和后退的问题

1.        左边为head,右边为tail

        定义相反数在abs()绝对值函数中实现

2. 在转向函数turn()中,如果绝对值不相等的时候才赋予方向的值

3.贪吃蛇吃食物的思路

3.1 初始化食物initFood(), 蛇碰到食物函数hasFood(),在移动过程中if函数把蛇的尾巴的行和列传过来调用hasFood()来初始化食物initFood()

3.2 static 是静态数值

   

3.2食物的随机

3.3撞到自己的时候死亡

在ifSnakedie()函数中,第一个if是判断是否撞墙,因为while每次都会遍历完到自己,

为了防止遍历到自己,p->next !=NULL 忽略自己与自己比较,

在移动蛇的函数里面做修改

4.完整贪吃蛇代码

#include <curses.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define UP       1
#define DOWN    -1
#define LEFT     2
#define RIGHT   -2

void initNcurses()
{
        initscr();
        keypad(stdscr,1);
        noecho();
}

struct Snake
{
        int hang;
        int lie;
        struct Snake *next;

};

struct Snake *head = NULL;
struct Snake *tail = NULL;
int key;
int dir;

struct Snake food;

void initFood()
{
        int x = rand()%20;
        int y = rand()%20;

        food.hang = x;
        food.lie = y;
}

int hasSnakeNode(int i,int j){       // da yin she shen ti
        struct Snake *p;
        p = head;  //ding tou 

        while(p != NULL){
                if(p -> hang == i && p ->lie == j){
                        return 1;
                }
                p = p ->next;
        }
        return 0;
}

int hasFood(int i,int j)
{
        if(food.hang == i && food.lie == j){
 return 1;
        }
        return 0;
}

void gamePic()
{
        int hang;
        int lie;

        move(0,0);

        for(hang = 0;hang<20;hang++){
                if(hang==0)
                {
                        for(lie=0;lie<20;lie++)
                        {
                                printw("--");
                        }
                        printw("\n");
                }
                if(hang>= 0 || hang <=18)
                {
                        for(lie = 0;lie<=20;lie++){
                                if(lie == 0 || lie == 20){
                                        printw("|");
                                }else if(hasSnakeNode(hang,lie))
                                {
                                        printw("[]");
                                }else if(hasFood(hang,lie))
                                {
                                        printw("##");
                                }
                                else
                                    {
                                                printw("  ");
                                    }
                        }
                        printw("\n");
                }
                if(hang == 19)
                {
                        for(lie=0;lie<20;lie++){
                                printw("--");
                        }
                        printw("\n");
                        printw("by caoshupei,key= %d\n",key);
                }
        }
}
void addNode()
{
        struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));
        new -> next =NULL;
        switch(dir){
                case UP:
                        new -> hang = tail -> hang-1;
                        new -> lie = tail -> lie;
                        break;
                case DOWN:
                        new -> hang = tail -> hang+1;
                        new -> lie = tail ->lie;
                        break;
                case LEFT:
                        new -> hang = tail -> hang;
                        new ->lie = tail -> lie-1;
                        break;
                case RIGHT:
                        new -> hang = tail -> hang;
                        new -> lie = tail -> lie+1;
                        break;


        }

        tail -> next = new;
        tail = new;
}

void deleteNode()
{
        struct Snake *p;
        p = head;
        head = head ->next;

        free(p);    // fang zhi zao cheng hen duo kong yu de jie dian
}

void  initSnakebody()
{
        struct Snake *p;

        dir = RIGHT;
                                                //di yi ci yun xing shi bu hui zou
        while(head != NULL)                  //yi kai shi deng yu kong
        {                                  //zhuang qiao la jiu shi bu wei kong 
                p = head;
                head = head -> next;
free(p);                   //shi fang nei cun
        }

        initFood();

        head = (struct Snake*)malloc(sizeof(struct Snake));
        head -> hang = 1;                   // gu ding chu shi weizhi 
        head -> lie = 1;
        head -> next = NULL;               // tou de xie yi ge wei kong

        tail = head;                       // wei ba bian cheng tou
        addNode();                         // zeng jia jie dian
        addNode();
        addNode();
}

int ifSnakedie()
{
        struct Snake *p;
        p =head;
        if(tail -> hang <0|| tail ->lie == 0|| tail->hang == 20 ||
                tail->lie ==20){
                return 1;  // zhuang dao bian jie
        }
        while(p->next!=NULL){
                if(p -> hang == tail -> hang && p ->lie == tail-> lie){
                        return 1;  // zhuang dao zi ji
                }
                p = p->next;
        }
        return 0;
}

void moveSnake()
{
        struct Snake *p;
        struct Snake *new;
        addNode();
        if(hasFood(tail->hang,tail->lie))
        {
                initFood();
        }else{
deleteNode();
        }
        if(ifSnakedie())
        {
                initSnakebody();
        }
}

void *refreshjiemian()
{
        while(1){
                        moveSnake();
                        gamePic();
                        refresh();
                        usleep(100000);
        }
}

void turn(int direction)
{
        if(abs(dir) != abs(direction))
        {
                dir = direction;
        }
}

void *changeDir()
{
        while(1){
                key = getch();
                switch(key){
                        case KEY_DOWN:
                                turn(DOWN);
                                break;
                        case KEY_UP:
                                turn(UP);
                                break;
                        case KEY_LEFT:
                                turn(LEFT);
                                break;
                        case KEY_RIGHT:
                                turn(RIGHT);
                                break;
                }
        }
}

int main()
{
        pthread_t t1;
        pthread_t t2;
        initNcurses();
        initSnakebody();
        gamePic();

        pthread_create(&t1,NULL,refreshjiemian,NULL);
        sleep(1);
        pthread_create(&t2,NULL,changeDir,NULL);
        while(1);
        getch();
        endwin();
        return 0;
}


相关推荐

  1. 贪吃游戏显示贪吃路线宽度

    2024-04-28 12:26:02       31 阅读
  2. js怎样控制浏览器前进退、页面跳转

    2024-04-28 12:26:02       11 阅读
  3. 前端软件开发两个重要部分

    2024-04-28 12:26:02       14 阅读
  4. 如何前端优化处理大量图像资源?

    2024-04-28 12:26:02       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-28 12:26:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-28 12:26:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-28 12:26:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-28 12:26:02       18 阅读

热门阅读

  1. 笔记:Python 注释(练习题)

    2024-04-28 12:26:02       13 阅读
  2. milvus indexcoord启动源码分析

    2024-04-28 12:26:02       30 阅读
  3. C++可调用对象的绑定器和包装器

    2024-04-28 12:26:02       11 阅读
  4. 探索Kotlin:最佳学习实践和资源指南

    2024-04-28 12:26:02       10 阅读
  5. XSS攻击

    XSS攻击

    2024-04-28 12:26:02      10 阅读
  6. 墨子时事周报

    2024-04-28 12:26:02       11 阅读
  7. C# 字符串左不足位数时补充0

    2024-04-28 12:26:02       12 阅读
  8. transformers - 预测中间词

    2024-04-28 12:26:02       11 阅读
  9. opencv动态识别人脸

    2024-04-28 12:26:02       10 阅读
  10. L2-052 吉利矩阵

    2024-04-28 12:26:02       11 阅读
  11. Centos编译安装python3.9

    2024-04-28 12:26:02       13 阅读
  12. 生成对抗网络(GAN)

    2024-04-28 12:26:02       11 阅读