嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
基于VC 的推箱子小游戏,只有一个关卡哦
// 推箱子.cpp: 定义控制台应用程序的入口点。
//
#include "stdio.h"
#include<stdlib.h >
#include "conio.h"
//0(lawn)、1(wall)、2(people)、3(box)、4(destination)
int map[8][8] = {
{1,1,1,1,1,1,1,1},
{1,1,2,0,1,1,1,1},
{1,1,0,3,0,0,1,1},
{1,1,1,0,1,0,0,1},
{1,4,1,0,1,0,0,1},
{1,4,3,0,0,1,0,1},
{1,4,0,0,0,3,0,1},
{1,1,1,1,1,1,1,1}};
void DrawMap() {
int i,j;
for (i = 0; i < 8; i ) {
for (j = 0; j < 8; j ) {
switch (map[i][j])
{
case 0:
printf(" ");
break;
case 1://1(wall)
//printf("oo");
printf("■");
break;
case 2://2(people)
//printf("yy");
printf("○");
break;
case 3://3(box)
printf("□");
break;
case 4://4(destination)
//printf("bb");
printf("☆");
break;
case 5://箱子在目的地
//printf("mm");
printf("□");
break;
case 6://人在目的地
//printf("yy");
printf("○");
break;
default:
break;
}
}
printf("\n");
}
}
void PlayGame() {
char input;
int i, j;
while (1) {
system("cls");//清空屏幕,包含头文件stdlib.h
DrawMap();
//获取人的位置
for (i = 0; i < 8;i ) {
for (j = 0; j < 8;j ) {
if (map[i][j] == 2|| map[i][j] == 6)
break;
}
if (map[i][j] == 2 || map[i][j] == 6)
break;
}
//用键盘输入,W向上,S向下,A向左,D向右
input = getch();//包含头文件conio.h
switch (input)
{
//上
case 'W':
case 'w':
//选择可以走的情况
if (map[i-1][j]==0) {//人的前面是空地
map[i - 1][j] = 2;
map[i][j] -= 2;
}
else if (map[i - 1][j] == 4) {//人的前面是目的地
map[i - 1][j] = 6;
map[i][j] -= 2;
}
else if (map[i - 1][j] == 3|| map[i - 1][j] == 5) {//人的前面是箱子,或者人的前面是“箱子在目的地上”
if (map[i - 2][j] == 0) {//人—>箱子/箱子在目的地上—>空地
map[i - 2][j] = 3;
map[i - 1][j] -= 1;
map[i][j] -= 2;
}
else if (map[i - 2][j] == 4) {//人—>箱子/箱子在目的地上—>目的地
map[i - 2][j] = 5;
map[i - 1][j] = 1;
map[i][j] -= 2;
}
}
break;
//下
case 'S':
case 's':
if (map[i 1][j] == 0) {
map[i 1][j] = 2;
map[i][j] -= 2;
}
else if (map[i 1][j] == 4) {
map[i 1][j] = 6;
map[i][j] -= 2;
}
else if (map[i 1][j] == 3 || map[i 1][j] == 5) {
if (map[i 2][j] == 0) {
map[i 2][j] = 3;
map[i 1][j] -= 1;
map[i][j] -= 2;
}
else if (map[i 2][j] == 4) {
map[i 2][j] = 5;
map[i 1][j] -= 1;
map[i][j] -= 2;
}
}
break;
//左
case 'A':
case 'a':
if (map[i][j-1] == 0) {
map[i][j-1] = 2;
map[i][j] -= 2;
}
else if (map[i][j-1] == 4) {
map[i][j-1] = 6;
map[i][j] -= 2;
}
else if (map[i][j-1] == 3 || map[i][j-1] == 5) {
if (map[i][j-2] == 0) {
map[i][j-2] = 3;
map[i][j-1] -= 1;
map[i][j] -= 2;
}
else if (map[i][j-2] == 4) {
map[i][j-2] = 5;
map[i][j-1] -= 1;
map[i][j] -= 2;
}
}
break;
//右
case 'D':
case 'd':
if (map[i][j 1] == 0) {
map[i][j 1] = 2;
map[i][j] -= 2;
}
else if (map[i][j 1] == 4) {
map[i][j 1] = 6;
map[i][j] -= 2;
}
else if (map[i][j 1] == 3 || map[i][j 1] == 5) {
if (map[i][j 2] == 0) {
map[i][j 2] = 3;
map[i][j 1] -= 1;
map[i][j] -= 2;
}
else if (map[i][j 2] == 4) {
map[i][j 2] = 5;
map[i][j 1] -= 1;
map[i][j] -= 2;
}
}
break;
default:
break;
}
}
}
int main()
{
PlayGame();
return 0;
}