当前位置: 首页 > wzjs >正文

网站卡密怎么做动态网站设计毕业论文

网站卡密怎么做,动态网站设计毕业论文,网页制作与网站建设广州,深圳网站建设学校贪吃蛇吃饭喽 所谓贪吃蛇的食物&#xff0c;也就是创建一个和蛇身一样的结构体&#xff0c;只是这个结构体不是链表&#xff0c;也是将这个结构体设置hang和lie坐标&#xff0c;放进gamepic进行扫描&#xff0c;扫到了就也是做操作将 ## 打出来 #include <curses.h> #i…

贪吃蛇吃饭喽

     所谓贪吃蛇的食物,也就是创建一个和蛇身一样的结构体,只是这个结构体不是链表,也是将这个结构体设置hang和lie坐标,放进gamepic进行扫描,扫到了就也是做操作将 ## 打出来

#include <curses.h>

#include <stdlib.h>

struct snake{

        int hang;

        int lie;

        struct snake *next;

};

struct snake food;

struct snake *head;

struct snake *tail;

int key;

int dir;

#define UP     1

#define DOWN  -1

#define LEFT   2

#define RIGHT -2

void initNcurse()

{

        initscr();

        keypad(stdscr,1);

        noecho();

}

int  hasSnakeNode(int i,int j)

{

        struct snake *p;

        p = head;

        while(p != NULL){

                if(p->hang==i && p->lie==j){

                        return 1;

                }

                p=p->next;

        }

        return 0;

}

int   ifSnakeFood(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<=19){

                        for(lie=0;lie<=20;lie++){

                                 if(lie==0||lie==20){

                                         printw("|");

                                 }else if(hasSnakeNode(hang,lie)){

                                        printw("[]");

                                 }else if(ifSnakeFood(hang,lie)){

                                        printw("##");

                                 }

                                 else{

                                         printw("  ");

                                 }

                        }

                        printw("\n");

                }

                if(hang == 19){

                        for(lie=0;lie<20;lie++){

                                 printw("--");

                        }

                        printw("\n");

                }

          }

          printw("by shijintao\n");

          printw("key =%d\n",key);

   }

void addNode()

{

        struct snake *new;

        new =(struct snake *)malloc(sizeof(struct snake));

        new->next=NULL;

                switch(dir){

                case UP:

                        new->hang=tail->hang-1;

                        new->lie=tail->lie;

                        tail->next=new;

                        tail = new;

                        break;

                case DOWN:

                        new->hang=tail->hang+1;

                        new->lie=tail->lie;

                        tail->next=new;

                        tail = new;

                        break;

               

   case LEFT:

                        new->lie=tail->lie-1;

                        new->hang=tail->hang;

                        tail->next=new;

                        tail = new;

                        break;

                case RIGHT:

                        new->lie=tail->lie+1;

                        new->hang=tail->hang;

                        tail->next=new;

                        tail = new;

                        break;

                 }

}

void initFood() //初始化食物  和蛇一样要初始化位置  行和列

{

      static int x=4;

      static int y=4;

      food.hang=x;

      food.lie=y;

      x+=2;

      y+=2;

}

void  initSnake()

{

        struct snake *p;

        while(head != NULL){

             p=head;

             head=head->next;

             free(p);

        }

        initFood();

        head = (struct snake *)malloc(sizeof(struct snake));

        dir = RIGHT;

        head->hang=2;

        head->lie=2;

        head->next=NULL;

        tail = head;

        addNode();

        addNode();

}

void deleteNode()

{

        struct snake *p;

        p = head;

        head = head->next;

        free(p);

}

void moveSnake()

{

        addNode();

       if(ifSnakeFood(tail->hang,tail->lie)){

                initFood();

        }else{

                deleteNode();

        }

        if(tail->hang==0||tail->hang==20||tail->lie==20||tail->lie==0){

                initSnake();

        }

}

//如果是在移动过程中吃到食物了,也是跟那个撞墙一个原理都要进行判断,但是此原理非彼原 理,不管是撞墙还是吃食物还是正常移动,都是要增加删除 ,把增加放在前面,就是为了如果吃到食物的话就可以增加,而不会删除,就会多一个节点,显示出吃了一个食物变成的效果。

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_RIGHT:

                                turn(RIGHT);

                                break;

                        case KEY_LEFT:

                                turn(LEFT);

                                break;

                }

        }

}

void * gamerefresh()

{

        while(1){

                moveSnake();

                gamepic();

                refresh();

                usleep(100000);

        }

}

int main()

{

                pthread_t th1;

                pthread_t th2;

                initNcurse();

                initSnake();

                gamepic();

                pthread_create(&th2,NULL,gamerefresh,NULL);

                pthread_create(&th1,NULL,changeDir,NULL);

                while(1);

                getch();

                endwin();

                return 0;

}

好的 现在发现食物不随机,那么我们修改一下代码

要用到c语言的rand()函数

让rand()%20    取余数20  那么值永远都为1~19

非常符合食物的范围hang和lie的值

#include <curses.h>

#include <stdlib.h>

struct snake{

        int hang;

        int lie;

        struct snake *next;

};

struct snake food;

struct snake *head;

struct snake *tail;

int key;

int dir;

#define UP     1

#define DOWN  -1

#define LEFT   2

#define RIGHT -2

void initNcurse()

{

        initscr();

        keypad(stdscr,1);

        noecho();

}

int  ifSnakeNode(int i,int j)

{

        struct snake *p;

        p = head;

        while(p != NULL){

                if(p->hang==i && p->lie==j){

                        return 1;

                }

                p=p->next;

        }

        return 0;

}

int  ifSnakeFood(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<=19){

                        for(lie=0;lie<=20;lie++){

                                 if(lie==0||lie==20){

                                         printw("|");

                                 }else if(ifSnakeNode(hang,lie)){

                                        printw("[]");

                                 }else if(ifSnakeFood(hang,lie)){

                                        printw("##");

                                 }

                                 else{

                                         printw("  ");

                                 }

                        }

                        printw("\n");

                }

                if(hang == 19){

                        for(lie=0;lie<20;lie++){

                                 printw("--");

                        }

                        printw("\n");

                }

          }

          printw("by shijintao\n");

          printw("key =%d\n",key);

   }

void addNode()

{

        struct snake *new;

        new =(struct snake *)malloc(sizeof(struct snake));

        new->next=NULL;

                switch(dir){

                case UP:

                        new->hang=tail->hang-1;

                        new->lie=tail->lie;

                        tail->next=new;

                        tail = new;

                        break;

                case DOWN:

                        new->hang=tail->hang+1;

                        new->lie=tail->lie;

                        tail->next=new;

                        tail = new;

                        break;

               

   case LEFT:

                        new->lie=tail->lie-1;

                        new->hang=tail->hang;

                        tail->next=new;

                        tail = new;

                        break;

                case RIGHT:

                        new->lie=tail->lie+1;

                        new->hang=tail->hang;

                        tail->next=new;

                        tail = new;

                        break;

                 }

}

void initFood()      //这段代码就是关键了 将食物的位置随机化用了rand()函数  并%20  这样x的值永远都会  0<=?<20.

{

      Int x=rand()%20;

      int y=rand()%20;

      food.hang=x;

      food.lie=y;

}

void  initSnake()

{

        struct snake *p;

        while(head != NULL){

             p=head;

             head=head->next;

             free(p);

        }

        initFood();

        head = (struct snake *)malloc(sizeof(struct snake));

        dir = RIGHT;

        head->hang=2;

        head->lie=2;

        head->next=NULL;

        tail = head;

        addNode();

        addNode();

}

void deleteNode()

{

        struct snake *p;

        p = head;

        head = head->next;

        free(p);

}

void moveSnake()

{

        addNode();

        if(hasSnakeFood(tail->hang,tail->lie)){

                initFood();

        }else{

                deleteNode();

        }

        if(tail->hang==0||tail->hang==20||tail->lie==20||tail->lie==0){

                initSnake();

        }

}

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_RIGHT:

                                turn(RIGHT);

                                break;

                        case KEY_LEFT:

                                turn(LEFT);

                                break;

                }

        }

}

void * gamerefresh()

{

        while(1){

                moveSnake();

                gamepic();

                refresh();

                usleep(100000);

        }

}

int main()

{

                pthread_t th1;

                pthread_t th2;

                initNcurse();

                initSnake(); //要注意蛇和食物都在这个函数里面初始化

                gamepic();

                pthread_create(&th2,NULL,gamerefresh,NULL);

                pthread_create(&th1,NULL,changeDir,NULL);

                while(1);

                getch();

                endwin();

                return 0;

}

http://www.dtcms.com/wzjs/440509.html

相关文章:

  • 网页设计旅游网站源代码免费网络营销策略的制定
  • 购物网站开发教学视频百度的网页地址
  • 贷款网站建设网络营销师证书
  • 租车做什么网站推广seoul是韩国哪个城市
  • 如何做服装微商城网站建设域名查询站长工具
  • 做贺卡 网站seo是什么牌子
  • 做网站电话沧州网络推广免费网站
  • wordpress一句话插件seo文章范文
  • 17zwd一起做网店潮汕站北京网站优化怎么样
  • 做电路设计的兼职网站百度指数分析大数据
  • 阿里云做影视网站怎么免费创建个人网站
  • 网站建设策划方案t百度快照是啥
  • 旅游网站首页日本网络ip地址域名
  • 网站建设销售工作好么万网域名注册官网阿里云
  • 做citation的网站新网站推广最直接的方法
  • 网站备案的用户名是什么域名解析查询站长工具
  • 公司商城网站建设深圳网络推广工资
  • 苏州哪个公司做网站好苏州关键词排名系统
  • 苏州化妆品网站建设sem优化托管公司
  • 网上课程网站成都百度百科
  • 营销型网站价格外贸营销网站建设介绍
  • 优化网站和网站建设谷歌google play官网下载
  • 广东网站建设定制广东seo教程
  • 建设个人你网站许昌seo公司
  • 织梦网站源码下载赣州网站建设公司
  • 玩具网站建设网站运营需要多少钱
  • 建设网站用户名是什么原因石家庄seo外包公司
  • 1高端网站建设橘子seo查询
  • wordpress老版合肥网络优化推广公司
  • 深圳企业网站建设与设计制作seo优化网站推广全域营销获客公司