C/C++做推箱子游戏(源码)

底层源码


		#define _CRT_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS
#include   
#include 
#include 
#include
#include 



IMAGE img_all[6];
int level=0;
int map[2][9][11] = {
{
    {0,1,1,1,1,1,1,1,1,1,0},  //0代表空地
    {0,1,0,0,0,1,0,0,0,1,0},  //1代表墙
    {0,1,0,4,4,4,4,4,0,1,0},  //3代表目的地
    {0,1,0,4,0,4,0,4,0,1,1},  //4代表箱子
    {0,1,0,0,0,0,0,0,4,0,1},  //5代表人 
    {1,1,0,1,1,1,1,0,4,0,1},
    {1,0,8,3,3,3,3,0,0,0,1},  //2 3 4 5 6 7 8 9 1 0
    {1,0,3,3,3,3,3,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1,0}
},

	{
    {0,1,1,1,1,1,1,1,1,1,0},  //0代表空地
    {0,1,0,0,0,1,0,0,0,1,0},  //1代表墙
    {0,1,0,4,4,4,4,4,0,1,0},  //3代表目的地
    {0,1,0,4,0,4,0,4,0,1,1},  //4代表箱子
    {0,1,0,0,0,0,0,0,4,0,1},  //5代表人 
    {1,1,0,1,1,1,1,0,4,0,1},
    {1,0,8,3,3,3,3,1,0,0,1},  //2 3 4 5 6 7 8 9 1 0
    {1,0,3,3,3,3,3,0,0,1,1},
    {1,1,1,1,1,1,1,1,1,1,0}
}
};

bool jude()
{


}
//绘制地图  //二维数组+switch()
void DrawMap()
{
   // 遍历二维数组  //0 打印空格  //1 墙   //3 目的地  //什么结构?
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 11; j++)
		{
            switch (map[0][i][j])
            {
            case 0:
                printf("  ");
                break;
            case 1:
                printf("■");
                break;
            case 3:
                printf("☆");
                break;
            case 4:
                printf("□");
                break;
            case 5:
                printf("♀");  //5人
                break;
            case 7:     //4 + 3  箱子在目的地中
                printf("★");
                break;
            case 8:     // 5 + 3  人在目的地当中   人?
                printf("♀");
                break;
            }
        }
        printf("\n");
    }
}


void PlayGame()
{
    int r, c;  //人的下标  //
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 11; j++)
        {
            if (map[0][i][j] == 5||map[0][i][j]==8)   //i j 人的下标?
            {
                r = i;
                c = j;
            }
        }
    }

    char ch;  //字符变量
    ch = _getch();  //键盘的输入保存到字符中
    // getch()     getchar()  接收键盘字符
     //getch()直接接收 不显示回文   getchar()显示回文可以修改 enter键结束

    //根据不同的按键  改变不同的值. 分支.
    switch (ch)
    {
    case 'W':  //W A S D方向   72  80  75  77 虚拟键值 ascii  windowVK_UP VK_TAB VK_RETUNE
    case 'w':
    case 72:
        if (map[0][r - 1][c] == 0|| map[0][r - 1][c] == 3) 
        {
            map[0][r - 1][c] += 5;
            map[0][r][c] -= 5;
        }
        else if (map[0][r - 1][c] == 4 || map[0][r - 1][c] == 7)
        {
            if (map[0][r - 2][c] == 0 || map[0][r - 2][c] == 3)
            {
                map[0][r - 2][c] += 4;
                map[0][r - 1][c] += 1;
                map[0][r][c] -= 5;
            }
        }



        break;

    case 'S':  //enter按键的作用  确认 返回
    case 's':
    case 80:
        if (map[0][r + 1][c] == 0 || map[0][r + 1][c] == 3)
        {
            map[0][r + 1][c] += 5;
            map[0][r][c] -= 5;
        }
        else if (map[0][r + 1][c] == 4 || map[0][r+ 1][c] == 7)
        {
            if (map[0][r + 2][c] == 0 || map[0][r + 2][c] == 3)
            {
                map[0][r + 2][c] += 4;
                map[0][r + 1][c] += 1;
                map[0][r][c] -= 5;
            }
        }
        break;

    case 'A':
    case 'a':
    case 75:
        if (map[0][r ][c - 1] == 0 || map[0][r ][c - 1] == 3)
        {
            map[0][r ][c - 1] += 5;
            map[0][r][c] -= 5;
        }
        else if (map[0][r][c - 1] == 4 || map[0][r][c - 1] == 7)
        {
            if (map[0][r ][c - 2] == 0 || map[0][r ][c - 2] == 3)
            {
                map[0][r ][c - 2] += 4;
                map[0][r ][c - 1] += 1;
                map[0][r][c] -= 5;
            }
        }
        break;

    case 'D':
    case 'd':
    case 77:
        if (map[0][r][c + 1] == 0 || map[0][r][c + 1] == 3)
        {
            map[0][r][c + 1] += 5;
            map[0][r][c] -= 5;
        }
        else if (map[0][r][c + 1] == 4 || map[0][r][c + 1] == 7)
        {
            if (map[0][r][c + 2] == 0 || map[0][r][c + 2] == 3)
            {
                map[0][r][c + 2] += 4;
                map[0][r][c + 1] += 1;
                map[0][r][c] -= 5;
            }
        }
        break;
    }
}



int main()  //主函数
{

    while (1)
    {
      system("cls");
        DrawMap();
		if(jude())
		{
		level++;
		if(level>2)
		{printf("通关");
		_getch();
		break;
		
		}
		
		}
        PlayGame();
    }

    return 0;
}

 效果:C/C++做推箱子游戏(源码)_第1张图片

 

推箱子 C/C++做推箱子游戏(源码)_第2张图片

 

贴图(easyx) 

#define _CRT_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS
#include   
#include 
#include 
#include
IMAGE img_all[6];
int map[9][11] = {
    {0,1,1,1,1,1,1,1,1,1,0},  //0代表空地
    {0,1,3,0,0,1,0,0,3,1,0},  //1代表墙
    {0,1,0,4,0,0,4,4,0,1,0},  //3代表目的地
    {0,1,0,4,0,0,0,4,0,1,1},  //4代表箱子
    {0,1,0,0,0,0,0,0,0,0,1},  //5代表人 
    {1,1,0,1,1,1,1,0,4,0,1},
    {1,0,8,3,3,3,3,0,0,0,1},  //2 3 4 5 6 7 8 9 1 0
    {1,0,3,3,3,3,3,0,0,1,1},
    {1,1,1,1,1,1,1,1,1,1,0} };

void loadimg()
{
//IMAG是一个类型,用来保存图片
for(int i=0;i<6;i++)
//1>c:\users\栗\documents\visual studio 2010\projects\推箱子终极版\推箱子终极版\tuu.cpp(24): warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
{//
	char file[30]="";//字符串初始化
	sprintf(file,"./images/%d.bmp",i);//打印六张图片,以字符串的形式
loadimage(img_all+i,file,64,64);
putimage(i*64,0,img_all+i);
}
}
//绘制地图  //二维数组+switch()
void DrawMap()
{
    //遍历二维数组  //0 打印空格  //1 墙   //3 目的地  //什么结构?
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 11; j++)
        {int x=j*64;
		int y=i*64;
            //if  else  switch
            switch (map[i][j])
            {
            case 0:
            putimage(x,y,img_all);
                break;
            case 1:
           putimage(x,y,img_all+1);
                break;
            case 3:
           putimage(x,y,img_all+2);
                break;
            case 4:
            putimage(x,y,img_all+3);
                break;
            case 5:
              putimage(x,y,img_all+4);
                break;
            case 7:     //4 + 3  箱子在目的地中
             putimage(x,y,img_all+5);
                break;
            case 8:     // 5 + 3  人在目的地当中   人?
             putimage(x,y,img_all+4);
                break;
            }
        }
        printf("\n");
    }
}


void PlayGame()
{
    int r, c;  //人的下标  //
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 11; j++)
        {
            if (map[i][j] == 5||map[i][j]==8)   //i j 人的下标?
            {
                r = i;
                c = j;
            }
        }
    }

    char ch;  //字符变量
    ch = _getch();  //键盘的输入保存到字符中
    // getch()     getchar()  接收键盘字符
    // getch()直接接收 不显示回文   getchar()显示回文可以修改 enter键结束

    //根据不同的按键  改变不同的值. 分支.
    switch (ch)
    {
    case 'W':  //W A S D方向   72  80  75  77 虚拟键值 ascii  windowVK_UP VK_TAB VK_RETUNE
    case 'w':
    case 72:
        if (map[r - 1][c] == 0|| map[r - 1][c] == 3) 
        {
            map[r - 1][c] += 5;
            map[r][c] -= 5;
        }
        else if (map[r - 1][c] == 4 || map[r - 1][c] == 7)
        {
            if (map[r - 2][c] == 0 || map[r - 2][c] == 3)
            {
                map[r - 2][c] += 4;
                map[r - 1][c] += 1;
                map[r][c] -= 5;
            }
        }



        break;

    case 'S':  //enter按键的作用  确认 返回
    case 's':
    case 80:
        if (map[r + 1][c] == 0 || map[r + 1][c] == 3)
        {
            map[r + 1][c] += 5;
            map[r][c] -= 5;
        }
        else if (map[r + 1][c] == 4 || map[r+ 1][c] == 7)
        {
            if (map[r + 2][c] == 0 || map[r + 2][c] == 3)
            {
                map[r + 2][c] += 4;
                map[r + 1][c] += 1;
                map[r][c] -= 5;
            }
        }
        break;

    case 'A':
    case 'a':
    case 75:
        if (map[r ][c - 1] == 0 || map[r ][c - 1] == 3)
        {
            map[r ][c - 1] += 5;
            map[r][c] -= 5;
        }
        else if (map[r][c - 1] == 4 || map[r][c - 1] == 7)
        {
            if (map[r ][c - 2] == 0 || map[r ][c - 2] == 3)
            {
                map[r ][c - 2] += 4;
                map[r ][c - 1] += 1;
                map[r][c] -= 5;
            }
        }
        break;

    case 'D':
    case 'd':
    case 77:
        if (map[r][c + 1] == 0 || map[r][c + 1] == 3)
        {
            map[r][c + 1] += 5;
            map[r][c] -= 5;
        }
        else if (map[r][c + 1] == 4 || map[r][c + 1] == 7)
        {
            if (map[r][c + 2] == 0 || map[r][c + 2] == 3)
            {
                map[r][c + 2] += 4;
                map[r][c + 1] += 1;
                map[r][c] -= 5;
            }
        }
        break;
    }
}

//
int main()  //主函数
{
	initgraph(11*64,11*64);//1是可以看见控制台
	loadimg();
    while (1)
    {
      // system("cls");
        DrawMap();
        PlayGame();
    }

    return 0;
}

 效果图:C/C++做推箱子游戏(源码)_第3张图片

 

 素材:

 

你可能感兴趣的:(c++,c语言,游戏)