| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1967 人关注过本帖
标题:关于链表和数组问题
只看楼主 加入收藏
hjx1120
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:李掌柜
等 级:贵宾
威 望:41
帖 子:1314
专家分:6927
注 册:2008-1-3
收藏
得分:0 
少年,我看好你哟!C区的未来可就在你小子身上了
2015-10-12 23:07
qq1625127317
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:185
专家分:450
注 册:2015-9-3
收藏
得分:0 
回复 31楼 hjx1120
找见了,就是按键检测函数中的赋值号错写成了等号!

静坐常思己过,闲谈莫论人非
2015-10-13 11:59
qq1625127317
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:185
专家分:450
注 册:2015-9-3
收藏
得分:0 
程序代码:
#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;
int speed = 400;
int SPEED = 400;
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(speed);
        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(45,3);
    printf("用W,S,A,D控制蛇身移动方向");
    gotoxy(45,5);
    printf("按P加速    按O减速");
    gotoxy(45,7);
    printf("SPEED : %d",SPEED);
    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;
            case 'P':
            case 'p' : if(speed > 0)
                       {
                           speed = speed - 100;
                           SPEED = SPEED + 50;
                       }
                           gotoxy(45,7);
                        printf("SPEED : %d",SPEED);
                       break;
            case 'O':
            case 'o' :if(speed < 700)
                      {
                           speed = speed + 100;
                           SPEED = SPEED - 50;
                      }
                        gotoxy(45,7);
                        printf("SPEED : %d",SPEED);
                      break;
            case ' ' : 
                    gotoxy(7,18);
                    printf("游戏已暂停,按任意键恢复游戏");
                    system("pause>nul");
                    gotoxy(7,18);
                    printf("                           ");
                    break;
            default :  break;
        }
    }
}

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



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

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