| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 373 人关注过本帖
标题:求高手们帮忙解决一问题
只看楼主 加入收藏
灰暗
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-18
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
求高手们帮忙解决一问题
怎么用二维数组画地图?
搜索更多相关主题的帖子: 地图 
2011-04-18 19:13
ucyan
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:61
专家分:198
注 册:2011-4-12
收藏
得分:10 
画什么地图?
2011-04-18 19:43
灰暗
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-18
收藏
得分:0 
就是画个小游戏的地图。你可以弄个二维数组画图的案例
2011-04-18 19:46
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:10 
程序代码:
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <stdio.h>

/*表示蛇移动的方向*/
#define DIR_UP        0
#define DIR_DOWN    1
#define DIR_LEFT    2
#define DIR_RIGHT    3

#define DIM_F        20
#define DIM_S        40
#define FENCE        '@'
#define BLANK        ' '
/**********************************/

/*定义蛇身的结点*/
typedef struct node
{
    COORD point;//坐标
    char data;//字符
    struct node *next;
}*Snake_list;

Snake_list snake_header = NULL;//定义表示蛇的单链表头
char map[DIM_F][DIM_S];//游戏地图
char key_code;//接收键盘输入的键的ASCII码值
COORD food_point, end;//定义游戏中食物的坐标, 和存储蛇尾的坐标end
char food_char;//食物字符
char director;//标识蛇走的方向
char food_flag; //表示是否有食物1(有), 0(没有)
char key_tuch;//表示是否有新的键值按下
int count=-1;//计分

COORD Create_snake_head();
void Add_snake_node(COORD p, char data);
void Init_map();
int Create_food();
void Get_key();
void Show_snake();
void Get_key_code();
void Moving();
void Show_game();
void Eat_food();
void IS_die();
void Eat_itself();
void Meet_fence();
void Show_mark();

int main()
{
    SetConsoleTitle("Snake 1.0.0");
    Init_map();
    Show_game();
    food_flag = Create_food();
    director = rand()%4;
    Add_snake_node(Create_snake_head(), rand()%26+65);
    Show_mark();
    HANDLE getkey_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) Get_key, NULL, 0, NULL);

    while( true )
    {
        DWORD state_key = WaitForSingleObject( getkey_handle, 0 );
        if( state_key = WAIT_OBJECT_0 )
        {
            Get_key();
        }
        if( key_tuch )
        {
            Get_key_code();
        }
        Moving();       
        if( food_flag == 0 )
        {
            Show_mark();
            food_flag = Create_food();
        }
        Show_game();

        Sleep(500);
    }

    return 0;
}
/*

 *功能:在刚进入游戏的时候先创建一个蛇头所在的坐标

 *返回值:蛇头的坐标

 */
COORD Create_snake_head()
{
    COORD point;
    point.X = rand()%20 + 10;
    point.Y = rand()%10 +  5;
    while( point.X == food_point.X && point.Y == food_point.Y )
    {
        point.X = rand()%20 + 10;
        point.Y = rand()%10 + 5;
    }

    return point;
}
/*

 *功能:向链表中插入一个结点

 *传入参数:结点的坐标值 显示的字符

 */
void Add_snake_node(COORD p, char data)
{
    if( snake_header == NULL )
    {
        snake_header = (Snake_list) malloc (sizeof(Snake_list));
        snake_header->next = NULL;
        snake_header->data = data;
        snake_header->point = p;
    }
    else
    {
        Snake_list temp = (Snake_list) malloc (sizeof(Snake_list));
        temp->data = data;
        temp->point = p;
        temp->next = snake_header;
        snake_header = temp;
    }

    return;
}
/*

 *功能:初始化游戏地图

 */
void Init_map()
{
    int x;
    for( x=0; x<DIM_F; ++x )
    {
        map[x][0] = map[x][DIM_S-1] = FENCE;
    }
    for( x=0; x<DIM_S; ++x )
    {
        map[0][x] = map[DIM_F-1][x] = FENCE;
    }

    return;
}
/*

 *功能:创建蛇的食物

 *创建成功

 *返回值1

 */
int Create_food()
{
    Snake_list temp = snake_header;
    srand(time(NULL));
    food_point.Y = rand()%(DIM_F-2) + 1;
    food_point.X = rand()%(DIM_S-2) + 1;
    while( temp )
    {
        if( (food_point.X == 1 && food_point.Y == 1) ||
            (food_point.X == DIM_S-2 && food_point.Y == DIM_F-2) ||
            (food_point.X == 1 && food_point.Y == DIM_F-2) ||
            (food_point.X == DIM_S-2 && food_point.Y == 1) )
        {
            food_point.Y = rand()%(DIM_F-2) + 1;
            food_point.X = rand()%(DIM_S-2) + 1;
            temp = snake_header;
        }
        else
        {
            if( temp->point.X != food_point.X || temp->point.Y != food_point.Y )
            {
                temp = temp->next;
            }
            else
            {
                food_point.Y = rand()%(DIM_F-2) + 1;
                food_point.X = rand()%(DIM_S-2) + 1;
                temp = snake_header;
            }
        }
    }
    srand(time(NULL));
    food_char = rand()%26+65;
    map[food_point.Y][food_point.X] = food_char;

    return 1;
}
/*

 *功能:获取按键

 */
void Get_key()
{
    while( true )
    {
        key_code = getch();
        key_tuch = 1;
        Sleep(40);
    }
}
/*

 *功能:把蛇身所在的位置标识相应的字母

 */
void Show_snake()
{
    Snake_list temp = snake_header;
    while( temp )
    {
        map[temp->point.Y][temp->point.X] = temp->data;
        temp = temp->next;
    }
    map[end.Y][end.X] = BLANK;

    return;
}
/*

 *功能:用按键的值转换成蛇移动的相应方向标识

 *并把按键标识 清零 表示当前没有要处理的按键

 */
void Get_key_code()
{
    switch( key_code )
    {
    case 'A':
    case 'a':
        director = DIR_LEFT;
        break;
    case 'S':
    case 's':
        director = DIR_DOWN;
        break;
    case 'W':
    case 'w':
        director = DIR_UP;
        break;
    case 'D':
    case 'd':
        director = DIR_RIGHT;
        break;
    default:
        break;
    }
    key_tuch = 0;

    return;
}
/*

 *功能:完成蛇的惯性移动

 */
void Moving()
{
    Eat_food();
    end.X = snake_header->point.X;
    end.Y = snake_header->point.Y;
    switch( director )
    {
    case DIR_LEFT://向左边移动
        end.X = end.X-1;
        break;
    case DIR_UP://向上移动
        end.Y = end.Y-1;
        break;
    case DIR_RIGHT://向右边移动
        end.X = end.X+1;
        break;
    case DIR_DOWN://向下移动
        end.Y = end.Y+1;
        break;
    default:
        break;
    }
    Snake_list temp = snake_header;
    COORD pp;
    while ( temp!=NULL )
    {
        pp.X = temp->point.X;
        pp.Y = temp->point.Y;
        temp->point.X = end.X;
        temp->point.Y = end.Y;
        end.X = pp.X;
        end.Y = pp.Y;
        temp = temp->next;
    }
    IS_die();

    return;
}
/*

 *功能:将数组全部打印一次

 */
void Show_game()
{
    COORD p;
    p.X = 0; p.Y = 0;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
    Show_snake();
    for( int i=0; i<DIM_F; ++i )
    {
        for( int j=0; j<DIM_S; ++j )
        {
            printf( "%c", map[i][j] );
        }
        printf("\n");
    }

    return;
}
/*

 *功能:判断是否可以吃食物

 *吃到后 把食物加到蛇身

 *并且 把食物标识清零 表示当前已经没有食物

 */
void Eat_food()
{
    switch( director )
    {
    case DIR_LEFT://向左边移动
        if( food_point.X == snake_header->point.X-1 && food_point.Y == snake_header->point.Y )
        {
            Add_snake_node(food_point, food_char);
            food_flag = 0;
        }
        break;
    case DIR_UP://向上移动
        if( food_point.Y == snake_header->point.Y-1 && food_point.X == snake_header->point.X )
        {
            Add_snake_node(food_point, food_char);
            food_flag = 0;
        }
        break;
    case DIR_RIGHT://向右边移动
        if( food_point.X == snake_header->point.X+1 && food_point.Y == snake_header->point.Y )
        {
            Add_snake_node(food_point, food_char);
            food_flag = 0;
        }
        break;
    case DIR_DOWN://向下移动
        if( food_point.Y == snake_header->point.Y+1 && food_point.X == snake_header->point.X )
        {
            Add_snake_node(food_point, food_char);
            food_flag = 0;
        }
        break;
    default:
        break;
    }

    return;
}
/*

 *功能:判断游戏是否结束

 */
void IS_die()
{
    Eat_itself();
    Meet_fence();

    return;
}
/*

 *功能:蛇头碰到了蛇身

 */
void Eat_itself()
{
    Snake_list temp1 =snake_header, temp2 = snake_header;
    temp2 = temp1->next;
    while( temp2 )
    {
        if( temp2->point.X == temp1->point.X && temp2->point.Y == temp1->point.Y )
        {       
            COORD point;
            point.X = DIM_S; point.Y = DIM_F;
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
            printf("吃到自己啦\n");
            exit(0);
        }
        temp2 = temp2->next;
    }
   
    return;
}
/*

 *功能:蛇头碰到了墙

 */
void Meet_fence()
{
    if( snake_header->point.X == 0 || snake_header->point.X == DIM_S-1 ||
        snake_header->point.Y == 0 || snake_header->point.Y == DIM_F-1)
    {
        COORD point;
        point.X = DIM_S; point.Y = DIM_F;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
        printf("撞墙\n");
        exit(0);
    }

    return;
}
/*

 *功能:显示得分

 */
void Show_mark()
{
    COORD point;
    point.X = DIM_S+10; point.Y = DIM_F/2;
    ++count;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
    printf("%d 分", count*100);

    return;
}
2011-04-18 20:49
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
/*
*功能:初始化游戏地图
*/
void Init_map()
{
    int x;
    for( x=0; x<DIM_F; ++x )
    {
        map[x][0] = map[x][DIM_S-1] = FENCE;
    }
    for( x=0; x<DIM_S; ++x )
    {
        map[0][x] = map[DIM_F-1][x] = FENCE;
    }

    return;
}

上面就是

不知道能不能跑   历史悠久啊   
2011-04-18 20:52
快速回复:求高手们帮忙解决一问题
数据加载中...
 
   



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

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