// 1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <stdio.h>//■
#include <cstdlib>
#include <conio.h>
using std::cout;
const int row = 12;
const int col = 13;
int x = 1;
int y = 11;
int map[row][col] = {
{2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,0,0,0,0,0,0,5,2},
{2,0,0,0,0,0,0,0,0,0,3,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,1,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2},
};
void draw() {
int i;
for (i = 0; i < 12; i++)
{
for (int j = 0; j < 13; j++)
{
switch (map[i][j])
{
case 0:
{
cout << " ";//0代表可以走的空格
break;
}
case 1:
{
cout << "★";//1代表箱子,用我就★代替了
break;
}
case 2:
{
cout << "■";//2代表了墙,不能逾越
break;
}
case 3:
{
cout << "☆";
break;
}
case 5:
{
cout << "♀";//5代表了人,可移动
break;
}
}
}
cout << "\n";
}
}
void move(int _x, int _y) {
if (map[x + _x][y + _y] == 2 || map[x + _x][y + _y] == 3 ||
map[x + _x][y + _y] == 5) {//判断边界人不能走出墙外
return;
}
if (map[x + _x][y + _y] == 1) {
if (map[x + 2 * _x][y + 2 * _y] == 2) {
return;
}
map[x + _x][y + _y] = 0;
map[x + 2 * _x][y + 2 * _y] = 1;
move(_x, _y);
return;
}
map[x][y] = 0;
x += _x;
y += _y;
map[x][y] = 5;
draw();
}
void step(char o) {
switch (o) {
case 'w': {//就像cf,cs那样,wsad代表上下左右移动
move(-1, 0);
break;
}
case 's': {
move(1, 0);
break;
}
case 'd': {
move(0, 1);
break;
}
case 'a': {
move(0, -1);
break;
}
}
}
int main()
{
while (true) {
draw();//根据二维数组的值来进行绘图
step(_getch());//直接获取屏幕输入
system("cls");//清屏来实现动画效果
}
}
推箱子游戏的C++实现
最新推荐文章于 2022-02-16 19:34:10 发布