数组:
// 2021.2.24数组.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//数组:一组数据的集合
//数组的定义:数组类型+数组名称[元素个数]
//int arrNum[5] = {};//数组元素之间用,隔开
//数组元素的访问:数组名称[索引]
//cout << arrNum[3] << endl;
//数组的遍历
// for (int i = 0; i < 5; i++)
// {
// cout << arrNum[i] << endl;
// }
//二维数组的定义:数组类型+数组名称[第一个维度的个数][第二个维度的个数]
//int arrNum[2][3] = {5,6};//5,6,0, 0,0,0
//二维数组元素的访问:数组名称[第一个维度的索引][第二个维度的索引]
//cout << arrNum[1][1] << endl;
int arrMap[15][20] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
//二维数组的遍历
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 20; j++)
{
if (1 == arrMap[i][j])
{
cout << "■";
}
else{
cout << " ";
}
}
cout << endl;
}
system("pause");
return 0;
}
推箱子:
// PushBox.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code)&0x8000?1:0)
//枚举类型
// enum 枚举类型名称
// {
// 枚举值,
// };
enum
{
//枚举值的命名规范:E_开头,大写,单词间用_隔开
//第一个枚举值如果没有给值,默认从0开始,后一个比前一个多1
E_MENU_START,// = 8,
E_MENU_SETTING,
E_MENU_EXIT
};
enum
{
E_GAME_MENU,
E_GAME_MAP
};
int _tmain(int argc, _TCHAR* argv[])
{
int arrMap[15][20] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
int nPlayerRow = 8;
int nPlayerCol = 9;
int nMenuState = 0;//用于控制箭头的位置的
int nGameState = 0;//用于控制界面的切换
while (true)
{
if (E_GAME_MENU == nGameState)
{
if (KEY_DOWN(VK_UP))
{
nMenuState++;
if (nMenuState > E_MENU_EXIT)
{
nMenuState = E_MENU_START;
}
}
else if (KEY_DOWN(VK_DOWN))
{
nMenuState--;
if (nMenuState < E_MENU_START)
{
nMenuState = E_MENU_EXIT;
}
}
else if (KEY_DOWN(VK_RETURN))
{
//
if (E_MENU_START == nMenuState)
{
nGameState = E_GAME_MAP;
}
}
cout << "■■■■■■■■■■■■■■■■■■■■" << endl;
cout << "■■■■■■■■■■■■■■■■■■■■" << endl;
if (E_MENU_START == nMenuState)
{
cout << "■■■ ->游戏开始 ■■■" << endl;
cout << "■■■ 游戏设置 ■■■" << endl;
cout << "■■■ 游戏退出 ■■■" << endl;
}
else if (E_MENU_SETTING == nMenuState)
{
cout << "■■■ 游戏开始 ■■■" << endl;
cout << "■■■ ->游戏设置 ■■■" << endl;
cout << "■■■ 游戏退出 ■■■" << endl;
}
else{
cout << "■■■ 游戏开始 ■■■" << endl;
cout << "■■■ 游戏设置 ■■■" << endl;
cout << "■■■ ->游戏退出 ■■■" << endl;
}
cout << "■■■■■■■■■■■■■■■■■■■■" << endl;
cout << "■■■■■■■■■■■■■■■■■■■■" << endl;
}
else if (E_GAME_MAP == nGameState)
{
if (KEY_DOWN(VK_UP))
{
nPlayerRow--;
}
else if (KEY_DOWN(VK_DOWN))
{
nPlayerRow++;
}
else if (KEY_DOWN(VK_LEFT))
{
nPlayerCol--;
}
else if (KEY_DOWN(VK_RIGHT))
{
nPlayerCol++;
}
//二维数组的遍历
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 20; j++)
{
//判断第i行第j列应该画什么
if (1 == arrMap[i][j])
{
cout << "■";
}
else if (i == nPlayerRow && j == nPlayerCol)
{
cout << "人";
}
else{
cout << " ";
}
}
cout << endl;
}
}
system("cls");//系统清屏
}
return 0;
}