c++迷宮小游戲


c++迷宮小游戲

一、總結

一句話總結:

顯示:根據map數組輸出圖像
走動:修改map數組的值,每走一步重新刷新一下圖像就好

 

1、如果走函數用z(),出現輸入s會向下走多步的情況,原因是什么?

 向下走兩層循環ij增加,而向下走i也是增加的,所以break跳出j后,照樣找到i不誤,所以會一直走到不能走為止
//走動函數 
void z(){
    char c=getch();
    //
    if(c=='s')
    {
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                //找到人物所在的位置 
                if(map[i][j]==3&&map[i+1][j]!=1)
                {
                    map[i+1][j]=3;//下面的位置變成人物所在的位置 
                    map[i][j]=0;//走過的地方變成0 
                    break;
                }
            
            }
        }
    }
    //
    if(c=='w')
    {
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==3&&map[i-1][j]!=1)
                {
                    map[i-1][j]=3;
                    map[i][j]=0;
                    break;
                }
            
            }
        }
    }
    //
    if(c=='a')
    {
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==3&&map[i][j-1]!=1)
                {
                    map[i][j-1]=3;
                    map[i][j]=0;
                    break;
                }
            
            }
        }
    }
    //
    if(c=='d')
    {
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==3&&map[i][j+1]!=1)
                {
                    map[i][j+1]=3;
                    map[i][j]=0;
                    break;
                }
            
            }
        }
    }
}

 

 

 

 

 

二、內容在總結中

截圖:

wsad分別對應上下左右

 

代碼:

#include<cstdio>
#include<windows.h>
#include<conio.h>
int map[10][10]={{1,1,1,1,3,1,1,1,1,1},
                {1,0,0,0,0,0,0,1,1,1},
                {1,0,0,1,0,1,0,0,1,1},
                {1,1,0,1,1,0,0,1,1,1},
                {1,0,0,0,1,0,0,0,0,1},
                {1,1,0,0,1,1,0,1,0,1},
                {1,1,0,1,0,1,1,0,0,1},
                {1,0,0,0,1,1,1,1,0,1},
                {1,1,1,1,1,1,1,1,2,1}};
int pos_y=0;//人物的y坐標 
int pos_x=4;//人物的x坐標
 
//打印地圖 
void jzmap()
{
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            if(map[i][j]==0) 
                printf("  ");//可走的地方 
            if(map[i][j]==1) 
                printf("");//障礙 
            if(map[i][j]==2)
                printf("!!");//出口 
            if(map[i][j]==3)
                printf("* ");//人物所在的位置 
        }
        printf("\n");
    }
}
//走動函數2 
void z2(){
    char c=getch();
        //
    if(c=='s')
    {
        //找到人物所在的位置 
        if(map[pos_y][pos_x]==3&&map[pos_y+1][pos_x]!=1)
        {
            map[pos_y+1][pos_x]=3;//下面的位置變成人物所在的位置 
            map[pos_y][pos_x]=0;//走過的地方變成0 
            pos_y++; 
        }
    }
    //
    if(c=='w')
    {
        if(map[pos_y][pos_x]==3&&map[pos_y-1][pos_x]!=1)
        {
            map[pos_y-1][pos_x]=3;
            map[pos_y][pos_x]=0;
            pos_y--;
        }
    }
    //
    if(c=='a')
    {
        if(map[pos_y][pos_x]==3&&map[pos_y][pos_x-1]!=1)
        {
            map[pos_y][pos_x-1]=3;
            map[pos_y][pos_x]=0;
            pos_x--;
        }
    }
    //
    if(c=='d')
    {
        if(map[pos_y][pos_x]==3&&map[pos_y][pos_x+1]!=1)
        {
            map[pos_y][pos_x+1]=3;
            map[pos_y][pos_x]=0;
            pos_x++;
        }
    }
} 
//走動函數 
void z(){
    char c=getch();
    int has_zou=0;
    //
    if(c=='s')
    {
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                //找到人物所在的位置 
                if(map[i][j]==3&&map[i+1][j]!=1&&!has_zou)
                {
                    has_zou=1;
                    map[i+1][j]=3;//下面的位置變成人物所在的位置 
                    map[i][j]=0;//走過的地方變成0 
                    break;
                }
            
            }
        }
    }
    //
    if(c=='w')
    {
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==3&&map[i-1][j]!=1&&!has_zou)
                {
                    has_zou=1;
                    map[i-1][j]=3;
                    map[i][j]=0;
                    break;
                }
            
            }
        }
    }
    //
    if(c=='a')
    {
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==3&&map[i][j-1]!=1&&!has_zou)
                {
                    has_zou=1;
                    map[i][j-1]=3;
                    map[i][j]=0;
                    break;
                }
            
            }
        }
    }
    //
    if(c=='d')
    {
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(map[i][j]==3&&map[i][j+1]!=1&&!has_zou)
                {
                    has_zou=1;
                    map[i][j+1]=3;
                    map[i][j]=0;
                    break;
                }
            
            }
        }
    }
}
void yx()
{
    jzmap();//重繪地圖 
    //z();
    z2();//走操作 
}
//結束 
bool js()
{
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            if(map[i][j]==map[9][8])
            {
                return 1;
            }
            else{
                return 0;
            }
        }
    }
}
int main()
{
    for(int i=0;i<100;i++)
    {
        system("cls");
        yx();
        if(js())
        {
            system("cls");
            printf("game over!");
            return 0;
        }
    }    
        return 0;
}

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM