汇编课程设计——推箱子

目标:模仿一个小游戏
成果(推箱子):在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
解析:
1:主函数

int main(){
	init();
	drawingTu();
	actionRen();
	return 0;
}

2:3个关卡的地图声明

int map1[7][9]={
	{0,0,0,1,1,1,1,1,0},
	{0,0,1,0,0,0,0,1,0},
	{0,0,1,0,3,0,0,1,0},
	{0,1,0,0,3,3,0,1,0},
	{0,1,2,1,1,0,0,0,1},
	{0,1,1,4,4,4,0,0,1},
	{0,0,1,1,1,1,1,1,1},
	};
	//0:空白的地方 1:墙 2:人
	//3:箱子 4:箱子的终点位置
	
int map2[7][9]={
	{1,1,1,0,0,0,0,0,0},
	{1,4,1,0,0,0,0,0,0},
	{1,4,1,1,1,1,1,1,1},
	{1,4,0,0,0,0,0,0,1},
	{1,0,0,0,3,3,3,0,1},
	{1,1,1,1,1,2,0,0,1},
	{0,0,0,0,1,1,1,1,1},
	};
	
int map3[7][9]={
	{1,1,1,1,1,0,1,1,1},
	{1,2,0,0,1,0,1,4,1},
	{1,0,3,3,1,1,1,4,1},
	{1,0,3,0,0,0,0,4,1},
	{1,1,1,0,0,0,0,0,1},
	{0,0,1,0,0,0,1,1,1},
	{0,0,1,1,1,1,1,0,0},
	};
	
int map[7][9];

3:关卡选择

void init(){
	system("cls");
	printf("请选择关卡(1~3):\n");
	char ch;
	while(ch = getch()){
		printf("%c\n",ch);
		if(ch == '1'){
			memcpy(map, map1, sizeof(map));
			break;
		} else if (ch == '2'){
			memcpy(map, map2, sizeof(map));
			break;
		} else if (ch == '3') {
			memcpy(map, map3, sizeof(map));
			break;
		} else {
			printf("输入错误,请重新输入\n");
		}
	}
}

4:刻画出图案

void drawingTu(){
	system("cls");
	int i, j;
	printf("\n\n");
	for(i = 0; i < 7; i++)
	{
		for(j = 0; j < 9; j++)
		{
	   	   	int t = map[i][j];
	   	   	if(t == 0)
	   	   	   	printf("  "); //空白的地方
	   	   	else if (t == 1)
	   	   	   	printf("■"); //墙 
	   	   	else if (t == 2)
	   	   		printf("♀"); //人 
	   	   	else if (t == 3)    	
				printf("□"); //箱子 
			else if (t == 4)
			   	printf("◎"); //终点地方 
			else if (t == 6)    	
				printf("@");//人加终点位置 
			else if (t == 7)		
				printf("★") ;//箱子到达终点位置
		}
	    printf("\n");
	}
	printf("\n\n\n■:墙 , ♀:人, □:箱子, ◎:箱子的终点位置");	   
	printf("\n\n上:W/w, 下:S/s, 左:A/a, 右:D/d\n");
}

5:判断输赢

void iswin(){
	int k = 0;
	int i, j;
	for(i = 0; i < 7; i++)
	{
	    for(j = 0; j < 9; j++)
		{
			if(map[i][j] == 3)
				k++;
		}
	}
	if(k == 0){
		printf("\n恭喜你,闯关成功!\n");
		Sleep(1000);
		init();
	}
} 

6:推箱子的过程(最关键的一步)

void actionRen(){
	while(1){
	int x = 0, y = 0;//行和列 
	int i, j;
	char ch;
	for(i = 0; i < 7; i++){
		for (j = 0; j < 9; j++)
		{
			if(map[i][j] == 2||map[i][j] == 6)
			{
				x = i;
				y = j;
				break;
			}
		}
		if(x != 0 && y != 0)
			break;	   
	}
	
		ch = getch();
		if (ch == 'W' || ch == 'w' || ch == 72) 	//上 
		{
			if (map[x - 1][y] == 0 || map[x - 1][y] == 4) {
				map[x - 1][y] += 2;
				map[x][y] -= 2;
			} else if (map[x - 1][y] == 3 || map[x - 1][y] == 7) {
				if(map[x-2][y] == 0||map[x-2][y] == 4)
				{
			    	map[x][y] -= 2;
					map[x-1][y] -= 1;
					map[x-2][y] += 3;
				}
			}
		}
		else if (ch == 'S' || ch == 's' || ch == 80) 	//下 
		{
			if(map[x + 1][y] == 0 || map[x + 1][y] == 4)
			{
				map[x][y] -= 2;
				map[x + 1][y] += 2;
			}
			else if(map[x + 2][y] == 0 || map[x + 2][y] == 4)
			{
				if(map[x + 1][y] == 3 || map[x + 1][y] == 7)
				{
			    	map[x][y] -= 2;
					map[x + 1][y] -= 1;
					map[x + 2][y] += 3;
				}
			}
		}
		else if (ch == 'A' || ch == 'a' || ch == 75) 	//左 
		{
			if(map[x][y - 1] == 0 || map[x][y - 1] == 4)
			{
				map[x][y] -= 2;
				map[x][y - 1] += 2;
			}
			else if(map[x][y - 2] == 0 || map[x][y - 2] == 4)
			{
				if(map[x][y - 1] == 3 || map[x][y - 1] == 7)
				{
			    	map[x][y] -= 2;
					map[x][y - 1] -= 1;
					map[x][y - 2] += 3;
				}
			}
		}
		else if (ch == 'D' || ch == 'd' || ch == 77) 	//右 
		{
			if(map[x][y + 1] == 0 || map[x][y + 1] == 4)
			{
				map[x][y] -= 2;
				map[x][y+1] += 2;
			}
			else if(map[x][y + 2] ==0 || map[x][y + 2] == 4)
			{
				if(map[x][y + 1] == 3 || map[x][y + 1] == 7)
				{
			    	map[x][y] -= 2;
					map[x][y + 1] -= 1;
					map[x][y+2] += 3;
				}
			}
		}
		drawingTu();
		iswin();
	}
}

7:汇编语言实现

.386
.model flat, stdcall
option casemap:none
includelib	msvcrt.lib
scanf PROTO C:DWORD,:vararg
printf PROTO C:DWORD,:vararg
system PROTO C:dword
sleep PROTO C:dword
getch PROTO C
.data
map1	dword 0,0,0,1,1,1,1,1,0
dword 0,0,1,0,0,0,0,1,0
dword 0,0,1,0,3,0,0,1,0
dword 0,1,0,0,3,3,0,1,0
dword 0,1,2,1,1,0,0,0,1
dword 0,1,1,4,4,4,0,0,1
dword 0,0,1,1,1,1,1,1,1

map2	dword 1,1,1,0,0,0,0,0,0
dword 1,4,1,0,0,0,0,0,0
dword 1,4,1,1,1,1,1,1,1
dword 1,4,0,0,0,0,0,0,1
dword 1,0,0,0,3,3,3,0,1
dword 1,1,1,1,1,2,0,0,1
dword 0,0,0,0,1,1,1,1,1

map3	dword 1,1,1,1,1,0,1,1,1
dword 1,2,0,0,1,0,1,4,1
dword 1,0,3,3,1,1,1,4,1
dword 1,0,3,0,0,0,0,4,1
dword 1,1,1,0,0,0,0,0,1
dword 0,0,1,0,0,0,1,1,1
dword 0,0,1,1,1,1,1,0,0

map dword 7 dup(9 dup(?))
k byte ' ',10,0
k0 byte '  ',0
k1 byte '■',0
k2 byte '♀',0
k3 byte '□',0
k4 byte '◎',0
k6 byte '@',0
k7 byte '★',0
s byte 'cls',0
s1 byte '请选择关卡(1~3):',10,0
s2 byte '%c',0
s3 byte '输入错误,请重新输入',10,0
s4 byte '恭喜你,闯关成功!',10,0
s5 byte '%d',10,0
x dword ?
xx dword ?
yy dword ?
kk dword ?
t1 dword 1
t2 dword 2
t3 dword 3
t4 dword 4
t7 dword 7
t9 dword 9
sss dword ?
ss1 dword ?
ss2 dword ?
ss3 dword ?
.code
init PROC	;初始化

invoke system,addr s
invoke printf,addr s1

.while 1
invoke getch
mov x,eax
invoke printf,addr s2,x
.if x=='1'

	mov esi,0
	mov ebx,0
	.while esi<7
		mov edi,0
		.while edi<9
			mov eax,map1[ebx+edi*4]
			mov map[ebx+edi*4],eax
			inc edi
		.endw
		inc esi
		mov eax,9
		shl eax,2
		lea ebx,[ebx+eax]
	.endw
	ret

.elseif x=='2'

	mov esi,0
	mov ebx,0
	.while esi<7
		mov edi,0
		.while edi<9
			mov eax,map2[ebx+edi*4]
			mov map[ebx+edi*4],eax
			inc edi
		.endw
		inc esi
		mov eax,9
		shl eax,2
		lea ebx,[ebx+eax]
	.endw
	ret

.elseif x=='3'

	mov esi,0
	mov ebx,0
	.while esi<7
		mov edi,0
		.while edi<9
			mov eax,map3[ebx+edi*4]
			mov map[ebx+edi*4],eax
			inc edi
		.endw
		inc esi
		mov eax,9
		shl eax,2
		lea ebx,[ebx+eax]
	.endw
	ret

.else
invoke printf,addr s3
.endif

.endw

ret
init endp

drawingTu PROC		;刻画出图案 

	invoke system,addr s
	mov esi,0     
	mov ebx,0																																																																																																																				
	invoke printf,addr k
	invoke printf,addr k
	.while esi<7
		mov edi,0
		.while edi<9
			mov eax,map[ebx+edi*4]
			.if eax == 0
				invoke printf,addr k0	;空白的地方
			.elseif eax == 1
				invoke printf,addr k1	;.elseif eax == 2
				invoke printf,addr k2	;.elseif eax == 3
				invoke printf,addr k3	;箱子
			.elseif eax == 4
				invoke printf,addr k4	;终点地方
			.elseif eax == 6
				invoke printf,addr k6	;人加终点位置
			.elseif eax == 7
				invoke printf,addr k7	;箱子到达终点位置
			.endif
			inc edi
		.endw
		invoke printf,addr k
		inc esi
		mov eax,9
		shl eax,2
		lea ebx,[ebx+eax]
	.endw

ret
drawingTu endp

iswin PROC		;判断输赢

mov kk,0
mov esi,0
mov ebx,0
.while esi<7
	mov edi,0
	.while edi<9
		.if map[ebx+edi*4] == 3	;箱子	箱子到达终点是状态7,只需要判断3的个数来判断几个箱子没有移到目的地
		inc kk
		.endif
		inc edi
	.endw
	inc esi
	mov eax,9
	shl eax,2
	lea ebx,[ebx+eax]
.endw
.if kk == 0
invoke printf,addr k
invoke printf,addr s4
invoke sleep,1000
invoke init
.endif

ret
iswin endp

actionRen PROC		;推箱子过程

.while 1

mov xx,0
mov yy,0
mov esi,0
mov ebx,0
.while esi<7
	mov edi,0
	.while edi<9
		.if map[ebx+edi*4] == 2 || map[ebx+edi*4] == 6
		mov xx,esi
		mov yy,edi
		.break
		.endif
		inc edi
	.endw
	inc esi
	mov eax,9
	shl eax,2
	lea ebx,[ebx+eax]
.endw
invoke getch
mov x,eax
.if x == 'W' || x == 'w' || x == 72	;上
	mov eax,xx
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	mul t4
	add eax,ss3
	mov sss,eax		;map[x][y]
	
	mov eax,xx
	sub eax,t1
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	mul t4
	add eax,ss3
	mov ss1,eax		;map[x-1][y]

	mov eax,xx
	sub eax,t2
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	mul t4
	add eax,ss3
	mov ss2,eax		;map[x-2][y]

	mov eax,ss1
	.if map[eax] == 0 || map[eax] == 4		;0,4涉及两个位置状态改变
		mov edx,map[eax]
		add edx,2
		mov map[eax],edx	;0->2	4->6

		mov eax,sss
		mov edx,map[eax]
		sub edx,2			
		mov map[eax],edx	;2->0	6->4
	.elseif map[eax] == 3 || map[eax] == 7		;3,7涉及三个位置状态改变
		mov eax,ss2
		.if map[eax] == 0 || map[eax] == 4
			mov eax,sss
			mov edx,map[eax]
			sub edx,t2
			mov map[eax],edx	;2->0	6->4

			mov eax,ss1
			mov edx,map[eax]
			sub edx,t1
			mov map[eax],edx	;3->2	7->6

			mov eax,ss2
			mov edx,map[eax]
			add edx,t3
			mov map[eax],edx	;0->3	4->7
		.endif
	.endif
.elseif x == 'S' || x == 's' || x == 80	;下
	mov eax,xx
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	mul t4
	add eax,ss3
	mov sss,eax		;map[x][y]
	
	mov eax,xx
	add eax,t1
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	mul t4
	add eax,ss3
	mov ss1,eax		;map[x+1][y]

	mov eax,xx
	add eax,t2
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	mul t4
	add eax,ss3
	mov ss2,eax		;map[x+2][y]

	mov eax,sss
	mov ebx,ss1
	mov edx,ss2
	.if map[ebx] == 0 || map[ebx] == 4
		mov ecx,map[eax]
		sub ecx,t2
		mov map[eax],ecx	;2->0	6->4

		mov ecx,map[ebx]
		add ecx,t2
		mov map[ebx],ecx	;0->2	4->6
	.elseif map[edx] == 0 || map[edx] == 4
		.if map[ebx] == 3 || map[ebx] == 7
			mov ecx,map[eax]
			sub ecx,t2
			mov map[eax],ecx	;2->0	6->4

			mov ecx,map[ebx]
			sub ecx,t1
			mov map[ebx],ecx	;3->2	7->6

			mov ecx,map[edx]
			add ecx,t3
			mov map[edx],ecx	;0->3	4->7
		.endif
	.endif
	
.elseif x == 'A' || x == 'a' || x == 75	;左
	mov eax,xx
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	mul t4
	add eax,ss3
	mov sss,eax		;map[x][y]
	
	mov eax,xx
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	sub eax,t1
	mul t4
	add eax,ss3
	mov ss1,eax		;map[x][y-1]

	mov eax,xx
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	sub eax,t2
	mul t4
	add eax,ss3
	mov ss2,eax		;map[x][y-2]

	mov eax,sss
	mov ebx,ss1
	mov edx,ss2
	.if map[ebx] == 0 || map[ebx] == 4
		mov ecx,map[eax]
		sub ecx,t2
		mov map[eax],ecx	;2->0	6->4

		mov ecx,map[ebx]
		add ecx,t2
		mov map[ebx],ecx	;0->2	4->6
	.elseif map[edx] == 0 || map[edx] == 4
		.if map[ebx] == 3 || map[ebx] == 7
			mov ecx,map[eax]
			sub ecx,t2
			mov map[eax],ecx	;2->0	6->4

			mov ecx,map[ebx]
			sub ecx,t1
			mov map[ebx],ecx	;3->2	7->6

			mov ecx,map[edx]
			add ecx,t3
			mov map[edx],ecx	;0->3	4->7
		.endif
	.endif
.elseif x == 'D' || x == 'd' || x == 77	;右 
	mov eax,xx
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	mul t4
	add eax,ss3
	mov sss,eax		;map[x][y]
	
	mov eax,xx
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	add eax,t1
	mul t4
	add eax,ss3
	mov ss1,eax		;map[x][y+1]

	mov eax,xx
	mul t4
	mul t9
	mov ss3,eax
	mov eax,yy
	add eax,t2
	mul t4
	add eax,ss3
	mov ss2,eax		;map[x][y+2]

	mov eax,sss
	mov ebx,ss1
	mov edx,ss2
	.if map[ebx] == 0 || map[ebx] == 4
		mov ecx,map[eax]
		sub ecx,t2
		mov map[eax],ecx	;2->0	6->4

		mov ecx,map[ebx]
		add ecx,t2
		mov map[ebx],ecx	;0->2	4->6
	.elseif map[edx] == 0 || map[edx] == 4
		.if map[ebx] == 3 || map[ebx] == 7
			mov ecx,map[eax]
			sub ecx,t2
			mov map[eax],ecx	;2->0	6->4

			mov ecx,map[ebx]
			sub ecx,t1
			mov map[ebx],ecx	;3->2	7->6

			mov ecx,map[edx]
			add ecx,t3
			mov map[edx],ecx	;0->3	4->7
		.endif
	.endif
.endif
invoke drawingTu
invoke iswin
.endw

ret
actionRen endp

start:
invoke init
invoke drawingTu
invoke actionRen
ret 
end	start

8:有兴趣的可以来玩一下哦

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I'm 程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值