| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1967 人关注过本帖
标题:关于链表和数组问题
只看楼主 加入收藏
qq1625127317
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:185
专家分:450
注 册:2015-9-3
收藏
得分:0 
回复 9楼 hjx1120
程序代码:
#include<stdio.h>        //哥哥,这是我仿照你写的。。其他的没毛病,可是我的按键检测函数好像不管用。。。怎么回事???
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
struct Snake 
{
    int x;
    int y;
    struct Snake * pnext;
};
struct Food
{   
    int x;
    int y;
};
struct Direction
{
    char direction;
};
char ch = 1;
char tn = 3;
int stop = 0;
char direction;
struct Food * food;
struct Direction * headir;
struct Snake * head,* tail;      //建立头指针
struct Snake * p;         //遍历指针
void ZHUYE();             //绘制主界面函数
void HideCursor();        //隐藏光标函数
void gotoxy(int x,int y); //移动光标函数
void creat_snake();       //初始化蛇身函数
void draw_snake();        //绘制蛇身
void creat_food();        //产生食物函数
void creat_node();        //添加头结点函数
void shanchu_tail();      //删除尾结点函数
void gameover();          //游戏结束判断函数
int selfpan();            //判断是否咬到自身
void snake_move();        //蛇身移动函数
void keyjiance();         //按键检测函数
void main()
{
    ZHUYE();   //主界面
    while(!stop)
    {
        keyjiance();
        snake_move();
        draw_snake();
        Sleep(500);
        gameover();
    }
}
void ZHUYE()              //绘制主页面函数
{  
    HideCursor();
    int i,j;
    for(i = 0; i < 40; i++)
    {
        gotoxy(i,0);
        printf("-");
        gotoxy(i,14);
        printf("-");
    }
    for(j = 0; j < 15; j++)
    {
        gotoxy(0,j);
        printf("|");
        gotoxy(39,j);
        printf("|");
    }
    gotoxy(12,7);
    printf("按任意键开始!");
    _getch();
    gotoxy(12,7);
    printf("              ");
    creat_snake();
    creat_food();
}
void HideCursor()         //隐藏光标
{
    CONSOLE_CURSOR_INFO cursor_info = {1, 0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x,int y)  //移动光标
{
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD loc={x,y};
    SetConsoleCursorPosition(hOut,loc);
}
void creat_snake()        //蛇身初始化
{
    int i;
    struct Snake * tail;
    tail = (struct Snake *)malloc(sizeof(struct Snake));
    headir = (struct Direction *)malloc(sizeof(struct Direction));
    food = (struct Food *)malloc(sizeof(struct Food));
    if(tail == NULL)
    {
        exit (-1);
    }
    tail -> x = 10;
    tail -> y = 6;
    tail -> pnext = NULL;
    headir -> direction = 'd';
    for(i = 1; i <= 4; i++)
    {
        head = (struct Snake *)malloc(sizeof(struct Snake));
        head -> pnext = tail;
        head -> x = 10 + i;
        head -> y = 6;
        tail = head;
    }
    head = tail;
    draw_snake();
}
void draw_snake()       //绘制蛇身结点函数
{
    struct Snake * temp;
    temp = head;
    while(temp != NULL)
    {
        gotoxy(temp -> x,temp -> y);
        printf("%c",ch);
        temp = temp -> pnext;
    }
}
void creat_food()
{
    struct Snake * temp;
    temp = head;
    srand(time(NULL));
    food -> x = rand()%38+1;
    food -> y = rand()%13+1;
    while(temp != NULL)
    {
        if(food -> x == temp -> x&&food -> y == temp -> y)
        {
            free(food);
            creat_food();
        }
        temp = temp -> pnext;
    }
    gotoxy(food -> x,food -> y);
    printf("%c",tn);
}
void creat_node(int x,int y)         //增添头结点
{
    struct Snake * p2;
    p2 = (struct Snake *)malloc(sizeof(struct Snake));
    p2 -> pnext = head;
    p2 -> x = x;
    p2 -> y = y;
    head = p2;
    gameover();
}
void shanchu_tail()                   //删除尾结点函数定义
{
    struct Snake * temp;
    p = head;
    while(p -> pnext != NULL)
    {
        temp = p;
        p = p -> pnext;
    }
    gotoxy(p -> x,p -> y);
    printf(" ");
    temp -> pnext = NULL;
    free(p);
}
int selfpan()                        //是否咬到自身
{
    struct Snake * temp;
    temp = head -> pnext;
    while(temp != NULL)
    {
        if(temp -> x == head -> x&& temp -> y == head -> y)
            return 1;
        temp = temp -> pnext;
    }
}
void gameover()                      //判断游戏结束
{
    struct Snake * temp;
    temp = head -> pnext;
    if(selfpan() == 1)
    {
        stop = 1;
        system("cls");
        gotoxy(18,7);
        printf("游戏结束!");
        gotoxy(8,18);
        printf(" ");
    }
    if(head -> x <1 || head -> x > 38 || head -> y <1 || head -> y >13)
    {
        stop = 1;
        system("cls");
        gotoxy(18,7);
        printf("游戏结束!");
        gotoxy(8,18);
        printf(" ");
    }
}
void snake_move()                    //蛇身移动函数定义
{
    if(headir -> direction == 'w')
    {
        if(food -> x == head -> x && food -> y == head -> y)
        {
            gotoxy(food -> x,food -> y);
            printf(" ");
            creat_node(head -> x,head -> y -1);
            creat_food();
        }
        else
        {
            creat_node(head -> x,head -> y -1);
            shanchu_tail();
        }
    }
    if(headir -> direction == 's')
    {
         if(food -> x == head -> x && food -> y == head -> y)
        {
            gotoxy(food -> x,food -> y);
            printf(" ");
            creat_node(head -> x,head -> y + 1);
            creat_food();
        }
        else
        {
            creat_node(head -> x,head -> y + 1);
            shanchu_tail();
        }
    }
     if(headir -> direction == 'd')
    {
         if(food -> x == head -> x && food -> y == head -> y)
         {
            gotoxy(food -> x,food -> y);
            printf(" ");
            creat_node(head -> x + 1,head -> y);
            creat_food();
        }
        else
        {
            creat_node(head -> x + 1,head -> y);
            shanchu_tail();
        }
    }
     if(headir -> direction == 'a')
    {
         if(food -> x == head -> x && food -> y == head -> y)
        {
            gotoxy(food -> x,food -> y);
            printf(" ");
            creat_node(head -> x -1,head -> y);
            creat_food();
        }
        else
        {
            creat_node(head -> x - 1,head -> y);
            shanchu_tail();
        }
    }
}
void keyjiance()          //按键检测函数定义
{
    char key;
    if(_kbhit())
    {
        key = getch();
        switch(key)
        {
            case 'w' : if(headir -> direction == 's')
                            break;
                       else
                           headir -> direction == 'w';break;
            case 's' : if(headir -> direction == 'w')
                           break;
                        else
                            headir -> direction == 's';break;
            case 'd' : if(headir -> direction == 'a')
                           break;
                        else 
                            headir -> direction == 'd';break;
            case 'a' : if(headir -> direction == 'd')
                            break;
                        else
                            headir -> direction == 'a';break;
            default :  break;
        }
    }
}

静坐常思己过,闲谈莫论人非
2015-10-12 21:57
springwater
Rank: 1
等 级:新手上路
帖 子:2
专家分:3
注 册:2015-10-11
收藏
得分:0 
回复 7楼 hjx1120
2015-10-12 22:41
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
回复 21楼 qq1625127317
那段代码亲测是没有什么问题的,
最后
请叫我 --》  叔叔大人,不要叫哥哥!
2015-10-12 22:50
qq1625127317
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:185
专家分:450
注 册:2015-9-3
收藏
得分:0 
回复 23楼 hjx1120
好吧。。。。可是为什么我的上下左右控制不了啊?

静坐常思己过,闲谈莫论人非
2015-10-12 22:51
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
我没那么年青,也不是小鲜肉,都快成老不死的了,还叫哥哥
折杀我也
2015-10-12 22:54
qq1625127317
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:185
专家分:450
注 册:2015-9-3
收藏
得分:0 
回复 25楼 hjx1120
有这么夸张吗???

静坐常思己过,闲谈莫论人非
2015-10-12 22:58
the_second
Rank: 2
等 级:论坛游民
帖 子:115
专家分:80
注 册:2015-9-13
收藏
得分:0 
图形文件能放在数组里吗
2015-10-12 22:59
qq1625127317
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:185
专家分:450
注 册:2015-9-3
收藏
得分:0 
回复 27楼 the_second
图形文件?其实我对这个概念不太了解。。是指这个蛇身的组成部件吗?

静坐常思己过,闲谈莫论人非
2015-10-12 23:02
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
回复 26楼 qq1625127317
Sorry ,那段好像是有Bug的版本,修定版本忘了放那个文件夹了
额,对 了,楼主你不是不看的嘛
2015-10-12 23:03
qq1625127317
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:185
专家分:450
注 册:2015-9-3
收藏
得分:0 
回复 29楼 hjx1120
住口!我只是小小的看了一下!

静坐常思己过,闲谈莫论人非
2015-10-12 23:05
快速回复:关于链表和数组问题
数据加载中...
 
   



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

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