| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1994 人关注过本帖
标题:刚刚写的贪吃蛇
只看楼主 加入收藏
luomin5417
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-4-23
收藏
 问题点数:0 回复次数:23 
刚刚写的贪吃蛇
在linux下写的,刚刚写好,图形化用的是调用curses.h
插个效果图
图片附件: 游客没有浏览图片的权限,请 登录注册

程序代码:
/**************************************

功能:贪吃蛇程序

***************************************/

//========================================

//设置光标初始位置

#include <stdio.h>

#include <curses.h>

#include <pthread.h>

#include <time.h>

#include <stdlib.h>



#define MAXY 25

#define MAXX 70



typedef struct dotS{

    int x;
    int y;

    struct dotS *next;        

}dotSnake, *dotLink;



int ch;



//初始视窗

void initail()

{

    initscr();

    cbreak();

    nonl();

    noecho();

    intrflush(stdscr,FALSE);

    keypad(stdscr,TRUE);

}



//初始化贪吃蛇

void initLink(dotLink *L)

{

    dotSnake *p;

    int m, n;

    
    *L = (dotSnake *)malloc(sizeof(dotSnake));

    (*L)->next = NULL;  

    p = (dotSnake *)malloc(sizeof(dotSnake));

    m = (rand() % (MAXX - 2)) + 1;

    n = (rand() % (MAXY - 2)) + 1;

    (*L)->next = p;

    p->x = m;

    p->y = n;

    p->next = NULL;  

}

//判断是否死亡函数

int judgeDeath(dotLink L, WINDOW *win)

{

    dotSnake *p, *q;

    p = L->next;
    q = p->next; 

    if (  p->x == 70 || p->y == 25 )

    {
       box(win, '*', '*');

       mvwaddstr(win, 13, 30,"the game is over!");
       wnoutrefresh(win);
       doupdate(); 

       getch();

       endwin();

       exit(1); 

    }
    if ( q != NULL )
    {
        q = q->next;
        while ( q != NULL )
        {
            if ( p->x == q->x && p->y == q->y)
            {
                box(win, '*', '*');

                mvwaddstr(win, 13, 30,"the game is over!");
                wnoutrefresh(win);
                doupdate(); 

                getch();

                endwin();

                exit(1);
            }
            q = q->next;
        }
    }
   

}



//生成随机食物

dotSnake *randFood(dotLink L, WINDOW *win)

{

    int m,n, flag = 1;

    dotSnake *p, *q;

    q = (dotSnake *)malloc(sizeof(dotSnake));

    while (flag)

    {   

        p = L->next;   

        m = (rand() % (MAXX - 2)) + 1;

        n = (rand() % (MAXY - 2)) + 1;

        while( p != NULL )

        {

            if ( p->x == m && p->y == n )

            {

                break;

            }

            p = p->next;

        }

        if ( p == NULL )

        {

            q->x = m;

            q->y = n;

            flag = 0;

        }

    }

    return q;

}



//贪吃蛇不断运动函数

void forwardMove(dotLink L, WINDOW *win)

{

     dotSnake *p;

     int m, n, i, j;

     p = L->next;

     if ( p->next != NULL )

     {

         m = p->x;

     n = p->y;

         while ( p->next != NULL )

         {             

          i = p->next->x;       

          j = p->next->y;

              p->next->x = m;

              p->next->y = n;

                            

              m = i;

              n = j;

              p = p->next;

         }

         p = L->next;

     (p->y)--; 

     }

     else

     {

         (p->x)++; 

     }

     p = L->next;

}

//线程函数

void *waitKey(void *prapra)

{

    while (1)

    {

        usleep(1000);
        ch = getch();

    }

}



//控制蛇运动方向函数

int controlSnake(dotLink L)

{

    int m = 0, n = 0, i = 0, j = 0;

    int flag = 0;

    dotSnake *p;

    p = L->next;

    switch(ch)

    {

    case KEY_UP: if ( p->next != NULL )

                 {

              

                     m = p->x;

                     n = p->y; 

                     while ( p->next != NULL )

                     {                 

                         i = p->next->x;       

                         j = p->next->y;

                             p->next->x = m;

                             p->next->y = n;

                            

                             m = i;

                             n = j;

                             p = p->next;

                             

                             

                      }

                     p = L->next;

                     (p->y)--;                   

                 }

                 else

                 {

                     (p->y)--;

                 }

                 flag = 1;

                 break;

         case KEY_DOWN: if ( p->next != NULL )

                    {

              

                           m = p->x;

                           n = p->y; 

                           while ( p->next != NULL )

                           {

                               i = p->next->x;       

                               j = p->next->y;

                                   p->next->x = m;

                                   p->next->y = n;

                                   m = i;

                                   n = j;

                                   p = p->next;
                                                                                                                                                                                  

                           }

                           p = L->next;

                           (p->y)++;                   

                     }

                     else

                     {

                           (p->y)++;

                     }

                     flag = 1;

                 break;

     case KEY_LEFT:  if ( p->next != NULL )

                     {

              

                            m = p->x;

                            n = p->y; 

                            while ( p->next != NULL )

                            {

                                 i = p->next->x;       

                                 j = p->next->y;

                                     p->next->x = m;

                                     p->next->y = n;

                                    m = i;

                                    n = j;

                                    p = p->next;

                            }

                            p = L->next;

                            (p->x)--;                   

                     }

                     else

                     {

                            (p->x)--;

                     }

                     flag = 1;

             break;

     case KEY_RIGHT: if ( p->next != NULL )

                     {

              

                            m = p->x;

                            n = p->y; 

                            while ( p->next != NULL )

                            {

                                i = p->next->x;       

                                j = p->next->y;

                                    p->next->x = m;

                                    p->next->y = n;

                                    m = i;

                                    n = j;

                                    p = p->next;

                            }

                            p = L->next;

                            (p->x)++;                   

                      }

                      else

                      {

                            (p->x)++;

                      }

                      flag = 1;

              break;

        case 27:    endwin();

                    exit(0);

       default :  break;    

    }

    return flag;

}





//贪吃蛇吃掉食物 

int judgeEat(dotLink L, dotSnake *p)

{

    dotSnake  *temp;
    dotLink q;

    if ( L->next->x == p->x && L->next->y == p->y )

    {

        q = L->next;

        temp = (dotSnake *)malloc(sizeof(dotSnake));

        while ( q->next != NULL )

        {

            q = q->next;

        }

        temp->next = q->next;

        q->next = temp;

        temp->x = q->x;

        temp->y = q->y;

        return 1;

    }

    else

    {

        return 0;

    }

}



//

void paint(dotLink L, dotSnake *p, WINDOW *win)

{

    dotSnake *q;

    q = L->next;

    mvwaddstr(win, p->y, p->x, "*");

    while ( q != NULL )

    {

        mvwaddstr(win, q->y, q->x, "*");

        q = q->next;

    }

    wnoutrefresh(win);
    doupdate();  

}



//建立视窗





int main()

{

    WINDOW *win;

    dotLink snake;

    dotSnake *food;

    int flag, ret;

    pthread_t tid;

    initLink(&snake);

    srand((unsigned int)time(NULL));

    initail(); 
   //wnoutrefresh(win);
    //doupdate();

    food = randFood(snake, win);

    ret = pthread_create(&tid, NULL, waitKey, NULL);

    if ( ret != 0 )

    {

        exit(1);

    }

    while (1)

    {
        win = (WINDOW *)malloc(sizeof(WINDOW));
        win = newwin(25, 70, 0, 0);

        box(win, '*', '*');
        paint (snake, food, win);

        usleep(200000); 

        flag = controlSnake(snake);

        if ( flag == 0 )

        {

           forwardMove(snake, win);

        }       

        if ( judgeEat(snake, food) == 1 )

        {

            food = randFood(snake, win);

            paint(snake, food, win);

        }

        judgeDeath(snake, win);
        free(win);

    }

    return 0;

}



搜索更多相关主题的帖子: 效果图 贪吃蛇 效果图 贪吃蛇 
2012-06-24 14:36
luomin5417
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-4-23
收藏
得分:0 
代码从linux里拷出来。。格式不知道怎么变了。。搞的代码格式难看死了
2012-06-24 14:38
jokerskill
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:392
专家分:554
注 册:2012-3-4
收藏
得分:0 
你的分去哪里了????
2012-06-24 19:23
qq383264679
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:155
专家分:130
注 册:2012-1-19
收藏
得分:0 
长死了
2012-06-24 20:03
neptunephoto
Rank: 1
来 自:江西
等 级:新手上路
帖 子:3
专家分:8
注 册:2012-6-25
收藏
得分:0 
老长老长了!!
2012-06-25 08:51
龙康1995
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-6-26
收藏
得分:0 
这是什么意思
2012-06-26 16:41
visionliao
Rank: 2
等 级:论坛游民
帖 子:19
专家分:32
注 册:2012-6-17
收藏
得分:0 
你是用什么编程工具写的这个? 我看系统是ubuntu,难道是直接用GCC写的? 求解。。。
2012-06-29 11:35
visionliao
Rank: 2
等 级:论坛游民
帖 子:19
专家分:32
注 册:2012-6-17
收藏
得分:0 
求上传源码资源包。。
2012-06-29 11:54
luomin5417
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-4-23
收藏
得分:0 
回复 7楼 visionliao
是用gcc编译的,你要的话可以发给你
2012-06-30 16:02
luomin5417
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2008-4-23
收藏
得分:0 
回复 8楼 visionliao
不过curses.h的包直接用apt-get install libncurses-dev命令就可以下载到的。。。源文件就上面贴的这个。
2012-06-30 16:09
快速回复:刚刚写的贪吃蛇
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028930 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved