• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

c链表Ncurses设计贪吃蛇项目

武飞扬头像
TX564
帮助1

项目介绍

1.地图:20x20的正方形区域

2.蛇身:【】【】【】【】

3.食物:##

4.游戏规则:按下↑↓←→来操控蛇的方向,蛇尾撞到边界重新开始,吃到食物蛇身边长

Ncurses:

选择Ncurses的原因是:printw()可以快速响应不需要像printf一样按下回车才开始运行

贪吃蛇游戏需要调用的库

1.#include<curses.h>

2.initscr():设定并初始化ncurses要用到的一些数据的值

3.endwin():恢复终端机到使用前的状态

4.getch():等待获取用户输入

6.printw():打印,与printf()作用相似

7.keypad(stdscr,1):功能键设定

  1.  
    KEY_DOWN 0402
  2.  
    KEY_UP 0403
  3.  
    KEY_LEFT 0404
  4.  
    KEY_RIGHT 0405

8.refresh():刷新界面

9.move(0.0):每次刷新界面不移动地图

10.noecho():防止产生乱码现象

需要用到的头文件

  1.  
    #include<curses.h>
  2.  
    #include<stdlib.h>
  3.  
    #include<pthread.h>
  4.  
    #include<stdio.h>
  5.  
    #include<time.h>
  6.  
    #define UP 1
  7.  
    #define DOWN -1
  8.  
    #define LEFT 2
  9.  
    #define RIGHT -2

完整代码如下

  1.  
    struct snake
  2.  
    {
  3.  
    int hang;
  4.  
    int lie;
  5.  
    struct snake *next;
  6.  
    };
  7.  
     
  8.  
    struct snake food;
  9.  
     
  10.  
    struct snake *head=NULL;
  11.  
    struct snake *tail=NULL;
  12.  
    int key;
  13.  
    int dir;
  14.  
     
  15.  
    void initncurse()
  16.  
    {
  17.  
    initscr();
  18.  
    keypad(stdscr,1);
  19.  
    noecho();
  20.  
    }
  21.  
     
  22.  
    void initfood()
  23.  
    {
  24.  
    int x=rand()%20;
  25.  
    int y=rand()%20;
  26.  
    food.hang=x;
  27.  
    food.lie=y;
  28.  
     
  29.  
    }
  30.  
     
  31.  
    int printfood(int hang , int lie)
  32.  
    {
  33.  
    if(food.hang == hang && food.lie == lie){
  34.  
    return 1;
  35.  
    }
  36.  
    return 0;
  37.  
    }
  38.  
     
  39.  
     
  40.  
    int snaker(int hang,int lie)
  41.  
    {
  42.  
     
  43.  
    struct snake* p=NULL;
  44.  
    p=head;
  45.  
    while(p != NULL){
  46.  
    if(p->hang == hang && p->lie == lie){
  47.  
    return 1;
  48.  
    }p=p->next;
  49.  
    }
  50.  
    return 0;
  51.  
    }
  52.  
     
  53.  
    void addnode()
  54.  
    {
  55.  
    struct snake *new=NULL;
  56.  
    new=(struct snake *)malloc(sizeof(struct snake));
  57.  
    switch(dir){
  58.  
    case UP:
  59.  
    new->hang=tail->hang-1;
  60.  
    new->lie=tail->lie;
  61.  
    break;
  62.  
    case DOWN:
  63.  
    new->hang=tail->hang 1;
  64.  
    new->lie=tail->lie;
  65.  
    break;
  66.  
    case LEFT:
  67.  
    new->hang=tail->hang;
  68.  
    new->lie=tail->lie-1;
  69.  
    break;
  70.  
    case RIGHT:
  71.  
    new->hang=tail->hang;
  72.  
    new->lie=tail->lie 1;
  73.  
    break;
  74.  
    }
  75.  
    tail->next=new;
  76.  
    tail=new;
  77.  
    }
  78.  
     
  79.  
    void initsnake()
  80.  
    {
  81.  
    struct snake *p;
  82.  
    dir=RIGHT;
  83.  
    while(head != NULL){
  84.  
    p=head;
  85.  
    head=head->next;
  86.  
    free(p);
  87.  
    }
  88.  
    initfood();
  89.  
    head=(struct snake *)malloc(sizeof(struct snake));
  90.  
    head->hang=2;
  91.  
    head->lie=1;
  92.  
    head->next=NULL;
  93.  
    tail=head;
  94.  
    addnode();
  95.  
    addnode();
  96.  
    addnode();
  97.  
    }
  98.  
     
  99.  
    void gameboard()
  100.  
    {
  101.  
    int hang,lie;
  102.  
    move(0,0);
  103.  
    for(hang=0;hang<20;hang ){
  104.  
    if(hang == 0){
  105.  
    for(lie=0;lie<20;lie ){
  106.  
    printw("--");
  107.  
    }
  108.  
    }printw("\n");
  109.  
    if(hang >=0 && hang <19){
  110.  
    for(lie=0;lie<=20;lie ){
  111.  
    if(lie == 0 || lie == 20){
  112.  
    printw("|");
  113.  
    }
  114.  
    else if(snaker(hang,lie)){
  115.  
    printw("[]");
  116.  
    }else if(printfood(hang,lie)) {
  117.  
    printw("##");
  118.  
    }else{
  119.  
    printw(" ");
  120.  
    }
  121.  
    }
  122.  
    }if(hang == 19){
  123.  
    for(lie=0;lie<20;lie ){
  124.  
    printw("--");
  125.  
    }
  126.  
    printw("\n");
  127.  
    printw("%d",key);
  128.  
    }
  129.  
    }
  130.  
    }
  131.  
     
  132.  
    void deletenode()
  133.  
    {
  134.  
     
  135.  
    head=head->next;
  136.  
     
  137.  
    }
  138.  
     
  139.  
    void movesnake()
  140.  
    {
  141.  
    addnode();
  142.  
    if(printfood(tail->hang,tail->lie)){
  143.  
    initfood();}
  144.  
    else{
  145.  
    deletenode();
  146.  
    }
  147.  
    }
  148.  
     
  149.  
    int snakedie()
  150.  
    {
  151.  
    struct snake *p;
  152.  
    p=head;
  153.  
    if(tail->hang == 0 || tail->lie == 0 || tail->hang == 20|| tail->lie == 20){
  154.  
    return 1;
  155.  
    }
  156.  
    /* while(p->next != NULL){
  157.  
    if(p->hang == tail->hang && p->lie == tail->lie);
  158.  
    {return 1;
  159.  
    }
  160.  
    p=p->next;
  161.  
    } */
  162.  
    return 0;
  163.  
    }
  164.  
     
  165.  
    void fresh()
  166.  
    {
  167.  
    while(1){
  168.  
    movesnake();
  169.  
    gameboard();
  170.  
    if(snakedie()){
  171.  
    initsnake();
  172.  
    }
  173.  
    refresh();
  174.  
    usleep(100000);
  175.  
    }
  176.  
    }
  177.  
     
  178.  
    void turn(int direction)
  179.  
    {
  180.  
    if(abs(dir) != abs(direction))
  181.  
    {
  182.  
    dir=direction;
  183.  
    }
  184.  
    }
  185.  
     
  186.  
     
  187.  
    void anjian()
  188.  
    {
  189.  
    while(1)
  190.  
    {
  191.  
    key= getch();
  192.  
    switch(key)
  193.  
    {
  194.  
    case KEY_DOWN:
  195.  
    turn(DOWN);
  196.  
    break;
  197.  
    case KEY_UP:
  198.  
    turn(UP);
  199.  
    break;
  200.  
    case KEY_LEFT:
  201.  
    turn(LEFT);
  202.  
    break;
  203.  
    case KEY_RIGHT:
  204.  
    turn(RIGHT);
  205.  
    break;
  206.  
     
  207.  
    }
  208.  
     
  209.  
    }
  210.  
    }
  211.  
     
  212.  
    int main()
  213.  
    {
  214.  
    pthread_t th1;
  215.  
    pthread_t th2;
  216.  
    initncurse();
  217.  
    initsnake();
  218.  
    gameboard();
  219.  
    pthread_create(&th1,NULL,fresh,NULL);
  220.  
    pthread_create(&th2,NULL,anjian,NULL);
  221.  
    while(1);
  222.  
    getch();
  223.  
    endwin();
  224.  
    return 0;
  225.  
    }
  226.  
     
学新通


 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgkiihh
系列文章
更多 icon
同类精品
更多 icon
继续加载