推箱子代码(推箱子代码c语言编程)
admin 发布:2022-12-19 23:43 129
本篇文章给大家谈谈推箱子代码,以及推箱子代码c语言编程对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
求一些C语言小游戏的源代码,谢谢
“推箱子”C代码:
#include stdio.h
#include conio.h
#includestdlib.h
#includewindows.h
int m =0; //m代表第几关
struct maps{short a[9][11]; };
struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5关,每关9行11列
0,1,1,1,1,1,1,1,0,0,0,
0,1,0,0,0,0,0,1,1,1,0,
1,1,4,1,1,1,0,0,0,1,0, //0空地,1墙
1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人
1,0,3,3,1,0,4,0,1,1,0, //3是目的地
1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)
0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,5,0,1,1,1,0,0,0,
0,0,1,0,4,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,3,1,0,1,0,0,1,0,0,
0,1,3,4,0,0,1,0,1,0,0,
0,1,3,0,0,0,4,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,0,
0,0,1,1,0,0,1,0,5,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,4,0,4,0,4,0,1,0,
0,0,1,0,4,1,1,0,0,1,0,
1,1,1,0,4,0,1,0,1,1,0,
1,3,3,3,3,3,0,0,1,0,0,
1,1,1,1,1,1,1,1,1,0,0,
0,1,1,1,1,1,1,1,1,1,0,
0,1,0,0,1,1,0,0,0,1,0,
0,1,0,0,0,4,0,0,0,1,0,
0,1,4,0,1,1,1,0,4,1,0,
0,1,0,1,3,3,3,1,0,1,0,
1,1,0,1,3,3,3,1,0,1,1,
1,0,4,0,0,4,0,0,4,0,1,
1,0,0,0,0,0,1,0,5,0,1,
1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,
0,1,1,1,0,0,0,0,1,0,0,
1,1,3,0,4,1,1,0,1,1,0,
1,3,3,4,0,4,0,0,5,1,0,
1,3,3,0,4,0,4,0,1,1,0,
1,1,1,1,1,1,0,0,1,0,0,
0,0,0,0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 };
void DrMap( ) //绘制地图
{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),cursor_info);
printf("\n\n \t\t\b推箱子");
printf("\n \t");
for (int i = 0; i 9; i++)
{for (int j = 0; j 11; j++)
{switch (map[m].a[i][j])
{case 0: printf(" "); break;
case 1: printf("■"); break;
case 3: printf("◎");break;
case 4: printf("□"); break;
case 5: printf("♀"); break; //5是人
case 7: printf("□"); break; //4 + 3箱子在目的地中
case 8: printf("♀");break; // 5 + 3人在目的地中
}
}
printf("\n\t");
}
}
void gtxy(int x, int y) //控制光标位置的函数
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void start( ) //开始游戏
{ int r, c; //人的下标
for (int i = 0; i 9; i++)
{ for (int j = 0; j 11; j++)
{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下标
}
char key;
key = getch( );
switch (key)
{case 'W':
case 'w':
case 72:
if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)
{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置输出字符
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)
{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)
{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;
map[m]. a [r][c] -= 5; }
} break;
case 'S':
case 's':
case 80:
if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)
{ gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)
{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)
{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'A':
case 'a':
case 75:
if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)
{ gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)
{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)
{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'D':
case 'd':
case 77:
if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)
{ gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)
{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)
{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
}
}
int ifwan( ) //是否完成(1是0否)
{ if(m==0){if(map[m].a[5][2]==7 map[m].a[5][3]==7
map[m].a[6][2]==7 map[m].a[6][3]==7) return 1;}
if(m==1){if(map[m].a[5][2]==7 map[m].a[6][2]==7
map[m].a[7][2]==7) return 1;}
if(m==2){if(map[m].a[7][1]==7 map[m].a[7][2]==7 map[m].a[7][3]==7
map[m].a[7][4]==7 map[m].a[7][5]==7) return 1;}
if(m==3){if(map[m].a[4][4]==7 map[m].a[4][5]==7 map[m].a[4][6]==7
map[m].a[5][4]==7 map[m].a[5][5]==7 map[m].a[5][6]==7) return 1;}
if(m==4){if(map[m].a[3][2]==7 map[m].a[4][1]==7 map[m].a[4][2]==7
map[m].a[5][1]==7 map[m].a[5][2]==7) return 1;}
return 0;
}
int main( ) //主函数
{ while (1)
{ system("cls");
DrMap( );
while (1)
{ start( );
if(ifwan()){printf("\007");break;} //完成后响铃
}
m+=1;
}
return 0;
}
求个推箱子的C++代码 要有注解 800到1000行
//*******************************************************
Sokoban.h: 类定义 Sokoban.c: 类成员函数实现
Use_Sokoban.c: 主函数
请用VC6(别编译器的也行)先运行Use_Sokoban.c文件,要编译该文件一下,
再点Project- Add To Project- Files 选择Sokoban.c文件,
即将Sokoban.c加载到工程里,最后运行就OK拉。
//*******************************************************
Sokoban.h
//*******************************************************
#ifndef SOKOBAN_H_ //防止文件重复包含
#define SOKOBAN_H_
#include queue
using std::queue;
//每一步的数据类型
struct node
{
int bx, by; //箱子的坐标
int px, py; //人的坐标
};
//推箱子类
class Sokoban
{
private:
enum {L = 15, H = 7};
char GameMap[H][L]; //地图
int Pex, Pey; //人的位置
int Boxx, Boxy; //箱子的位置
int Succeed, Prove; //是否成功到目的地, 是否可玩性
int dx[4], dy[4]; //方向数组
protected:
char Empty;
char People;
char Box;
char Block;
char Target;
int dir; //记录按键方向
node s, e;
public:
Sokoban(); //构建函数
~Sokoban() {} //析构函数,即为inline
//地图初始化函数
void Initial();
//箱子路劲验证函数,参数为箱子坐标(bx,by),人坐标(px,py)
void Box_Bfs(int bx, int by, int px, int py);
//人路劲验证函数,人所到的目的地(ex,ey)
bool People_Bfs(int ex, int ey);
//地图刷新函数
void Show();
//按键判断函数
void Button();
//箱子人移动函数
void Move();
//验证越界函数
bool Check(int x, int y);
};
#endif
//*******************************************************
Sokoban.cpp
//*******************************************************
#include "Sokoban.h"
#include cstring
#include cstdlib
#include ctime
#include iostream
#include conio.h
using std::cout;
using std::endl;
Sokoban::Sokoban() //构建函数即对变量初始化
{
dir = -1;
Succeed = Prove = 0;
memset(GameMap, '.', sizeof(GameMap));
Empty = '.';
People = 'P';
Box = '#';
Block = '*';
Target = 'T';
//方向依次为上右下左
dx[0] = -1; dx[1] = 0; dx[2] = 1; dx[3] = 0;
dy[0] = 0; dy[1] = 1; dy[2] = 0; dy[3] = -1;
//随机种子,使程序每次运行时所产生的随机数不同
srand(time(0));
}
//地图初始化函数
void Sokoban::Initial()
{
int count = 0, x, y;
//对地图中随机产生25个阻碍物
while(count != 25)
{
x = rand()%H;
y = rand()%L;
if(GameMap[x][y] == Empty)
{
GameMap[x][y] = Block;
count++;
}
}
while(true) //随机产生人开始的位置
{
x = rand()%H;
y = rand()%L;
if(GameMap[x][y] == Empty)
{
GameMap[x][y] = People;
Pex = x;
Pey = y;
break;
}
}
while(true) //随机产生箱子开始的位置
{
x = rand()%H;
y = rand()%L;
//不让箱子在地图的边界处
if(GameMap[x][y] == Empty x != 0 y != 0
x != H-1 y != L-1)
{
GameMap[x][y] = Box;
Boxx = x;
Boxy = y;
break;
}
}
while(true) //随机产生目标的位置
{
x = rand()%H;
y = rand()%L;
if(GameMap[x][y] == Empty)
{
GameMap[x][y] = Target;
break;
}
}
//对游戏地图检查是否可将箱子推到目的地,即判断游戏可玩性
Sokoban::Box_Bfs(Boxx, Boxy, Pex, Pey);
//如游戏不可玩,即再随机产生地图
if(!Prove)
{
memset(GameMap, '.', sizeof(GameMap));
Sokoban::Initial();
}
else
Sokoban::Show();
}
//箱子路劲验证函数
//用BFS算法对箱子验证是否可到目的地
void Sokoban::Box_Bfs(int bx, int by, int px, int py)
{
queuenode_Box; //创建箱子队列
//visit对上一步走到下一步的记录,防止箱子走重复路劲
//visit[i][j][z][k]表示箱子从点(i,j)到点(z,k)
//visit[][][][]为0时表示为走过,1时表示已走过
int visit[H][L][H][L];
memset(visit, 0, sizeof(visit)); //visit数组初始化
s.bx = bx; s.by = by; //将起始的箱子、人位置放入队列
s.px = px; s.py = py;
_Box.push(s);
int pe_x, pe_y;
while(!_Box.empty()) //队列为空时跳出
{
s = _Box.front();
_Box.pop();
if(GameMap[s.bx][s.by] == Target) //到达目的地
{
Prove = 1;
break;
}
for(int i = 0; i 4; i++)
{
e.bx = s.bx + dx[i]; e.by = s.by + dy[i];
switch(i) //人推箱子的位置
{
case 0: pe_x = s.bx + dx[2]; pe_y = s.by + dy[2]; break;
case 1: pe_x = s.bx + dx[3]; pe_y = s.by + dy[3]; break;
case 2: pe_x = s.bx + dx[0]; pe_y = s.by + dy[0]; break;
case 3: pe_x = s.bx + dx[1]; pe_y = s.by + dy[1]; break;
}
//验证箱子和人的位置的合法性
if(!Check(e.bx, e.by) || !Check(pe_x, pe_y)
|| GameMap[e.bx][e.by] == Block || GameMap[pe_x][pe_y] == Block
|| visit[s.bx][s.by][e.bx][e.by] )
continue;
//如人可推箱子即进入队列
if(Sokoban::People_Bfs(pe_x, pe_y))
{
//保存人推箱子后的位置
e.px = pe_x; e.py = pe_y;
_Box.push(e);
visit[s.bx][s.by][e.bx][e.by] = 1; //箱子路劲的标记
}
}
}
}
//人路劲验证函数
//用BFS算法对人验证是否可推箱子
bool Sokoban::People_Bfs(int ex, int ey)
{
queuenode_People;
node t, end;
//visit数组对人的路劲进行标记,0为未走过,1为走过
int visit[H][L];
//visit数组初始化为0
memset(visit, 0, sizeof(visit));
t.px = s.px; t.py = s.py; //人初始位置进入队列
_People.push(t);
visit[t.px][t.py] = 1;
while(!_People.empty()) //对立为空时跳出
{
t = _People.front();
_People.pop();
if(t.px == ex t.py == ey) //人可到达(ex,ey)该点
return 1;
for(int i = 0; i 4; i++)
{
end.px = t.px + dx[i]; end.py = t.py + dy[i];
//检查人的位置合法性
if(!Check(end.px, end.py) || GameMap[end.px][end.py] == Block
|| GameMap[end.px][end.py] == Box || visit[end.px][end.py])
continue;
//进入队列
_People.push(end);
visit[end.px][end.py] = 1; //记录
}
}
return 0;
}
//地图刷新函数
void Sokoban::Show()
{
int i, j;
while(true)
{
//每半秒刷新一次地图
clock_t s = clock();
while(clock() - s CLOCKS_PER_SEC/2)
;
//先判断按键在移动
Sokoban::Button();
Sokoban::Move();
system("cls");
for(i = 0; i H; i++)
{
for(j = 0; j L; j++)
cout GameMap[i][j];
cout endl;
}
cout endl;
cout "\n**********************************" endl;
cout "* 小小C++语言推箱子游戏 *" endl;
cout "* 游戏规则: *" endl;
cout "* P: 人 #: 箱子 *" endl;
cout "* *: 障碍物 T: 目的地 *" endl;
cout "**********************************" endl;
cout "* 每次游戏地图不一样 *" endl;
cout "* 人将箱子推到目的地即过关 *" endl;
cout "*所给地图,一定可过关,请慎重移箱子*" endl;
cout "* 箱子无路可走时,机器不会提示 *" endl;
cout "**********************************" endl;
//箱子成功到达目的地
if(Succeed)
{
cout "\n ^_^ _" endl;
cout "恭喜过关成功! 再来一盘吧" endl;
getchar();
break;
}
}
}
//按键判断函数
void Sokoban::Button()
{
int key;
if(kbhit() != 0) //检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
{
while(kbhit() != 0) //可能存在多个按键,要全部取完,以最后一个为主
key = getch(); //将按键从控制台中取出并保存到key中
switch(key)
{
//上
case 72: dir = 0;
break;
//右
case 77: dir = 1;
break;
//下
case 80: dir = 2;
break;
//左
case 75: dir = 3;
break;
}
}
}
//人推箱子移动函数
void Sokoban::Move()
{
int x, y;
//有按键时
if(dir != -1)
{
//人所推向的位置坐标
x = Pex + dx[dir]; y = Pey + dy[dir];
//人所推位置为空,即走向该位置
if(Check(x, y) GameMap[x][y] == '.')
{
GameMap[Pex][Pey] = '.'; //人的位置改变
GameMap[x][y] = 'P';
Pex = x; Pey = y;
dir = -1; //按键记录为无即-1
}
else //人所推位置为箱子,即将箱子推向该方向的前面这点
if(Check(x, y) GameMap[x][y] == '#'
Check(x+dx[dir], y+dy[dir])
GameMap[ x+dx[dir] ][ y+dy[dir] ] == '.')
{
GameMap[Boxx][Boxy] = '.'; //箱子的位置改变
GameMap[x+dx[dir] ][ y+dy[dir] ] = '#';
Boxx = x + dx[dir]; Boxy = y + dy[dir];
GameMap[Pex][Pey] = '.'; //人的位置改变
GameMap[x][y] = 'P';
Pex = x; Pey = y;
dir = -1;
}
else //将箱子推向该方向的前面这点为目的地
if(Check(x, y) GameMap[x][y] == '#'
Check(x+dx[dir], y+dy[dir])
GameMap[ x+dx[dir] ][ y+dy[dir] ] == 'T')
{
GameMap[Boxx][Boxy] = '.'; //箱子的位置改变
GameMap[x+dx[dir] ][ y+dy[dir] ] = '#';
Boxx = x + dx[dir]; Boxy = y + dy[dir];
GameMap[Pex][Pey] = '.'; //人的位置改变
GameMap[x][y] = 'P';
Pex = x; Pey = y;
dir = -1;
Succeed = 1; //记录成功到达目的地
}
}
}
//判断越界情况
bool Sokoban::Check(int x, int y)
{
if(x 0 || x = H || y 0 || y = L)
return 0;
else
return 1;
}
//*************************************************
Use_Sokoban.cpp
//*************************************************
#include iostream
#include "Sokoban.h"
using namespace std;
int main()
{
Sokoban s;
s.Initial();
return 0;
}
//*************************************************
c语言推箱子代码
Here you are!
编译通过。
/* 推箱子游戏 */
#include dos.h
#include stdio.h
#include stdlib.h
#include ctype.h
#include conio.h
#include bios.h
#include alloc.h
/* 定义二维数组ghouse来记录屏幕上各点的状态,
其中:0表示什么都没有,'b'表示箱子,'w'表示墙壁,'m'表示目的地,'i'表示箱子在目的地。 */
char ghouse[20][20];
/* 以下函数为直接写屏函数,很酷的函数哦!是我朋友告诉我的。 */
char far *screen=(char far* )0xb8000000;
void putchxy(int y,int x,char ch,char fc,char bc)
{
screen[(x*160)+(y1)+0]=ch;
screen[(x*160)+(y1)+1]=(bc*16)+fc;
}
/* 定义判断是否胜利的数据结构 */
typedef struct winer {
int x,y;
struct winer *p;
}winer;
/* 箱子位置的数据结构 */
typedef struct boxs {
int x,y;
struct boxs *next;
}boxs;
/* 在特定的坐标上画墙壁并用数组记录状态的函数 */
void printwall(int x,int y)
{
putchxy(y-1,x-1,219,MAGENTA,BLACK);
ghouse[x][y]='w';
}
/* 在特定的坐标上画箱子并用数组记录状态的函数 */
void printbox(int x,int y)
{
putchxy(y-1,x-1,10,WHITE,BLACK);
ghouse[x][y]='b';
}
/* 在特定的坐标上画目的地并用数组记录状态的函数 */
void printwhither1(int x,int y,winer **win,winer **pw)
{
winer *qw;
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer* )malloc(sizeof(winer));
(*pw)-x=x;(*pw)-y=y;(*pw)-p=NULL;
}
else
{
qw=(winer* )malloc(sizeof(winer));
qw-x=x;qw-y=y;(*pw)-p=qw;(*pw)=qw;qw-p=NULL;
}
}
/* 在特定的坐标上画目的地并用数组记录状态的函数 */
void printwhither(int x,int y)
{
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
}
/* 在特定的坐标上画人的函数 */
void printman(int x,int y)
{
gotoxy(y,x);
_AL=02;_CX=01;_AH=0xa;
geninterrupt(0x10);
}
/* 在特定的坐标上画箱子在目的地上并用数组记录状态的函数 */
void printboxin(int x,int y)
{
putchxy(y-1,x-1,10,YELLOW,BLACK);
ghouse[x][y]='i';
}
/* 初始化函数,初始化数组和屏幕 */
void init()
{
int i,j;
system("cls");
for(i=0;i20;i++)
for(j=0;j20;j++)
ghouse[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome to push box world!");
gotoxy(40,6);
printf("Press up,down,left,right to play.");
gotoxy(40,8);
printf("Press Esc to quit it.");
gotoxy(40,10);
printf("Press space to reset the game.");
gotoxy(40,12);
printf("April 30th 2004.");
}
/* 第一关的图象初始化 */
winer *inithouse1()
{
int x,y;
winer *win=NULL,*pw;
gotoxy(8,2);
printf("Level No.1");
for(x=1,y=5;y=9;y++)
printwall(x+4,y+10);
for(y=5,x=2;x=5;x++)
printwall(x+4,y+10);
for(y=9,x=2;x=5;x++)
printwall(x+4,y+10);
for(y=1,x=3;x=8;x++)
printwall(x+4,y+10);
for(x=3,y=3;x=5;x++)
printwall(x+4,y+10);
for(x=5,y=8;x=9;x++)
printwall(x+4,y+10);
for(x=7,y=4;x=9;x++)
printwall(x+4,y+10);
for(x=9,y=5;y=7;y++)
printwall(x+4,y+10);
for(x=8,y=2;y=3;y++)
printwall(x+4,y+10);
printwall(5+4,4+10);
printwall(5+4,7+10);
printwall(3+4,2+10);
printbox(3+4,6+10);
printbox(3+4,7+10);
printbox(4+4,7+10);
printwhither1(4+4,2+10,win,pw);
printwhither1(5+4,2+10,win,pw);
printwhither1(6+4,2+10,win,pw);
printman(2+4,8+10);
return win;
}
/* 第三关的图象初始化 */
winer *inithouse3()
{int x,y;
winer *win=NULL,*pw;
gotoxy(8,3);
printf("Level No.3");
for(x=1,y=2;y=8;y++)
printwall(x+4,y+10);
for(x=2,y=2;x=4;x++)
printwall(x+4,y+10);
for(x=4,y=1;y=3;y++)
printwall(x+4,y+10);
for(x=5,y=1;x=8;x++)
printwall(x+4,y+10);
for(x=8,y=2;y=5;y++)
printwall(x+4,y+10);
for(x=5,y=5;x=7;x++)
printwall(x+4,y+10);
for(x=7,y=6;y=9;y++)
printwall(x+4,y+10);
for(x=3,y=9;x=6;x++)
printwall(x+4,y+10);
for(x=3,y=6;y=8;y++)
printwall(x+4,y+10);
printwall(2+4,8+10);
printwall(5+4,7+10);
printbox(6+4,3+10);
printbox(4+4,4+10);
printbox(5+4,6+10);
printwhither1(2+4,5+10,win,pw);
printwhither1(2+4,6+10,win,pw);
printwhither1(2+4,7+10,win,pw);
printman(2+4,4+10);
return win;
}
/* 第二关的图象初始化 */
winer *inithouse2()
{int x,y;
winer *win=NULL,*pw;
gotoxy(8,2);
printf("Level No.2");
for(x=1,y=4;y=7;y++)
printwall(x+4,y+10);
for(x=2,y=2;y=4;y++)
printwall(x+4,y+10);
for(x=2,y=7;x=4;x++)
printwall(x+4,y+10);
for(x=4,y=1;x=8;x++)
printwall(x+4,y+10);
for(x=8,y=2;y=8;y++)
printwall(x+4,y+10);
for(x=4,y=8;x=8;x++)
printwall(x+4,y+10);
for(x=4,y=6;x=5;x++)
printwall(x+4,y+10);
for(x=3,y=2;x=4;x++)
printwall(x+4,y+10);
for(x=4,y=4;x=5;x++)
printwall(x+4,y+10);
printwall(6+4,3+10);
printbox(3+4,5+10);
printbox(6+4,6+10);
printbox(7+4,3+10);
printwhither1(5+4,7+10,win,pw);
printwhither1(6+4,7+10,win,pw);
printwhither1(7+4,7+10,win,pw);
printman(2+4,6+10);
return win;
}
/* 第四关的图象初始化 */
winer *inithouse4()
{int x,y;
winer *win=NULL,*pw;
gotoxy(8,2);
printf("Level No.4");
for(x=1,y=1;y=6;y++)
printwall(x+4,y+10);
for(x=2,y=7;y=8;y++)
printwall(x+4,y+10);
for(x=2,y=1;x=7;x++)
printwall(x+4,y+10);
for(x=7,y=2;y=4;y++)
printwall(x+4,y+10);
for(x=6,y=4;y=9;y++)
printwall(x+4,y+10);
for(x=3,y=9;x=5;x++)
printwall(x+4,y+10);
for(x=3,y=3;y=4;y++)
printwall(x+4,y+10);
printwall(3+4,8+10);
printbox(3+4,5+10);
printbox(4+4,4+10);
printbox(4+4,6+10);
printbox(5+4,5+10);
printbox(5+4,3+10);
printwhither1(3+4,7+10,win,pw);
printwhither1(4+4,7+10,win,pw);
printwhither1(5+4,7+10,win,pw);
printwhither1(4+4,8+10,win,pw);
printwhither1(5+4,8+10,win,pw);
printman(2+4,2+10);
return win;
}
/* 移动在空地上的箱子到空地上 */
movebox(int x,int y,char a)
{switch(a)
{
case 'u':ghouse[x-1][y]=0;printf(" ");
printbox(x-2,y);printman(x-1,y);
ghouse[x-2][y]='b';break;
case 'd':ghouse[x+1][y]=0;printf(" ");
printbox(x+2,y);printman(x+1,y);
ghouse[x+2][y]='b';break;
case 'l':ghouse[x][y-1]=0;printf(" ");
printbox(x,y-2);printman(x,y-1);
ghouse[x][y-2]='b';break;
case 'r':ghouse[x][y+1]=0;printf(" ");
printbox(x,y+2);printman(x,y+1);
ghouse[x][y+2]='b';break;
default: break;
}
}
/* 移动在目的地上的箱子到空地上 */
moveinbox(int x,int y,char a)
{switch(a)
{
case 'u':ghouse[x-1][y]='m';printf(" ");
printbox(x-2,y);printman(x-1,y);
ghouse[x-2][y]='b';break;
case 'd':ghouse[x+1][y]='m';printf(" ");
printbox(x+2,y);printman(x+1,y);
ghouse[x+2][y]='b';break;
case 'l':ghouse[x][y-1]='m';printf(" ");
printbox(x,y-2);printman(x,y-1);
ghouse[x][y-2]='b';break;
case 'r':ghouse[x][y+1]='m';printf(" ");
printbox(x,y+2);printman(x,y+1);
ghouse[x][y+2]='b';break;
default: break;
}
}
/* 移动在空地上的箱子到目的地上 */
moveboxin(int x,int y,char a)
{switch(a)
{
case 'u':ghouse[x-1][y]=0;printf(" ");
printboxin(x-2,y);printman(x-1,y);
ghouse[x-2][y]='i';break;
case 'd':ghouse[x+1][y]=0;printf(" ");
printboxin(x+2,y);printman(x+1,y);
ghouse[x+2][y]='i';break;
case 'l':ghouse[x][y-1]=0;printf(" ");
printboxin(x,y-2);printman(x,y-1);
ghouse[x][y-2]='i';break;
case 'r':ghouse[x][y+1]=0;printf(" ");
printboxin(x,y+2);printman(x,y+1);
ghouse[x][y+2]='i';break;
default: break;
}
}
/* 移动在目的地上的箱子到目的地 */
moveinboxin(int x,int y,char a)
{switch(a)
{
case 'u':ghouse[x-1][y]='m';printf(" ");
printboxin(x-2,y);printman(x-1,y);
ghouse[x-2][y]='i';break;
case 'd':ghouse[x+1][y]='m';printf(" ");
printboxin(x+2,y);printman(x+1,y);
ghouse[x+2][y]='i';break;
case 'l':ghouse[x][y-1]='m';printf(" ");
printboxin(x,y-2);printman(x,y-1);
ghouse[x][y-2]='i';break;
case 'r':ghouse[x][y+1]='m';printf(" ");
printboxin(x,y+2);printman(x,y+1);
ghouse[x][y+2]='i';break;
default: break;
}
}
/* 判断特定的坐标上的状态 */
int judge(int x,int y)
{int i;
switch(ghouse[x][y])
{
case 0: i=1;break;
case 'w': i=0;break;
case 'b': i=2;break;
case 'i': i=4;break;
case 'm': i=3;break;
default: break;
}
return i;
}
/* 处理按下键盘后,人物移动的主函数 */
move(int x,int y,char a)
{switch(a)
{
case 'u':if(!judge(x-1,y)) {gotoxy(y,x);break;}
else if(judge(x-1,y)==1||judge(x-1,y)==3)
{if(judge(x,y)==3)
{ printwhither(x,y);printman(x-1,y);break;}
else
{printf(" ");printman(x-1,y);break;}
}
else if(judge(x-1,y)==2)
{ if(judge(x-2,y)==1)
{movebox(x,y,'u');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{ moveboxin(x,y,'u');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y,x-1);
}
else gotoxy(y,x);
break;
}
else if(judge(x-1,y)==4)
{ if(judge(x-2,y)==1)
{moveinbox(x,y,'u');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x-1);
}
else if(judge(x-2,y)==3)
{ moveinboxin(x,y,'u');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x-1);
}
else gotoxy(y,x);
break;
}
case 'd':if(!judge(x+1,y)) {gotoxy(y,x);break;}
else if(judge(x+1,y)==1||judge(x+1,y)==3)
{if(judge(x,y)==3)
{ printwhither(x,y);printman(x+1,y);break;}
else
{printf(" ");printman(x+1,y);break;}
}
else if(judge(x+1,y)==2)
{ if(judge(x+2,y)==1)
{movebox(x,y,'d');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{moveboxin(x,y,'d');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);
}
else gotoxy(y,x);
break;
}
else if(judge(x+1,y)==4)
{ if(judge(x+2,y)==1)
{moveinbox(x,y,'d');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);
}
else if(judge(x+2,y)==3)
{moveinboxin(x,y,'d');
if(judge(x,y)==3) printwhither(x,y);gotoxy(y,x+1);
}
else gotoxy(y,x);
break;
}
case 'l':if(!judge(x,y-1)) {gotoxy(y,x);break;}
else if(judge(x,y-1)==1||judge(x,y-1)==3)
{if(judge(x,y)==3)
{ printwhither(x,y);printman(x,y-1);break;}
else
{printf(" ");printman(x,y-1);break;}
}
else if(judge(x,y-1)==2)
{ if(judge(x,y-2)==1)
{movebox(x,y,'l');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{moveboxin(x,y,'l');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);
}
else gotoxy(y,x);
break;
}
else if(judge(x,y-1)==4)
{ if(judge(x,y-2)==1)
{moveinbox(x,y,'l');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);
}
else if(judge(x,y-2)==3)
{moveinboxin(x,y,'l');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y-1,x);
}
else gotoxy(y,x);
break;
}
case 'r':if(!judge(x,y+1)) {gotoxy(y,x);break;}
else if(judge(x,y+1)==1||judge(x,y+1)==3)
{if(judge(x,y)==3)
{printwhither(x,y);printman(x,y+1);break;}
else
{printf(" ");printman(x,y+1);break;}
}
else if(judge(x,y+1)==2)
{ if(judge(x,y+2)==1)
{movebox(x,y,'r');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{moveboxin(x,y,'r');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);
}
else gotoxy(y,x);
break;
}
else if(judge(x,y+1)==4)
{ if(judge(x,y+2)==1)
{moveinbox(x,y,'r');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);
}
else if(judge(x,y+2)==3)
{moveinboxin(x,y,'r');
if(judge(x,y)==3) printwhither(x,y); gotoxy(y+1,x);
}
else gotoxy(y,x);
break;
}
default: break;
}
}
/* 按下空格键后,回到本关开头的函数 */
void reset(int i)
{switch(i)
{
case 0: init();
inithouse1();break;
case 1: init();
inithouse2();break;
case 2: init();
inithouse3();break;
case 3: init();
inithouse4();break;
default:break;
}
}
/* 主函数main */
main()
{int key,x,y,s,i=0;
winer *win,*pw;
_AL=3;_AH=0;
geninterrupt(0x10);
init();
win=inithouse1();
do{
_AH=3;
geninterrupt(0x10);
x=_DH+1;y=_DL+1;
while(bioskey(1)==0);
key=bioskey(0);
switch(key)
{
case 0x4800:move(x,y,'u');break; /* 按下向上键后 */
case 0x5000:move(x,y,'d');break; /* 按下向下键后 */
case 0x4b00:move(x,y,'l');break; /* 按下向左键后 */
case 0x4d00:move(x,y,'r');break; /* 按下向右键后 */
case 0x3920:reset(i);break; /* 按下空格键后 */
default:break;
}
s=0;
pw=win;
while(pw)
{
if(ghouse[pw-x][pw-y]=='m') s++;
pw=pw-p;
}
if(s==0)
{
free(win);
gotoxy(25,2);
printf("Congratulate! You have passed Level %d!",i+1);
getch();
i++;
switch(i)
{
case 1: init();
win=inithouse2();break;
case 2: init();
win=inithouse3();break;
case 3: init();
win=inithouse4();break;
case 4: gotoxy(8,14);
printf("Great! You have passed all the levels! Press any key to quit!");
key=0x011b;getch();break;
default: break;
}
}
}while(key!=0x011b);
_AL=3;
_AH=0;
geninterrupt(0x10);
}
关于推箱子代码和推箱子代码c语言编程的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-09网页代码,网页代码快捷键
- 05-06单页网站的代码(完整的网页代码)[20240506更新]
- 05-06个人主页图片代码(个人主页图片代码怎么弄)[20240506更新]
- 05-06提取微信名片代码(微信名片信息提取)[20240506更新]
- 05-06php后台权限管理代码(php管理员权限)[20240506更新]
- 05-06付费观看代码php(付费观看代码)[20240506更新]
- 05-06在线html执行代码(html怎么运行)[20240506更新]
- 05-06源代码管理资源管理器(资源管理器运行代码)[20240506更新]
- 05-06代码源软件库(程序代码库)[20240506更新]
- 05-06点击弹出密码代码(点击弹出密码代码错误)[20240506更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接