A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

#include <stdio.h>
#include <stdlib.h>

#define LINE 30//地图的行
#define COLUMN 30//地图的列

#define ENDLINE 29//终点y坐标
#define ENDCOLUMN 28//终点x坐标

char gameMap[LINE][COLUMN] =
{
    {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*'},
    {'*','0','*',' ',' ',' ','*','*',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ','*','*',' ','*'},
    {'*',' ','*',' ','*',' ','*',' ',' ','*',' ',' ',' ','*',' ','*','*','*',' ',' ',' ',' ','*',' ','*',' ',' ',' ',' ','*'},
    {'*',' ','*',' ','*',' ','*',' ','*','*',' ','*','*',' ','*','*',' ',' ','*','*',' ',' ','*','*','*',' ','*','*','*','*'},
    {'*',' ',' ',' ','*',' ',' ',' ','*',' ','*',' ','*',' ','*',' ','*',' ',' ',' ','*','*','*',' ',' ',' ',' ',' ','*','*'},
    {'*','*','*','*','*','*','*','*','*',' ',' ',' ',' ',' ','*',' ','*','*','*','*','*',' ','*',' ','*','*','*',' ','*','*'},
    {'*',' ',' ',' ',' ',' ','*',' ','*','*','*',' ','*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*','*','*'},
    {'*','*','*',' ','*',' ','*',' ','*',' ','*',' ','*',' ','*','*',' ','*','*',' ','*','*','*','*','*',' ','*',' ',' ','*'},
    {'*',' ','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ','*',' ','*',' ','*','*'},
    {'*',' ',' ',' ','*',' ',' ',' ',' ','*','*',' ','*','*',' ',' ','*','*','*',' ','*','*',' ','*',' ',' ','*',' ','*','*'},
    {'*',' ','*',' ',' ',' ','*',' ','*',' ',' ',' ',' ',' ','*','*',' ',' ',' ','*',' ',' ',' ',' ',' ','*','*',' ',' ','*'},
    {'*','*','*','*',' ','*',' ',' ','*',' ','*','*',' ','*',' ','*','*','*','*',' ',' ','*','*','*',' ','*','*','*',' ','*'},
    {'*',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ','*',' ',' ','*',' ','*'},
    {'*','*',' ','*','*','*','*',' ',' ','*',' ','*','*',' ','*','*',' ','*',' ','*',' ','*','*',' ','*','*',' ','*',' ','*'},
    {'*',' ',' ',' ',' ',' ','*','*','*','*',' ',' ',' ',' ','*','*',' ','*',' ','*','*',' ',' ','*','*',' ',' ',' ',' ','*'},
    {'*','*','*','*','*',' ',' ',' ',' ',' ','*',' ','*',' ',' ',' ',' ','*',' ','*',' ',' ','*',' ','*',' ','*','*','*','*'},
    {'*',' ',' ',' ','*',' ','*',' ','*','*',' ',' ',' ','*',' ','*','*','*',' ','*',' ','*','*',' ','*',' ','*',' ',' ','*'},
    {'*',' ','*',' ','*',' ','*',' ',' ','*',' ','*','*','*',' ','*',' ',' ','*','*','*',' ',' ','*',' ','*',' ','*',' ','*'},
    {'*',' ','*',' ',' ',' ',' ',' ',' ',' ','*','*',' ','*',' ',' ',' ',' ','*',' ',' ',' ','*',' ',' ','*',' ','*',' ','*'},
    {'*',' ',' ','*','*','*','*',' ','*',' ',' ',' ',' ',' ',' ','*','*','*',' ',' ','*','*',' ','*',' ','*',' ','*',' ','*'},
    {'*','*','*',' ',' ',' ','*','*',' ',' ','*','*','*',' ','*','*',' ',' ',' ','*',' ',' ','*',' ',' ',' ',' ',' ',' ','*'},
    {'*',' ',' ','*',' ','*',' ',' ',' ','*',' ',' ','*',' ',' ','*',' ','*','*','*',' ','*',' ','*','*','*','*','*',' ','*'},
    {'*',' ','*',' ',' ',' ',' ','*',' ',' ',' ',' ',' ','*',' ',' ',' ','*',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ','*'},
    {'*',' ',' ',' ',' ','*','*',' ','*','*','*',' ',' ',' ','*','*','*','*','*','*','*',' ',' ','*','*',' ','*','*',' ','*'},
    {'*',' ',' ','*','*','*','*',' ',' ',' ',' ','*','*',' ',' ',' ',' ',' ','*','*',' ',' ','*','*','*',' ',' ',' ','*','*'},
    {'*',' ','*',' ',' ',' ',' ',' ','*','*',' ',' ','*','*','*','*','*',' ',' ','*',' ',' ',' ',' ',' ',' ','*','*','*','*'},
    {'*',' ',' ',' ','*',' ','*',' ',' ',' ','*',' ',' ',' ',' ',' ','*','*','*',' ','*','*',' ','*',' ',' ',' ',' ',' ','*'},
    {'*',' ','*',' ','*','*',' ','*',' ','*','*',' ','*','*','*',' ','*',' ','*',' ',' ',' ','*','*','*','*',' ','*',' ','*'},
    {'*','*',' ',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ',' ',' ','*',' ',' ',' ',' ',' ',' ','*',' ','*'},
    {'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',' ','*'}
};
//小人当前在地图上的行坐标
int persenCurrentLine = 1;
//小人当前在地图上的列坐标.
int personCurrentColumn =1;
/**
*  打印游戏地图
*/
void showgameMap();

/**
*  接收小人的前进方向
*
*  @return 小人的前进方向
*/
char receivingDirection();

/**
*  使小人向上移动
*/
void personMoveUp();

/**
*  使小人向下移动
*/
void personMoveDown();

/**
*  使小人向左移动
*/
void personMoveLeft();

/**
*  使小人向右移动
*/
void personMoveRight();




9 个回复

倒序浏览
int main()
{
    while (1) {
        
        system("clear");//清空终端,好像没什么用
        
        showgameMap();//打印游戏
        
        if(persenCurrentLine == ENDLINE && personCurrentColumn == ENDCOLUMN){//是否通关
            printf("=======恭喜你通关成功=======\n");
            break;
        }
        else
        {
            switch (receivingDirection()) {//接收小人的前进方向并判断方向
                    
                case 'w':
                case 'W':
                    personMoveUp();//使小人向上移动
                    break;
                    
                case 's':
                case 'S':
                    personMoveDown();//使小人向下移动
                    break;
                    
                case 'a':
                case 'A':
                    personMoveLeft();//使小人向左移动
                    break;
                    
                case 'd':
                case 'D':
                    personMoveRight();//使小人向右移动
                    break;
                    
                case 'q':
                case 'Q':
                    printf("=======真菜!!!=======\n");
                    return 0;//退出游戏
                    
            }
        }
    }
    return 0;
}

void showgameMap()// 打印游戏地图
{
    for (int i = 0;i < LINE;i++)
    {
        for (int j = 0;j < COLUMN;j++)
        {
            printf("%c ",gameMap[i][j]);
        }
        printf("\n");
    }
}

char receivingDirection()//接收小人的前进方向
{
    printf("请输入小人的前进方向 'w'向上移动,'s'向下移动,'a'向左移动,'d'向右移动,'q'退出游戏:\n");
   
    return getchar();//接收小人的前进方向并返回给调用者
}

void personMoveUp()//使小人向上移动
{
    if (gameMap[persenCurrentLine-1][personCurrentColumn] == ' ') {//小人的下一步是路进入
        gameMap[persenCurrentLine][personCurrentColumn] = ' ';//使小人的当前坐标变成路(空格)
        gameMap[persenCurrentLine-1][personCurrentColumn] = '0';//使小人的下一步坐标变成0(小人)
        persenCurrentLine -= 1;
    }
}

void personMoveDown()//使小人向下移动
{
    if (gameMap[persenCurrentLine+1][personCurrentColumn] == ' ') {//小人的下一步是路进入
        gameMap[persenCurrentLine][personCurrentColumn] = ' ';//使小人的当前坐标变成路(空格)
        gameMap[persenCurrentLine+1][personCurrentColumn] = '0';//使小人的下一步坐标变成0(小人)
        persenCurrentLine += 1;
    }
}

void personMoveLeft()//使小人向左移动
{
    if (gameMap[persenCurrentLine][personCurrentColumn-1] == ' ') {//小人的下一步是路进入
        gameMap[persenCurrentLine][personCurrentColumn] = ' ';//使小人的当前坐标变成路(空格)
        gameMap[persenCurrentLine][personCurrentColumn-1] = '0';//使小人的下一步坐标变成0(小人)
        personCurrentColumn -= 1;
    }
}

void personMoveRight()//使小人向右移动
{
    if (gameMap[persenCurrentLine][personCurrentColumn+1] == ' ') {//小人的下一步是路进入
        gameMap[persenCurrentLine][personCurrentColumn] = ' ';//使小人的当前坐标变成路(空格
        gameMap[persenCurrentLine][personCurrentColumn+1] = '0';//使小人的下一步坐标变成0(小人)
        personCurrentColumn += 1;
    }
}
回复 使用道具 举报
main.c.zip (1.83 KB, 下载次数: 67)
这个是源代码 main.c 文件
回复 使用道具 举报
可以 谢谢分享了,我前一阵子刚学完二维数组的时候,也写了一个,自己又加了一个迷雾的功能.
回复 使用道具 举报
小海盗。 发表于 2016-9-11 01:24
可以 谢谢分享了,我前一阵子刚学完二维数组的时候,也写了一个,自己又加了一个迷雾的功能. ...

分享一下
回复 使用道具 举报
[C] 纯文本查看 复制代码
[align=left][color=rgb(0, 132, 0)][font=Menlo][size=15px]//[/size][/font][/color][/align][align=left][color=rgb(0, 132, 0)][font=Menlo][size=15px]//  main.c[/size][/font][/color][/align][align=left][color=rgb(0, 132, 0)][font=Menlo][size=15px]//  08-迷宫游戏------更新版[/size][/font][/color][/align][align=left][color=rgb(0, 132, 0)][font=Menlo][size=15px]//[/size][/font][/color][/align][align=left][color=rgb(0, 132, 0)][font=Menlo][size=15px]//  Created by Guogh on 16/9/2.[/size][/font][/color][/align][align=left][color=rgb(0, 132, 0)][font=Menlo][size=15px]//  Copyright © 2016年 **. All rights reserved.[/size][/font][/color][/align][align=left][color=rgb(0, 132, 0)][font=Menlo][size=15px]//[/size][/font][/color][/align]


#include <stdio.h>
#include <stdlib.h>
#pragma mark 定义地图长和宽
#define ROW 20
#define COL 20
#define KJD 4//地图可见度
//#define ROAD ' '
/**
 *  绘制地图
 */
#pragma  mark 绘制地图实现
void print_map(char map[ROW][COL]);

/**
 *  绘制迷雾地图
 */
void print_map1(char map[ROW][COL],int mapStartX,int mapStartY,int mapEndX,int mapEndY );



/**
 *  小人移动
 */
void Move(char map[ROW][COL],int oldX,int oldY,int newX,int newY);
/**
 *  处理地图迷雾
 *
 *
 */
void chuLiMap(int currentX,int currentY, int *mapStartX,int *mapStartY,int *mapEndX,int *mapEndY);

int main(int argc, const char * argv[]) {
    
    //定义地图
    char map[ROW][COL] = {
        {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
        {'#','0',' ',' ','#',' ',' ',' ','#','#',' ',' ',' ','#',' ','#',' ',' ',' ','#'},
        {'#',' ','#',' ',' ',' ','#','#','#','#',' ','#',' ','#',' ',' ',' ','#',' ','#'},
        {'#',' ','#',' ','#',' ','#',' ','#','#',' ','#',' ','#',' ','#','#',' ',' ','#'},
        {'#',' ','#',' ','#',' ','#',' ',' ','#',' ','#',' ','#',' ','#',' ',' ','#','#'},
        {'#',' ','#',' ','#',' ','#','#',' ',' ',' ','#',' ','#',' ',' ','#',' ','#','#'},
        {'#',' ',' ',' ','#',' ',' ',' ','#','#','#','#','#',' ','#',' ','#',' ',' ','#'},
        {'#','#','#','#','#','#','#',' ','#','#','#','#','#',' ','#',' ',' ','#',' ',' '},
        {'#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ','#','#',' ',' ','#','#'},
        {'#',' ',' ',' ',' ',' ',' ','#','#',' ','#',' ','#',' ','#',' ','#',' ','#','#'},
        {'#','#','#','#','#','#',' ','#','#',' ','#',' ','#',' ','#',' ','#',' ','#','#'},
        {'#',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#'},
        {'#',' ','#',' ','#',' ','#','#',' ',' ',' ',' ','#',' ','#',' ','#',' ','#','#'},
        {'#',' ','#',' ','#',' ','#','#',' ','#','#',' ','#',' ',' ',' ','#',' ','#','#'},
        {'#',' ','#',' ',' ',' ','#','#',' ','#','#',' ','#',' ','#',' ','#',' ',' ','#'},
        {'#',' ','#',' ','#',' ','#','#',' ','#',' ',' ','#',' ','#',' ',' ','#',' ','#'},
        {'#',' ','#',' ','#',' ',' ','#',' ','#',' ','#','#',' ','#','#',' ','#',' ','#'},
        {'#',' ','#','#','#','#','#','#',' ','#',' ','#','#',' ','#','#',' ','#',' ','#'},
        {'#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ','#'},
        {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
    };
    //0代表小人,定义小人所在下标
    /**
     *  往下代表x+1,往右代表y+1
     */
    int currentX = 1,currentY = 1;
    //地图迷雾  可见地图范围
    int mapStartX = 0;
    int mapStartY = 0;
    int mapEndX = 0;
    int mapEndY = 0;
    //
    //NULL;
    int currentMap = 0;
    //计算地图可见范围
    chuLiMap(currentX,currentY, &mapStartX, &mapStartY, &mapEndX, &mapEndY);
    
    //绘制地图
    //printf("%c\n",map[0][0]);
    //    for (int i = 0; i < ROW; i++) {
    //        puts(map[i]);
    //    }
    print_map1(map, mapStartX, mapStartY, mapEndX, mapEndY);
    
   // print_map(map);
    //提示用户开始游戏
    printf("请控制小人移动: a.A左  s.S下  d.D右  w.W上\n");
    char user;
    char road = ' ';
    //循环控制
    while (1) {
        rewind(stdin);
        scanf("%c",&user);
        switch (user) {
            case 'a':
            case 'A':
                if (map[currentX][currentY-1] == road){
                    Move(map,currentX,currentY,currentX,currentY-1);
                    currentY -= 1;
                }
                break;
            case 'd':
            case 'D':
                if (map[currentX][currentY+1] == road){
                    Move(map,currentX,currentY,currentX,currentY+1);
                    currentY += 1;
                }
                break;
            case 'w':
            case 'W':
                if (map[currentX-1][currentY] == road){
                    Move(map,currentX,currentY,currentX-1,currentY);
                    currentX -= 1;
                }
                break;
            case 's':
            case 'S':
                if (map[currentX+1][currentY] == road){
                    Move(map,currentX,currentY,currentX+1,currentY);
                    currentX += 1;
                }
                break;
            case 'q':
            case 'Q':
                //Move(map[ROW][COL],currentX,currentY,currentX,currentY-1);
                return 0;
                break;
            default:
                break;
        }
        //清空控制台
        system("clear");
        //处理地图迷雾
        if (currentMap == 2) {
            chuLiMap(currentX,currentY, &mapStartX, &mapStartY, &mapEndX, &mapEndY);
            currentMap = 0;
        }
        //chuLiMap(currentX,currentY, &mapStartX, &mapStartY, &mapEndX, &mapEndY);
        
        //重新绘制地图
        print_map1(map, mapStartX, mapStartY, mapEndX, mapEndY);
        currentMap++;
        //print_map(map);
        printf("\n");
        //判断胜利
        if (currentY == COL - 1) {
            printf("恭喜你游戏胜利!\n");
            break;
        }
        
    }
    
    
    
    
    return 0;
}
/**
 *  绘制地图
 */
void print_map(char map[ROW][COL]){
    for (int i =0; i < ROW; i++) {
        for (int j = 0; j < COL; j++) {
            printf("%c",map[i][j]);
        }
        printf("\n");
    }
}

/**
 *  小人移动
 */
void Move(char map[ROW][COL],int oldX,int oldY,int newX,int newY){
    //if (map[newX][newY] == ' ') {
    char temp = map[newX][newY];
    map[newX][newY] = map[oldX][oldY];
    map[oldX][oldY] = temp;
    
    //}
}
/**
 *  处理地图迷雾
 *
 *
 */
//void chuLiMap(int currentX,int currentY,int *mapStartX,int *mapStartY,int *mapEndX,int *mapEndY){
//    if (currentX - *mapStartX <= KJD) {
//        *mapStartX = 0;
//        *mapEndX = KJD * 2;
//    }else if(*mapEndX - currentX <= KJD){
//        *mapStartX = currentX - KJD;
//        *mapEndX = COL;
//    }else{
//        *mapStartX = currentX - KJD;
//        *mapEndX = currentX + KJD;
//    }
//    if (currentY - *mapStartY <= KJD) {
//        *mapStartY = 0;
//        *mapEndY = KJD * 2;
//    }else if(*mapEndY - currentY <= KJD){
//        *mapStartY = currentY - KJD;
//        *mapEndY = COL;
//    }else{
//        *mapStartY = currentY - KJD;
//        *mapEndY = currentY + KJD;
//    }
//
//}
void chuLiMap(int currentX,int currentY,int *mapStartX,int *mapStartY,int *mapEndX,int *mapEndY){
 
    *mapStartX = currentX - KJD;
    *mapEndX = currentX + KJD;
    
    *mapStartY = currentY - KJD;
    *mapEndY = currentY + KJD;
    if (*mapStartX < 0) {
        *mapStartX = 0;
        *mapEndX = KJD * 2;
    }
    //处理数组越界时
    if (*mapEndX > ROW) {
        *mapEndX = ROW;
        *mapStartX = ROW - KJD * 2;
    }
    if (*mapStartY < 0) {
        *mapStartY = 0;
        *mapEndY = KJD * 2;
    }
    if (*mapEndY > COL) {
        *mapEndY = COL;
        *mapStartY = COL - KJD * 2;
    }

}

/**
 *  绘制迷雾地图
 */
void print_map1(char map[ROW][COL],int mapStartX,int mapStartY,int mapEndX,int mapEndY ){
    
    for (int i = mapStartX; i < mapEndX; i++) {
        if (i == mapStartX) printf("============\n");
        printf("||");
        for (int j = mapStartY; j < mapEndY; j++) {
            printf("%c",map[i][j]);
        }
        printf("||\n");
        if (i == mapEndX - 1) printf("============\n");
    }
}

懒得修改整理 凑和这看吧 ....

回复 使用道具 举报
....新人还看不懂,先收藏再说
回复 使用道具 举报
hugin 中级黑马 2016-9-16 23:49:10
8#
足球骑士szw 发表于 2016-9-16 22:48
....新人还看不懂,先收藏再说

6666666666666666
回复 使用道具 举报
day2up 中级黑马 2016-9-16 23:54:58
9#
大神求carry呀
回复 使用道具 举报
Mr.Canon 来自手机 中级黑马 2016-9-16 23:56:34
10#
迷雾是什么?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马